matter.c

Cross-platform minimalist libc
git clone git://git.finwo.net/lib/matter.c
Log | Files | Refs | README | LICENSE

commit 5d0c7b7fd05f0d0d976119a159c830eebfd255a7
parent 955f1c3254fb267454c5debbdd86d2352c11ea79
Author: Yersa Nordman <finwo@pm.me>
Date:   Wed, 17 Jun 2020 16:00:01 +0200

Added strlen

Diffstat:
March/wasm32/config.mk | 3++-
Darch/wasm32/include/.gitkeep | 0
Aarch/wasm32/include/bits/alltypes.h | 15+++++++++++++++
Ainclude/float.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude/stdarg.h | 20++++++++++++++++++++
Ainclude/stdbool.h | 14++++++++++++++
Minclude/stddef.h | 7+++++--
Minclude/stdint.h | 260+++++++++++++++++++++++++++++++++++++++----------------------------------------
Minclude/string.h | 1+
Asrc/string/strlen.c | 20++++++++++++++++++++
10 files changed, 254 insertions(+), 136 deletions(-)

diff --git a/arch/wasm32/config.mk b/arch/wasm32/config.mk @@ -1,3 +1,4 @@ -LLE=clang -emit-llvm --target=wasm32 +CC=clang --target=wasm32 +LLE=$(CC) -emit-llvm LLC=llc -march=wasm32 AR=llvm-ar diff --git a/arch/wasm32/include/.gitkeep b/arch/wasm32/include/.gitkeep diff --git a/arch/wasm32/include/bits/alltypes.h b/arch/wasm32/include/bits/alltypes.h @@ -0,0 +1,15 @@ +#ifndef _BITS_ALLTYPES_H_ +#define _BITS_ALLTYPES_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define _Addr int + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _BITS_ALLTYPES_H_ + diff --git a/include/float.h b/include/float.h @@ -0,0 +1,50 @@ +#ifndef _FLOAT_H_ +#define _FLOAT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +int __flt_rounds(void); +#define FLT_ROUNDS (__flt_rounds()) + +#define FLT_RADIX 2 + +#define FLT_TRUE_MIN 1.40129846432481707092e-45F +#define FLT_MIN 1.17549435082228750797e-38F +#define FLT_MAX 3.40282346638528859812e+38F +#define FLT_EPSILON 1.1920928955078125e-07F + +#define FLT_MANT_DIG 24 +#define FLT_MIN_EXP (-125) +#define FLT_MAX_EXP 128 +#define FLT_HAS_SUBNORM 1 + +#define FLT_DIG 6 +#define FLT_DECIMAL_DIG 9 +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_10_EXP 38 + +#define DBL_TRUE_MIN 4.94065645841246544177e-324 +#define DBL_MIN 2.22507385850720138309e-308 +#define DBL_MAX 1.79769313486231570815e+308 +#define DBL_EPSILON 2.22044604925031308085e-16 + +#define DBL_MANT_DIG 53 +#define DBL_MIN_EXP (-1021) +#define DBL_MAX_EXP 1024 +#define DBL_HAS_SUBNORM 1 + +#define DBL_DIG 15 +#define DBL_DECIMAL_DIG 17 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_10_EXP 308 + +#define LDBL_HAS_SUBNORM 1 +#define LDBL_DECIMAL_DIG DECIMAL_DIG + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _FLOAT_H_ diff --git a/include/stdarg.h b/include/stdarg.h @@ -0,0 +1,20 @@ +#ifndef _STDARG_H_ +#define _STDARG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef __builtin_va_list va_list; +typedef __builtin_va_list __isoc_va_list; + +#define va_start(v,l) __builtin_va_start(v,l) +#define va_end(v) __builtin_va_end(v) +#define va_arg(v,l) __builtin_va_arg(v,l) +#define va_copy(d,s) __builtin_va_copy(d,s) + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _STDARG_H_ diff --git a/include/stdbool.h b/include/stdbool.h @@ -0,0 +1,14 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +#ifndef __cplusplus + +#define true 1 +#define false 0 +#define bool _Bool + +#define __bool_true_false_are_defined 1 + +#endif + +#endif diff --git a/include/stddef.h b/include/stddef.h @@ -1,13 +1,16 @@ #ifndef _STDDEF_H_ #define _STDDEF_H_ +#include <bits/alltypes.h> + #ifdef __cplusplus #define NULL 0L #else #define NULL ((void*)0) #endif -typedef unsigned long size_t; -typedef signed long ssize_t; +typedef unsigned _Addr size_t; +typedef _Addr ssize_t; +typedef _Addr ptrdiff_t; #endif // _STDDEF_H_ diff --git a/include/stdint.h b/include/stdint.h @@ -16,17 +16,11 @@ <http://www.gnu.org/licenses/>. */ /* - * ISO C99: 7.18 Integer types <stdint.h> + * ISO C99: 7.18 Integer types <stdint.h> */ #ifndef _STDINT_H_ -#define _STDINT_H_ 1 - -#include <features.h> -#ifdef __UCLIBC_HAS_WCHAR__ -#include <bits/wchar.h> -#endif /* __UCLIBC_HAS_WCHAR__ */ -#include <bits/wordsize.h> +#define _STDINT_H_ /* Exact integral types. */ @@ -35,111 +29,111 @@ /* There is some amount of overlap with <sys/types.h> as known by inet code */ #ifndef __int8_t_defined # define __int8_t_defined -typedef signed char int8_t; -typedef short int int16_t; -typedef int int32_t; +typedef signed char int8_t; +typedef short int int16_t; +typedef int int32_t; # if __WORDSIZE == 64 -typedef long int int64_t; +typedef long int int64_t; # else __extension__ -typedef long long int int64_t; +typedef long long int int64_t; # endif #endif /* Unsigned. */ -typedef unsigned char uint8_t; -typedef unsigned short int uint16_t; +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; #ifndef __uint32_t_defined -typedef unsigned int uint32_t; +typedef unsigned int uint32_t; # define __uint32_t_defined #endif #if __WORDSIZE == 64 -typedef unsigned long int uint64_t; +typedef unsigned long int uint64_t; #else __extension__ -typedef unsigned long long int uint64_t; +typedef unsigned long long int uint64_t; #endif /* Small types. */ /* Signed. */ -typedef signed char int_least8_t; -typedef short int int_least16_t; -typedef int int_least32_t; +typedef signed char int_least8_t; +typedef short int int_least16_t; +typedef int int_least32_t; #if __WORDSIZE == 64 -typedef long int int_least64_t; +typedef long int int_least64_t; #else __extension__ -typedef long long int int_least64_t; +typedef long long int int_least64_t; #endif /* Unsigned. */ -typedef unsigned char uint_least8_t; -typedef unsigned short int uint_least16_t; -typedef unsigned int uint_least32_t; +typedef unsigned char uint_least8_t; +typedef unsigned short int uint_least16_t; +typedef unsigned int uint_least32_t; #if __WORDSIZE == 64 -typedef unsigned long int uint_least64_t; +typedef unsigned long int uint_least64_t; #else __extension__ -typedef unsigned long long int uint_least64_t; +typedef unsigned long long int uint_least64_t; #endif /* Fast types. */ /* Signed. */ -typedef signed char int_fast8_t; +typedef signed char int_fast8_t; #if __WORDSIZE == 64 -typedef long int int_fast16_t; -typedef long int int_fast32_t; -typedef long int int_fast64_t; +typedef long int int_fast16_t; +typedef long int int_fast32_t; +typedef long int int_fast64_t; #else -typedef int int_fast16_t; -typedef int int_fast32_t; +typedef int int_fast16_t; +typedef int int_fast32_t; __extension__ -typedef long long int int_fast64_t; +typedef long long int int_fast64_t; #endif /* Unsigned. */ -typedef unsigned char uint_fast8_t; +typedef unsigned char uint_fast8_t; #if __WORDSIZE == 64 -typedef unsigned long int uint_fast16_t; -typedef unsigned long int uint_fast32_t; -typedef unsigned long int uint_fast64_t; +typedef unsigned long int uint_fast16_t; +typedef unsigned long int uint_fast32_t; +typedef unsigned long int uint_fast64_t; #else -typedef unsigned int uint_fast16_t; -typedef unsigned int uint_fast32_t; +typedef unsigned int uint_fast16_t; +typedef unsigned int uint_fast32_t; __extension__ -typedef unsigned long long int uint_fast64_t; +typedef unsigned long long int uint_fast64_t; #endif /* Types for `void *' pointers. */ #if __WORDSIZE == 64 # ifndef __intptr_t_defined -typedef long int intptr_t; +typedef long int intptr_t; # define __intptr_t_defined # endif -typedef unsigned long int uintptr_t; +typedef unsigned long int uintptr_t; #else # ifndef __intptr_t_defined -typedef int intptr_t; +typedef int intptr_t; # define __intptr_t_defined # endif -typedef unsigned int uintptr_t; +typedef unsigned int uintptr_t; #endif /* Largest integral types. */ #if __WORDSIZE == 64 -typedef long int intmax_t; -typedef unsigned long int uintmax_t; +typedef long int intmax_t; +typedef unsigned long int uintmax_t; #else __extension__ -typedef long long int intmax_t; +typedef long long int intmax_t; __extension__ -typedef unsigned long long int uintmax_t; +typedef unsigned long long int uintmax_t; #endif @@ -148,140 +142,140 @@ typedef unsigned long long int uintmax_t; #if !defined __cplusplus || defined __STDC_LIMIT_MACROS # if __WORDSIZE == 64 -# define __INT64_C(c) c ## L -# define __UINT64_C(c) c ## UL +# define __INT64_C(c) c ## L +# define __UINT64_C(c) c ## UL # else -# define __INT64_C(c) c ## LL -# define __UINT64_C(c) c ## ULL +# define __INT64_C(c) c ## LL +# define __UINT64_C(c) c ## ULL # endif /* Limits of integral types. */ /* Minimum of signed integral types. */ -# define INT8_MIN (-128) -# define INT16_MIN (-32767-1) -# define INT32_MIN (-2147483647-1) -# define INT64_MIN (-__INT64_C(9223372036854775807)-1) +# define INT8_MIN (-128) +# define INT16_MIN (-32767-1) +# define INT32_MIN (-2147483647-1) +# define INT64_MIN (-__INT64_C(9223372036854775807)-1) /* Maximum of signed integral types. */ -# define INT8_MAX (127) -# define INT16_MAX (32767) -# define INT32_MAX (2147483647) -# define INT64_MAX (__INT64_C(9223372036854775807)) +# define INT8_MAX (127) +# define INT16_MAX (32767) +# define INT32_MAX (2147483647) +# define INT64_MAX (__INT64_C(9223372036854775807)) /* Maximum of unsigned integral types. */ -# define UINT8_MAX (255) -# define UINT16_MAX (65535) -# define UINT32_MAX (4294967295U) -# define UINT64_MAX (__UINT64_C(18446744073709551615)) +# define UINT8_MAX (255) +# define UINT16_MAX (65535) +# define UINT32_MAX (4294967295U) +# define UINT64_MAX (__UINT64_C(18446744073709551615)) /* Minimum of signed integral types having a minimum size. */ -# define INT_LEAST8_MIN (-128) -# define INT_LEAST16_MIN (-32767-1) -# define INT_LEAST32_MIN (-2147483647-1) -# define INT_LEAST64_MIN (-__INT64_C(9223372036854775807)-1) +# define INT_LEAST8_MIN (-128) +# define INT_LEAST16_MIN (-32767-1) +# define INT_LEAST32_MIN (-2147483647-1) +# define INT_LEAST64_MIN (-__INT64_C(9223372036854775807)-1) /* Maximum of signed integral types having a minimum size. */ -# define INT_LEAST8_MAX (127) -# define INT_LEAST16_MAX (32767) -# define INT_LEAST32_MAX (2147483647) -# define INT_LEAST64_MAX (__INT64_C(9223372036854775807)) +# define INT_LEAST8_MAX (127) +# define INT_LEAST16_MAX (32767) +# define INT_LEAST32_MAX (2147483647) +# define INT_LEAST64_MAX (__INT64_C(9223372036854775807)) /* Maximum of unsigned integral types having a minimum size. */ -# define UINT_LEAST8_MAX (255) -# define UINT_LEAST16_MAX (65535) -# define UINT_LEAST32_MAX (4294967295U) -# define UINT_LEAST64_MAX (__UINT64_C(18446744073709551615)) +# define UINT_LEAST8_MAX (255) +# define UINT_LEAST16_MAX (65535) +# define UINT_LEAST32_MAX (4294967295U) +# define UINT_LEAST64_MAX (__UINT64_C(18446744073709551615)) /* Minimum of fast signed integral types having a minimum size. */ -# define INT_FAST8_MIN (-128) +# define INT_FAST8_MIN (-128) # if __WORDSIZE == 64 -# define INT_FAST16_MIN (-9223372036854775807L-1) -# define INT_FAST32_MIN (-9223372036854775807L-1) +# define INT_FAST16_MIN (-9223372036854775807L-1) +# define INT_FAST32_MIN (-9223372036854775807L-1) # else -# define INT_FAST16_MIN (-2147483647-1) -# define INT_FAST32_MIN (-2147483647-1) +# define INT_FAST16_MIN (-2147483647-1) +# define INT_FAST32_MIN (-2147483647-1) # endif -# define INT_FAST64_MIN (-__INT64_C(9223372036854775807)-1) +# define INT_FAST64_MIN (-__INT64_C(9223372036854775807)-1) /* Maximum of fast signed integral types having a minimum size. */ -# define INT_FAST8_MAX (127) +# define INT_FAST8_MAX (127) # if __WORDSIZE == 64 -# define INT_FAST16_MAX (9223372036854775807L) -# define INT_FAST32_MAX (9223372036854775807L) +# define INT_FAST16_MAX (9223372036854775807L) +# define INT_FAST32_MAX (9223372036854775807L) # else -# define INT_FAST16_MAX (2147483647) -# define INT_FAST32_MAX (2147483647) +# define INT_FAST16_MAX (2147483647) +# define INT_FAST32_MAX (2147483647) # endif -# define INT_FAST64_MAX (__INT64_C(9223372036854775807)) +# define INT_FAST64_MAX (__INT64_C(9223372036854775807)) /* Maximum of fast unsigned integral types having a minimum size. */ -# define UINT_FAST8_MAX (255) +# define UINT_FAST8_MAX (255) # if __WORDSIZE == 64 -# define UINT_FAST16_MAX (18446744073709551615UL) -# define UINT_FAST32_MAX (18446744073709551615UL) +# define UINT_FAST16_MAX (18446744073709551615UL) +# define UINT_FAST32_MAX (18446744073709551615UL) # else -# define UINT_FAST16_MAX (4294967295U) -# define UINT_FAST32_MAX (4294967295U) +# define UINT_FAST16_MAX (4294967295U) +# define UINT_FAST32_MAX (4294967295U) # endif -# define UINT_FAST64_MAX (__UINT64_C(18446744073709551615)) +# define UINT_FAST64_MAX (__UINT64_C(18446744073709551615)) /* Values to test for integral types holding `void *' pointer. */ # if __WORDSIZE == 64 -# define INTPTR_MIN (-9223372036854775807L-1) -# define INTPTR_MAX (9223372036854775807L) -# define UINTPTR_MAX (18446744073709551615UL) +# define INTPTR_MIN (-9223372036854775807L-1) +# define INTPTR_MAX (9223372036854775807L) +# define UINTPTR_MAX (18446744073709551615UL) # else -# define INTPTR_MIN (-2147483647-1) -# define INTPTR_MAX (2147483647) -# define UINTPTR_MAX (4294967295U) +# define INTPTR_MIN (-2147483647-1) +# define INTPTR_MAX (2147483647) +# define UINTPTR_MAX (4294967295U) # endif /* Minimum for largest signed integral type. */ -# define INTMAX_MIN (-__INT64_C(9223372036854775807)-1) +# define INTMAX_MIN (-__INT64_C(9223372036854775807)-1) /* Maximum for largest signed integral type. */ -# define INTMAX_MAX (__INT64_C(9223372036854775807)) +# define INTMAX_MAX (__INT64_C(9223372036854775807)) /* Maximum for largest unsigned integral type. */ -# define UINTMAX_MAX (__UINT64_C(18446744073709551615)) +# define UINTMAX_MAX (__UINT64_C(18446744073709551615)) /* Limits of other integer types. */ /* Limits of `ptrdiff_t' type. */ # if __WORDSIZE == 64 -# define PTRDIFF_MIN (-9223372036854775807L-1) -# define PTRDIFF_MAX (9223372036854775807L) +# define PTRDIFF_MIN (-9223372036854775807L-1) +# define PTRDIFF_MAX (9223372036854775807L) # else -# define PTRDIFF_MIN (-2147483647-1) -# define PTRDIFF_MAX (2147483647) +# define PTRDIFF_MIN (-2147483647-1) +# define PTRDIFF_MAX (2147483647) # endif /* Limits of `sig_atomic_t'. */ -# define SIG_ATOMIC_MIN (-2147483647-1) -# define SIG_ATOMIC_MAX (2147483647) +# define SIG_ATOMIC_MIN (-2147483647-1) +# define SIG_ATOMIC_MAX (2147483647) /* Limit of `size_t' type. */ # if __WORDSIZE == 64 -# define SIZE_MAX (18446744073709551615UL) +# define SIZE_MAX (18446744073709551615UL) # else -# define SIZE_MAX (4294967295U) +# define SIZE_MAX (4294967295U) # endif #ifdef __UCLIBC_HAS_WCHAR__ /* Limits of `wchar_t'. */ # ifndef WCHAR_MIN /* These constants might also be defined in <wchar.h>. */ -# define WCHAR_MIN __WCHAR_MIN -# define WCHAR_MAX __WCHAR_MAX +# define WCHAR_MIN __WCHAR_MIN +# define WCHAR_MAX __WCHAR_MAX # endif /* Limits of `wint_t'. */ -# define WINT_MIN (0u) -# define WINT_MAX (4294967295u) +# define WINT_MIN (0u) +# define WINT_MAX (4294967295u) #endif /* __UCLIBC_HAS_WCHAR__ */ -#endif /* C++ && limit macros */ +#endif /* C++ && limit macros */ /* The ISO C99 standard specifies that in C++ implementations these @@ -289,34 +283,34 @@ typedef unsigned long long int uintmax_t; #if !defined __cplusplus || defined __STDC_CONSTANT_MACROS /* Signed. */ -# define INT8_C(c) c -# define INT16_C(c) c -# define INT32_C(c) c +# define INT8_C(c) c +# define INT16_C(c) c +# define INT32_C(c) c # if __WORDSIZE == 64 -# define INT64_C(c) c ## L +# define INT64_C(c) c ## L # else -# define INT64_C(c) c ## LL +# define INT64_C(c) c ## LL # endif /* Unsigned. */ -# define UINT8_C(c) c -# define UINT16_C(c) c -# define UINT32_C(c) c ## U +# define UINT8_C(c) c +# define UINT16_C(c) c +# define UINT32_C(c) c ## U # if __WORDSIZE == 64 -# define UINT64_C(c) c ## UL +# define UINT64_C(c) c ## UL # else -# define UINT64_C(c) c ## ULL +# define UINT64_C(c) c ## ULL # endif /* Maximal type. */ # if __WORDSIZE == 64 -# define INTMAX_C(c) c ## L -# define UINTMAX_C(c) c ## UL +# define INTMAX_C(c) c ## L +# define UINTMAX_C(c) c ## UL # else -# define INTMAX_C(c) c ## LL -# define UINTMAX_C(c) c ## ULL +# define INTMAX_C(c) c ## LL +# define UINTMAX_C(c) c ## ULL # endif -#endif /* C++ && constant macros */ +#endif /* C++ && constant macros */ #endif // _STDINT_H_ diff --git a/include/string.h b/include/string.h @@ -6,5 +6,6 @@ int memcmp (const void *str1, const void *str2, int count); void *memcpy (void *dest, const void *src, size_t len); void *memset (void *dest, int val, size_t len); +static unsigned int strlen(const char *s); #endif // _STRING_H_ diff --git a/src/string/strlen.c b/src/string/strlen.c @@ -0,0 +1,20 @@ +#ifndef _STRING_H_ +#define _STRING_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +static unsigned int +mini_strlen(const char *s) +{ + unsigned int len = 0; + while (s[len] != '\0') len++; + return len; +} + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _STRING_H_