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.207 by root, Fri Mar 25 14:34:01 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 0x0001000c
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;
454/* count trailing zero bits and count # of one bits */ 454/* count trailing zero bits and count # of one bits */
455#if ECB_GCC_VERSION(3,4) \ 455#if ECB_GCC_VERSION(3,4) \
456 || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \ 456 || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \
457 && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \ 457 && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \
458 && ECB_CLANG_BUILTIN(__builtin_popcount)) 458 && ECB_CLANG_BUILTIN(__builtin_popcount))
459 /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
460 #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
461 #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
462 #define ecb_ctz32(x) __builtin_ctz (x) 459 #define ecb_ctz32(x) __builtin_ctz (x)
460 #define ecb_ctz64(x) (__SIZEOF_LONG__ == 64 ? __builtin_ctzl (x) : __builtin_ctzll (x))
463 #define ecb_ctz64(x) __builtin_ctzll (x) 461 #define ecb_clz32(x) __builtin_clz (x)
462 #define ecb_clz64(x) (__SIZEOF_LONG__ == 64 ? __builtin_clzl (x) : __builtin_clzll (x))
463 #define ecb_ld32(x) (ecb_clz32 (x) ^ 31)
464 #define ecb_ld64(x) (ecb_clz64 (x) ^ 63)
464 #define ecb_popcount32(x) __builtin_popcount (x) 465 #define ecb_popcount32(x) __builtin_popcount (x)
465 /* no popcountll */ 466 /* ecb_popcount64 is more difficult, see below */
466#else 467#else
467 ecb_function_ ecb_const int ecb_ctz32 (uint32_t x); 468 ecb_function_ ecb_const int ecb_ctz32 (uint32_t x);
468 ecb_function_ ecb_const int 469 ecb_function_ ecb_const int
469 ecb_ctz32 (uint32_t x) 470 ecb_ctz32 (uint32_t x)
470 { 471 {
472 unsigned long r; 473 unsigned long r;
473 _BitScanForward (&r, x); 474 _BitScanForward (&r, x);
474 return (int)r; 475 return (int)r;
475#else 476#else
476 int r = 0; 477 int r = 0;
478
479 /* todo: use david seal's algorithm */
477 480
478 x &= ~x + 1; /* this isolates the lowest bit */ 481 x &= ~x + 1; /* this isolates the lowest bit */
479 482
480#if ECB_branchless_on_i386 483#if ECB_branchless_on_i386
481 r += !!(x & 0xaaaaaaaa) << 0; 484 r += !!(x & 0xaaaaaaaa) << 0;
591 x = ( x >> 16 ) | ( x << 16); 594 x = ( x >> 16 ) | ( x << 16);
592 595
593 return x; 596 return x;
594} 597}
595 598
596/* popcount64 is only available on 64 bit cpus as gcc builtin */
597/* so for this version we are lazy */
598ecb_function_ ecb_const int ecb_popcount64 (uint64_t x); 599ecb_function_ ecb_const int ecb_popcount64 (uint64_t x);
599ecb_function_ ecb_const int 600ecb_function_ ecb_const int
600ecb_popcount64 (uint64_t x) 601ecb_popcount64 (uint64_t x)
601{ 602{
603 /* popcount64 is only available on 64 bit cpus as gcc builtin. */
604 /* also, gcc/clang make this surprisingly difficult to use */
605#if (__SIZEOF_LONG__ == 8) && (ECB_GCC_VERSION(3,4) || ECB_CLANG_BUILTIN (__builtin_popcountl))
606 return __builtin_popcountl (x);
607#else
602 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); 608 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
609#endif
603} 610}
604 611
605ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count); 612ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count);
606ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count); 613ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count);
607ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count); 614ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count);
609ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count); 616ecb_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); 617ecb_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); 618ecb_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); 619ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count);
613 620
614ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } 621ecb_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); } 622ecb_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); } 623ecb_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); } 624ecb_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); } 625ecb_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); } 626ecb_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); } 627ecb_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); } 628ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (-count & 63)) | (x >> (count & 63)); }
622 629
623#if ECB_CPP 630#if ECB_CPP
624 631
625inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); } 632inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); }
626inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); } 633inline 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)); } 781ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); }
775 782
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)); } 783ecb_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)); } 784ecb_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)); } 785ecb_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 786
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)); } 787ecb_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)); } 788ecb_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)); } 789ecb_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 790
784#if ECB_CPP 791#if ECB_CPP
805template<typename T> inline void ecb_poke_u (void *ptr, T v) { memcpy (ptr, &v, sizeof (v)); } 812template<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)); } 813template<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)); } 814template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); }
808 815
809#endif 816#endif
817
818/*****************************************************************************/
819/* pointer/integer hashing */
820
821/* based on hash by Chris Wellons, https://nullprogram.com/blog/2018/07/31/ */
822ecb_function_ uint32_t ecb_mix32 (uint32_t v);
823ecb_function_ uint32_t ecb_mix32 (uint32_t v)
824{
825 v ^= v >> 16; v *= 0x7feb352dU;
826 v ^= v >> 15; v *= 0x846ca68bU;
827 v ^= v >> 16;
828 return v;
829}
830
831ecb_function_ uint32_t ecb_unmix32 (uint32_t v);
832ecb_function_ uint32_t ecb_unmix32 (uint32_t v)
833{
834 v ^= v >> 16 ; v *= 0x43021123U;
835 v ^= v >> 15 ^ v >> 30; v *= 0x1d69e2a5U;
836 v ^= v >> 16 ;
837 return v;
838}
839
840/* based on splitmix64, by Sebastiona Vigna, https://prng.di.unimi.it/splitmix64.c */
841ecb_function_ uint64_t ecb_mix64 (uint64_t v);
842ecb_function_ uint64_t ecb_mix64 (uint64_t v)
843{
844 v ^= v >> 30; v *= 0xbf58476d1ce4e5b9U;
845 v ^= v >> 27; v *= 0x94d049bb133111ebU;
846 v ^= v >> 31;
847 return v;
848}
849
850ecb_function_ uint64_t ecb_unmix64 (uint64_t v);
851ecb_function_ uint64_t ecb_unmix64 (uint64_t v)
852{
853 v ^= v >> 31 ^ v >> 62; v *= 0x319642b2d24d8ec3U;
854 v ^= v >> 27 ^ v >> 54; v *= 0x96de1b173f119089U;
855 v ^= v >> 30 ^ v >> 60;
856 return v;
857}
858
859ecb_function_ uintptr_t ecb_ptrmix (void *p);
860ecb_function_ uintptr_t ecb_ptrmix (void *p)
861{
862 #if ECB_PTRSIZE <= 4
863 return ecb_mix32 ((uint32_t)p);
864 #else
865 return ecb_mix64 ((uint64_t)p);
866 #endif
867}
868
869ecb_function_ void *ecb_ptrunmix (uintptr_t v);
870ecb_function_ void *ecb_ptrunmix (uintptr_t v)
871{
872 #if ECB_PTRSIZE <= 4
873 return (void *)ecb_unmix32 (v);
874 #else
875 return (void *)ecb_unmix64 (v);
876 #endif
877}
878
879#if ECB_CPP
880
881template<typename T>
882inline uintptr_t ecb_ptrmix (T *p)
883{
884 return ecb_ptrmix (static_cast<void *>(p));
885}
886
887template<typename T>
888inline T *ecb_ptrunmix (uintptr_t v)
889{
890 return static_cast<T *>(ecb_ptrunmix (v));
891}
892
893#endif
894
895/*****************************************************************************/
896/* gray code */
897
898ecb_function_ uint_fast8_t ecb_gray8_encode (uint_fast8_t b) { return b ^ (b >> 1); }
899ecb_function_ uint_fast16_t ecb_gray16_encode (uint_fast16_t b) { return b ^ (b >> 1); }
900ecb_function_ uint_fast32_t ecb_gray32_encode (uint_fast32_t b) { return b ^ (b >> 1); }
901ecb_function_ uint_fast64_t ecb_gray64_encode (uint_fast64_t b) { return b ^ (b >> 1); }
902
903ecb_function_ uint8_t ecb_gray8_decode (uint8_t g)
904{
905 g ^= g >> 1;
906 g ^= g >> 2;
907 g ^= g >> 4;
908
909 return g;
910}
911
912ecb_function_ uint16_t ecb_gray16_decode (uint16_t g)
913{
914 g ^= g >> 1;
915 g ^= g >> 2;
916 g ^= g >> 4;
917 g ^= g >> 8;
918
919 return g;
920}
921
922ecb_function_ uint32_t ecb_gray32_decode (uint32_t g)
923{
924 g ^= g >> 1;
925 g ^= g >> 2;
926 g ^= g >> 4;
927 g ^= g >> 8;
928 g ^= g >> 16;
929
930 return g;
931}
932
933ecb_function_ uint64_t ecb_gray64_decode (uint64_t g)
934{
935 g ^= g >> 1;
936 g ^= g >> 2;
937 g ^= g >> 4;
938 g ^= g >> 8;
939 g ^= g >> 16;
940 g ^= g >> 32;
941
942 return g;
943}
944
945#if ECB_CPP
946
947ecb_function_ uint8_t ecb_gray_encode (uint8_t b) { return ecb_gray8_encode (b); }
948ecb_function_ uint16_t ecb_gray_encode (uint16_t b) { return ecb_gray16_encode (b); }
949ecb_function_ uint32_t ecb_gray_encode (uint32_t b) { return ecb_gray32_encode (b); }
950ecb_function_ uint64_t ecb_gray_encode (uint64_t b) { return ecb_gray64_encode (b); }
951
952ecb_function_ uint8_t ecb_gray_decode (uint8_t g) { return ecb_gray8_decode (g); }
953ecb_function_ uint16_t ecb_gray_decode (uint16_t g) { return ecb_gray16_decode (g); }
954ecb_function_ uint32_t ecb_gray_decode (uint32_t g) { return ecb_gray32_decode (g); }
955ecb_function_ uint64_t ecb_gray_decode (uint64_t g) { return ecb_gray64_decode (g); }
956
957#endif
958
959/*****************************************************************************/
960/* 2d hilbert curves */
961
962/* algorithm from the book Hacker's Delight, modified to not */
963/* run into undefined behaviour for n==16 */
964static uint32_t
965ecb_hilbert2d_index_to_coord32 (int n, uint32_t s)
966{
967 uint32_t comp, swap, cs, t, sr;
968
969 /* pad s on the left (unused) bits with 01 (no change groups) */
970 s |= 0x55555555U << n << n;
971 /* "s shift right" */
972 sr = (s >> 1) & 0x55555555U;
973 /* compute complement and swap info in two-bit groups */
974 cs = ((s & 0x55555555U) + sr) ^ 0x55555555U;
975
976 /* parallel prefix xor op to propagate both complement
977 * and swap info together from left to right (there is
978 * no step "cs ^= cs >> 1", so in effect it computes
979 * two independent parallel prefix operations on two
980 * interleaved sets of sixteen bits).
981 */
982 cs ^= cs >> 2;
983 cs ^= cs >> 4;
984 cs ^= cs >> 8;
985 cs ^= cs >> 16;
986
987 /* separate swap and complement bits */
988 swap = cs & 0x55555555U;
989 comp = (cs >> 1) & 0x55555555U;
990
991 /* calculate coordinates in odd and even bit positions */
992 t = (s & swap) ^ comp;
993 s = s ^ sr ^ t ^ (t << 1);
994
995 /* unpad/clear out any junk on the left */
996 s = s & ((1 << n << n) - 1);
997
998 /* Now "unshuffle" to separate the x and y bits. */
999 t = (s ^ (s >> 1)) & 0x22222222U; s ^= t ^ (t << 1);
1000 t = (s ^ (s >> 2)) & 0x0c0c0c0cU; s ^= t ^ (t << 2);
1001 t = (s ^ (s >> 4)) & 0x00f000f0U; s ^= t ^ (t << 4);
1002 t = (s ^ (s >> 8)) & 0x0000ff00U; s ^= t ^ (t << 8);
1003
1004 /* now s contains two 16-bit coordinates */
1005 return s;
1006}
1007
1008/* 64 bit, a straightforward extension to the 32 bit case */
1009static uint64_t
1010ecb_hilbert2d_index_to_coord64 (int n, uint64_t s)
1011{
1012 uint64_t comp, swap, cs, t, sr;
1013
1014 /* pad s on the left (unused) bits with 01 (no change groups) */
1015 s |= 0x5555555555555555U << n << n;
1016 /* "s shift right" */
1017 sr = (s >> 1) & 0x5555555555555555U;
1018 /* compute complement and swap info in two-bit groups */
1019 cs = ((s & 0x5555555555555555U) + sr) ^ 0x5555555555555555U;
1020
1021 /* parallel prefix xor op to propagate both complement
1022 * and swap info together from left to right (there is
1023 * no step "cs ^= cs >> 1", so in effect it computes
1024 * two independent parallel prefix operations on two
1025 * interleaved sets of thirty-two bits).
1026 */
1027 cs ^= cs >> 2;
1028 cs ^= cs >> 4;
1029 cs ^= cs >> 8;
1030 cs ^= cs >> 16;
1031 cs ^= cs >> 32;
1032
1033 /* separate swap and complement bits */
1034 swap = cs & 0x5555555555555555U;
1035 comp = (cs >> 1) & 0x5555555555555555U;
1036
1037 /* calculate coordinates in odd and even bit positions */
1038 t = (s & swap) ^ comp;
1039 s = s ^ sr ^ t ^ (t << 1);
1040
1041 /* unpad/clear out any junk on the left */
1042 s = s & ((1 << n << n) - 1);
1043
1044 /* Now "unshuffle" to separate the x and y bits. */
1045 t = (s ^ (s >> 1)) & 0x2222222222222222U; s ^= t ^ (t << 1);
1046 t = (s ^ (s >> 2)) & 0x0c0c0c0c0c0c0c0cU; s ^= t ^ (t << 2);
1047 t = (s ^ (s >> 4)) & 0x00f000f000f000f0U; s ^= t ^ (t << 4);
1048 t = (s ^ (s >> 8)) & 0x0000ff000000ff00U; s ^= t ^ (t << 8);
1049 t = (s ^ (s >> 16)) & 0x00000000ffff0000U; s ^= t ^ (t << 16);
1050
1051 /* now s contains two 32-bit coordinates */
1052 return s;
1053}
1054
1055/* algorithm from the book Hacker's Delight, but a similar algorithm*/
1056/* is given in https://doi.org/10.1002/spe.4380160103 */
1057/* this has been slightly improved over the original version */
1058ecb_function_ uint32_t
1059ecb_hilbert2d_coord_to_index32 (int n, uint32_t xy)
1060{
1061 uint32_t row;
1062 uint32_t state = 0;
1063 uint32_t s = 0;
1064
1065 do
1066 {
1067 --n;
1068
1069 row = 4 * state
1070 | (2 & (xy >> n >> 15))
1071 | (1 & (xy >> n ));
1072
1073 /* these funky constants are lookup tables for two-bit values */
1074 s = (s << 2) | (0x361e9cb4U >> 2 * row) & 3;
1075 state = (0x8fe65831U >> 2 * row) & 3;
1076 }
1077 while (n > 0);
1078
1079 return s;
1080}
1081
1082/* 64 bit, essentially the same as 32 bit */
1083ecb_function_ uint64_t
1084ecb_hilbert2d_coord_to_index64 (int n, uint64_t xy)
1085{
1086 uint32_t row;
1087 uint32_t state = 0;
1088 uint64_t s = 0;
1089
1090 do
1091 {
1092 --n;
1093
1094 row = 4 * state
1095 | (2 & (xy >> n >> 31))
1096 | (1 & (xy >> n ));
1097
1098 /* these funky constants are lookup tables for two-bit values */
1099 s = (s << 2) | (0x361e9cb4U >> 2 * row) & 3;
1100 state = (0x8fe65831U >> 2 * row) & 3;
1101 }
1102 while (n > 0);
1103
1104 return s;
1105}
810 1106
811/*****************************************************************************/ 1107/*****************************************************************************/
812/* division */ 1108/* division */
813 1109
814#if ECB_GCC_VERSION(3,0) || ECB_C99 1110#if ECB_GCC_VERSION(3,0) || ECB_C99
948} 1244}
949 1245
950/*******************************************************************************/ 1246/*******************************************************************************/
951/* fast integer to ascii */ 1247/* fast integer to ascii */
952 1248
1249/*
1250 * This code is pretty complicated because it is general. The idea behind it,
1251 * however, is pretty simple: first, the number is multiplied with a scaling
1252 * factor (2**bits / 10**(digits-1)) to convert the integer into a fixed-point
1253 * number with the first digit in the upper bits.
1254 * Then this digit is converted to text and masked out. The resulting number
1255 * is then multiplied by 10, by multiplying the fixed point representation
1256 * by 5 and shifting the (binary) decimal point one to the right, so a 4.28
1257 * format becomes 5.27, 6.26 and so on.
1258 * The rest involves only advancing the pointer if we already generated a
1259 * non-zero digit, so leading zeroes are overwritten.
1260 */
1261
953// simply return a mask with "bits" bits set 1262/* simply return a mask with "bits" bits set */
954#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) 1263#define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1)
955 1264
956// oputput a single digit. maskvalue is 10**digitidx 1265/* oputput a single digit. maskvalue is 10**digitidx */
957#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ 1266#define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \
958 if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \ 1267 if (digitmask >= maskvalue) /* constant, used to decide how many digits to generate */ \
959 { \ 1268 { \
960 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ 1269 char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \
961 *ptr = digit + '0'; /* output it */ \ 1270 *ptr = digit + '0'; /* output it */ \
962 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ 1271 nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \
963 ptr += nz; /* output digit only if non-zero digit seen */ \ 1272 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 */ \ 1273 x = (x & ecb_i2a_mask (type, bits - digitidx)) * 5; /* *10, but shift decimal point right */ \
965 } 1274 }
966 1275
967// convert integer to fixed point format and multiply out digits, highest first 1276/* 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 1277/* 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) \ 1278#define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \
970ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ 1279ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \
971{ \ 1280{ \
972 char nz = lz; /* non-zero digit seen? */ \ 1281 char nz = lz; /* non-zero digit seen? */ \
973 /* convert to x.bits fixed-point */ \ 1282 /* convert to x.bits fixed-point */ \
984 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ 1293 ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \
985 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ 1294 ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \
986 return ptr; \ 1295 return ptr; \
987} 1296}
988 1297
989// predefined versions of the above, for various digits 1298/* predefined versions of the above, for various digits */
990// ecb_i2a_xN = almost N digits, limit defined by macro 1299/* ecb_i2a_xN = almost N digits, limit defined by macro */
991// ecb_i2a_N = up to N digits, leading zeroes suppressed 1300/* ecb_i2a_N = up to N digits, leading zeroes suppressed */
992// ecb_i2a_0N = exactly N digits, including leading zeroes 1301/* ecb_i2a_0N = exactly N digits, including leading zeroes */
993 1302
994// non-leading-zero versions, limited range 1303/* non-leading-zero versions, limited range */
995#define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 1304#define ECB_I2A_MAX_X5 59074 /* limit for ecb_i2a_x5 */
996#define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 1305#define ECB_I2A_MAX_X10 2932500665 /* limit for ecb_i2a_x10 */
997ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) 1306ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0)
998ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) 1307ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0)
999 1308
1000// non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit 1309/* 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) 1310ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0)
1002ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) 1311ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0)
1003ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) 1312ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0)
1004ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) 1313ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0)
1005ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) 1314ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0)
1006ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) 1315ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0)
1007ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) 1316ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0)
1008ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) 1317ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0)
1009 1318
1010// leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit 1319/* 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) 1320ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1)
1012ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) 1321ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1)
1013ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) 1322ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1)
1014ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) 1323ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1)
1015ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) 1324ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1)
1016ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1) 1325ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1)
1017ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1) 1326ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1)
1018ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1) 1327ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1)
1019 1328
1020#define ECB_I2A_I32_DIGITS 11 1329#define ECB_I2A_I32_DIGITS 11
1021#define ECB_I2A_U32_DIGITS 10 1330#define ECB_I2A_U32_DIGITS 10
1022#define ECB_I2A_I64_DIGITS 20 1331#define ECB_I2A_I64_DIGITS 20
1023#define ECB_I2A_U32_DIGITS 21 1332#define ECB_I2A_U64_DIGITS 21
1024#define ECB_I2A_DIGITS 21 1333#define ECB_I2A_MAX_DIGITS 21
1025 1334
1026ecb_inline char * 1335ecb_inline char *
1027ecb_i2a_u32 (char *ptr, uint32_t u) 1336ecb_i2a_u32 (char *ptr, uint32_t u)
1028{ 1337{
1029 #if ECB_64BIT_NATIVE 1338 #if ECB_64BIT_NATIVE
1030 if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) 1339 if (ecb_expect_true (u <= ECB_I2A_MAX_X10))
1031 ptr = ecb_i2a_x10 (ptr, u); 1340 ptr = ecb_i2a_x10 (ptr, u);
1032 else // x10 almost, but not fully, covers 32 bit 1341 else /* x10 almost, but not fully, covers 32 bit */
1033 { 1342 {
1034 uint32_t u1 = u % 1000000000; 1343 uint32_t u1 = u % 1000000000;
1035 uint32_t u2 = u / 1000000000; 1344 uint32_t u2 = u / 1000000000;
1036 1345
1037 *ptr++ = u2 + '0'; 1346 *ptr++ = u2 + '0';
1069{ 1378{
1070 *ptr = '-'; ptr += v < 0; 1379 *ptr = '-'; ptr += v < 0;
1071 uint32_t u = v < 0 ? -(uint32_t)v : v; 1380 uint32_t u = v < 0 ? -(uint32_t)v : v;
1072 1381
1073 #if ECB_64BIT_NATIVE 1382 #if ECB_64BIT_NATIVE
1074 ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit 1383 ptr = ecb_i2a_x10 (ptr, u); /* x10 fully covers 31 bit */
1075 #else 1384 #else
1076 ptr = ecb_i2a_u32 (ptr, u); 1385 ptr = ecb_i2a_u32 (ptr, u);
1077 #endif 1386 #endif
1078 1387
1079 return ptr; 1388 return ptr;
1142 uint64_t u1 = u % 1000000000; 1451 uint64_t u1 = u % 1000000000;
1143 uint64_t ua = u / 1000000000; 1452 uint64_t ua = u / 1000000000;
1144 uint64_t u2 = ua % 1000000000; 1453 uint64_t u2 = ua % 1000000000;
1145 uint64_t u3 = ua / 1000000000; 1454 uint64_t u3 = ua / 1000000000;
1146 1455
1147 // 2**31 is 19 digits, so the top is exactly one digit 1456 /* 2**31 is 19 digits, so the top is exactly one digit */
1148 *ptr++ = u3 + '0'; 1457 *ptr++ = u3 + '0';
1149 ptr = ecb_i2a_09 (ptr, u2); 1458 ptr = ecb_i2a_09 (ptr, u2);
1150 ptr = ecb_i2a_09 (ptr, u1); 1459 ptr = ecb_i2a_09 (ptr, u1);
1151 } 1460 }
1152 #else 1461 #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines