ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.h
(Generate patch)

Comparing libecb/ecb.h (file contents):
Revision 1.198 by root, Sat Jul 31 16:13:30 2021 UTC vs.
Revision 1.201 by root, Fri Aug 20 20:13:20 2021 UTC

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 0x00010009 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;
807template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); } 807template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); }
808 808
809#endif 809#endif
810 810
811/*****************************************************************************/ 811/*****************************************************************************/
812/* pointer/integer hashing */
813
814/* based on hash by Chris Wellons, https://nullprogram.com/blog/2018/07/31/ */
815ecb_function_ uint32_t ecb_mix32 (uint32_t v);
816ecb_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
824ecb_function_ uint32_t ecb_unmix32 (uint32_t v);
825ecb_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 */
834ecb_function_ uint64_t ecb_mix64 (uint64_t v);
835ecb_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
843ecb_function_ uint64_t ecb_unmix64 (uint64_t v);
844ecb_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
852ecb_function_ uintptr_t ecb_ptrmix (void *p);
853ecb_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
862ecb_function_ void *ecb_ptrunmix (uintptr_t v);
863ecb_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
874template<typename T>
875inline uintptr_t ecb_ptrmix (T *p)
876{
877 return ecb_ptrmix (static_cast<void *>(p));
878}
879
880template<typename T>
881inline T *ecb_ptrunmix (uintptr_t v)
882{
883 return static_cast<T *>(ecb_ptrunmix (v));
884}
885
886#endif
887
888/*****************************************************************************/
812/* division */ 889/* division */
813 890
814#if ECB_GCC_VERSION(3,0) || ECB_C99 891#if ECB_GCC_VERSION(3,0) || ECB_C99
815 /* C99 tightened the definition of %, so we can use a more efficient version */ 892 /* C99 tightened the definition of %, so we can use a more efficient version */
816 #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))
961 * format becomes 5.27, 6.26 and so on. 1038 * format becomes 5.27, 6.26 and so on.
962 * The rest involves only advancing the pointer if we already generated a 1039 * The rest involves only advancing the pointer if we already generated a
963 * non-zero digit, so leading zeroes are overwritten. 1040 * non-zero digit, so leading zeroes are overwritten.
964 */ 1041 */
965 1042
966// simply return a mask with "bits" bits set 1043/* simply return a mask with "bits" bits set */
967#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) 1044#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1)
968 1045
969// oputput a single digit. maskvalue is 10**digitidx 1046/* oputput a single digit. maskvalue is 10**digitidx */
970#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ 1047#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \
971 if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \ 1048 if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \
972 { \ 1049 { \
973 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ 1050 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \
974 *ptr = digit + '0'; /* output it */ \ 1051 *ptr = digit + '0'; /* output it */ \
975 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ 1052 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \
976 ptr += nz; /* output digit only if non-zero digit seen */ \ 1053 ptr += nz; /* output digit only if non-zero digit seen */ \
977 x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \ 1054 x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \
978 } 1055 }
979 1056
980// convert integer to fixed point format and multiply out digits, highest first 1057/* convert integer to fixed point format and multiply out digits, highest first */
981// requires magic constants: max. digits and number of bits after the decimal point 1058/* requires magic constants: max. digits and number of bits after the decimal point */
982#define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ 1059#define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \
983ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ 1060ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \
984{ \ 1061{ \
985 char nz = lz; /* non-zero digit seen? */ \ 1062 char nz = lz; /* non-zero digit seen? */ \
986 /* convert to x.bits fixed-point */ \ 1063 /* convert to x.bits fixed-point */ \
997 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ 1074 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \
998 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ 1075 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \
999 return ptr; \ 1076 return ptr; \
1000} 1077}
1001 1078
1002// predefined versions of the above, for various digits 1079/* predefined versions of the above, for various digits */
1003// ecb_i2a_xN = almost N digits, limit defined by macro 1080/* ecb_i2a_xN = almost N digits, limit defined by macro */
1004// ecb_i2a_N = up to N digits, leading zeroes suppressed 1081/* ecb_i2a_N = up to N digits, leading zeroes suppressed */
1005// ecb_i2a_0N = exactly N digits, including leading zeroes 1082/* ecb_i2a_0N = exactly N digits, including leading zeroes */
1006 1083
1007// non-leading-zero versions, limited range 1084/* non-leading-zero versions, limited range */
1008#define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 1085#define ECB_I2A_MAX_X5 59074 /* limit for ecb_i2a_x5 */
1009#define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 1086#define ECB_I2A_MAX_X10 2932500665 /* limit for ecb_i2a_x10 */
1010ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) 1087ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0)
1011ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) 1088ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0)
1012 1089
1013// non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit 1090/* non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit */
1014ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) 1091ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0)
1015ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) 1092ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0)
1016ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) 1093ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0)
1017ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) 1094ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0)
1018ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) 1095ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0)
1019ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) 1096ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0)
1020ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) 1097ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0)
1021ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) 1098ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0)
1022 1099
1023// leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit 1100/* leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit */
1024ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) 1101ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1)
1025ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) 1102ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1)
1026ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) 1103ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1)
1027ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) 1104ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1)
1028ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) 1105ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1)
1040ecb_i2a_u32 (char *ptr, uint32_t u) 1117ecb_i2a_u32 (char *ptr, uint32_t u)
1041{ 1118{
1042 #if ECB_64BIT_NATIVE 1119 #if ECB_64BIT_NATIVE
1043 if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) 1120 if (ecb_expect_true (u <= ECB_I2A_MAX_X10))
1044 ptr = ecb_i2a_x10 (ptr, u); 1121 ptr = ecb_i2a_x10 (ptr, u);
1045 else // x10 almost, but not fully, covers 32 bit 1122 else /* x10 almost, but not fully, covers 32 bit */
1046 { 1123 {
1047 uint32_t u1 = u % 1000000000; 1124 uint32_t u1 = u % 1000000000;
1048 uint32_t u2 = u / 1000000000; 1125 uint32_t u2 = u / 1000000000;
1049 1126
1050 *ptr++ = u2 + '0'; 1127 *ptr++ = u2 + '0';
1082{ 1159{
1083 *ptr = '-'; ptr += v < 0; 1160 *ptr = '-'; ptr += v < 0;
1084 uint32_t u = v < 0 ? -(uint32_t)v : v; 1161 uint32_t u = v < 0 ? -(uint32_t)v : v;
1085 1162
1086 #if ECB_64BIT_NATIVE 1163 #if ECB_64BIT_NATIVE
1087 ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit 1164 ptr = ecb_i2a_x10 (ptr, u); /* x10 fully covers 31 bit */
1088 #else 1165 #else
1089 ptr = ecb_i2a_u32 (ptr, u); 1166 ptr = ecb_i2a_u32 (ptr, u);
1090 #endif 1167 #endif
1091 1168
1092 return ptr; 1169 return ptr;
1155 uint64_t u1 = u % 1000000000; 1232 uint64_t u1 = u % 1000000000;
1156 uint64_t ua = u / 1000000000; 1233 uint64_t ua = u / 1000000000;
1157 uint64_t u2 = ua % 1000000000; 1234 uint64_t u2 = ua % 1000000000;
1158 uint64_t u3 = ua / 1000000000; 1235 uint64_t u3 = ua / 1000000000;
1159 1236
1160 // 2**31 is 19 digits, so the top is exactly one digit 1237 /* 2**31 is 19 digits, so the top is exactly one digit */
1161 *ptr++ = u3 + '0'; 1238 *ptr++ = u3 + '0';
1162 ptr = ecb_i2a_09 (ptr, u2); 1239 ptr = ecb_i2a_09 (ptr, u2);
1163 ptr = ecb_i2a_09 (ptr, u1); 1240 ptr = ecb_i2a_09 (ptr, u1);
1164 } 1241 }
1165 #else 1242 #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines