/src/glaze/include/glaze/util/itoa.hpp
Line | Count | Source |
1 | | // Glaze Library |
2 | | // For the license information refer to glaze.hpp |
3 | | |
4 | | #pragma once |
5 | | |
6 | | // Default integer to ascii conversion |
7 | | // Uses 400 bytes of lookup tables (char_table + digit_pairs) |
8 | | // For maximum speed with 40KB tables, use itoa_40kb.hpp instead |
9 | | |
10 | | #include <array> |
11 | | #include <bit> |
12 | | #include <concepts> |
13 | | #include <cstdint> |
14 | | #include <cstring> |
15 | | |
16 | | #include "glaze/util/inline.hpp" |
17 | | |
18 | | namespace glz |
19 | | { |
20 | | namespace itoa_impl |
21 | | { |
22 | | // 2-digit character pairs table (200 bytes) - cache-line aligned |
23 | | alignas(64) inline constexpr char char_table[200] = { |
24 | | '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', |
25 | | '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', |
26 | | '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', |
27 | | '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', |
28 | | '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', |
29 | | '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', |
30 | | '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', |
31 | | '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', |
32 | | '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', |
33 | | '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'}; |
34 | | |
35 | | // 2-digit uint16_t table for direct 2-byte memcpy (100 × 2 = 200 bytes) |
36 | | // Generated at compile-time with correct byte order for target endianness |
37 | | inline constexpr auto digit_pairs = []() consteval { |
38 | | std::array<uint16_t, 100> table{}; |
39 | | for (uint32_t i = 0; i < 100; ++i) { |
40 | | const auto d0 = uint16_t('0' + i / 10); |
41 | | const auto d1 = uint16_t('0' + i % 10); |
42 | | if constexpr (std::endian::native == std::endian::little) { |
43 | | table[i] = d0 | (d1 << 8); |
44 | | } |
45 | | else { |
46 | | table[i] = (d0 << 8) | d1; |
47 | | } |
48 | | } |
49 | | return table; |
50 | | }(); |
51 | | |
52 | | // 128-bit multiplication for efficient division by 100000000 |
53 | | // Modern compilers optimize regular division the same way, but this ensures it |
54 | | #if defined(__SIZEOF_INT128__) |
55 | | GLZ_ALWAYS_INLINE uint64_t div_1e8(uint64_t value) noexcept |
56 | 3.81k | { |
57 | 3.81k | constexpr __uint128_t multiplier = 0xabcc77118461cefdULL; |
58 | 3.81k | constexpr uint64_t shift = 90; |
59 | 3.81k | return static_cast<uint64_t>((static_cast<__uint128_t>(value) * multiplier) >> shift); |
60 | 3.81k | } |
61 | | #else |
62 | | GLZ_ALWAYS_INLINE uint64_t div_1e8(uint64_t value) noexcept { return value / 100000000ULL; } |
63 | | #endif |
64 | | |
65 | | // ==================== uint32_t implementations ==================== |
66 | | // Note: We use `buf + N - lz` offsets instead of `buf -= lz` followed by `buf + N`. |
67 | | // Forming a pointer before the start of an array is undefined behavior in C++, |
68 | | // even if the pointer is never dereferenced. `buf -= lz` when buf points to the |
69 | | // start of an allocation and lz=1 violates this. GCC _FORTIFY_SOURCE with LTO |
70 | | // correctly detects and aborts on this. |
71 | | // The compiler optimizes `buf + N - lz` identically (single sub, then offsets). |
72 | | |
73 | | GLZ_ALWAYS_INLINE char* u32_2(char* buf, uint32_t val) noexcept |
74 | 11.0M | { |
75 | 11.0M | const uint32_t lz = val < 10; |
76 | 11.0M | std::memcpy(buf, char_table + ((val * 2) | lz), 2); |
77 | 11.0M | return buf + 2 - lz; |
78 | 11.0M | } |
79 | | |
80 | | GLZ_ALWAYS_INLINE char* u32_4(char* buf, uint32_t val) noexcept |
81 | 1.55M | { |
82 | 1.55M | const uint32_t aa = (val * 5243) >> 19; // val / 100 |
83 | 1.55M | const uint32_t lz = aa < 10; |
84 | 1.55M | std::memcpy(buf, char_table + ((aa * 2) | lz), 2); |
85 | 1.55M | std::memcpy(buf + 2 - lz, &digit_pairs[val - aa * 100], 2); |
86 | 1.55M | return buf + 4 - lz; |
87 | 1.55M | } |
88 | | |
89 | | GLZ_ALWAYS_INLINE char* u32_6(char* buf, uint32_t val) noexcept |
90 | 581k | { |
91 | 581k | const uint32_t aa = uint32_t((uint64_t(val) * 429497) >> 32); // val / 10000 |
92 | 581k | const uint32_t bbcc = val - aa * 10000; |
93 | 581k | const uint32_t bb = (bbcc * 5243) >> 19; // bbcc / 100 |
94 | 581k | const uint32_t lz = aa < 10; |
95 | 581k | std::memcpy(buf, char_table + ((aa * 2) | lz), 2); |
96 | 581k | std::memcpy(buf + 2 - lz, &digit_pairs[bb], 2); |
97 | 581k | std::memcpy(buf + 4 - lz, &digit_pairs[bbcc - bb * 100], 2); |
98 | 581k | return buf + 6 - lz; |
99 | 581k | } |
100 | | |
101 | | GLZ_ALWAYS_INLINE char* u32_8(char* buf, uint32_t val) noexcept |
102 | 3.03k | { |
103 | 3.03k | const uint32_t aabb = uint32_t((uint64_t(val) * 109951163) >> 40); // val / 10000 |
104 | 3.03k | const uint32_t ccdd = val - aabb * 10000; |
105 | 3.03k | const uint32_t aa = (aabb * 5243) >> 19; // aabb / 100 |
106 | 3.03k | const uint32_t cc = (ccdd * 5243) >> 19; // ccdd / 100 |
107 | 3.03k | const uint32_t lz = aa < 10; |
108 | 3.03k | std::memcpy(buf, char_table + ((aa * 2) | lz), 2); |
109 | 3.03k | std::memcpy(buf + 2 - lz, &digit_pairs[aabb - aa * 100], 2); |
110 | 3.03k | std::memcpy(buf + 4 - lz, &digit_pairs[cc], 2); |
111 | 3.03k | std::memcpy(buf + 6 - lz, &digit_pairs[ccdd - cc * 100], 2); |
112 | 3.03k | return buf + 8 - lz; |
113 | 3.03k | } |
114 | | |
115 | | GLZ_ALWAYS_INLINE char* u32_10(char* buf, uint32_t val) noexcept |
116 | 320 | { |
117 | 320 | const uint32_t aabbcc = uint32_t((uint64_t(val) * 3518437209ul) >> 45); // val / 10000 |
118 | 320 | const uint32_t aa = uint32_t((uint64_t(aabbcc) * 429497) >> 32); // aabbcc / 10000 |
119 | 320 | const uint32_t ddee = val - aabbcc * 10000; |
120 | 320 | const uint32_t bbcc = aabbcc - aa * 10000; |
121 | 320 | const uint32_t bb = (bbcc * 5243) >> 19; |
122 | 320 | const uint32_t dd = (ddee * 5243) >> 19; |
123 | 320 | const uint32_t lz = aa < 10; |
124 | 320 | std::memcpy(buf, char_table + ((aa * 2) | lz), 2); |
125 | 320 | std::memcpy(buf + 2 - lz, &digit_pairs[bb], 2); |
126 | 320 | std::memcpy(buf + 4 - lz, &digit_pairs[bbcc - bb * 100], 2); |
127 | 320 | std::memcpy(buf + 6 - lz, &digit_pairs[dd], 2); |
128 | 320 | std::memcpy(buf + 8 - lz, &digit_pairs[ddee - dd * 100], 2); |
129 | 320 | return buf + 10 - lz; |
130 | 320 | } |
131 | | |
132 | | // Fixed 8-digit output (no leading zero handling) |
133 | | GLZ_ALWAYS_INLINE char* u64_len_8(char* buf, uint32_t val) noexcept |
134 | 3.81k | { |
135 | 3.81k | const uint32_t aabb = uint32_t((uint64_t(val) * 109951163) >> 40); |
136 | 3.81k | const uint32_t ccdd = val - aabb * 10000; |
137 | 3.81k | const uint32_t aa = (aabb * 5243) >> 19; |
138 | 3.81k | const uint32_t cc = (ccdd * 5243) >> 19; |
139 | 3.81k | std::memcpy(buf, &digit_pairs[aa], 2); |
140 | 3.81k | std::memcpy(buf + 2, &digit_pairs[aabb - aa * 100], 2); |
141 | 3.81k | std::memcpy(buf + 4, &digit_pairs[cc], 2); |
142 | 3.81k | std::memcpy(buf + 6, &digit_pairs[ccdd - cc * 100], 2); |
143 | 3.81k | return buf + 8; |
144 | 3.81k | } |
145 | | |
146 | | // Fixed 4-digit output (no leading zero handling) |
147 | | GLZ_ALWAYS_INLINE char* u64_len_4(char* buf, uint32_t val) noexcept |
148 | 590k | { |
149 | 590k | const uint32_t aa = (val * 5243) >> 19; |
150 | 590k | std::memcpy(buf, &digit_pairs[aa], 2); |
151 | 590k | std::memcpy(buf + 2, &digit_pairs[val - aa * 100], 2); |
152 | 590k | return buf + 4; |
153 | 590k | } |
154 | | |
155 | | // 1-8 digits with leading zero handling |
156 | | GLZ_ALWAYS_INLINE char* u64_len_1_8(char* buf, uint32_t val) noexcept |
157 | 0 | { |
158 | 0 | if (val < 100) { |
159 | 0 | return u32_2(buf, val); |
160 | 0 | } |
161 | 0 | else if (val < 10000) { |
162 | 0 | return u32_4(buf, val); |
163 | 0 | } |
164 | 0 | else if (val < 1000000) { |
165 | 0 | return u32_6(buf, val); |
166 | 0 | } |
167 | 0 | else { |
168 | 0 | return u32_8(buf, val); |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | // 5-8 digits with leading zero handling |
173 | | GLZ_ALWAYS_INLINE char* u64_len_5_8(char* buf, uint32_t val) noexcept |
174 | 2.15k | { |
175 | 2.15k | if (val < 1000000) { |
176 | 545 | return u32_6(buf, val); |
177 | 545 | } |
178 | 1.61k | else { |
179 | 1.61k | return u32_8(buf, val); |
180 | 1.61k | } |
181 | 2.15k | } |
182 | | } // namespace itoa_impl |
183 | | |
184 | | // ==================== Public API ==================== |
185 | | // Default integer to chars (uses 400 bytes of tables) |
186 | | // For maximum speed with 40KB tables, use to_chars_40kb from itoa_40kb.hpp |
187 | | |
188 | | template <class T> |
189 | | requires std::same_as<std::remove_cvref_t<T>, uint32_t> |
190 | | inline char* to_chars(char* buf, T val) noexcept |
191 | 3.44M | { |
192 | 3.44M | using namespace itoa_impl; |
193 | 3.44M | if (val < 100) { |
194 | 1.76M | return u32_2(buf, val); |
195 | 1.76M | } |
196 | 1.67M | else if (val < 10000) { |
197 | 1.09M | return u32_4(buf, val); |
198 | 1.09M | } |
199 | 579k | else if (val < 1000000) { |
200 | 578k | return u32_6(buf, val); |
201 | 578k | } |
202 | 469 | else if (val < 100000000) { |
203 | 149 | return u32_8(buf, val); |
204 | 149 | } |
205 | 320 | else { |
206 | 320 | return u32_10(buf, val); |
207 | 320 | } |
208 | 3.44M | } |
209 | | |
210 | | template <class T> |
211 | | requires std::same_as<std::remove_cvref_t<T>, int32_t> |
212 | | inline char* to_chars(char* buf, T val) noexcept |
213 | 3.43M | { |
214 | 3.43M | *buf = '-'; |
215 | 3.43M | return to_chars(buf + (val < 0), uint32_t(val ^ (val >> 31)) - (val >> 31)); |
216 | 3.43M | } |
217 | | |
218 | | template <class T> |
219 | | requires std::same_as<std::remove_cvref_t<T>, uint64_t> |
220 | | inline char* to_chars(char* buf, T val) noexcept |
221 | 4.66M | { |
222 | 4.66M | using namespace itoa_impl; |
223 | 4.66M | if (val < 100000000) { |
224 | 4.65M | return u64_len_1_8(buf, uint32_t(val)); |
225 | 4.65M | } |
226 | 3.81k | else if (val < 10000000000000000ull) { |
227 | 1.65k | const uint64_t hgh = div_1e8(val); |
228 | 1.65k | const uint32_t low = uint32_t(val - hgh * 100000000); |
229 | 1.65k | buf = u64_len_1_8(buf, uint32_t(hgh)); |
230 | 1.65k | return u64_len_8(buf, low); |
231 | 1.65k | } |
232 | 2.15k | else { |
233 | 2.15k | const uint64_t tmp = div_1e8(val); |
234 | 2.15k | const uint32_t low = uint32_t(val - tmp * 100000000); |
235 | 2.15k | const uint32_t hgh = uint32_t(tmp / 10000); |
236 | 2.15k | const uint32_t mid = uint32_t(tmp - uint64_t(hgh) * 10000); |
237 | 2.15k | buf = u64_len_5_8(buf, hgh); |
238 | 2.15k | buf = u64_len_4(buf, mid); |
239 | 2.15k | return u64_len_8(buf, low); |
240 | 2.15k | } |
241 | 4.66M | } |
242 | | |
243 | | template <class T> |
244 | | requires std::same_as<std::remove_cvref_t<T>, int64_t> |
245 | | inline char* to_chars(char* buf, T val) noexcept |
246 | 1.57k | { |
247 | 1.57k | *buf = '-'; |
248 | 1.57k | return to_chars(buf + (val < 0), uint64_t(val ^ (val >> 63)) - (val >> 63)); |
249 | 1.57k | } |
250 | | |
251 | | // ==================== Small integer types ==================== |
252 | | // Optimized for 8-bit and 16-bit integers with compact code paths |
253 | | // The 40KB digit_quads table doesn't help for these small ranges |
254 | | |
255 | | // uint8_t: 0-255 (1-3 digits) |
256 | | template <class T> |
257 | | requires std::same_as<std::remove_cvref_t<T>, uint8_t> |
258 | | inline char* to_chars(char* buf, T val) noexcept |
259 | 4.31M | { |
260 | 4.31M | using namespace itoa_impl; |
261 | 4.31M | if (val < 100) { |
262 | 3.83M | return u32_2(buf, val); |
263 | 3.83M | } |
264 | 477k | else { |
265 | | // 100-255: 3 digits |
266 | 477k | const uint32_t q = val / 100; |
267 | 477k | *buf = char('0' + q); |
268 | 477k | std::memcpy(buf + 1, &digit_pairs[val - q * 100], 2); |
269 | 477k | return buf + 3; |
270 | 477k | } |
271 | 4.31M | } |
272 | | |
273 | | // int8_t: -128 to 127 (1-4 chars) |
274 | | template <class T> |
275 | | requires std::same_as<std::remove_cvref_t<T>, int8_t> |
276 | | inline char* to_chars(char* buf, T val) noexcept |
277 | 1.34M | { |
278 | 1.34M | *buf = '-'; |
279 | 1.34M | return to_chars(buf + (val < 0), uint8_t(val ^ (val >> 7)) - (val >> 7)); |
280 | 1.34M | } |
281 | | |
282 | | // uint16_t: 0-65535 (1-5 digits) |
283 | | template <class T> |
284 | | requires std::same_as<std::remove_cvref_t<T>, uint16_t> |
285 | | inline char* to_chars(char* buf, T val) noexcept |
286 | 1.84M | { |
287 | 1.84M | using namespace itoa_impl; |
288 | 1.84M | if (val < 100) { |
289 | 815k | return u32_2(buf, val); |
290 | 815k | } |
291 | 1.03M | else if (val < 10000) { |
292 | 443k | return u32_4(buf, val); |
293 | 443k | } |
294 | 588k | else { |
295 | | // 10000-65535: 5 digits |
296 | 588k | const uint32_t q = val / 10000; |
297 | 588k | *buf = char('0' + q); |
298 | 588k | return u64_len_4(buf + 1, val - q * 10000); |
299 | 588k | } |
300 | 1.84M | } |
301 | | |
302 | | // int16_t: -32768 to 32767 (1-6 chars) |
303 | | template <class T> |
304 | | requires std::same_as<std::remove_cvref_t<T>, int16_t> |
305 | | inline char* to_chars(char* buf, T val) noexcept |
306 | 2.08M | { |
307 | 2.08M | *buf = '-'; |
308 | 2.08M | return to_chars(buf + (val < 0), uint16_t(val ^ (val >> 15)) - (val >> 15)); |
309 | 2.08M | } |
310 | | |
311 | | // Shared two-digit char table; re-exposed at glz scope for callers that need it. |
312 | | inline constexpr auto& char_table = itoa_impl::char_table; |
313 | | } // namespace glz |