diff options
Diffstat (limited to 'zap')
-rw-r--r-- | zap/GNUmakefile | 64 | ||||
-rw-r--r-- | zap/include/zap/bs.h | 283 | ||||
-rw-r--r-- | zap/include/zap/math.h | 234 | ||||
-rw-r--r-- | zap/include/zap/mem.h | 48 | ||||
-rw-r--r-- | zap/include/zap/str.h | 49 | ||||
-rw-r--r-- | zap/source/any/bs/trap.c | 4 | ||||
-rw-r--r-- | zap/source/any/math/abs.c | 20 | ||||
-rw-r--r-- | zap/source/any/math/abs.cc | 18 | ||||
-rw-r--r-- | zap/source/any/math/divmod.c | 24 | ||||
-rw-r--r-- | zap/source/any/math/divmod.cc | 15 | ||||
-rw-r--r-- | zap/source/any/math/exp.c | 27 | ||||
-rw-r--r-- | zap/source/any/math/exp.cc | 23 | ||||
-rw-r--r-- | zap/source/any/mem/cp.c | 12 | ||||
-rw-r--r-- | zap/source/any/mem/eq.c | 14 | ||||
-rw-r--r-- | zap/source/any/mem/fill.c | 10 | ||||
-rw-r--r-- | zap/source/any/mem/srch.c | 13 | ||||
-rw-r--r-- | zap/source/any/mem/streq.c | 23 | ||||
-rw-r--r-- | zap/source/any/str/fmt.cc | 43 | ||||
-rw-r--r-- | zap/source/any/str/fmtlen.cc | 37 | ||||
-rw-r--r-- | zap/source/any/str/numdig.hh | 12 | ||||
-rw-r--r-- | zap/source/any/str/streq.c (renamed from zap/source/any/mem/strlen.c) | 14 | ||||
-rw-r--r-- | zap/source/any/str/strlen.c (renamed from zap/source/any/math/div0.c) | 10 | ||||
-rw-r--r-- | zap/source/any/str/utf8dec.c (renamed from zap/source/any/mem/utf8dec.c) | 12 | ||||
-rw-r--r-- | zap/source/any/str/utf8declen.c (renamed from zap/source/any/mem/utf8declen.c) | 21 | ||||
-rw-r--r-- | zap/source/any/str/utf8enc.c (renamed from zap/source/any/mem/utf8enc.c) | 12 | ||||
-rw-r--r-- | zap/source/any/str/utf8enclen.c (renamed from zap/source/any/mem/utf8enclen.c) | 13 | ||||
-rw-r--r-- | zap/source/any/str/win1252dec.c (renamed from zap/source/any/mem/win1252dec.c) | 12 | ||||
-rw-r--r-- | zap/source/any/str/win1252enc.c (renamed from zap/source/any/mem/win1252enc.c) | 12 |
28 files changed, 749 insertions, 330 deletions
diff --git a/zap/GNUmakefile b/zap/GNUmakefile index 2814f55..8b38661 100644 --- a/zap/GNUmakefile +++ b/zap/GNUmakefile @@ -10,27 +10,36 @@ endif $(info Detected architecture $(arch) !) endif +ifeq "$(stdc)" "" +stdc := c99 +endif + +ifeq "$(stdcxx)" "" +stdcxx := c++14 +endif + OBJ_BS_TRAP := source/any/bs/trap.o OBJ_MATH_ABS := source/any/math/abs.o -OBJ_MATH_DIV0 := source/any/math/div0.o OBJ_MATH_DIVMOD := source/any/math/divmod.o OBJ_MATH_EXP := source/any/math/exp.o OBJ_MEM_CP := source/any/mem/cp.o OBJ_MEM_EQ := source/any/mem/eq.o OBJ_MEM_FILL := source/any/mem/fill.o OBJ_MEM_SRCH := source/any/mem/srch.o -OBJ_MEM_STREQ := source/any/mem/streq.o -OBJ_MEM_STRLEN := source/any/mem/strlen.o -OBJ_MEM_UTF8DEC := source/any/mem/utf8dec.o -OBJ_MEM_UTF8DECLEN := source/any/mem/utf8declen.o -OBJ_MEM_UTF8ENC := source/any/mem/utf8enc.o -OBJ_MEM_UTF8ENCLEN := source/any/mem/utf8enclen.o -OBJ_MEM_WIN1252DEC := source/any/mem/win1252dec.o -OBJ_MEM_WIN1252ENC := source/any/mem/win1252enc.o +OBJ_STR_FMT := source/any/str/fmt.o +OBJ_STR_FMTLEN := source/any/str/fmtlen.o +OBJ_STR_STREQ := source/any/str/streq.o +OBJ_STR_STRLEN := source/any/str/strlen.o +OBJ_STR_UTF8DEC := source/any/str/utf8dec.o +OBJ_STR_UTF8DECLEN := source/any/str/utf8declen.o +OBJ_STR_UTF8ENC := source/any/str/utf8enc.o +OBJ_STR_UTF8ENCLEN := source/any/str/utf8enclen.o +OBJ_STR_WIN1252DEC := source/any/str/win1252dec.o +OBJ_STR_WIN1252ENC := source/any/str/win1252enc.o ifeq "$(arch)" "amd64" OBJ_BS_TRAP := source/$(arch)/bs/trap.o -OBJ_MATH_ABS := source/$(arch)/math/abs.o +#OBJ_MATH_ABS := source/$(arch)/math/abs.o OBJ_MEM_CP := source/$(arch)/mem/cp.o OBJ_MEM_FILL := source/$(arch)/mem/fill.o else ifeq "$(arch)" "arm64" @@ -41,21 +50,22 @@ endif OBJS := \ $(OBJ_BS_TRAP) \ $(OBJ_MATH_ABS) \ - $(OBJ_MATH_DIV0) \ $(OBJ_MATH_DIVMOD) \ $(OBJ_MATH_EXP) \ $(OBJ_MEM_CP) \ $(OBJ_MEM_EQ) \ $(OBJ_MEM_FILL) \ $(OBJ_MEM_SRCH) \ - $(OBJ_MEM_STREQ) \ - $(OBJ_MEM_STRLEN) \ - $(OBJ_MEM_UTF8DEC) \ - $(OBJ_MEM_UTF8DECLEN) \ - $(OBJ_MEM_UTF8ENC) \ - $(OBJ_MEM_UTF8ENCLEN) \ - $(OBJ_MEM_WIN1252DEC) \ - $(OBJ_MEM_WIN1252ENC) + $(OBJ_STR_FMT) \ + $(OBJ_STR_FMTLEN) \ + $(OBJ_STR_STREQ) \ + $(OBJ_STR_STRLEN) \ + $(OBJ_STR_UTF8DEC) \ + $(OBJ_STR_UTF8DECLEN) \ + $(OBJ_STR_UTF8ENC) \ + $(OBJ_STR_UTF8ENCLEN) \ + $(OBJ_STR_WIN1252DEC) \ + $(OBJ_STR_WIN1252ENC) LIB := libzap.a @@ -69,13 +79,25 @@ CFLAGS := \ -Oz \ -Wall \ -Wextra \ - -Wpadded \ -Wpedantic \ -ffreestanding \ -fshort-enums \ -nostdlib \ -pipe \ - -std=c99 + -std=$(stdc) + +CXXFLAGS := \ + -Iinclude \ + -Oz \ + -Wall \ + -Wextra \ + -Wpedantic \ + -ffreestanding \ + -fno-exceptions \ + -fshort-enums \ + -nostdlib \ + -pipe \ + -std=$(stdcxx) .PHONY: clean install purge diff --git a/zap/include/zap/bs.h b/zap/include/zap/bs.h index 0b7a770..78d09f9 100644 --- a/zap/include/zap/bs.h +++ b/zap/include/zap/bs.h @@ -9,7 +9,16 @@ #include <csys.h> -#if defined(__cplusplus) +#if \ + __cplusplus < 201402 \ + && __STDC_VERSION__ < 199901 +// C99: long long +// C++11: constexpr +// C++14: template variables +#error At least C99 or C++14 is required! +#endif + +#if __cplusplus #define zap_priv_cdecl extern "C" { #define zap_priv_cdeclend } #else @@ -17,11 +26,47 @@ #define zap_priv_cdeclend #endif +#if defined(__has_builtin) +#define zap_priv_hasbuiltin(builtin) __has_builtin(builtin) +#else +#define zap_priv_hasbuiltin(builtin) ((int)+0x0) +#endif + +#if zap_priv_hasbuiltin(__builtin_expect) +#define zap_priv_likly(expr) if (__builtin_expect((expr),0x1)) +#define zap_priv_ulikly(expr) if (__builtin_expect((expr),0x0)) +#elif __cplusplus > 202002 +#define zap_priv_likly(expr) if ((expr)) [[likely]] +#define zap_priv_ulikly(expr) if ((expr)) [[unlikely]] +#else +#define zap_priv_likly(expr) if ((expr)) +#define zap_priv_ulikly(expr) if ((expr)) +#endif + +#if __cplusplus +#if zap_priv_hasbuiltin(__builtin_is_constant_evaluated) +#define zap_priv_ifconsteval if (__builtin_is_constant_evaluated()) +#define zap_priv_ifnconsteval if (!__builtin_is_constant_evaluated()) +#elif __cplusplus > 202002 +#define zap_priv_ifconsteval if consteval +#define zap_priv_ifnconsteval if !consteval +#else +#define zap_priv_ifconsteval if (false) +#define zap_priv_ifnconsteval if (true) +#endif +#endif + +#if defined(__GNUC__) +#define zap_priv_inln __attribute__ ((always_inline)) +#else +#define zap_priv_inln +#endif + #if defined(__GNUC__) #define zap_priv_noret __attribute__ ((noreturn)) -#elif __STDC_VERSION__ == 202311 || __cplusplus == 201103 +#elif __STDC_VERSION__ >= 202311 || __cplusplus >= 201103 #define zap_priv_noret [[noreturn]] -#elif __STDC_VERSION__ == 199901 +#elif __STDC_VERSION__ >= 199901 #define zap_priv_noret _Noreturn #else #define zap_priv_noret @@ -35,7 +80,7 @@ #if defined(__GNUC__) #define zap_priv_restr __restrict -#elif __STDC_VERSION__ == 199901 +#elif __STDC_VERSION__ >= 199901 #define zap_priv_restr restrict #else #define zap_priv_restr @@ -43,7 +88,7 @@ #if defined(__GNUC__) #define zap_priv_unseq __attribute__ ((const)) -#elif __STDC_VERSION__ == 202311 +#elif __STDC_VERSION__ >= 202311 #define zap_priv_unseq [[unsequenced]] #else #define zap_priv_unseq @@ -51,28 +96,29 @@ zap_priv_cdecl -#define zap_ver ((unsigned long)+0x17u) +#define zap_ver ((unsigned long)+0x18u) -#define zap_bytelen ((zap_sz)+0x8u) -#define zap_nopos zap_maxval_sz +#define zap_nopos zap_maxvalsz #define zap_nullptr ((void *)0x0) -#define zap_minval_uc ((unsigned char) +0x0u) -#define zap_minval_us ((unsigned short) +0x0u) -#define zap_minval_ui ((unsigned int) +0x0u) -#define zap_minval_ul ((unsigned long) +0x0u) -#define zap_minval_ull ((unsigned long long)+0x0) -#define zap_minval_sc ((signed char) -0x80) -#define zap_minval_s ((short) -0x8000) -#define zap_minval_ll ((long long) -0x8000000000000000) - -#define zap_maxval_uc ((unsigned char) +0xFFu) -#define zap_maxval_us ((unsigned short) +0xFFFFu) -#define zap_maxval_ull ((unsigned long long)+0xFFFFFFFFFFFFFFFFu) -#define zap_maxval_sc ((signed char) +0x7F) -#define zap_maxval_s ((short) +0x7FFF) -#define zap_maxval_ll ((long long) +0x7FFFFFFFFFFFFFFF) +#define zap_bytelen ((zap_sz)+0x8u) + +#define zap_minvaluc ((unsigned char) +0x0u) +#define zap_minvalus ((unsigned short) +0x0u) +#define zap_minvalui ((unsigned int) +0x0u) +#define zap_minvalul ((unsigned long) +0x0u) +#define zap_minvalull ((unsigned long long)+0x0) +#define zap_minvalsc ((signed char) -0x80) +#define zap_minvals ((short) -0x8000) +#define zap_minvalll ((long long) -0x8000000000000000) + +#define zap_maxvaluc ((unsigned char) +0xFFu) +#define zap_maxvalus ((unsigned short) +0xFFFFu) +#define zap_maxvalull ((unsigned long long)+0xFFFFFFFFFFFFFFFFu) +#define zap_maxvalsc ((signed char) +0x7F) +#define zap_maxvals ((short) +0x7FFF) +#define zap_maxvalll ((long long) +0x7FFFFFFFFFFFFFFF) typedef unsigned char zap_i8; typedef unsigned short zap_i01; @@ -80,24 +126,24 @@ typedef unsigned short zap_i01; typedef signed char zap_i8s; typedef short zap_i01s; -#define zap_minval_i8 zap_minval_uc -#define zap_minval_i01 zap_minval_us -#define zap_minval_i8s zap_minval_sc -#define zap_minval_i01s zap_minval_s +#define zap_minval8 zap_minvaluc +#define zap_minval01 zap_minvalus +#define zap_minval8s zap_minvalsc +#define zap_minval01s zap_minvals -#define zap_maxval_i8 zap_maxval_uc -#define zap_maxval_i01 zap_maxval_us -#define zap_maxval_i8s zap_maxval_sc -#define zap_maxval_i01s zap_maxval_s +#define zap_maxval8 zap_maxvaluc +#define zap_maxval01 zap_maxvalus +#define zap_maxval8s zap_maxvalsc +#define zap_maxval01s zap_maxvals #if \ csys_arch_arm \ || csys_arch_arm64 -#define zap_minval_c zap_minval_uc -#define zap_maxval_c zap_maxval_uc +#define zap_minvalc zap_minvaluc +#define zap_maxvalc zap_maxvaluc #else -#define zap_minval_c zap_minval_sc -#define zap_maxval_c zap_maxval_sc +#define zap_minvalc zap_minvalsc +#define zap_maxvalc zap_maxvalsc #endif #if \ @@ -106,85 +152,85 @@ typedef short zap_i01s; || csys_arch_arm \ || csys_arch_ia32 /* int: 32 */ -#define zap_minval_i ((int) -0x80000000) +#define zap_minvali ((int) -0x80000000) -#define zap_maxval_ui ((unsigned int)+0xFFFFFFFFu) -#define zap_maxval_i ((int) +0x7FFFFFFF) +#define zap_maxvalui ((unsigned int)+0xFFFFFFFFu) +#define zap_maxvali ((int) +0x7FFFFFFF) typedef unsigned int zap_i02; typedef int zap_i02s; -#define zap_minval_i02 zap_minval_ui -#define zap_minval_i02s zap_minval_i +#define zap_minval02 zap_minvalui +#define zap_minval02s zap_minvali -#define zap_maxval_i02 zap_maxval_ui -#define zap_maxval_i02s zap_maxval_i +#define zap_maxval02 zap_maxvalui +#define zap_maxval02s zap_maxvali #else /* int: 16 */ -#define zap_minval_i ((int) -0x8000) +#define zap_minvali ((int) -0x8000) -#define zap_maxval_ui ((unsigned int)+0xFFFFu) -#define zap_maxval_i ((int) +0x7FFF) +#define zap_maxvalui ((unsigned int)+0xFFFFu) +#define zap_maxvali ((int) +0x7FFF) typedef unsigned long zap_i02; typedef long zap_i02s; -#define zap_minval_i02 zap_minval_ul -#define zap_minval_i02s zap_minval_l +#define zap_minval02 zap_minvalul +#define zap_minval02s zap_minvall -#define zap_maxval_i02 zap_maxval_ul -#define zap_maxval_i02s zap_maxval_l +#define zap_maxval02 zap_maxvalul +#define zap_maxval02s zap_maxvall #endif #if \ csys_arch_amd64 \ || csys_arch_arm64 /* long: 64 */ -#define zap_minval_l ((long) -0x8000000000000000) +#define zap_minvall ((long) -0x8000000000000000) -#define zap_maxval_ul ((unsigned long)+0xFFFFFFFFFFFFFFFFu) -#define zap_maxval_l ((long) +0x7FFFFFFFFFFFFFFF) +#define zap_maxvalul ((unsigned long)+0xFFFFFFFFFFFFFFFFu) +#define zap_maxvall ((long) +0x7FFFFFFFFFFFFFFF) typedef unsigned long zap_i04; typedef long zap_i04s; -#define zap_minval_i04 zap_minval_ul -#define zap_minval_i04s zap_minval_l +#define zap_minval04 zap_minvalul +#define zap_minval04s zap_minvall -#define zap_maxval_i04 zap_maxval_ul -#define zap_maxval_i04s zap_maxval_l +#define zap_maxval04 zap_maxvalul +#define zap_maxval04s zap_maxvall #else /* long: 32 */ -#define zap_minval_l ((long) -0x80000000) +#define zap_minvall ((long) -0x80000000) -#define zap_maxval_ul ((unsigned long)+0xFFFFFFFFu) -#define zap_maxval_l ((long) +0x7FFFFFFF) +#define zap_maxvalul ((unsigned long)+0xFFFFFFFFu) +#define zap_maxvall ((long) +0x7FFFFFFF) typedef unsigned long long zap_i04; typedef long long zap_i04s; -#define zap_minval_i04 zap_minval_ull -#define zap_minval_i04s zap_minval_ll +#define zap_minval04 zap_minvalull +#define zap_minval04s zap_minvalll -#define zap_maxval_i04 zap_maxval_ull -#define zap_maxval_i04s zap_maxval_ll +#define zap_maxval04 zap_maxvalull +#define zap_maxval04s zap_maxvalll #endif #if \ csys_arch_arm \ || csys_arch_ia32 -#define zap_minval_ptr zap_minval_i02 -#define zap_minval_sz zap_minval_i02 -#define zap_maxval_ptr zap_maxval_i02 -#define zap_maxval_sz zap_maxval_i02 +#define zap_minvalptr zap_minval02 +#define zap_minvalsz zap_minval02 +#define zap_maxvalptr zap_maxval02 +#define zap_maxvalsz zap_maxval02 typedef zap_i02 zap_ptr; typedef zap_i02 zap_sz; #else -#define zap_minval_ptr zap_minval_i04 -#define zap_minval_sz zap_minval_i04 -#define zap_maxval_ptr zap_maxval_i04 -#define zap_maxval_sz zap_maxval_i04 +#define zap_minvalptr zap_minval04 +#define zap_minvalsz zap_minval04 +#define zap_maxvalptr zap_maxval04 +#define zap_maxvalsz zap_maxval04 typedef zap_i04 zap_ptr; typedef zap_i04 zap_sz; @@ -194,4 +240,99 @@ zap_priv_noret void zap_priv_trap(void); zap_priv_cdeclend +#if __cplusplus + +namespace zap { + namespace impl { + template<typename typ> struct minval {constexpr static typ val = {};}; + + template<> struct minval<char> {constexpr static auto val = zap_minvalc;}; + template<> struct minval<int> {constexpr static auto val = zap_minvali;}; + template<> struct minval<long> {constexpr static auto val = zap_minvall;}; + template<> struct minval<long long> {constexpr static auto val = zap_minvalll;}; + template<> struct minval<short> {constexpr static auto val = zap_minvals;}; + template<> struct minval<signed char> {constexpr static auto val = zap_minvalsc;}; + template<> struct minval<unsigned char> {constexpr static auto val = zap_minvaluc;}; + template<> struct minval<unsigned int> {constexpr static auto val = zap_minvalui;}; + template<> struct minval<unsigned long> {constexpr static auto val = zap_minvalul;}; + template<> struct minval<unsigned long long> {constexpr static auto val = zap_minvalull;}; + template<> struct minval<unsigned short> {constexpr static auto val = zap_minvalus;}; + + template<typename typ> struct maxval {constexpr static typ val = {};}; + + template<> struct maxval<char> {constexpr static auto val = zap_maxvalc;}; + template<> struct maxval<int> {constexpr static auto val = zap_maxvali;}; + template<> struct maxval<long> {constexpr static auto val = zap_maxvall;}; + template<> struct maxval<long long> {constexpr static auto val = zap_maxvalll;}; + template<> struct maxval<short> {constexpr static auto val = zap_maxvals;}; + template<> struct maxval<signed char> {constexpr static auto val = zap_maxvalsc;}; + template<> struct maxval<unsigned char> {constexpr static auto val = zap_maxvaluc;}; + template<> struct maxval<unsigned int> {constexpr static auto val = zap_maxvalui;}; + template<> struct maxval<unsigned long> {constexpr static auto val = zap_maxvalul;}; + template<> struct maxval<unsigned long long> {constexpr static auto val = zap_maxvalull;}; + template<> struct maxval<unsigned short> {constexpr static auto val = zap_maxvalus;}; + } + + template<typename typ> constexpr auto minval = ::zap::impl::minval<typ>::val; + + template<typename typ> constexpr auto maxval = ::zap::impl::maxval<typ>::val; +} + +namespace zap { + namespace impl { + template<typename ityp> struct sign {using typ = ityp;}; + + template<> struct sign<unsigned char> {using typ = signed char;}; + template<> struct sign<unsigned int> {using typ = int;}; + template<> struct sign<unsigned long> {using typ = long;}; + template<> struct sign<unsigned long long> {using typ = long long;}; + template<> struct sign<unsigned short> {using typ = short;}; + + template<typename ityp> struct usign {using typ = ityp;}; + + template<> struct usign<signed char> {using typ = unsigned char;}; + template<> struct usign<int> {using typ = unsigned int;}; + template<> struct usign<long> {using typ = unsigned long;}; + template<> struct usign<long long> {using typ = unsigned long long;}; + template<> struct usign<short> {using typ = unsigned short;}; + } + + template<typename ityp> using sign = typename ::zap::impl::sign<ityp>::typ; + + template<typename ityp> using usign = typename ::zap::impl::usign<ityp>::typ; +} + +namespace zap { + namespace impl { + template<typename ltyp,typename rtyp> struct typeq {constexpr static bool eq = false;}; + + template<typename typ> struct typeq<typ,typ> {constexpr static auto eq = true;}; + } + + template<typename ltyp,typename rtyp> constexpr auto typeq = ::zap::impl::typeq<ltyp,rtyp>::eq; +} + +namespace zap { + using i8 = ::zap_i8; + using i01 = ::zap_i01; + using i02 = ::zap_i02; + using i04 = ::zap_i04; + + using i8s = ::zap_i8s; + using i01s = ::zap_i01s; + using i02s = ::zap_i02s; + using i04s = ::zap_i04s; + + using ptr = ::zap_ptr; + using sz = ::zap_sz; +} + +namespace zap { + constexpr auto ver = zap_ver; + constexpr auto bytelen = zap_bytelen; + constexpr auto nopos = zap_nopos; +} + +#endif + #endif diff --git a/zap/include/zap/math.h b/zap/include/zap/math.h index 5cacbef..c6f17c2 100644 --- a/zap/include/zap/math.h +++ b/zap/include/zap/math.h @@ -12,52 +12,214 @@ zap_priv_cdecl typedef struct { - zap_i8 quot; - zap_i8 rem; -} zap_quotrem8; - + signed char num; + signed char den; +} zap_fracsc; typedef struct { - zap_i01 quot; - zap_i01 rem; -} zap_quotrem01; - + short num; + short den; +} zap_fracs; typedef struct { - zap_i02 quot; - zap_i02 rem; -} zap_quotrem02; - + int num; + int den; +} zap_fraci; typedef struct { - zap_i04 quot; - zap_i04 rem; -} zap_quotrem04; + long num; + long den; +} zap_fracl; +typedef struct { + long long num; + long long den; +} zap_fracll; -zap_i04 zap_priv_div0(void); +typedef struct { + signed char quot; + signed char rem; +} zap_quotremsc; +typedef struct { + short quot; + short rem; +} zap_quotrems; +typedef struct { + int quot; + int rem; +} zap_quotremi; +typedef struct { + long quot; + long rem; +} zap_quotreml; +typedef struct { + long long quot; + long long rem; +} zap_quotremll; -zap_priv_unseq zap_i8 zap_abs8( zap_i8s val); -zap_priv_unseq zap_i01 zap_abs01(zap_i01s val); -zap_priv_unseq zap_i02 zap_abs02(zap_i02s val); -zap_priv_unseq zap_i04 zap_abs04(zap_i04s val); +zap_priv_nothrw zap_priv_unseq unsigned char zap_abssc(signed char val); +zap_priv_nothrw zap_priv_unseq unsigned short zap_abss( short val); +zap_priv_nothrw zap_priv_unseq unsigned int zap_absi( int val); +zap_priv_nothrw zap_priv_unseq unsigned long zap_absl( long val); +zap_priv_nothrw zap_priv_unseq unsigned long long zap_absll(long long val); +zap_priv_nothrw zap_priv_unseq float zap_absf( float val); +zap_priv_nothrw zap_priv_unseq double zap_absd( double val); +zap_priv_nothrw zap_priv_unseq long double zap_absld(long double val); -zap_priv_unseq zap_quotrem8 zap_divmod8( zap_i8 num,zap_i8 den); -zap_priv_unseq zap_quotrem01 zap_divmod01(zap_i01 num,zap_i01 den); -zap_priv_unseq zap_quotrem02 zap_divmod02(zap_i02 num,zap_i02 den); -zap_priv_unseq zap_quotrem04 zap_divmod04(zap_i04 num,zap_i04 den); +zap_priv_nothrw zap_priv_unseq zap_quotremsc zap_divmodsc(signed char num,signed char den); +zap_priv_nothrw zap_priv_unseq zap_quotrems zap_divmods( short num,short den); +zap_priv_nothrw zap_priv_unseq zap_quotremi zap_divmodi( int num,int den); +zap_priv_nothrw zap_priv_unseq zap_quotreml zap_divmodl( long num,long den); +zap_priv_nothrw zap_priv_unseq zap_quotremll zap_divmodll(long long num,long long den); +zap_priv_nothrw zap_priv_unseq zap_quotremsc zap_divmodsc(signed char num,signed char den); +zap_priv_nothrw zap_priv_unseq zap_quotrems zap_divmods( short num,short den); +zap_priv_nothrw zap_priv_unseq zap_quotremi zap_divmodi( int num,int den); +zap_priv_nothrw zap_priv_unseq zap_quotreml zap_divmodl( long num,long den); +zap_priv_nothrw zap_priv_unseq zap_quotremll zap_divmodll(long long num,long long den); -zap_priv_unseq zap_i8 zap_exp8( zap_i8 val,zap_i8 n); -zap_priv_unseq zap_i01 zap_exp01(zap_i01 val,zap_i01 n); -zap_priv_unseq zap_i02 zap_exp02(zap_i02 val,zap_i02 n); -zap_priv_unseq zap_i04 zap_exp04(zap_i04 val,zap_i04 n); +zap_priv_nothrw zap_priv_unseq unsigned char zap_loguc( unsigned char val,unsigned char n); +zap_priv_nothrw zap_priv_unseq unsigned short zap_logus( unsigned short val,unsigned short n); +zap_priv_nothrw zap_priv_unseq unsigned int zap_logui( unsigned int val,unsigned int n); +zap_priv_nothrw zap_priv_unseq unsigned long zap_logul( unsigned long val,unsigned long n); +zap_priv_nothrw zap_priv_unseq unsigned long long zap_logull(unsigned long long val,unsigned long long n); +zap_priv_nothrw zap_priv_unseq char zap_logsc( char val,signed char n); +zap_priv_nothrw zap_priv_unseq short zap_logs( short val,short n); +zap_priv_nothrw zap_priv_unseq int zap_logi( int val,int n); +zap_priv_nothrw zap_priv_unseq long zap_logl( long val,long n); +zap_priv_nothrw zap_priv_unseq long long zap_logll( long long val,long long n); +zap_priv_nothrw zap_priv_unseq float zap_logf( float val,float n); +zap_priv_nothrw zap_priv_unseq double zap_logd( double val,double n); +zap_priv_nothrw zap_priv_unseq long double zap_logld( long double val,long double n); -zap_priv_unseq zap_i8 zap_log8( zap_i8 val,zap_i8 n); -zap_priv_unseq zap_i01 zap_log01(zap_i01 val,zap_i01 n); -zap_priv_unseq zap_i02 zap_log02(zap_i02 val,zap_i02 n); -zap_priv_unseq zap_i04 zap_log04(zap_i04 val,zap_i04 n); +zap_priv_nothrw zap_priv_unseq unsigned char zap_expuc( unsigned char val,unsigned char n); +zap_priv_nothrw zap_priv_unseq unsigned short zap_expus( unsigned short val,unsigned short n); +zap_priv_nothrw zap_priv_unseq unsigned int zap_expui( unsigned int val,unsigned int n); +zap_priv_nothrw zap_priv_unseq unsigned long zap_expul( unsigned long val,unsigned long n); +zap_priv_nothrw zap_priv_unseq unsigned long long zap_expull(unsigned long long val,unsigned long long n); +zap_priv_nothrw zap_priv_unseq signed char zap_expsc( signed char val,signed char n); +zap_priv_nothrw zap_priv_unseq short zap_exps( short val,short n); +zap_priv_nothrw zap_priv_unseq int zap_expi( int val,int n); +zap_priv_nothrw zap_priv_unseq long zap_expl( long val,long n); +zap_priv_nothrw zap_priv_unseq long long zap_expll( long long val,long long n); +zap_priv_nothrw zap_priv_unseq float zap_expf( float val,float n); +zap_priv_nothrw zap_priv_unseq double zap_expd( double val,double n); +zap_priv_nothrw zap_priv_unseq long double zap_expld( long double val,long double n); -zap_priv_unseq zap_i8 zap_root8( zap_i8 val,zap_i8 n); -zap_priv_unseq zap_i01 zap_root01(zap_i01 val,zap_i01 n); -zap_priv_unseq zap_i02 zap_root02(zap_i02 val,zap_i02 n); -zap_priv_unseq zap_i04 zap_root04(zap_i04 val,zap_i04 n); +zap_priv_nothrw zap_priv_unseq unsigned char zap_rootuc( unsigned char val,unsigned char n); +zap_priv_nothrw zap_priv_unseq unsigned short zap_rootus( unsigned short val,unsigned short n); +zap_priv_nothrw zap_priv_unseq unsigned int zap_rootui( unsigned int val,unsigned int n); +zap_priv_nothrw zap_priv_unseq unsigned long zap_rootul( unsigned long val,unsigned long n); +zap_priv_nothrw zap_priv_unseq unsigned long long zap_rootull(unsigned long long val,unsigned long long n); +zap_priv_nothrw zap_priv_unseq char zap_rootsc( char val,signed char n); +zap_priv_nothrw zap_priv_unseq short zap_roots( short val,short n); +zap_priv_nothrw zap_priv_unseq int zap_rooti( int val,int n); +zap_priv_nothrw zap_priv_unseq long zap_rootl( long val,long n); +zap_priv_nothrw zap_priv_unseq long long zap_rootll( long long val,long long n); +zap_priv_nothrw zap_priv_unseq float zap_rootf( float val,float n); +zap_priv_nothrw zap_priv_unseq double zap_rootd( double val,double n); +zap_priv_nothrw zap_priv_unseq long double zap_rootld( long double val,long double n); zap_priv_cdeclend +#if __cplusplus + +namespace zap { + namespace impl { + template<typename typ> struct inf {constexpr static auto val = ::zap::maxval<typ>;}; + +#if zap_priv_hasbuiltin(__builtin_huge_valf) + template<> struct inf<float> {constexpr static auto val = __builtin_huge_valf();}; +#endif + +#if zap_priv_hasbuiltin(__builtin_huge_val) + template<> struct inf<double> {constexpr static auto val = __builtin_huge_val();}; +#endif + +#if zap_priv_hasbuiltin(__builtin_huge_vall) + template<> struct inf<long double> {constexpr static auto val = __builtin_huge_vall();}; +#endif + } + + template<typename typ> constexpr auto inf = ::zap::impl::inf<typ>::val; +} + +namespace zap { + namespace impl { + template<typename ityp> struct cfractyp {using typ = ityp;}; + + template<> struct cfractyp<signed char> {using typ = ::zap_fracsc;}; + template<> struct cfractyp<short> {using typ = ::zap_fracs;}; + template<> struct cfractyp<int> {using typ = ::zap_fraci;}; + template<> struct cfractyp<long> {using typ = ::zap_fracl;}; + template<> struct cfractyp<long long> {using typ = ::zap_fracll;}; + + template<typename ityp> struct cquotremtyp {using typ = ityp;}; + + template<> struct cquotremtyp<signed char> {using typ = ::zap_quotremsc;}; + template<> struct cquotremtyp<short> {using typ = ::zap_quotrems;}; + template<> struct cquotremtyp<int> {using typ = ::zap_quotremi;}; + template<> struct cquotremtyp<long> {using typ = ::zap_quotreml;}; + template<> struct cquotremtyp<long long> {using typ = ::zap_quotremll;}; + } + + template<typename typ> class frac { + public: + using cfractyp = typename ::zap::impl::cfractyp<typ>::typ; + + typ num; + typ den; + + constexpr auto cfrac() noexcept -> cfractyp { + cfractyp frac; + frac.num = this->num; + frac.den = this->den; + return frac; + } + }; + + template<typename typ> class quotrem { + public: + using cquotremtyp = typename ::zap::impl::cquotremtyp<typ>::typ; + + typ quot; + typ rem; + + constexpr auto cquotrem() noexcept -> cquotremtyp { + cquotremtyp quotrem; + quotrem.quot = this->quot; + quotrem.rem = this->rem; + return quotrem; + } + }; +} + +namespace zap { + template<typename typ> constexpr auto abs(typ const val) noexcept -> ::zap::usign<typ> { + using newtyp = ::zap::usign<typ>; + if (val > typ {0x0}) return static_cast<newtyp>(val); + return 0x0u-static_cast<newtyp>(val); + } + + template<typename typ> constexpr auto divmod(typ const num,typ const den) noexcept -> ::zap::quotrem<typ> { + ::zap::quotrem<typ> quotrem; + zap_priv_ulikly (den == 0x0) { + quotrem.quot = ::zap::inf<typ>; + quotrem.rem = quotrem.quot; + return quotrem; + } + for (quotrem = ::zap::quotrem<typ> {typ {0x0},num};quotrem.rem >= den;++quotrem.quot,quotrem.rem -= den) {} + return quotrem; + } + + template<typename typ> constexpr auto exp(typ const val,typ const n) noexcept -> typ { + zap_priv_ulikly (n == typ {0x0}) { + return typ {0x1}; + } + zap_priv_ulikly (val == typ {0x0}) { + return typ {0x0}; + } + typ exp = val; + for (typ i = typ {0x1};i < n;++i) exp *= val; + return exp; + } +} + +#endif + #endif diff --git a/zap/include/zap/mem.h b/zap/include/zap/mem.h index 8110a85..36f06b9 100644 --- a/zap/include/zap/mem.h +++ b/zap/include/zap/mem.h @@ -11,22 +11,42 @@ zap_priv_cdecl -void zap_cp( void * zap_priv_restr dest,void const * zap_priv_restr src, zap_sz num); -zap_i8 zap_eq( void const * lbuf,void const * rbuf,zap_sz num); -void zap_fill(void * dest,zap_i8 val, zap_sz num); -void * zap_srch(void const * buf, zap_i8 val, zap_sz num); +zap_priv_nothrw void zap_cp( void * zap_priv_restr dest,void const * zap_priv_restr src, zap_sz num); +zap_priv_nothrw zap_i8 zap_eq( void const * lbuf,void const * rbuf,zap_sz num); +zap_priv_nothrw void zap_fill(void * dest,unsigned char val, zap_sz num); +zap_priv_nothrw void * zap_srch(void const * buf, unsigned char val, zap_sz num); -zap_i8 zap_streq( char const * lstr,char const * rstr); -zap_sz zap_strlen(char const * str); - -zap_sz zap_utf8declen(zap_i8 const * buf); -zap_sz zap_utf8enclen(zap_i02 const * buf); +zap_priv_cdeclend -void zap_utf8dec( zap_i02 * zap_priv_restr dest,zap_i8 const * zap_priv_restr src); -void zap_utf8enc( zap_i8 * zap_priv_restr dest,zap_i02 const * zap_priv_restr src); -void zap_win1252dec(zap_i02 * zap_priv_restr dest,zap_i8 const * zap_priv_restr src); -void zap_win1252enc(zap_i8 * zap_priv_restr dest,zap_i02 const * zap_priv_restr src); +#if __cplusplus + +namespace zap { + template<typename typ> constexpr auto cp(typ * zap_priv_restr dest,typ const * zap_priv_restr src,::zap::sz const num) noexcept -> void { + typ * const zap_priv_restr stop = dest + num; + while (dest != stop) *dest++ = *src++; + } + + template<typename typ> constexpr auto eq(typ const * lbuf,typ const * rbuf,::zap::sz const num) noexcept -> bool { + typ const * const stop = lbuf + num; + while (lbuf != stop) if (*lbuf++ != *rbuf++) return false; + return true; + } + + template<typename typ> constexpr auto fill(typ * dest,typ const val,::zap::sz const num) noexcept -> void { + typ * const stop = dest + num; + while (dest != stop) *dest++ = val; + } + + template<typename typ> constexpr auto srch(typ const * const buf,::zap::i8 const val,::zap::sz const num) noexcept -> typ * { + typ const * const stop = buf + num; + while (buf != stop) { + typ const * addr = buf++; + if (*addr == val) return (void *)addr; + } + return nullptr; + } +} -zap_priv_cdeclend +#endif #endif diff --git a/zap/include/zap/str.h b/zap/include/zap/str.h new file mode 100644 index 0000000..42519dc --- /dev/null +++ b/zap/include/zap/str.h @@ -0,0 +1,49 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#if !defined(zap_priv_hdr_str) +#define zap_priv_hdr_str + +#include <zap/bs.h> + +zap_priv_cdecl + +zap_i8 zap_streq( char const * lstr,char const * rstr); +zap_sz zap_strlen(char const * str); + +zap_sz zap_utf8declen(zap_i8 const * buf); +zap_sz zap_utf8enclen(zap_i02 const * buf); + +void zap_utf8dec( zap_i02 * zap_priv_restr dest,zap_i8 const * zap_priv_restr src); +void zap_utf8enc( zap_i8 * zap_priv_restr dest,zap_i02 const * zap_priv_restr src); +void zap_win1252dec(zap_i02 * zap_priv_restr dest,zap_i8 const * zap_priv_restr src); +void zap_win1252enc(zap_i8 * zap_priv_restr dest,zap_i02 const * zap_priv_restr src); + +void zap_fmti( zap_i02 * buf,int val,zap_i8 bs,zap_i8 rtl); +void zap_fmtl( zap_i02 * buf,long val,zap_i8 bs,zap_i8 rtl); +void zap_fmtll( zap_i02 * buf,long long val,zap_i8 bs,zap_i8 rtl); +void zap_fmts( zap_i02 * buf,short val,zap_i8 bs,zap_i8 rtl); +void zap_fmtsc( zap_i02 * buf,signed char val,zap_i8 bs,zap_i8 rtl); +void zap_fmtuc( zap_i02 * buf,unsigned char val,zap_i8 bs,zap_i8 rtl); +void zap_fmtui( zap_i02 * buf,unsigned int val,zap_i8 bs,zap_i8 rtl); +void zap_fmtul( zap_i02 * buf,unsigned long val,zap_i8 bs,zap_i8 rtl); +void zap_fmtull(zap_i02 * buf,unsigned long long val,zap_i8 bs,zap_i8 rtl); +void zap_fmtus( zap_i02 * buf,unsigned short val,zap_i8 bs,zap_i8 rtl); + +zap_i8 zap_fmtleni( int val,zap_i8 bs); +zap_i8 zap_fmtlenl( long val,zap_i8 bs); +zap_i8 zap_fmtlenll( long long val,zap_i8 bs); +zap_i8 zap_fmtlens( short val,zap_i8 bs); +zap_i8 zap_fmtlensc( signed char val,zap_i8 bs); +zap_i8 zap_fmtlenuc( unsigned char val,zap_i8 bs); +zap_i8 zap_fmtlenui( unsigned int val,zap_i8 bs); +zap_i8 zap_fmtlenul( unsigned long val,zap_i8 bs); +zap_i8 zap_fmtlenull(unsigned long long val,zap_i8 bs); +zap_i8 zap_fmtlenus( unsigned short val,zap_i8 bs); + +zap_priv_cdeclend + +#endif diff --git a/zap/source/any/bs/trap.c b/zap/source/any/bs/trap.c index 9428d44..552572d 100644 --- a/zap/source/any/bs/trap.c +++ b/zap/source/any/bs/trap.c @@ -7,10 +7,8 @@ #include <zap/bs.h> void zap_priv_trap(void) { -#if defined(__has_builtin) -#if __has_builtin(__builtin_trap) +#if zap_priv_hasbuiltin(__builtin_trap) __builtin_trap(); #endif -#endif for (;;) {} } diff --git a/zap/source/any/math/abs.c b/zap/source/any/math/abs.c deleted file mode 100644 index d12b6a6..0000000 --- a/zap/source/any/math/abs.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - Copyright 2022-2023 Gabriel Jensen. - This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. - If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. -*/ - -#include <zap/math.h> - -#define zap_priv_abs(_wdth) \ -zap_i##_wdth zap_abs##_wdth(zap_i##_wdth##s const _val) { \ - if (_val > 0x0) { \ - return (zap_i##_wdth)_val; \ - } \ - return (zap_i##_wdth)(0x0 - _val); \ -} - -zap_priv_abs(8) -zap_priv_abs(01) -zap_priv_abs(02) -zap_priv_abs(04) diff --git a/zap/source/any/math/abs.cc b/zap/source/any/math/abs.cc new file mode 100644 index 0000000..3e6f53e --- /dev/null +++ b/zap/source/any/math/abs.cc @@ -0,0 +1,18 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/math.h> + +extern "C" { + zap_priv_nothrw unsigned char zap_abssc(signed char const val) {return ::zap::abs(val);} + zap_priv_nothrw unsigned short zap_abss( short const val) {return ::zap::abs(val);} + zap_priv_nothrw unsigned int zap_absi( int const val) {return ::zap::abs(val);} + zap_priv_nothrw unsigned long zap_absl( long const val) {return ::zap::abs(val);} + zap_priv_nothrw unsigned long long zap_absll(long long const val) {return ::zap::abs(val);} + zap_priv_nothrw float zap_absf( float const val) {return ::zap::abs(val);} + zap_priv_nothrw double zap_absd( double const val) {return ::zap::abs(val);} + zap_priv_nothrw long double zap_absld(long double const val) {return ::zap::abs(val);} +} diff --git a/zap/source/any/math/divmod.c b/zap/source/any/math/divmod.c deleted file mode 100644 index 4214651..0000000 --- a/zap/source/any/math/divmod.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - Copyright 2022-2023 Gabriel Jensen. - This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. - If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. -*/ - -#include <zap/math.h> - -#define zap_priv_divmod(_wdth) \ -zap_quotrem##_wdth zap_divmod##_wdth(zap_i##_wdth const _num,zap_i##_wdth const _den) { \ -zap_quotrem##_wdth quotrem; \ - if (__builtin_expect(_den == 0x0,0x0)) { \ - quotrem.quot = zap_priv_div0(); \ - quotrem.rem = quotrem.quot; \ - return quotrem; \ - } \ - for (quotrem = (zap_quotrem##_wdth){.quot = 0x0u,.rem = _num};quotrem.rem >= _den;++quotrem.quot,quotrem.rem -= _den) {} \ - return quotrem; \ -} - -zap_priv_divmod(8) -zap_priv_divmod(01) -zap_priv_divmod(02) -zap_priv_divmod(04) diff --git a/zap/source/any/math/divmod.cc b/zap/source/any/math/divmod.cc new file mode 100644 index 0000000..c686c15 --- /dev/null +++ b/zap/source/any/math/divmod.cc @@ -0,0 +1,15 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/math.h> + +extern "C" { + zap_priv_nothrw auto zap_divmodsc(signed char const num,signed char const den) -> ::zap_quotremsc {return ::zap::divmod(num,den).cquotrem();} + zap_priv_nothrw auto zap_divmods( short const num,short const den) -> ::zap_quotrems {return ::zap::divmod(num,den).cquotrem();} + zap_priv_nothrw auto zap_divmodi( int const num,int const den) -> ::zap_quotremi {return ::zap::divmod(num,den).cquotrem();} + zap_priv_nothrw auto zap_divmodl( long const num,long const den) -> ::zap_quotreml {return ::zap::divmod(num,den).cquotrem();} + zap_priv_nothrw auto zap_divmodll(long long const num,long long const den) -> ::zap_quotremll {return ::zap::divmod(num,den).cquotrem();} +} diff --git a/zap/source/any/math/exp.c b/zap/source/any/math/exp.c deleted file mode 100644 index bf0869d..0000000 --- a/zap/source/any/math/exp.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - Copyright 2022-2023 Gabriel Jensen. - This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. - If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. -*/ - -#include <zap/math.h> - -#define zap_priv_exp(_wdth) \ -zap_i##_wdth zap_exp##_wdth(zap_i##_wdth const _val,zap_i##_wdth const _n) { \ - if (__builtin_expect(_n == 0x0u,0x0)) { \ - return 0x1u; \ - } \ - if (__builtin_expect(_val == 0x0u,0x0)) { \ - return 0x0u; \ - } \ - zap_i##_wdth val = _val; \ - for (zap_i##_wdth i = 0x1u;i < _n;++i) { \ - val *= _val; \ - } \ - return val; \ -} - -zap_priv_exp(8) -zap_priv_exp(01) -zap_priv_exp(02) -zap_priv_exp(04) diff --git a/zap/source/any/math/exp.cc b/zap/source/any/math/exp.cc new file mode 100644 index 0000000..2d4831b --- /dev/null +++ b/zap/source/any/math/exp.cc @@ -0,0 +1,23 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/math.h> + +extern "C" { + zap_priv_nothrw unsigned char zap_expuc( unsigned char val,unsigned char n) {return ::zap::exp(val,n);} + zap_priv_nothrw unsigned short zap_expus( unsigned short val,unsigned short n) {return ::zap::exp(val,n);} + zap_priv_nothrw unsigned int zap_expui( unsigned int val,unsigned int n) {return ::zap::exp(val,n);} + zap_priv_nothrw unsigned long zap_expul( unsigned long val,unsigned long n) {return ::zap::exp(val,n);} + zap_priv_nothrw unsigned long long zap_expull(unsigned long long val,unsigned long long n) {return ::zap::exp(val,n);} + zap_priv_nothrw signed char zap_expsc( signed char val,signed char n) {return ::zap::exp(val,n);} + zap_priv_nothrw short zap_exps( short val,short n) {return ::zap::exp(val,n);} + zap_priv_nothrw int zap_expi( int val,int n) {return ::zap::exp(val,n);} + zap_priv_nothrw long zap_expl( long val,long n) {return ::zap::exp(val,n);} + zap_priv_nothrw long long zap_expll( long long val,long long n) {return ::zap::exp(val,n);} + zap_priv_nothrw float zap_expf( float val,float n) {return ::zap::exp(val,n);} + zap_priv_nothrw double zap_expd( double val,double n) {return ::zap::exp(val,n);} + zap_priv_nothrw long double zap_expld( long double val,long double n) {return ::zap::exp(val,n);} +} diff --git a/zap/source/any/mem/cp.c b/zap/source/any/mem/cp.c index 2c1b265..9752721 100644 --- a/zap/source/any/mem/cp.c +++ b/zap/source/any/mem/cp.c @@ -6,11 +6,9 @@ #include <zap/mem.h> -void zap_cp(void * const zap_priv_restr _dest,void const * const zap_priv_restr _src,zap_sz const _num) { - zap_i8 * dest; - zap_i8 const * src; - zap_i8 * const stop = (zap_i8 *)_dest + _num; - for (dest = _dest,src = _src;dest != stop;++dest,++src) { - *dest = *src; - } +void zap_cp(void * const zap_priv_restr voiddest,void const * const zap_priv_restr voidsrc,zap_sz const num) { + unsigned char * dest = voiddest; + unsigned char const * src = voidsrc; + unsigned char * const stop = dest + num; + while (dest != stop) *dest++ = *src++; } diff --git a/zap/source/any/mem/eq.c b/zap/source/any/mem/eq.c index 3f6cd8e..5ff5c4f 100644 --- a/zap/source/any/mem/eq.c +++ b/zap/source/any/mem/eq.c @@ -6,14 +6,10 @@ #include <zap/mem.h> -zap_i8 zap_eq(void const * const _lbuf,void const * const _rbuf,zap_sz const _num) { - zap_i8 const * lbuf; - zap_i8 const * rbuf; - zap_i8 * const stop = (zap_i8 *)_lbuf + _num; - for (lbuf = _lbuf,rbuf = _rbuf;lbuf != stop;++lbuf,++rbuf) { - if (*lbuf != *rbuf) { - return 0x0u; - } - } +zap_i8 zap_eq(void const * const voidlbuf,void const * const voidrbuf,zap_sz const num) { + unsigned char const * lbuf = voidlbuf; + unsigned char const * rbuf = voidrbuf; + unsigned char const * const stop = lbuf + num; + while (lbuf != stop) if (*lbuf++ != *rbuf++) return 0x0u; return 0x1u; } diff --git a/zap/source/any/mem/fill.c b/zap/source/any/mem/fill.c index dc59263..548dfeb 100644 --- a/zap/source/any/mem/fill.c +++ b/zap/source/any/mem/fill.c @@ -6,10 +6,8 @@ #include <zap/mem.h> -void zap_fill(void * const _dest,zap_i8 const _val,zap_sz const _num) { - zap_i8 * dest; - zap_i8 * const stop = (zap_i8 *)_dest + _num; - for (dest = _dest;dest != stop;++dest) { - *dest = _val; - } +void zap_fill(void * const voiddest,unsigned char const val,zap_sz const num) { + unsigned char * dest = voiddest; + unsigned char * const stop = dest + num; + while (dest != stop) *dest++ = val; } diff --git a/zap/source/any/mem/srch.c b/zap/source/any/mem/srch.c index 9f86634..46c8ec8 100644 --- a/zap/source/any/mem/srch.c +++ b/zap/source/any/mem/srch.c @@ -6,13 +6,12 @@ #include <zap/mem.h> -void * zap_srch(void const * const _buf,zap_i8 const _val,zap_sz const _num) { - zap_i8 const * buf; - zap_i8 * const stop = (zap_i8 *)_buf + _num; - for (buf = _buf;buf != stop;++buf) { - if (*buf == _val) { - return (void *)buf; - } +void * zap_srch(void const * const voidbuf,zap_i8 const val,zap_sz const num) { + unsigned char const * buf = voidbuf; + unsigned char const * const stop = buf + num; + while (buf != stop) { + unsigned char const * addr = buf++; + if (*addr == val) return (void *)addr; } return zap_nullptr; } diff --git a/zap/source/any/mem/streq.c b/zap/source/any/mem/streq.c deleted file mode 100644 index 69c6bf6..0000000 --- a/zap/source/any/mem/streq.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright 2022-2023 Gabriel Jensen. - This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. - If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. -*/ - -#include <zap/mem.h> - -zap_i8 zap_streq(char const * const _lstr,char const * const _rstr) { - char const * lpos = _lstr; - char const * rpos = _rstr; - for (;;++lpos,++rpos) { - char const lchr = *lpos; - char const rchr = *rpos; - if (lchr != rchr) { - return 0x0u; - } - if (lchr == '\x0') { - break; - } - } - return zap_maxval_i8; -} diff --git a/zap/source/any/str/fmt.cc b/zap/source/any/str/fmt.cc new file mode 100644 index 0000000..b2c0c75 --- /dev/null +++ b/zap/source/any/str/fmt.cc @@ -0,0 +1,43 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/str.h> + +#include "numdig.hh" + +namespace zap { + namespace impl { + template<typename signtyp> zap_priv_inln inline static auto fmt(::zap::i02 * buf,signtyp signval,::zap::i8 const bs,::zap::i8 const rtl) noexcept -> void { + using typ = typename ::zap::usign<signtyp>; + char32_t const * digs = U"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + if (bs == 0xCu) digs = U"0123456789\u218A\u218B"; + if (signval < 0x0) { + signval = -signval; + *buf++ = 0x2212u; + } + typ val = static_cast<typ>(signval); + if (rtl) { + buf += ::zap::impl::numdig(val,bs) - 0x1u; + for (;val > 0x0u;val /= bs) *buf-- = static_cast<::zap::i02>(digs[static_cast<::zap::sz>(val % static_cast<typ>(bs))]); + return; + } + for (;val > 0x0u;val /= bs) *buf++ = static_cast<::zap::i02>(digs[static_cast<::zap::sz>(val % static_cast<typ>(bs))]); + } + } +} + +extern "C" { + auto zap_fmti( ::zap::i02 * const buf,int const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtl( ::zap::i02 * const buf,long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtll( ::zap::i02 * const buf,long long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmts( ::zap::i02 * const buf,short const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtsc( ::zap::i02 * const buf,signed char const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtuc( ::zap::i02 * const buf,unsigned char const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtui( ::zap::i02 * const buf,unsigned int const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtul( ::zap::i02 * const buf,unsigned long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtull(::zap::i02 * const buf,unsigned long long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} + auto zap_fmtus( ::zap::i02 * const buf,unsigned short const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);} +} diff --git a/zap/source/any/str/fmtlen.cc b/zap/source/any/str/fmtlen.cc new file mode 100644 index 0000000..866d011 --- /dev/null +++ b/zap/source/any/str/fmtlen.cc @@ -0,0 +1,37 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. +*/ + +#include <zap/math.h> +#include <zap/str.h> + +#include "numdig.hh" + +namespace zap { + namespace impl { + template<typename typ> zap_priv_inln inline static auto fmtlen(typ val,::zap::i8 const bs) noexcept -> ::zap::i8 { + ::zap::i8 len = 0x0u; + if (val < 0x0) { + val = static_cast<typ>(::zap::abs(val)); + len = 0x1u; + } + len += ::zap::impl::numdig(val,bs); + return len; + } + } +} + +extern "C" { + auto zap_fmtleni( int const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenl( long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenll( long long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlens( short const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlensc( signed char const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenuc( unsigned char const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenui( unsigned int const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenul( unsigned long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenull(unsigned long long const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} + auto zap_fmtlenus( unsigned short const val,::zap::i8 const bs) -> ::zap::i8 {return ::zap::impl::fmtlen(val,bs);} +} diff --git a/zap/source/any/str/numdig.hh b/zap/source/any/str/numdig.hh new file mode 100644 index 0000000..65756b9 --- /dev/null +++ b/zap/source/any/str/numdig.hh @@ -0,0 +1,12 @@ +#pragma once + +namespace zap { + namespace impl { + template<typename typ> zap_priv_inln inline static auto numdig(typ fmtval,::zap::i8 const bs) noexcept -> ::zap::i8 { + ::zap::i8 len = 0x0u; + if (fmtval == typ {0x0}) return 0x1u; + for (typ val = fmtval;val > 0x0;val /= static_cast<typ>(bs)) ++len; + return len; + } + } +} diff --git a/zap/source/any/mem/strlen.c b/zap/source/any/str/streq.c index 200269c..6b1002d 100644 --- a/zap/source/any/mem/strlen.c +++ b/zap/source/any/str/streq.c @@ -4,10 +4,14 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -zap_sz zap_strlen(char const * const _str) { - char const * pos = _str; - for (;*pos != '\x0';++pos) {} - return (zap_sz)(pos - _str); +zap_i8 zap_streq(char const * lstr,char const * rstr) { + for (;;++lstr,++rstr) { + char const lchr = *lstr; + char const rchr = *rstr; + if (lchr != rchr) return 0x0u; + if (lchr == '\x0') break; + } + return zap_maxval8; } diff --git a/zap/source/any/math/div0.c b/zap/source/any/str/strlen.c index 07c15a4..e6e9019 100644 --- a/zap/source/any/math/div0.c +++ b/zap/source/any/str/strlen.c @@ -4,10 +4,10 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/math.h> +#include <zap/str.h> -#include <csys.h> - -zap_i04 zap_priv_div0(void) { - return (zap_i04)-0x1; +zap_sz zap_strlen(char const * str) { + char const * const start = str; + while (*str++); + return (zap_sz)(str - start) - 0x1u; } diff --git a/zap/source/any/mem/utf8dec.c b/zap/source/any/str/utf8dec.c index 6f3141a..add8697 100644 --- a/zap/source/any/mem/utf8dec.c +++ b/zap/source/any/str/utf8dec.c @@ -4,16 +4,12 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -void zap_utf8dec(zap_i02 * const _dest,zap_i8 const * const _src) { - zap_i02 * dest; - zap_i8 const * src; - for (dest = _dest,src = _src;;++dest) { +void zap_utf8dec(zap_i02 * dest,zap_i8 const * src) { + for (;;++dest) { zap_i8 const oct = *src; - if (oct == 0x0u) { - break; - } + if (oct == 0x0u) break; if (oct >= 0xF0u) { /* Four octets. */ zap_i02 chr = ((zap_i02)oct ^ 0xF0u) << 0x12u; ++src; diff --git a/zap/source/any/mem/utf8declen.c b/zap/source/any/str/utf8declen.c index 99c224b..f8b7ae8 100644 --- a/zap/source/any/mem/utf8declen.c +++ b/zap/source/any/str/utf8declen.c @@ -4,29 +4,26 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -zap_sz zap_utf8declen(zap_i8 const * const _buf) { +zap_sz zap_utf8declen(zap_i8 const * buf) { zap_sz len = 0x0u; - zap_i8 const * pos; - for (pos = _buf;;++len) { - zap_i8 const oct = *pos; - if (oct == 0x0u) { - break; - } + for (;;++len) { + zap_i8 const oct = *buf; + if (oct == 0x0u) break; if (oct >= 0xF0u) { - pos += 0x4u; + buf += 0x4u; continue; } if (oct >= 0xE0u) { - pos += 0x3u; + buf += 0x3u; continue; } if (oct >= 0xC0u) { - pos += 0x2u; + buf += 0x2u; continue; } - ++pos; + ++buf; } return len; } diff --git a/zap/source/any/mem/utf8enc.c b/zap/source/any/str/utf8enc.c index ea62d52..a6b31c6 100644 --- a/zap/source/any/mem/utf8enc.c +++ b/zap/source/any/str/utf8enc.c @@ -4,12 +4,10 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -void zap_utf8enc(zap_i8 * const _dest,zap_i02 const * const _src) { - zap_i8 * dest; - zap_i02 const * src; - for (dest = _dest,src = _src;;++src) { +void zap_utf8enc(zap_i8 * dest,zap_i02 const * src) { + for (;;++src) { zap_i02 const chr = *src; if (chr > 0xFFFFu) { /* Four octets. */ *dest = 0xF0u + (chr >> 0x12u); @@ -41,8 +39,6 @@ void zap_utf8enc(zap_i8 * const _dest,zap_i02 const * const _src) { /* One octet. */ *dest = chr; ++dest; - if (chr == 0x0u) { - break; - } + if (chr == 0x0u) break; } } diff --git a/zap/source/any/mem/utf8enclen.c b/zap/source/any/str/utf8enclen.c index c159653..f49031e 100644 --- a/zap/source/any/mem/utf8enclen.c +++ b/zap/source/any/str/utf8enclen.c @@ -4,16 +4,13 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -zap_sz zap_utf8enclen(zap_i02 const * const _buf) { +zap_sz zap_utf8enclen(zap_i02 const * buf) { zap_sz len = 0x0u; - zap_i02 const * pos; - for (pos = _buf;;++pos) { - zap_i02 const chr = *pos; - if (chr == 0x0u) { - break; - } + for (;;++buf) { + zap_i02 const chr = *buf; + if (chr == 0x0u) break; if (chr >= 0x10000u) { len += 0x4u; continue; diff --git a/zap/source/any/mem/win1252dec.c b/zap/source/any/str/win1252dec.c index 7dc7067..0e29a5a 100644 --- a/zap/source/any/mem/win1252dec.c +++ b/zap/source/any/str/win1252dec.c @@ -4,16 +4,12 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -void zap_win1252dec(zap_i02 * const _dest,zap_i8 const * const _src) { - zap_i02 * dest; - zap_i8 const * src; - for (dest = _dest,src = _src;;++src,++dest) { +void zap_win1252dec(zap_i02 * dest,zap_i8 const * src) { + for (;;++src,++dest) { zap_i8 const chr = *src; - if (chr == 0x0u) { - break; - } + if (chr == 0x0u) break; switch (chr) { default: *dest = *src; diff --git a/zap/source/any/mem/win1252enc.c b/zap/source/any/str/win1252enc.c index 8f80288..d7ae548 100644 --- a/zap/source/any/mem/win1252enc.c +++ b/zap/source/any/str/win1252enc.c @@ -4,16 +4,12 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -#include <zap/mem.h> +#include <zap/str.h> -void zap_win1252enc(zap_i8 * const _dest,zap_i02 const * const _src) { - zap_i8 * dest; - zap_i02 const * src; - for (dest = _dest,src = _src;;++src,++dest) { +void zap_win1252enc(zap_i8 * dest,zap_i02 const * src) { + for (;;++src,++dest) { zap_i02 const chr = *src; - if (chr == 0x0u) { - break; - } + if (chr == 0x0u) break; zap_i8 const bad = 0x3Fu; switch (chr) { default: |