… | |
… | |
1038 | * format becomes 5.27, 6.26 and so on. |
1038 | * format becomes 5.27, 6.26 and so on. |
1039 | * The rest involves only advancing the pointer if we already generated a |
1039 | * The rest involves only advancing the pointer if we already generated a |
1040 | * non-zero digit, so leading zeroes are overwritten. |
1040 | * non-zero digit, so leading zeroes are overwritten. |
1041 | */ |
1041 | */ |
1042 | |
1042 | |
1043 | // simply return a mask with "bits" bits set |
1043 | /* simply return a mask with "bits" bits set *7 |
1044 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
1044 | #define ecb_i2a_mask(type,bits) ((((type)1) << (bits)) - 1) |
1045 | |
1045 | |
1046 | // oputput a single digit. maskvalue is 10**digitidx |
1046 | /* oputput a single digit. maskvalue is 10**digitidx */ |
1047 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ |
1047 | #define ecb_i2a_digit(type,bits,digitmask,maskvalue,digitidx) \ |
1048 | 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 */ \ |
1049 | { \ |
1049 | { \ |
1050 | char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ |
1050 | char digit = x >> (bits - digitidx); /* calculate the topmost digit */ \ |
1051 | *ptr = digit + '0'; /* output it */ \ |
1051 | *ptr = digit + '0'; /* output it */ \ |
1052 | nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ |
1052 | nz = (digitmask == maskvalue) || nz || digit; /* first term == always output last digit */ \ |
1053 | ptr += nz; /* output digit only if non-zero digit seen */ \ |
1053 | ptr += nz; /* output digit only if non-zero digit seen */ \ |
1054 | 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 */ \ |
1055 | } |
1055 | } |
1056 | |
1056 | |
1057 | // 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 */ |
1058 | // 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 */ |
1059 | #define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ |
1059 | #define ecb_i2a_def(suffix,ptr,v,type,bits,digitmask,lz) \ |
1060 | ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ |
1060 | ecb_inline char *ecb_i2a_ ## suffix (char *ptr, uint32_t u) \ |
1061 | { \ |
1061 | { \ |
1062 | char nz = lz; /* non-zero digit seen? */ \ |
1062 | char nz = lz; /* non-zero digit seen? */ \ |
1063 | /* convert to x.bits fixed-point */ \ |
1063 | /* convert to x.bits fixed-point */ \ |
… | |
… | |
1074 | ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ |
1074 | ecb_i2a_digit (type,bits,digitmask, 100000000, 8); \ |
1075 | ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ |
1075 | ecb_i2a_digit (type,bits,digitmask, 1000000000, 9); \ |
1076 | return ptr; \ |
1076 | return ptr; \ |
1077 | } |
1077 | } |
1078 | |
1078 | |
1079 | // predefined versions of the above, for various digits |
1079 | /* predefined versions of the above, for various digits */ |
1080 | // ecb_i2a_xN = almost N digits, limit defined by macro |
1080 | /* ecb_i2a_xN = almost N digits, limit defined by macro */ |
1081 | // ecb_i2a_N = up to N digits, leading zeroes suppressed |
1081 | /* ecb_i2a_N = up to N digits, leading zeroes suppressed */ |
1082 | // ecb_i2a_0N = exactly N digits, including leading zeroes |
1082 | /* ecb_i2a_0N = exactly N digits, including leading zeroes */ |
1083 | |
1083 | |
1084 | // non-leading-zero versions, limited range |
1084 | /* non-leading-zero versions, limited range */ |
1085 | #define ECB_I2A_MAX_X5 59074 // limit for ecb_i2a_x5 |
1085 | #define ECB_I2A_MAX_X5 59074 /* limit for ecb_i2a_x5 */ |
1086 | #define ECB_I2A_MAX_X10 2932500665 // limit for ecb_i2a_x10 |
1086 | #define ECB_I2A_MAX_X10 2932500665 /* limit for ecb_i2a_x10 */ |
1087 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
1087 | ecb_i2a_def ( x5, ptr, v, uint32_t, 26, 10000, 0) |
1088 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
1088 | ecb_i2a_def (x10, ptr, v, uint64_t, 60, 1000000000, 0) |
1089 | |
1089 | |
1090 | // 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 */ |
1091 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
1091 | ecb_i2a_def ( 2, ptr, v, uint32_t, 10, 10, 0) |
1092 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
1092 | ecb_i2a_def ( 3, ptr, v, uint32_t, 12, 100, 0) |
1093 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
1093 | ecb_i2a_def ( 4, ptr, v, uint32_t, 26, 1000, 0) |
1094 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
1094 | ecb_i2a_def ( 5, ptr, v, uint64_t, 30, 10000, 0) |
1095 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
1095 | ecb_i2a_def ( 6, ptr, v, uint64_t, 36, 100000, 0) |
1096 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
1096 | ecb_i2a_def ( 7, ptr, v, uint64_t, 44, 1000000, 0) |
1097 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
1097 | ecb_i2a_def ( 8, ptr, v, uint64_t, 50, 10000000, 0) |
1098 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
1098 | ecb_i2a_def ( 9, ptr, v, uint64_t, 56, 100000000, 0) |
1099 | |
1099 | |
1100 | // 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 */ |
1101 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
1101 | ecb_i2a_def (02, ptr, v, uint32_t, 10, 10, 1) |
1102 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
1102 | ecb_i2a_def (03, ptr, v, uint32_t, 12, 100, 1) |
1103 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
1103 | ecb_i2a_def (04, ptr, v, uint32_t, 26, 1000, 1) |
1104 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
1104 | ecb_i2a_def (05, ptr, v, uint64_t, 30, 10000, 1) |
1105 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
1105 | ecb_i2a_def (06, ptr, v, uint64_t, 36, 100000, 1) |
… | |
… | |
1117 | ecb_i2a_u32 (char *ptr, uint32_t u) |
1117 | ecb_i2a_u32 (char *ptr, uint32_t u) |
1118 | { |
1118 | { |
1119 | #if ECB_64BIT_NATIVE |
1119 | #if ECB_64BIT_NATIVE |
1120 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
1120 | if (ecb_expect_true (u <= ECB_I2A_MAX_X10)) |
1121 | ptr = ecb_i2a_x10 (ptr, u); |
1121 | ptr = ecb_i2a_x10 (ptr, u); |
1122 | else // x10 almost, but not fully, covers 32 bit |
1122 | else /* x10 almost, but not fully, covers 32 bit */ |
1123 | { |
1123 | { |
1124 | uint32_t u1 = u % 1000000000; |
1124 | uint32_t u1 = u % 1000000000; |
1125 | uint32_t u2 = u / 1000000000; |
1125 | uint32_t u2 = u / 1000000000; |
1126 | |
1126 | |
1127 | *ptr++ = u2 + '0'; |
1127 | *ptr++ = u2 + '0'; |
… | |
… | |
1159 | { |
1159 | { |
1160 | *ptr = '-'; ptr += v < 0; |
1160 | *ptr = '-'; ptr += v < 0; |
1161 | uint32_t u = v < 0 ? -(uint32_t)v : v; |
1161 | uint32_t u = v < 0 ? -(uint32_t)v : v; |
1162 | |
1162 | |
1163 | #if ECB_64BIT_NATIVE |
1163 | #if ECB_64BIT_NATIVE |
1164 | ptr = ecb_i2a_x10 (ptr, u); // x10 fully covers 31 bit |
1164 | ptr = ecb_i2a_x10 (ptr, u); /* x10 fully covers 31 bit */ |
1165 | #else |
1165 | #else |
1166 | ptr = ecb_i2a_u32 (ptr, u); |
1166 | ptr = ecb_i2a_u32 (ptr, u); |
1167 | #endif |
1167 | #endif |
1168 | |
1168 | |
1169 | return ptr; |
1169 | return ptr; |
… | |
… | |
1232 | uint64_t u1 = u % 1000000000; |
1232 | uint64_t u1 = u % 1000000000; |
1233 | uint64_t ua = u / 1000000000; |
1233 | uint64_t ua = u / 1000000000; |
1234 | uint64_t u2 = ua % 1000000000; |
1234 | uint64_t u2 = ua % 1000000000; |
1235 | uint64_t u3 = ua / 1000000000; |
1235 | uint64_t u3 = ua / 1000000000; |
1236 | |
1236 | |
1237 | // 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 */ |
1238 | *ptr++ = u3 + '0'; |
1238 | *ptr++ = u3 + '0'; |
1239 | ptr = ecb_i2a_09 (ptr, u2); |
1239 | ptr = ecb_i2a_09 (ptr, u2); |
1240 | ptr = ecb_i2a_09 (ptr, u1); |
1240 | ptr = ecb_i2a_09 (ptr, u1); |
1241 | } |
1241 | } |
1242 | #else |
1242 | #else |