1 | /* |
1 | /* |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
2 | * libecb - http://software.schmorp.de/pkg/libecb |
3 | * |
3 | * |
4 | * Copyright (©) 2009-2015,2018-2020 Marc Alexander Lehmann <libecb@schmorp.de> |
4 | * Copyright (©) 2009-2015,2018-2021 Marc Alexander Lehmann <libecb@schmorp.de> |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
5 | * Copyright (©) 2011 Emanuele Giaquinta |
6 | * All rights reserved. |
6 | * All rights reserved. |
7 | * |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * Redistribution and use in source and binary forms, with or without modifica- |
9 | * tion, are permitted provided that the following conditions are met: |
9 | * tion, are permitted provided that the following conditions are met: |
… | |
… | |
40 | |
40 | |
41 | #ifndef ECB_H |
41 | #ifndef ECB_H |
42 | #define ECB_H |
42 | #define ECB_H |
43 | |
43 | |
44 | /* 16 bits major, 16 bits minor */ |
44 | /* 16 bits major, 16 bits minor */ |
45 | #define ECB_VERSION 0x00010008 |
45 | #define ECB_VERSION 0x0001000a |
46 | |
46 | |
47 | #include <string.h> /* for memcpy */ |
47 | #include <string.h> /* for memcpy */ |
48 | |
48 | |
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
49 | #if defined (_WIN32) && !defined (__MINGW32__) |
50 | typedef signed char int8_t; |
50 | typedef signed char int8_t; |
… | |
… | |
102 | #if _ILP32 |
102 | #if _ILP32 |
103 | #define ECB_AMD64_X32 1 |
103 | #define ECB_AMD64_X32 1 |
104 | #else |
104 | #else |
105 | #define ECB_AMD64 1 |
105 | #define ECB_AMD64 1 |
106 | #endif |
106 | #endif |
|
|
107 | #endif |
|
|
108 | |
|
|
109 | #if ECB_PTRSIZE >= 8 || ECB_AMD64_X32 |
|
|
110 | #define ECB_64BIT_NATIVE 1 |
|
|
111 | #else |
|
|
112 | #define ECB_64BIT_NATIVE 0 |
107 | #endif |
113 | #endif |
108 | |
114 | |
109 | /* many compilers define _GNUC_ to some versions but then only implement |
115 | /* many compilers define _GNUC_ to some versions but then only implement |
110 | * what their idiot authors think are the "more important" extensions, |
116 | * what their idiot authors think are the "more important" extensions, |
111 | * causing enormous grief in return for some better fake benchmark numbers. |
117 | * causing enormous grief in return for some better fake benchmark numbers. |
… | |
… | |
242 | #if ECB_GCC_VERSION(4,7) |
248 | #if ECB_GCC_VERSION(4,7) |
243 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
249 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
244 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
250 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
245 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
251 | #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE) |
246 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
252 | #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
253 | #undef ECB_MEMORY_FENCE_RELAXED |
247 | #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED) |
254 | #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED) |
248 | |
255 | |
249 | #elif ECB_CLANG_EXTENSION(c_atomic) |
256 | #elif ECB_CLANG_EXTENSION(c_atomic) |
250 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
257 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
251 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
258 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
252 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
259 | #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE) |
253 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
260 | #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE) |
|
|
261 | #undef ECB_MEMORY_FENCE_RELAXED |
254 | #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED) |
262 | #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED) |
255 | |
263 | |
256 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
264 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
257 | #define ECB_MEMORY_FENCE __sync_synchronize () |
265 | #define ECB_MEMORY_FENCE __sync_synchronize () |
258 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
266 | #elif _MSC_VER >= 1500 /* VC++ 2008 */ |
… | |
… | |
601 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); |
609 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); |
602 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); |
610 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); |
603 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); |
611 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); |
604 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); |
612 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); |
605 | |
613 | |
606 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } |
614 | ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> (-count & 7)) | (x << (count & 7)); } |
607 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } |
615 | ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << (-count & 7)) | (x >> (count & 7)); } |
608 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } |
616 | ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (-count & 15)) | (x << (count & 15)); } |
609 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } |
617 | ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (-count & 15)) | (x >> (count & 15)); } |
610 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } |
618 | ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (-count & 31)) | (x << (count & 31)); } |
611 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } |
619 | ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (-count & 31)) | (x >> (count & 31)); } |
612 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } |
620 | ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (-count & 63)) | (x << (count & 63)); } |
613 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } |
621 | ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (-count & 63)) | (x >> (count & 63)); } |
614 | |
622 | |
615 | #if ECB_CPP |
623 | #if ECB_CPP |
616 | |
624 | |
617 | inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } |
625 | inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } |
618 | inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } |
626 | inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } |
… | |
… | |
766 | ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); } |
774 | ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); } |
767 | |
775 | |
768 | ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); } |
776 | ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); } |
769 | ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); } |
777 | ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); } |
770 | ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); } |
778 | ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); } |
771 | |
779 | |
772 | ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); } |
780 | ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); } |
773 | ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); } |
781 | ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); } |
774 | ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); } |
782 | ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); } |
775 | |
783 | |
776 | #if ECB_CPP |
784 | #if ECB_CPP |
… | |
… | |
799 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
807 | template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } |
800 | |
808 | |
801 | #endif |
809 | #endif |
802 | |
810 | |
803 | /*****************************************************************************/ |
811 | /*****************************************************************************/ |
|
|
812 | /* pointer/integer hashing */ |
|
|
813 | |
|
|
814 | /* based on hash by Chris Wellons, https://nullprogram.com/blog/2018/07/31/ */ |
|
|
815 | ecb_function_ uint32_t ecb_mix32 (uint32_t v); |
|
|
816 | ecb_function_ uint32_t ecb_mix32 (uint32_t v) |
|
|
817 | { |
|
|
818 | v ^= v >> 16; v *= 0x7feb352dU; |
|
|
819 | v ^= v >> 15; v *= 0x846ca68bU; |
|
|
820 | v ^= v >> 16; |
|
|
821 | return v; |
|
|
822 | } |
|
|
823 | |
|
|
824 | ecb_function_ uint32_t ecb_unmix32 (uint32_t v); |
|
|
825 | ecb_function_ uint32_t ecb_unmix32 (uint32_t v) |
|
|
826 | { |
|
|
827 | v ^= v >> 16 ; v *= 0x43021123U; |
|
|
828 | v ^= v >> 15 ^ v >> 30; v *= 0x1d69e2a5U; |
|
|
829 | v ^= v >> 16 ; |
|
|
830 | return v; |
|
|
831 | } |
|
|
832 | |
|
|
833 | /* based on splitmix64, by Sebastiona Vigna, https://prng.di.unimi.it/splitmix64.c */ |
|
|
834 | ecb_function_ uint64_t ecb_mix64 (uint64_t v); |
|
|
835 | ecb_function_ uint64_t ecb_mix64 (uint64_t v) |
|
|
836 | { |
|
|
837 | v ^= v >> 30; v *= 0xbf58476d1ce4e5b9U; |
|
|
838 | v ^= v >> 27; v *= 0x94d049bb133111ebU; |
|
|
839 | v ^= v >> 31; |
|
|
840 | return v; |
|
|
841 | } |
|
|
842 | |
|
|
843 | ecb_function_ uint64_t ecb_unmix64 (uint64_t v); |
|
|
844 | ecb_function_ uint64_t ecb_unmix64 (uint64_t v) |
|
|
845 | { |
|
|
846 | v ^= v >> 31 ^ v >> 62; v *= 0x319642b2d24d8ec3U; |
|
|
847 | v ^= v >> 27 ^ v >> 54; v *= 0x96de1b173f119089U; |
|
|
848 | v ^= v >> 30 ^ v >> 60; |
|
|
849 | return v; |
|
|
850 | } |
|
|
851 | |
|
|
852 | ecb_function_ uintptr_t ecb_ptrmix (void *p); |
|
|
853 | ecb_function_ uintptr_t ecb_ptrmix (void *p) |
|
|
854 | { |
|
|
855 | #if ECB_PTRSIZE <= 4 |
|
|
856 | return ecb_mix32 ((uint32_t)p); |
|
|
857 | #else |
|
|
858 | return ecb_mix64 ((uint64_t)p); |
|
|
859 | #endif |
|
|
860 | } |
|
|
861 | |
|
|
862 | ecb_function_ void *ecb_ptrunmix (uintptr_t v); |
|
|
863 | ecb_function_ void *ecb_ptrunmix (uintptr_t v) |
|
|
864 | { |
|
|
865 | #if ECB_PTRSIZE <= 4 |
|
|
866 | return (void *)ecb_unmix32 (v); |
|
|
867 | #else |
|
|
868 | return (void *)ecb_unmix64 (v); |
|
|
869 | #endif |
|
|
870 | } |
|
|
871 | |
|
|
872 | #if ECB_CPP |
|
|
873 | |
|
|
874 | template<typename T> |
|
|
875 | inline uintptr_t ecb_ptrmix (T *p) |
|
|
876 | { |
|
|
877 | return ecb_ptrmix (static_cast<void *>(p)); |
|
|
878 | } |
|
|
879 | |
|
|
880 | template<typename T> |
|
|
881 | inline T *ecb_ptrunmix (uintptr_t v) |
|
|
882 | { |
|
|
883 | return static_cast<T *>(ecb_ptrunmix (v)); |
|
|
884 | } |
|
|
885 | |
|
|
886 | #endif |
|
|
887 | |
|
|
888 | /*****************************************************************************/ |
|
|
889 | /* division */ |
804 | |
890 | |
805 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
891 | #if ECB_GCC_VERSION(3,0) || ECB_C99 |
|
|
892 | /* C99 tightened the definition of %, so we can use a more efficient version */ |
806 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
893 | #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) |
807 | #else |
894 | #else |
808 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
895 | #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) |
809 | #endif |
896 | #endif |
810 | |
897 | |
… | |
… | |
821 | } |
908 | } |
822 | #else |
909 | #else |
823 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
910 | #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) |
824 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
911 | #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) |
825 | #endif |
912 | #endif |
|
|
913 | |
|
|
914 | /*****************************************************************************/ |
|
|
915 | /* array length */ |
826 | |
916 | |
827 | #if ecb_cplusplus_does_not_suck |
917 | #if ecb_cplusplus_does_not_suck |
828 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
918 | /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ |
829 | template<typename T, int N> |
919 | template<typename T, int N> |
830 | static inline int ecb_array_length (const T (&arr)[N]) |
920 | static inline int ecb_array_length (const T (&arr)[N]) |
… | |
… | |
834 | #else |
924 | #else |
835 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
925 | #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) |
836 | #endif |
926 | #endif |
837 | |
927 | |
838 | /*****************************************************************************/ |
928 | /*****************************************************************************/ |
|
|
929 | /* IEEE 754-2008 half float conversions */ |
839 | |
930 | |
840 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
931 | ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x); |
841 | ecb_function_ ecb_const uint32_t |
932 | ecb_function_ ecb_const uint32_t |
842 | ecb_binary16_to_binary32 (uint32_t x) |
933 | ecb_binary16_to_binary32 (uint32_t x) |
843 | { |
934 | { |
… | |
… | |
872 | ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); |
963 | ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x); |
873 | ecb_function_ ecb_const uint16_t |
964 | ecb_function_ ecb_const uint16_t |
874 | ecb_binary32_to_binary16 (uint32_t x) |
965 | ecb_binary32_to_binary16 (uint32_t x) |
875 | { |
966 | { |
876 | unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ |
967 | unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */ |
877 | unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ |
968 | int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */ |
878 | unsigned int m = x & 0x007fffff; |
969 | unsigned int m = x & 0x007fffff; |
879 | |
970 | |
880 | x &= 0x7fffffff; |
971 | x &= 0x7fffffff; |
881 | |
972 | |
882 | /* if it's within range of binary16 normals, use fast path */ |
973 | /* if it's within range of binary16 normals, use fast path */ |
… | |
… | |
929 | |
1020 | |
930 | /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */ |
1021 | /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */ |
931 | m >>= 13; |
1022 | m >>= 13; |
932 | |
1023 | |
933 | return s | 0x7c00 | m | !m; |
1024 | return s | 0x7c00 | m | !m; |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | /*******************************************************************************/ |
|
|
1028 | /* fast integer to ascii */ |
|
|
1029 | |
|
|
1030 | /* |
|
|
1031 | * This code is pretty complicated because it is general. The idea behind it, |
|
|
1032 | * however, is pretty simple: first, the number is multiplied with a scaling |
|
|
1033 | * factor (2**bits / 10**(digits-1)) to convert the integer into a fixed-point |
|
|
1034 | * number with the first digit in the upper bits. |
|
|
1035 | * Then this digit is converted to text and masked out. The resulting number |
|
|
1036 | * is then multiplied by 10, by multiplying the fixed point representation |
|
|
1037 | * by 5 and shifting the (binary) decimal point one to the right, so a 4.28 |
|
|
1038 | * format becomes 5.27, 6.26 and so on. |
|
|
1039 | * The rest involves only advancing the pointer if we already generated a |
|
|
1040 | * non-zero digit, so leading zeroes are overwritten. |
|
|
1041 | */ |
|
|
1042 | |
|
|
1043 | // simply return a mask with "bits" bits set |
|
|
1044 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
|
|
1045 | |
|
|
1046 | // oputput a single digit. maskvalue is 10**digitidx |
|
|
1047 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ |
|
|
1048 | if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \ |
|
|
1049 | { \ |
|
|
1050 | char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ |
|
|
1051 | *ptr = digit + '0'; /* output it */ \ |
|
|
1052 | nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ |
|
|
1053 | ptr += nz; /* output digit only if non-zero digit seen */ \ |
|
|
1054 | x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \ |
|
|
1055 | } |
|
|
1056 | |
|
|
1057 | // convert integer to fixed point format and multiply out digits, highest first |
|
|
1058 | // requires magic constants: max. digits and number of bits after the decimal point |
|
|
1059 | #define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ |
|
|
1060 | ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ |
|
|
1061 | { \ |
|
|
1062 | char nz = lz; /* non-zero digit seen? */ \ |
|
|
1063 | /* convert to x.bits fixed-point */ \ |
|
|
1064 | type x = u * ((ecb_i2a_mask (type, bits) + digitmask) / digitmask); \ |
|
|
1065 | /* output up to 10 digits */ \ |
|
|
1066 | ecb_i2a_digit (type,bits,digitmask, 1, 0); \ |
|
|
1067 | ecb_i2a_digit (type,bits,digitmask, 10, 1); \ |
|
|
1068 | ecb_i2a_digit (type,bits,digitmask, 100, 2); \ |
|
|
1069 | ecb_i2a_digit (type,bits,digitmask, 1000, 3); \ |
|
|
1070 | ecb_i2a_digit (type,bits,digitmask, 10000, 4); \ |
|
|
1071 | ecb_i2a_digit (type,bits,digitmask, 100000, 5); \ |
|
|
1072 | ecb_i2a_digit (type,bits,digitmask, 1000000, 6); \ |
|
|
1073 | ecb_i2a_digit (type,bits,digitmask, 10000000, 7); \ |
|
|
1074 | ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ |
|
|
1075 | ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ |
|
|
1076 | return ptr; \ |
|
|
1077 | } |
|
|
1078 | |
|
|
1079 | // predefined versions of the above, for various digits |
|
|
1080 | // ecb_i2a_xN = almost N digits, limit defined by macro |
|
|
1081 | // ecb_i2a_N = up to N digits, leading zeroes suppressed |
|
|
1082 | // ecb_i2a_0N = exactly N digits, including leading zeroes |
|
|
1083 | |
|
|
1084 | // non-leading-zero versions, limited range |
|
|
1085 | #define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 |
|
|
1086 | #define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 |
|
|
1087 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
|
|
1088 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
|
|
1089 | |
|
|
1090 | // non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit |
|
|
1091 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
|
|
1092 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
|
|
1093 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
|
|
1094 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
|
|
1095 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
|
|
1096 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
|
|
1097 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
|
|
1098 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
|
|
1099 | |
|
|
1100 | // leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit |
|
|
1101 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
|
|
1102 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
|
|
1103 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
|
|
1104 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
|
|
1105 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
|
|
1106 | ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1) |
|
|
1107 | ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1) |
|
|
1108 | ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1) |
|
|
1109 | |
|
|
1110 | #define ECB_I2A_I32_DIGITS 11 |
|
|
1111 | #define ECB_I2A_U32_DIGITS 10 |
|
|
1112 | #define ECB_I2A_I64_DIGITS 20 |
|
|
1113 | #define ECB_I2A_U64_DIGITS 21 |
|
|
1114 | #define ECB_I2A_MAX_DIGITS 21 |
|
|
1115 | |
|
|
1116 | ecb_inline char * |
|
|
1117 | ecb_i2a_u32 (char *ptr, uint32_t u) |
|
|
1118 | { |
|
|
1119 | #if ECB_64BIT_NATIVE |
|
|
1120 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
|
|
1121 | ptr = ecb_i2a_x10 (ptr, u); |
|
|
1122 | else // x10 almost, but not fully, covers 32 bit |
|
|
1123 | { |
|
|
1124 | uint32_t u1 = u % 1000000000; |
|
|
1125 | uint32_t u2 = u / 1000000000; |
|
|
1126 | |
|
|
1127 | *ptr++ = u2 + '0'; |
|
|
1128 | ptr = ecb_i2a_09 (ptr, u1); |
|
|
1129 | } |
|
|
1130 | #else |
|
|
1131 | if (ecb_expect_true (u <= ECB_I2A_MAX_X5)) |
|
|
1132 | ecb_i2a_x5 (ptr, u); |
|
|
1133 | else if (ecb_expect_true (u <= ECB_I2A_MAX_X5 * 10000)) |
|
|
1134 | { |
|
|
1135 | uint32_t u1 = u % 10000; |
|
|
1136 | uint32_t u2 = u / 10000; |
|
|
1137 | |
|
|
1138 | ptr = ecb_i2a_x5 (ptr, u2); |
|
|
1139 | ptr = ecb_i2a_04 (ptr, u1); |
|
|
1140 | } |
|
|
1141 | else |
|
|
1142 | { |
|
|
1143 | uint32_t u1 = u % 10000; |
|
|
1144 | uint32_t ua = u / 10000; |
|
|
1145 | uint32_t u2 = ua % 10000; |
|
|
1146 | uint32_t u3 = ua / 10000; |
|
|
1147 | |
|
|
1148 | ptr = ecb_i2a_2 (ptr, u3); |
|
|
1149 | ptr = ecb_i2a_04 (ptr, u2); |
|
|
1150 | ptr = ecb_i2a_04 (ptr, u1); |
|
|
1151 | } |
|
|
1152 | #endif |
|
|
1153 | |
|
|
1154 | return ptr; |
|
|
1155 | } |
|
|
1156 | |
|
|
1157 | ecb_inline char * |
|
|
1158 | ecb_i2a_i32 (char *ptr, int32_t v) |
|
|
1159 | { |
|
|
1160 | *ptr = '-'; ptr += v < 0; |
|
|
1161 | uint32_t u = v < 0 ? -(uint32_t)v : v; |
|
|
1162 | |
|
|
1163 | #if ECB_64BIT_NATIVE |
|
|
1164 | ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit |
|
|
1165 | #else |
|
|
1166 | ptr = ecb_i2a_u32 (ptr, u); |
|
|
1167 | #endif |
|
|
1168 | |
|
|
1169 | return ptr; |
|
|
1170 | } |
|
|
1171 | |
|
|
1172 | ecb_inline char * |
|
|
1173 | ecb_i2a_u64 (char *ptr, uint64_t u) |
|
|
1174 | { |
|
|
1175 | #if ECB_64BIT_NATIVE |
|
|
1176 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
|
|
1177 | ptr = ecb_i2a_x10 (ptr, u); |
|
|
1178 | else if (ecb_expect_false (u <= ECB_I2A_MAX_X10 * 1000000000)) |
|
|
1179 | { |
|
|
1180 | uint64_t u1 = u % 1000000000; |
|
|
1181 | uint64_t u2 = u / 1000000000; |
|
|
1182 | |
|
|
1183 | ptr = ecb_i2a_x10 (ptr, u2); |
|
|
1184 | ptr = ecb_i2a_09 (ptr, u1); |
|
|
1185 | } |
|
|
1186 | else |
|
|
1187 | { |
|
|
1188 | uint64_t u1 = u % 1000000000; |
|
|
1189 | uint64_t ua = u / 1000000000; |
|
|
1190 | uint64_t u2 = ua % 1000000000; |
|
|
1191 | uint64_t u3 = ua / 1000000000; |
|
|
1192 | |
|
|
1193 | ptr = ecb_i2a_2 (ptr, u3); |
|
|
1194 | ptr = ecb_i2a_09 (ptr, u2); |
|
|
1195 | ptr = ecb_i2a_09 (ptr, u1); |
|
|
1196 | } |
|
|
1197 | #else |
|
|
1198 | if (ecb_expect_true (u <= ECB_I2A_MAX_X5)) |
|
|
1199 | ptr = ecb_i2a_x5 (ptr, u); |
|
|
1200 | else |
|
|
1201 | { |
|
|
1202 | uint64_t u1 = u % 10000; |
|
|
1203 | uint64_t u2 = u / 10000; |
|
|
1204 | |
|
|
1205 | ptr = ecb_i2a_u64 (ptr, u2); |
|
|
1206 | ptr = ecb_i2a_04 (ptr, u1); |
|
|
1207 | } |
|
|
1208 | #endif |
|
|
1209 | |
|
|
1210 | return ptr; |
|
|
1211 | } |
|
|
1212 | |
|
|
1213 | ecb_inline char * |
|
|
1214 | ecb_i2a_i64 (char *ptr, int64_t v) |
|
|
1215 | { |
|
|
1216 | *ptr = '-'; ptr += v < 0; |
|
|
1217 | uint64_t u = v < 0 ? -(uint64_t)v : v; |
|
|
1218 | |
|
|
1219 | #if ECB_64BIT_NATIVE |
|
|
1220 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
|
|
1221 | ptr = ecb_i2a_x10 (ptr, u); |
|
|
1222 | else if (ecb_expect_false (u <= ECB_I2A_MAX_X10 * 1000000000)) |
|
|
1223 | { |
|
|
1224 | uint64_t u1 = u % 1000000000; |
|
|
1225 | uint64_t u2 = u / 1000000000; |
|
|
1226 | |
|
|
1227 | ptr = ecb_i2a_x10 (ptr, u2); |
|
|
1228 | ptr = ecb_i2a_09 (ptr, u1); |
|
|
1229 | } |
|
|
1230 | else |
|
|
1231 | { |
|
|
1232 | uint64_t u1 = u % 1000000000; |
|
|
1233 | uint64_t ua = u / 1000000000; |
|
|
1234 | uint64_t u2 = ua % 1000000000; |
|
|
1235 | uint64_t u3 = ua / 1000000000; |
|
|
1236 | |
|
|
1237 | // 2**31 is 19 digits, so the top is exactly one digit |
|
|
1238 | *ptr++ = u3 + '0'; |
|
|
1239 | ptr = ecb_i2a_09 (ptr, u2); |
|
|
1240 | ptr = ecb_i2a_09 (ptr, u1); |
|
|
1241 | } |
|
|
1242 | #else |
|
|
1243 | ptr = ecb_i2a_u64 (ptr, u); |
|
|
1244 | #endif |
|
|
1245 | |
|
|
1246 | return ptr; |
934 | } |
1247 | } |
935 | |
1248 | |
936 | /*******************************************************************************/ |
1249 | /*******************************************************************************/ |
937 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
1250 | /* floating point stuff, can be disabled by defining ECB_NO_LIBM */ |
938 | |
1251 | |