… | |
… | |
948 | } |
948 | } |
949 | |
949 | |
950 | /*******************************************************************************/ |
950 | /*******************************************************************************/ |
951 | /* fast integer to ascii */ |
951 | /* fast integer to ascii */ |
952 | |
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 nthe 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 | */ |
|
|
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 |
957 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ |
970 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,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 |