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

Comparing libecb/ecb.h (file contents):
Revision 1.192 by root, Mon Jun 21 23:59:58 2021 UTC vs.
Revision 1.200 by root, Fri Aug 20 20:12:33 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;
609ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); 609ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count);
610ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count); 610ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count);
611ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count); 611ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count);
612ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count); 612ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count);
613 613
614ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } 614ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> (-count & 7)) | (x << (count & 7)); }
615ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } 615ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << (-count & 7)) | (x >> (count & 7)); }
616ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } 616ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (-count & 15)) | (x << (count & 15)); }
617ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } 617ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (-count & 15)) | (x >> (count & 15)); }
618ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } 618ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (-count & 31)) | (x << (count & 31)); }
619ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } 619ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (-count & 31)) | (x >> (count & 31)); }
620ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } 620ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (-count & 63)) | (x << (count & 63)); }
621ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } 621ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (-count & 63)) | (x >> (count & 63)); }
622 622
623#if ECB_CPP 623#if ECB_CPP
624 624
625inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } 625inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); }
626inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } 626inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); }
774ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); } 774ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); }
775 775
776ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); } 776ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); }
777ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); } 777ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); }
778ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); } 778ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); }
779 779
780ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); } 780ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); }
781ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); } 781ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); }
782ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); } 782ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); }
783 783
784#if ECB_CPP 784#if ECB_CPP
803template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); } 803template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); }
804template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); } 804template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); }
805template<typename T> inline void ecb_poke_u (void *ptr, T v) { memcpy (ptr, &v, sizeof (v)); } 805template<typename T> inline void ecb_poke_u (void *ptr, T v) { memcpy (ptr, &v, sizeof (v)); }
806template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); } 806template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (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)); } 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
809#endif
810
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}
808 885
809#endif 886#endif
810 887
811/*****************************************************************************/ 888/*****************************************************************************/
812/* division */ 889/* division */
948} 1025}
949 1026
950/*******************************************************************************/ 1027/*******************************************************************************/
951/* fast integer to ascii */ 1028/* fast integer to ascii */
952 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
953// simply return a mask with "bits" bits set 1043/* simply return a mask with "bits" bits set *7
954#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) 1044#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1)
955 1045
956// oputput a single digit. maskvalue is 10**digitidx 1046/* oputput a single digit. maskvalue is 10**digitidx */
957#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ 1047#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \
958 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 */ \
959 { \ 1049 { \
960 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ 1050 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \
961 *ptr = digit + '0'; /* output it */ \ 1051 *ptr = digit + '0'; /* output it */ \
962 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ 1052 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \
963 ptr += nz; /* output digit only if non-zero digit seen */ \ 1053 ptr += nz; /* output digit only if non-zero digit seen */ \
964 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 */ \
965 } 1055 }
966 1056
967// 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 */
968// 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 */
969#define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ 1059#define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \
970ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ 1060ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \
971{ \ 1061{ \
972 char nz = lz; /* non-zero digit seen? */ \ 1062 char nz = lz; /* non-zero digit seen? */ \
973 /* convert to x.bits fixed-point */ \ 1063 /* convert to x.bits fixed-point */ \
984 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ 1074 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \
985 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ 1075 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \
986 return ptr; \ 1076 return ptr; \
987} 1077}
988 1078
989// predefined versions of the above, for various digits 1079/* predefined versions of the above, for various digits */
990// ecb_i2a_xN = almost N digits, limit defined by macro 1080/* ecb_i2a_xN = almost N digits, limit defined by macro */
991// ecb_i2a_N = up to N digits, leading zeroes suppressed 1081/* ecb_i2a_N = up to N digits, leading zeroes suppressed */
992// ecb_i2a_0N = exactly N digits, including leading zeroes 1082/* ecb_i2a_0N = exactly N digits, including leading zeroes */
993 1083
994// non-leading-zero versions, limited range 1084/* non-leading-zero versions, limited range */
995#define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 1085#define ECB_I2A_MAX_X5 59074 /* limit for ecb_i2a_x5 */
996#define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 1086#define ECB_I2A_MAX_X10 2932500665 /* limit for ecb_i2a_x10 */
997ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) 1087ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0)
998ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) 1088ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0)
999 1089
1000// 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 */
1001ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) 1091ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0)
1002ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) 1092ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0)
1003ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) 1093ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0)
1004ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) 1094ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0)
1005ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) 1095ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0)
1006ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) 1096ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0)
1007ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) 1097ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0)
1008ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) 1098ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0)
1009 1099
1010// 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 */
1011ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) 1101ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1)
1012ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) 1102ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1)
1013ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) 1103ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1)
1014ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) 1104ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1)
1015ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) 1105ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1)
1016ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1) 1106ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1)
1017ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1) 1107ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1)
1018ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1) 1108ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1)
1019 1109
1020#define ECB_I2A_I32_DIGITS 11 1110#define ECB_I2A_I32_DIGITS 11
1021#define ECB_I2A_U32_DIGITS 10 1111#define ECB_I2A_U32_DIGITS 10
1022#define ECB_I2A_I64_DIGITS 20 1112#define ECB_I2A_I64_DIGITS 20
1023#define ECB_I2A_U32_DIGITS 21 1113#define ECB_I2A_U64_DIGITS 21
1024#define ECB_I2A_DIGITS 21 1114#define ECB_I2A_MAX_DIGITS 21
1025 1115
1026ecb_inline char * 1116ecb_inline char *
1027ecb_i2a_u32 (char *ptr, uint32_t u) 1117ecb_i2a_u32 (char *ptr, uint32_t u)
1028{ 1118{
1029 #if ECB_64BIT_NATIVE 1119 #if ECB_64BIT_NATIVE
1030 if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) 1120 if (ecb_expect_true (u <= ECB_I2A_MAX_X10))
1031 ptr = ecb_i2a_x10 (ptr, u); 1121 ptr = ecb_i2a_x10 (ptr, u);
1032 else // x10 almost, but not fully, covers 32 bit 1122 else /* x10 almost, but not fully, covers 32 bit */
1033 { 1123 {
1034 uint32_t u1 = u % 1000000000; 1124 uint32_t u1 = u % 1000000000;
1035 uint32_t u2 = u / 1000000000; 1125 uint32_t u2 = u / 1000000000;
1036 1126
1037 *ptr++ = u2 + '0'; 1127 *ptr++ = u2 + '0';
1069{ 1159{
1070 *ptr = '-'; ptr += v < 0; 1160 *ptr = '-'; ptr += v < 0;
1071 uint32_t u = v < 0 ? -(uint32_t)v : v; 1161 uint32_t u = v < 0 ? -(uint32_t)v : v;
1072 1162
1073 #if ECB_64BIT_NATIVE 1163 #if ECB_64BIT_NATIVE
1074 ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit 1164 ptr = ecb_i2a_x10 (ptr, u); /* x10 fully covers 31 bit */
1075 #else 1165 #else
1076 ptr = ecb_i2a_u32 (ptr, u); 1166 ptr = ecb_i2a_u32 (ptr, u);
1077 #endif 1167 #endif
1078 1168
1079 return ptr; 1169 return ptr;
1142 uint64_t u1 = u % 1000000000; 1232 uint64_t u1 = u % 1000000000;
1143 uint64_t ua = u / 1000000000; 1233 uint64_t ua = u / 1000000000;
1144 uint64_t u2 = ua % 1000000000; 1234 uint64_t u2 = ua % 1000000000;
1145 uint64_t u3 = ua / 1000000000; 1235 uint64_t u3 = ua / 1000000000;
1146 1236
1147 // 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 */
1148 *ptr++ = u3 + '0'; 1238 *ptr++ = u3 + '0';
1149 ptr = ecb_i2a_09 (ptr, u2); 1239 ptr = ecb_i2a_09 (ptr, u2);
1150 ptr = ecb_i2a_09 (ptr, u1); 1240 ptr = ecb_i2a_09 (ptr, u1);
1151 } 1241 }
1152 #else 1242 #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines