Commit 577ee0d8 authored by Oliver Horst's avatar Oliver Horst
Browse files

[chg] Integrated addons by Ulrich

Added the following functionality:
- atoi
- strncmp
- strtok
- rand
parent 08d65644
......@@ -22,6 +22,11 @@ find_package(
REQUIRED
)
find_package(
sfmt 1.4.1
REQUIRED
)
add_library(
toki-libc-repl
......
......@@ -10,6 +10,12 @@ project(
LANGUAGES C ASM
)
find_package(
sfmt-headers 1.4.1
REQUIRED
)
add_library(
toki-libc-repl-headers
#
......
#ifndef _ATOI_H
#define _ATOI_H
int atoi(const char *str);
#endif /* _ATOI_H */
//
// Created by ulrich on 11.05.20.
//
#include <stddef.h>
int rand(void);
int rand_r(unsigned int *seedp);
void srand(unsigned int seed);
......@@ -5,11 +5,15 @@
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
void *memmove(void *dst_void, const void *src_void, size_t length);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);
char * strtok(char *string, const char *delimiters);
#define memchr __builtin_memchr
#define memcmp __builtin_memcmp
......@@ -19,8 +23,6 @@ size_t strnlen(const char *s, size_t maxlen);
#define strcpy __builtin_strcpy
#define strcspn __builtin_strcspn
#define strncat __builtin_strncat
#define strncmp __builtin_strncmp
#define strncpy __builtin_strncpy
#define strpbrk __builtin_strpbrk
#define strrchr __builtin_strrchr
#define strspn __builtin_strspn
......
......@@ -2,6 +2,8 @@
include(CMakeFindDependencyMacro)
find_dependency(sfmt-headers)
# Import targets
include(${CMAKE_CURRENT_LIST_DIR}/toki-libc-repl-headers-targets.cmake)
......
......@@ -2,4 +2,5 @@ cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
add_subdirectory(malloc)
add_subdirectory(printf)
add_subdirectory(stdlib)
add_subdirectory(string)
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
target_sources(
toki-libc-repl
#
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/atoi.c"
"${CMAKE_CURRENT_LIST_DIR}/rand.c"
)
BSD with Attribution License
Copyright (c) 2015-2020 fortiss GmbH - Research Institute of the
Free State of Bavaria, Guerickestr. 25, 80805 Munich, Germany
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
4. Redistributions of any form whatsoever must retain the following
acknowledgment: 'This product includes software developed by the
fortiss GmbH -- Research Institute of the Free State of Bavaria,
Munich, Germany (https://www.fortiss.org/).'
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SPDX-License-Identifier: BSD-3-Clause-Attribution
#include <stddef.h>
int atoi(const char *str) {
int num = 0;
for (size_t i = 0; str[i] != '\0'; ++i) {
if (str[i] >= 0 && str[i] <= 9) {
num = num * 10 + str[i] - '0';
} else {
return 0;
}
}
return num;
}
#include <stddef.h>
#include <sfmt/SFMT.h>
sfmt_t *sfmt = NULL;
int rand_r(sfmt_t *state)
{
/*
* If the state is not initialized do this now and set seed to 1
* to comply with Linux kernel
*/
if (!state) {
sfmt_init_gen_rand(state, 1);
}
return sfmt_genrand_uint32(state);
}
int rand()
{
// Just call reentrant method with global state
return rand_r(sfmt);
}
void srand(unsigned int seed)
{
// Initialize the global state with the provided seed
sfmt_init_gen_rand(sfmt, seed);
}
......@@ -6,6 +6,9 @@ target_sources(
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/memcpy.c"
"${CMAKE_CURRENT_LIST_DIR}/memset.c"
"${CMAKE_CURRENT_LIST_DIR}/memmove.S"
"${CMAKE_CURRENT_LIST_DIR}/strcmp.c"
"${CMAKE_CURRENT_LIST_DIR}/strncmp.c"
"${CMAKE_CURRENT_LIST_DIR}/strlen.c"
"${CMAKE_CURRENT_LIST_DIR}/strtok.c"
)
BSD 3-Clause Revised License
Copyright (c) 2019-2020 fortiss GmbH - Research Institute of the
Free State of Bavaria, Guerickestr. 25, 80805 Munich, Germany
Copyright (c)
2013, Linaro Limited
2015, ARM Ltd
2019-2020, fortiss GmbH - Research Institute of the Free State of
Bavaria, Guerickestr. 25, 80805 Munich, Germany
All rights reserved.
Redistribution and use in source and binary forms, with or without
......
/* Copyright (c) 2013, Linaro Limited
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Linaro nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/*
* Copyright (c) 2015 ARM Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the company may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Assumptions:
*
* ARMv8-a, AArch64, unaligned accesses
*/
#if (defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED))
/* See memmove-stub.c */
#else
.macro def_fn f p2align=0
.text
.p2align \p2align
.global \f
.type \f, %function
\f:
.endm
/* Parameters and result. */
#define dstin x0
#define src x1
#define count x2
#define srcend x3
#define dstend x4
#define tmp1 x5
#define A_l x6
#define A_h x7
#define B_l x8
#define B_h x9
#define C_l x10
#define C_h x11
#define D_l x12
#define D_h x13
#define E_l count
#define E_h tmp1
/* All memmoves up to 96 bytes are done by memcpy as it supports overlaps.
Larger backwards copies are also handled by memcpy. The only remaining
case is forward large copies. The destination is aligned, and an
unrolled loop processes 64 bytes per iteration.
*/
def_fn memmove, 6
sub tmp1, dstin, src
cmp count, 96
ccmp tmp1, count, 2, hi
b.hs memcpy
cbz tmp1, 3f
add dstend, dstin, count
add srcend, src, count
/* Align dstend to 16 byte alignment so that we don't cross cache line
boundaries on both loads and stores. There are at least 96 bytes
to copy, so copy 16 bytes unaligned and then align. The loop
copies 64 bytes per iteration and prefetches one iteration ahead. */
and tmp1, dstend, 15
ldp D_l, D_h, [srcend, -16]
sub srcend, srcend, tmp1
sub count, count, tmp1
ldp A_l, A_h, [srcend, -16]
stp D_l, D_h, [dstend, -16]
ldp B_l, B_h, [srcend, -32]
ldp C_l, C_h, [srcend, -48]
ldp D_l, D_h, [srcend, -64]!
sub dstend, dstend, tmp1
subs count, count, 128
b.ls 2f
nop
1:
stp A_l, A_h, [dstend, -16]
ldp A_l, A_h, [srcend, -16]
stp B_l, B_h, [dstend, -32]
ldp B_l, B_h, [srcend, -32]
stp C_l, C_h, [dstend, -48]
ldp C_l, C_h, [srcend, -48]
stp D_l, D_h, [dstend, -64]!
ldp D_l, D_h, [srcend, -64]!
subs count, count, 64
b.hi 1b
/* Write the last full set of 64 bytes. The remainder is at most 64
bytes, so it is safe to always copy 64 bytes from the start even if
there is just 1 byte left. */
2:
ldp E_l, E_h, [src, 48]
stp A_l, A_h, [dstend, -16]
ldp A_l, A_h, [src, 32]
stp B_l, B_h, [dstend, -32]
ldp B_l, B_h, [src, 16]
stp C_l, C_h, [dstend, -48]
ldp C_l, C_h, [src]
stp D_l, D_h, [dstend, -64]
stp E_l, E_h, [dstin, 48]
stp A_l, A_h, [dstin, 32]
stp B_l, B_h, [dstin, 16]
stp C_l, C_h, [dstin]
3: ret
.size memmove, . - memmove
#endif
#include <stddef.h>
int strncmp(const char *s1, const char *s2, size_t n)
{
size_t pos = 0;
while (*s1 != '\0' && *s1 == *s2 && pos < n)
{
s1++;
s2++;
pos++;
}
return (*(unsigned char *) s1) - (*(unsigned char *) s2);
}
#include <stddef.h>
char* sp = NULL;
char * strtok(char *string, const char *delimiters)
{
// Check if a new string was provided and reset everything
if (string) {
sp = string;
}
// Safety check that the provided pointer to the string is valid
if (sp != NULL) {
return NULL;
}
// Set the token we will return
char *token = sp;
// Find the next delimiter from the current offset and end the string there to return the token
for (; *sp; ++sp) {
// We might have got multiple delimiters - check each
for(const char* dp = delimiters; *dp; ++dp) {
if (*sp == *dp) {
// Modify string so token ends at the delimiter
*sp = '\0';
// Forward the string to the start of the next token for the following iteration
++sp;
// Return the token
return token;
}
}
}
// Seems like there was no delimiter in the string. We'll just return the full string as token
return token;
}
......@@ -4,6 +4,7 @@ include(CMakeFindDependencyMacro)
find_dependency(toki-libc-repl-headers)
find_dependency(toki-libc-repl-port)
find_dependency(sfmt)
# Import targets
include(${CMAKE_CURRENT_LIST_DIR}/toki-libc-repl-targets.cmake)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment