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.202 by root, Wed Mar 23 09:58:06 2022 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 0x0001000b
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/*****************************************************************************/
889/* gray code */
890
891ecb_function_ uint_fast8_t ecb_gray8_encode (uint_fast8_t b) { return b ^ (b >> 1); }
892ecb_function_ uint_fast16_t ecb_gray16_encode (uint_fast16_t b) { return b ^ (b >> 1); }
893ecb_function_ uint_fast32_t ecb_gray32_encode (uint_fast32_t b) { return b ^ (b >> 1); }
894ecb_function_ uint_fast64_t ecb_gray64_encode (uint_fast64_t b) { return b ^ (b >> 1); }
895
896ecb_function_ uint8_t ecb_gray8_decode (uint8_t g)
897{
898 g = g ^ (g >> 1);
899 g = g ^ (g >> 2);
900 g = g ^ (g >> 4);
901 return g;
902}
903
904ecb_function_ uint16_t ecb_gray16_decode (uint16_t g)
905{
906 g = g ^ (g >> 1);
907 g = g ^ (g >> 2);
908 g = g ^ (g >> 4);
909 g = g ^ (g >> 8);
910 return g;
911}
912
913ecb_function_ uint32_t ecb_gray32_decode (uint32_t g)
914{
915 g = g ^ (g >> 1);
916 g = g ^ (g >> 2);
917 g = g ^ (g >> 4);
918 g = g ^ (g >> 8);
919 g = g ^ (g >> 16);
920 return g;
921}
922
923ecb_function_ uint64_t ecb_gray64_decode (uint64_t g)
924{
925 g = g ^ (g >> 1);
926 g = g ^ (g >> 2);
927 g = g ^ (g >> 4);
928 g = g ^ (g >> 8);
929 g = g ^ (g >> 16);
930 g = g ^ (g >> 32);
931 return g;
932}
933
934#if ECB_CPP
935
936ecb_function_ uint8_t ecb_gray_encode (uint8_t b) { return ecb_gray8_encode (b); }
937ecb_function_ uint16_t ecb_gray_encode (uint16_t b) { return ecb_gray16_encode (b); }
938ecb_function_ uint32_t ecb_gray_encode (uint32_t b) { return ecb_gray32_encode (b); }
939ecb_function_ uint64_t ecb_gray_encode (uint64_t b) { return ecb_gray64_encode (b); }
940
941ecb_function_ uint8_t ecb_gray_decode (uint8_t g) { return ecb_gray8_decode (g); }
942ecb_function_ uint16_t ecb_gray_decode (uint16_t g) { return ecb_gray16_decode (g); }
943ecb_function_ uint32_t ecb_gray_decode (uint32_t g) { return ecb_gray32_decode (g); }
944ecb_function_ uint64_t ecb_gray_decode (uint64_t g) { return ecb_gray64_decode (g); }
945
946#endif
947
948/*****************************************************************************/
812/* division */ 949/* division */
813 950
814#if ECB_GCC_VERSION(3,0) || ECB_C99 951#if ECB_GCC_VERSION(3,0) || ECB_C99
815 /* C99 tightened the definition of %, so we can use a more efficient version */ 952 /* 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)) 953 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
961 * format becomes 5.27, 6.26 and so on. 1098 * format becomes 5.27, 6.26 and so on.
962 * The rest involves only advancing the pointer if we already generated a 1099 * The rest involves only advancing the pointer if we already generated a
963 * non-zero digit, so leading zeroes are overwritten. 1100 * non-zero digit, so leading zeroes are overwritten.
964 */ 1101 */
965 1102
966// simply return a mask with "bits" bits set 1103/* simply return a mask with "bits" bits set */
967#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) 1104#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1)
968 1105
969// oputput a single digit. maskvalue is 10**digitidx 1106/* oputput a single digit. maskvalue is 10**digitidx */
970#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ 1107#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \
971 if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \ 1108 if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \
972 { \ 1109 { \
973 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ 1110 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \
974 *ptr = digit + '0'; /* output it */ \ 1111 *ptr = digit + '0'; /* output it */ \
975 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ 1112 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \
976 ptr += nz; /* output digit only if non-zero digit seen */ \ 1113 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 */ \ 1114 x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \
978 } 1115 }
979 1116
980// convert integer to fixed point format and multiply out digits, highest first 1117/* 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 1118/* 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) \ 1119#define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \
983ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ 1120ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \
984{ \ 1121{ \
985 char nz = lz; /* non-zero digit seen? */ \ 1122 char nz = lz; /* non-zero digit seen? */ \
986 /* convert to x.bits fixed-point */ \ 1123 /* convert to x.bits fixed-point */ \
997 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ 1134 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \
998 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ 1135 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \
999 return ptr; \ 1136 return ptr; \
1000} 1137}
1001 1138
1002// predefined versions of the above, for various digits 1139/* predefined versions of the above, for various digits */
1003// ecb_i2a_xN = almost N digits, limit defined by macro 1140/* ecb_i2a_xN = almost N digits, limit defined by macro */
1004// ecb_i2a_N = up to N digits, leading zeroes suppressed 1141/* ecb_i2a_N = up to N digits, leading zeroes suppressed */
1005// ecb_i2a_0N = exactly N digits, including leading zeroes 1142/* ecb_i2a_0N = exactly N digits, including leading zeroes */
1006 1143
1007// non-leading-zero versions, limited range 1144/* non-leading-zero versions, limited range */
1008#define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 1145#define ECB_I2A_MAX_X5 59074 /* limit for ecb_i2a_x5 */
1009#define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 1146#define ECB_I2A_MAX_X10 2932500665 /* limit for ecb_i2a_x10 */
1010ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) 1147ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0)
1011ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) 1148ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0)
1012 1149
1013// non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit 1150/* 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) 1151ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0)
1015ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) 1152ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0)
1016ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) 1153ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0)
1017ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) 1154ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0)
1018ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) 1155ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0)
1019ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) 1156ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0)
1020ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) 1157ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0)
1021ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) 1158ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0)
1022 1159
1023// leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit 1160/* 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) 1161ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1)
1025ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) 1162ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1)
1026ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) 1163ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1)
1027ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) 1164ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1)
1028ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) 1165ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1)
1040ecb_i2a_u32 (char *ptr, uint32_t u) 1177ecb_i2a_u32 (char *ptr, uint32_t u)
1041{ 1178{
1042 #if ECB_64BIT_NATIVE 1179 #if ECB_64BIT_NATIVE
1043 if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) 1180 if (ecb_expect_true (u <= ECB_I2A_MAX_X10))
1044 ptr = ecb_i2a_x10 (ptr, u); 1181 ptr = ecb_i2a_x10 (ptr, u);
1045 else // x10 almost, but not fully, covers 32 bit 1182 else /* x10 almost, but not fully, covers 32 bit */
1046 { 1183 {
1047 uint32_t u1 = u % 1000000000; 1184 uint32_t u1 = u % 1000000000;
1048 uint32_t u2 = u / 1000000000; 1185 uint32_t u2 = u / 1000000000;
1049 1186
1050 *ptr++ = u2 + '0'; 1187 *ptr++ = u2 + '0';
1082{ 1219{
1083 *ptr = '-'; ptr += v < 0; 1220 *ptr = '-'; ptr += v < 0;
1084 uint32_t u = v < 0 ? -(uint32_t)v : v; 1221 uint32_t u = v < 0 ? -(uint32_t)v : v;
1085 1222
1086 #if ECB_64BIT_NATIVE 1223 #if ECB_64BIT_NATIVE
1087 ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit 1224 ptr = ecb_i2a_x10 (ptr, u); /* x10 fully covers 31 bit */
1088 #else 1225 #else
1089 ptr = ecb_i2a_u32 (ptr, u); 1226 ptr = ecb_i2a_u32 (ptr, u);
1090 #endif 1227 #endif
1091 1228
1092 return ptr; 1229 return ptr;
1155 uint64_t u1 = u % 1000000000; 1292 uint64_t u1 = u % 1000000000;
1156 uint64_t ua = u / 1000000000; 1293 uint64_t ua = u / 1000000000;
1157 uint64_t u2 = ua % 1000000000; 1294 uint64_t u2 = ua % 1000000000;
1158 uint64_t u3 = ua / 1000000000; 1295 uint64_t u3 = ua / 1000000000;
1159 1296
1160 // 2**31 is 19 digits, so the top is exactly one digit 1297 /* 2**31 is 19 digits, so the top is exactly one digit */
1161 *ptr++ = u3 + '0'; 1298 *ptr++ = u3 + '0';
1162 ptr = ecb_i2a_09 (ptr, u2); 1299 ptr = ecb_i2a_09 (ptr, u2);
1163 ptr = ecb_i2a_09 (ptr, u1); 1300 ptr = ecb_i2a_09 (ptr, u1);
1164 } 1301 }
1165 #else 1302 #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines