… | |
… | |
609 | 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); |
610 | 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); |
611 | 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); |
612 | 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); |
613 | |
613 | |
614 | 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)); } |
615 | 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)); } |
616 | 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)); } |
617 | 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)); } |
618 | 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)); } |
619 | 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)); } |
620 | 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)); } |
621 | 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)); } |
622 | |
622 | |
623 | #if ECB_CPP |
623 | #if ECB_CPP |
624 | |
624 | |
625 | 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); } |
626 | 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); } |
… | |
… | |
774 | 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)); } |
775 | |
775 | |
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)); } |
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)); } |
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)); } |
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)); } |
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)); } |
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)); } |
779 | |
779 | |
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)); } |
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)); } |
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)); } |
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)); } |
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)); } |
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)); } |
783 | |
783 | |
784 | #if ECB_CPP |
784 | #if ECB_CPP |
… | |
… | |
947 | return s | 0x7c00 | m | !m; |
947 | return s | 0x7c00 | m | !m; |
948 | } |
948 | } |
949 | |
949 | |
950 | /*******************************************************************************/ |
950 | /*******************************************************************************/ |
951 | /* fast integer to ascii */ |
951 | /* fast integer to ascii */ |
|
|
952 | |
|
|
953 | /* |
|
|
954 | * This code is pretty complicated because it is general. The idea behind it, |
|
|
955 | * however, is pretty simple: first, the number is multiplied with a scaling |
|
|
956 | * factor (2**bits / 10**(digits-1)) to convert the integer into a fixed-point |
|
|
957 | * number with the first digit in the upper bits. |
|
|
958 | * Then this digit is converted to text and masked out. The resulting number |
|
|
959 | * is then multiplied by 10, by multiplying the fixed point representation |
|
|
960 | * by 5 and shifting the (binary) decimal point one to the right, so a 4.28 |
|
|
961 | * format becomes 5.27, 6.26 and so on. |
|
|
962 | * The rest involves only advancing the pointer if we already generated a |
|
|
963 | * non-zero digit, so leading zeroes are overwritten. |
|
|
964 | */ |
952 | |
965 | |
953 | // simply return a mask with "bits" bits set |
966 | // simply return a mask with "bits" bits set |
954 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
967 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
955 | |
968 | |
956 | // oputput a single digit. maskvalue is 10**digitidx |
969 | // oputput a single digit. maskvalue is 10**digitidx |
… | |
… | |
996 | #define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 |
1009 | #define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 |
997 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
1010 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
998 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
1011 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
999 | |
1012 | |
1000 | // non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit |
1013 | // non-leading zero versions, all digits, 4 and 9 are optimal for 32/64 bit |
1001 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
1014 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
1002 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
1015 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
1003 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
1016 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
1004 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
1017 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
1005 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
1018 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
1006 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
1019 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
1007 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
1020 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
1008 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
1021 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
1009 | |
1022 | |
1010 | // leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit |
1023 | // leading-zero versions, all digits, 04 and 09 are optimal for 32/64 bit |
1011 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
1024 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
1012 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
1025 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
1013 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
1026 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
1014 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
1027 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
1015 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
1028 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
1016 | ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1) |
1029 | ecb_i2a_def (07, ptr, v, uint64_t, 44, 1000000, 1) |
1017 | ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1) |
1030 | ecb_i2a_def (08, ptr, v, uint64_t, 50, 10000000, 1) |
1018 | ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1) |
1031 | ecb_i2a_def (09, ptr, v, uint64_t, 56, 100000000, 1) |
1019 | |
1032 | |
1020 | #define ECB_I2A_I32_DIGITS 11 |
1033 | #define ECB_I2A_I32_DIGITS 11 |
1021 | #define ECB_I2A_U32_DIGITS 10 |
1034 | #define ECB_I2A_U32_DIGITS 10 |
1022 | #define ECB_I2A_I64_DIGITS 20 |
1035 | #define ECB_I2A_I64_DIGITS 20 |
1023 | #define ECB_I2A_U32_DIGITS 21 |
1036 | #define ECB_I2A_U64_DIGITS 21 |
1024 | #define ECB_I2A_DIGITS 21 |
1037 | #define ECB_I2A_MAX_DIGITS 21 |
1025 | |
1038 | |
1026 | ecb_inline char * |
1039 | ecb_inline char * |
1027 | ecb_i2a_u32 (char *ptr, uint32_t u) |
1040 | ecb_i2a_u32 (char *ptr, uint32_t u) |
1028 | { |
1041 | { |
1029 | #if ECB_64BIT_NATIVE |
1042 | #if ECB_64BIT_NATIVE |