diff options
Diffstat (limited to 'zap/include')
-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 |
4 files changed, 493 insertions, 121 deletions
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 |