1 // std::to_chars implementation for floating-point types -*- C++ -*-
2
3 // Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 // Activate __glibcxx_assert within this file to shake out any bugs.
26 #define _GLIBCXX_ASSERTIONS 1
27
28 #include <charconv>
29
30 #include <bit>
31 #include <cfenv>
32 #include <cassert>
33 #include <cmath>
34 #include <cstdio>
35 #include <cstring>
36 #if __has_include(<langinfo.h>)
37 # include <langinfo.h> // for nl_langinfo
38 #endif
39 #include <optional>
40 #include <string_view>
41 #include <type_traits>
42
43 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
44 #ifndef __LONG_DOUBLE_IBM128__
45 #error "floating_to_chars.cc must be compiled with -mabi=ibmlongdouble"
46 #endif
47 // sprintf for __ieee128
48 extern "C" int __sprintfieee128(char*, const char*, ...);
49 #endif
50
51 // This implementation crucially assumes float/double have the
52 // IEEE binary32/binary64 formats.
53 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
54 /* And it also assumes that uint64_t POW10_SPLIT_2[3133][3] is valid. */\
55 && __SIZE_WIDTH__ >= 32
56
57 // Determine the binary format of 'long double'.
58
59 // We support the binary64, float80 (i.e. x86 80-bit extended precision),
60 // binary128, and ibm128 formats.
61 #define LDK_UNSUPPORTED 0
62 #define LDK_BINARY64 1
63 #define LDK_FLOAT80 2
64 #define LDK_BINARY128 3
65 #define LDK_IBM128 4
66
67 #if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
68 # define LONG_DOUBLE_KIND LDK_BINARY64
69 #elif __LDBL_MANT_DIG__ == 64
70 # define LONG_DOUBLE_KIND LDK_FLOAT80
71 #elif __LDBL_MANT_DIG__ == 113
72 # define LONG_DOUBLE_KIND LDK_BINARY128
73 #elif __LDBL_MANT_DIG__ == 106
74 # define LONG_DOUBLE_KIND LDK_IBM128
75 #else
76 # define LONG_DOUBLE_KIND LDK_UNSUPPORTED
77 #endif
78
79 #if defined _GLIBCXX_USE_FLOAT128 && __FLT128_MANT_DIG__ == 113
80 // Define overloads of std::to_chars for __float128.
81 # define FLOAT128_TO_CHARS 1
82 #endif
83
84 // For now we only support __float128 when it's the powerpc64 __ieee128 type.
85 #ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
86 # undef FLOAT128_TO_CHARS
87 #endif
88
89 #ifdef FLOAT128_TO_CHARS
90 using F128_type = __float128;
91 #else
92 using F128_type = void;
93 #endif
94
95 namespace
96 {
97 #if defined __SIZEOF_INT128__
98 using uint128_t = unsigned __int128;
99 #else
100 # include "uint128_t.h"
101 #endif
102
103 namespace ryu
104 {
105 #include "ryu/common.h"
106 #include "ryu/digit_table.h"
107 #include "ryu/d2s_intrinsics.h"
108 #include "ryu/d2s_full_table.h"
109 #include "ryu/d2fixed_full_table.h"
110 #include "ryu/f2s_intrinsics.h"
111 #include "ryu/d2s.c"
112 #include "ryu/d2fixed.c"
113 #include "ryu/f2s.c"
114
115 namespace generic128
116 {
117 // Put the generic Ryu bits in their own namespace to avoid name conflicts.
118 # include "ryu/generic_128.h"
119 # include "ryu/ryu_generic_128.h"
120 # include "ryu/generic_128.c"
121 } // namespace generic128
122
123 using generic128::floating_decimal_128;
124 using generic128::generic_binary_to_decimal;
125
126 int
to_chars(const floating_decimal_128 v,char * const result)127 to_chars(const floating_decimal_128 v, char* const result)
128 { return generic128::generic_to_chars(v, result); }
129 } // namespace ryu
130
131 // A traits class that contains pertinent information about the binary
132 // format of each of the floating-point types we support.
133 template<typename T>
134 struct floating_type_traits
135 { };
136
137 template<>
138 struct floating_type_traits<float>
139 {
140 static constexpr int mantissa_bits = 23;
141 static constexpr int exponent_bits = 8;
142 static constexpr bool has_implicit_leading_bit = true;
143 using mantissa_t = uint32_t;
144 using shortest_scientific_t = ryu::floating_decimal_32;
145
146 static constexpr uint64_t pow10_adjustment_tab[]
147 = { 0b0000000000011101011100110101100101101110000000000000000000000000 };
148 };
149
150 template<>
151 struct floating_type_traits<double>
152 {
153 static constexpr int mantissa_bits = 52;
154 static constexpr int exponent_bits = 11;
155 static constexpr bool has_implicit_leading_bit = true;
156 using mantissa_t = uint64_t;
157 using shortest_scientific_t = ryu::floating_decimal_64;
158
159 static constexpr uint64_t pow10_adjustment_tab[]
160 = { 0b0000000000000000000000011000110101110111000001100101110000111100,
161 0b0111100011110101011000011110000000110110010101011000001110011111,
162 0b0101101100000000011100100100111100110110110100010001010101110000,
163 0b0011110010111000101111110101100011101100010001010000000101100111,
164 0b0001010000011001011100100001010000010101101000001101000000000000 };
165 };
166
167 #if LONG_DOUBLE_KIND == LDK_BINARY128 || defined FLOAT128_TO_CHARS
168 // Traits for the IEEE binary128 format.
169 struct floating_type_traits_binary128
170 {
171 static constexpr int mantissa_bits = 112;
172 static constexpr int exponent_bits = 15;
173 static constexpr bool has_implicit_leading_bit = true;
174 using mantissa_t = uint128_t;
175 using shortest_scientific_t = ryu::floating_decimal_128;
176
177 static constexpr uint64_t pow10_adjustment_tab[]
178 = { 0b0000000000000000000000000000000000000000000000000100000010000000,
179 0b1011001111110100000100010101101110011100100110000110010110011000,
180 0b1010100010001101111111000000001101010010100010010000111011110111,
181 0b1011111001110001111000011111000010110111000111110100101010100101,
182 0b0110100110011110011011000011000010011001110001001001010011100011,
183 0b0000011111110010101111101011101010000110011111100111001110100111,
184 0b0100010101010110000010111011110100000010011001001010001110111101,
185 0b1101110111000010001101100000110100000111001001101011000101011011,
186 0b0100111011101101010000001101011000101100101110010010110000101011,
187 0b0100000110111000000110101000010011101000110100010110000011101101,
188 0b1011001101001000100001010001100100001111011101010101110001010110,
189 0b1000000001000000101001110010110010001111101101010101001100000110,
190 0b0101110110100110000110000001001010111110001110010000111111010011,
191 0b1010001111100111000100011100100100111100100101000001011001000111,
192 0b1010011000011100110101100111001011100101111111100001110100000100,
193 0b1100011100100010100000110001001010000000100000001001010111011101,
194 0b0101110000100011001111101101000000100110000010010111010001111010,
195 0b0100111100011010110111101000100110000111001001101100000001111100,
196 0b1100100100111110101011000100000101011010110111000111110100110101,
197 0b0110010000010111010100110011000000111010000010111011010110000100,
198 0b0101001001010010110111010111000101011100000111100111000001110010,
199 0b1101111111001011101010110001000111011010111101001011010110100100,
200 0b0001000100110000011111101011001101110010110110010000000011100100,
201 0b0001000000000101001001001000000000011000100011001110101001001110,
202 0b0010010010001000111010011011100001000110011011011110110100111000,
203 0b0000100110101100000111100010100100011100110111011100001111001100,
204 0b1011111010001110001100000011110111111111100000001011111111101100,
205 0b0000011100001111010101110000100110111100101101110111101001000001,
206 0b1100010001110110111100001001001101101000011100000010110101001011,
207 0b0100101001101011111001011110101101100011011111011100101010101111,
208 0b0001101001111001110000101101101100001011010001011110011101000010,
209 0b1111000000101001101111011010110011101110100001011011001011100010,
210 0b0101001010111101101100001111100010010110001101001000001101100100,
211 0b0101100101011110001100101011111000111001111001001001101101100001,
212 0b1111001101010010100100011011000110110010001111000111010001001101,
213 0b0001110010011000000001000110110111011000011100001000011001110111,
214 0b0100001011011011011011110011101100100101111111101100101000001110,
215 0b0101011110111101010111100111101111000101111111111110100011011010,
216 0b1110101010001001110100000010110111010111111010111110100110010110,
217 0b1010001111100001001100101000110100001100011100110010000011010111,
218 0b1111111101101111000100111100000101011000001110011011101010111001,
219 0b1111101100001110100101111101011001000100000101110000110010100011,
220 0b1001010110110101101101000101010001010000101011011111010011010000,
221 0b0111001110110011101001100111000001000100001010110000010000001101,
222 0b0101111100111110100111011001111001111011011110010111010011101010,
223 0b1110111000000001100100111001100100110001011011001110101111110111,
224 0b0001010001001101010111101010011111000011110001101101011001111111,
225 0b0101000011100011010010001101100001011101011010100110101100100010,
226 0b0001000101011000100101111100110110000101101101111000110001001011,
227 0b0101100101001011011000010101000000010100011100101101000010011111,
228 0b1000010010001011101001011010100010111011110100110011011000100111,
229 0b1000011011100001010111010111010011101100100010010010100100101001,
230 0b1001001001010111110101000010111010000000101111010100001010010010,
231 0b0011011110110010010101111011000001000000000011011111000011111011,
232 0b1011000110100011001110000001000100000001011100010111010010011110,
233 0b0111101110110101110111110000011000000100011100011000101101101110,
234 0b1001100101111011011100011110101011001111100111101010101010110111,
235 0b1100110010010001100011001111010000000100011101001111011101001111,
236 0b1000111001111010100101000010000100000001001100101010001011001101,
237 0b0011101011110000110010100101010100110010100001000010101011111101,
238 0b1100000000000110000010101011000000011101000110011111100010111111,
239 0b0010100110000011011100010110111100010110101100110011101110001101,
240 0b0010111101010011111000111001111100110111111100100011110001101110,
241 0b1001110111001001101001001001011000010100110001000000100011010110,
242 0b0011110101100111011011111100001000011001010100111100100101111010,
243 0b0010001101000011000010100101110000010101101000100110000100001010,
244 0b0010000010100110010101100101110011101111000111111111001001100001,
245 0b0100111111011011011011100111111011000010011101101111011111110110,
246 0b1111111111010110101011101000100101110100001110001001101011100111,
247 0b1011111101000101110000111100100010111010100001010000010010110010,
248 0b1111010101001011101011101010000100110110001110111100100110111111,
249 0b1011001101000001001101000010101010010110010001100001011100011010,
250 0b0101001011011101010001110100010000010001111100100100100001001101,
251 0b0010100000111001100011000101100101000001111100111001101000000010,
252 0b1011001111010101011001000100100110100100110111110100000110111000,
253 0b0101011111010011100011010010111101110010100001111111100010001001,
254 0b0010111011101100100000000000001111111010011101100111100001001101,
255 0b1101000000000000000000000000000000000000000000000000000000000000 };
256 };
257
258 # ifdef FLOAT128_TO_CHARS
259 template<>
260 struct floating_type_traits<__float128> : floating_type_traits_binary128
261 { };
262 # endif
263 #endif
264
265 #if LONG_DOUBLE_KIND == LDK_BINARY64
266 // When long double is equivalent to double, we just forward the long double
267 // overloads to the double overloads, so we don't need to define a
268 // floating_type_traits<long double> specialization in this case.
269 #elif LONG_DOUBLE_KIND == LDK_FLOAT80
270 template<>
271 struct floating_type_traits<long double>
272 {
273 static constexpr int mantissa_bits = 64;
274 static constexpr int exponent_bits = 15;
275 static constexpr bool has_implicit_leading_bit = false;
276 using mantissa_t = uint64_t;
277 using shortest_scientific_t = ryu::floating_decimal_128;
278
279 static constexpr uint64_t pow10_adjustment_tab[]
280 = { 0b0000000000000000000000000000110101011111110100010100110000011101,
281 0b1001100101001111010011011111101000101111110001011001011101110000,
282 0b0000101111111011110010001000001010111101011110111111010100011001,
283 0b0011100000011111001101101011111001111100100010000101001111101001,
284 0b0100100100000000100111010010101110011000110001101101110011001010,
285 0b0111100111100010100000010011000010010110101111110101000011110100,
286 0b1010100111100010011110000011011101101100010110000110101010101010,
287 0b0000001111001111000000101100111011011000101000110011101100110010,
288 0b0111000011100100101101010100001101111110101111001000010011111111,
289 0b0010111000100110100100100010101100111010110001101010010111001000,
290 0b0000100000010110000011001001000111000001111010100101101000001111,
291 0b0010101011101000111100001011000010011101000101010010010000101111,
292 0b1011111011101101110010101011010001111000101000101101011001100011,
293 0b1010111011011011110111110011001010000010011001110100101101000101,
294 0b0011000001110110011010010000011100100011001011001100001101010110,
295 0b0100011111011000111111101000011110000010111110101001000000001001,
296 0b1110000001110001001101101110011000100000001010000111100010111010,
297 0b1110001001010011101000111000001000010100110000010110100011110000,
298 0b0000011010110000110001111000011111000011001101001101001001000110,
299 0b1010010111001000101001100101010110100100100010010010000101000010,
300 0b1011001110000111100010100110000011100011111001110111001100000101,
301 0b0110101001001000010110001000010001010101110101100001111100011001,
302 0b1111100011110101011110011010101001010010100011000010110001101001,
303 0b0100000100001000111101011100010011011111011001000000001100011000,
304 0b1110111111000111100101110111110000000011001110011100011011011001,
305 0b1100001100100000010001100011011000111011110000110011010101000011,
306 0b1111111011100111011101001111111000010000001111010111110010000100,
307 0b1110111001111110101111000101000000001010001110011010001000111010,
308 0b1000010001011000101111111010110011111101110101101001111000111010,
309 0b0100000111101001000111011001101000001010111011101001101111000100,
310 0b0000011100110001000111011100111100110001101111111010110111100000,
311 0b0000011101011100100110010011110101010100010011110010010111010000,
312 0b0011011001100111110101111100001001101110101101001110110011110110,
313 0b1011000101000001110100111001100100111100110011110000000001101000,
314 0b1011100011110100001001110101010110111001000000001011101001011110,
315 0b1111001010010010100000010110101010101011101000101000000000001100,
316 0b1000001111100100111001110101100001010011111111000001000011110000,
317 0b0001011101001000010000101101111000001110101100110011001100110111,
318 0b1110011100000010101011011111001010111101111110100000011100000011,
319 0b1001110110011100101010011110100010110001001110110000101011100110,
320 0b1001101000100011100111010000011011100001000000110101100100001001,
321 0b1010111000101000101101010111000010001100001010100011111100000100,
322 0b0111101000100011000101101011111011100010001101110111001111001011,
323 0b1110100111010110001110110110000000010110100011110000010001111100,
324 0b1100010100011010001011001000111001010101011110100101011001000000,
325 0b0000110001111001100110010110111010101101001101000000000010010101,
326 0b0001110111101000001111101010110010010000111110111100000111110100,
327 0b0111110111001001111000110001101101001010101110110101111110000100,
328 0b0000111110111010101111100010111010011100010110011011011001000001,
329 0b1010010100100100101110111111111000101100000010111111101101000110,
330 0b1000100111111101100011001101000110001000000100010101010100001101,
331 0b1100101010101000111100101100001000110001110010100000000010110101,
332 0b1010000100111101100100101010010110100010000000110101101110000100,
333 0b1011111011110001110000100100000000001010111010001101100000100100,
334 0b0111101101100011001110011100000001000101101101111000100111011111,
335 0b0100111010010011011001010011110100001100111010010101111111100011,
336 0b0010001001011000111000001100110111110111110010100011000110110110,
337 0b0101010110000000010000100000110100111011111101000100000111010010,
338 0b0110000011011101000001010100110101101110011100110101000000001001,
339 0b1101100110100000011000001111000100100100110001100110101010101100,
340 0b0010100101010110010010001010101000011111111111001011001010001111,
341 0b0111001010001111001100111001010101001000110101000011110000001000,
342 0b0110010011001001001111110001010010001011010010001101110110110011,
343 0b0110010100111011000100111000001001101011111001110010111110111111,
344 0b0101110111001001101100110100101001110010101110011001101110001000,
345 0b0100110101010111011010001100010111100011010011111001010100111000,
346 0b0111000110110111011110100100010111000110000110110110110001111110,
347 0b1000101101010100100100111110100011110110110010011001110011110101,
348 0b1001101110101001010100111101101011000101000010110101101111110000,
349 0b0100100101001011011001001011000010001101001010010001010110101000,
350 0b0010100001001011100110101000010110000111000111000011100101011011,
351 0b0110111000011001111101101011111010001000000010101000101010011110,
352 0b1000110110100001111011000001111100001001000000010110010100100100,
353 0b1001110100011111100111101011010000010101011100101000010010100110,
354 0b0001010110101110100010101010001110110110100011101010001001111100,
355 0b1010100101101100000010110011100110100010010000100100001110000100,
356 0b0001000000010000001010000010100110000001110100111001110111101101,
357 0b1100000000000000000000000000000000000000000000000000000000000000 };
358 };
359 #elif LONG_DOUBLE_KIND == LDK_BINARY128
360 template<>
361 struct floating_type_traits<long double> : floating_type_traits_binary128
362 { };
363 #elif LONG_DOUBLE_KIND == LDK_IBM128
364 template<>
365 struct floating_type_traits<long double>
366 {
367 static constexpr int mantissa_bits = 105;
368 static constexpr int exponent_bits = 11;
369 static constexpr bool has_implicit_leading_bit = true;
370 using mantissa_t = uint128_t;
371 using shortest_scientific_t = ryu::floating_decimal_128;
372
373 static constexpr uint64_t pow10_adjustment_tab[]
374 = { 0b0000000000000000000000000000000000000000000000001000000100000000,
375 0b0000000000000000000100000000000000000000001000000000000000000010,
376 0b0000100000000000000000001001000000000000000001100100000000000000,
377 0b0011000000000000000000000000000001110000010000000000000000000000,
378 0b0000100000000000001000000000000000000000000000100000000000000000 };
379 };
380 #endif
381
382 // An IEEE-style decomposition of a floating-point value of type T.
383 template<typename T>
384 struct ieee_t
385 {
386 typename floating_type_traits<T>::mantissa_t mantissa;
387 uint32_t biased_exponent;
388 bool sign;
389 };
390
391 // Decompose the floating-point value into its IEEE components.
392 template<typename T>
393 ieee_t<T>
get_ieee_repr(const T value)394 get_ieee_repr(const T value)
395 {
396 using mantissa_t = typename floating_type_traits<T>::mantissa_t;
397 constexpr int mantissa_bits = floating_type_traits<T>::mantissa_bits;
398 constexpr int exponent_bits = floating_type_traits<T>::exponent_bits;
399 constexpr int total_bits = mantissa_bits + exponent_bits + 1;
400
401 constexpr auto get_uint_t = [] {
402 if constexpr (total_bits <= 32)
403 return uint32_t{};
404 else if constexpr (total_bits <= 64)
405 return uint64_t{};
406 else if constexpr (total_bits <= 128)
407 return uint128_t{};
408 };
409 using uint_t = decltype(get_uint_t());
410 uint_t value_bits = 0;
411 memcpy(&value_bits, &value, sizeof(value));
412
413 ieee_t<T> ieee_repr;
414 ieee_repr.mantissa
415 = static_cast<mantissa_t>(value_bits & ((uint_t{1} << mantissa_bits) - 1u));
416 value_bits >>= mantissa_bits;
417 ieee_repr.biased_exponent
418 = static_cast<uint32_t>(value_bits & ((uint_t{1} << exponent_bits) - 1u));
419 value_bits >>= exponent_bits;
420 ieee_repr.sign = (value_bits & 1) != 0;
421 return ieee_repr;
422 }
423
424 #if LONG_DOUBLE_KIND == LDK_IBM128
425 template<>
426 ieee_t<long double>
get_ieee_repr(const long double value)427 get_ieee_repr(const long double value)
428 {
429 // The layout of __ibm128 isn't compatible with the standard IEEE format.
430 // So we transform it into an IEEE-compatible format, suitable for
431 // consumption by the generic Ryu API, with an 11-bit exponent and 105-bit
432 // mantissa (plus an implicit leading bit). We use the exponent and sign
433 // of the high part, and we merge the mantissa of the high part with the
434 // mantissa (and the implicit leading bit) of the low part.
435 uint64_t value_bits[2] = {};
436 memcpy(value_bits, &value, sizeof(value_bits));
437
438 const uint64_t value_hi = value_bits[0];
439 const uint64_t value_lo = value_bits[1];
440
441 uint64_t mantissa_hi = value_hi & ((1ull << 52) - 1);
442 unsigned exponent_hi = (value_hi >> 52) & ((1ull << 11) - 1);
443 const int sign_hi = (value_hi >> 63) & 1;
444
445 uint64_t mantissa_lo = value_lo & ((1ull << 52) - 1);
446 const unsigned exponent_lo = (value_lo >> 52) & ((1ull << 11) - 1);
447 const int sign_lo = (value_lo >> 63) & 1;
448
449 {
450 // The following code for adjusting the low-part mantissa to combine
451 // it with the high-part mantissa is taken from the glibc source file
452 // sysdeps/ieee754/ldbl-128ibm/printf_fphex.c.
453 mantissa_lo <<= 7;
454 if (exponent_lo != 0)
455 mantissa_lo |= (1ull << (52 + 7));
456 else
457 mantissa_lo <<= 1;
458
459 const int ediff = exponent_hi - exponent_lo - 53;
460 if (ediff > 63)
461 mantissa_lo = 0;
462 else if (ediff > 0)
463 mantissa_lo >>= ediff;
464 else if (ediff < 0)
465 mantissa_lo <<= -ediff;
466
467 if (sign_lo != sign_hi && mantissa_lo != 0)
468 {
469 mantissa_lo = (1ull << 60) - mantissa_lo;
470 if (mantissa_hi == 0)
471 {
472 mantissa_hi = 0xffffffffffffeLL | (mantissa_lo >> 59);
473 mantissa_lo = 0xfffffffffffffffLL & (mantissa_lo << 1);
474 exponent_hi--;
475 }
476 else
477 mantissa_hi--;
478 }
479 }
480
481 ieee_t<long double> ieee_repr;
482 ieee_repr.mantissa = ((uint128_t{mantissa_hi} << 64)
483 | (uint128_t{mantissa_lo} << 4)) >> 11;
484 ieee_repr.biased_exponent = exponent_hi;
485 ieee_repr.sign = sign_hi;
486 return ieee_repr;
487 }
488 #endif
489
490 // Invoke Ryu to obtain the shortest scientific form for the given
491 // floating-point number.
492 template<typename T>
493 typename floating_type_traits<T>::shortest_scientific_t
floating_to_shortest_scientific(const T value)494 floating_to_shortest_scientific(const T value)
495 {
496 if constexpr (std::is_same_v<T, float>)
497 return ryu::floating_to_fd32(value);
498 else if constexpr (std::is_same_v<T, double>)
499 return ryu::floating_to_fd64(value);
500 else if constexpr (std::is_same_v<T, long double>
501 || std::is_same_v<T, F128_type>)
502 {
503 constexpr int mantissa_bits
504 = floating_type_traits<T>::mantissa_bits;
505 constexpr int exponent_bits
506 = floating_type_traits<T>::exponent_bits;
507 constexpr bool has_implicit_leading_bit
508 = floating_type_traits<T>::has_implicit_leading_bit;
509
510 const auto [mantissa, exponent, sign] = get_ieee_repr(value);
511 return ryu::generic_binary_to_decimal(mantissa, exponent, sign,
512 mantissa_bits, exponent_bits,
513 !has_implicit_leading_bit);
514 }
515 }
516
517 // This subroutine returns true if the shortest scientific form fd is a
518 // positive power of 10, and the floating-point number that has this shortest
519 // scientific form is smaller than this power of 10.
520 //
521 // For instance, the exactly-representable 64-bit number
522 // 99999999999999991611392.0 has the shortest scientific form 1e23, so its
523 // exact value is smaller than its shortest scientific form.
524 //
525 // For these powers of 10 the length of the fixed form is one digit less
526 // than what the scientific exponent suggests.
527 //
528 // This subroutine inspects a lookup table to detect when fd is such a
529 // "rounded up" power of 10.
530 template<typename T>
531 bool
is_rounded_up_pow10_p(const typename floating_type_traits<T>::shortest_scientific_t fd)532 is_rounded_up_pow10_p(const typename
533 floating_type_traits<T>::shortest_scientific_t fd)
534 {
535 if (fd.exponent < 0 || fd.mantissa != 1) [[likely]]
536 return false;
537
538 constexpr auto& pow10_adjustment_tab
539 = floating_type_traits<T>::pow10_adjustment_tab;
540 __glibcxx_assert(fd.exponent/64 < (int)std::size(pow10_adjustment_tab));
541 return (pow10_adjustment_tab[fd.exponent/64]
542 & (1ull << (63 - fd.exponent%64)));
543 }
544
545 int
get_mantissa_length(const ryu::floating_decimal_32 fd)546 get_mantissa_length(const ryu::floating_decimal_32 fd)
547 { return ryu::decimalLength9(fd.mantissa); }
548
549 int
get_mantissa_length(const ryu::floating_decimal_64 fd)550 get_mantissa_length(const ryu::floating_decimal_64 fd)
551 { return ryu::decimalLength17(fd.mantissa); }
552
553 int
get_mantissa_length(const ryu::floating_decimal_128 fd)554 get_mantissa_length(const ryu::floating_decimal_128 fd)
555 { return ryu::generic128::decimalLength(fd.mantissa); }
556
557 #if !defined __SIZEOF_INT128__
558 // An implementation of base-10 std::to_chars for the uint128_t class type,
559 // used by targets that lack __int128.
560 std::to_chars_result
to_chars(char * first,char * const last,uint128_t x)561 to_chars(char* first, char* const last, uint128_t x)
562 {
563 const int len = ryu::generic128::decimalLength(x);
564 if (last - first < len)
565 return {last, std::errc::value_too_large};
566 if (x == 0)
567 {
568 *first++ = '0';
569 return {first, std::errc{}};
570 }
571 for (int i = 0; i < len; ++i)
572 {
573 first[len - 1 - i] = '0' + static_cast<char>(x % 10);
574 x /= 10;
575 }
576 __glibcxx_assert(x == 0);
577 return {first + len, std::errc{}};
578 }
579 #endif
580 } // anon namespace
581
582 namespace std _GLIBCXX_VISIBILITY(default)
583 {
584 _GLIBCXX_BEGIN_NAMESPACE_VERSION
585
586 // This subroutine of __floating_to_chars_* handles writing nan, inf and 0 in
587 // all formatting modes.
588 template<typename T>
589 static optional<to_chars_result>
__handle_special_value(char * first,char * const last,const T value,const chars_format fmt,const int precision)590 __handle_special_value(char* first, char* const last, const T value,
591 const chars_format fmt, const int precision)
592 {
593 __glibcxx_assert(precision >= 0);
594
595 string_view str;
596 switch (__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
597 FP_ZERO, value))
598 {
599 case FP_INFINITE:
600 str = "-inf";
601 break;
602
603 case FP_NAN:
604 str = "-nan";
605 break;
606
607 case FP_ZERO:
608 break;
609
610 default:
611 case FP_SUBNORMAL:
612 case FP_NORMAL: [[likely]]
613 return nullopt;
614 }
615
616 if (!str.empty())
617 {
618 // We're formatting +-inf or +-nan.
619 if (!__builtin_signbit(value))
620 str.remove_prefix(strlen("-"));
621
622 if (last - first < (int)str.length())
623 return {{last, errc::value_too_large}};
624
625 memcpy(first, &str[0], str.length());
626 first += str.length();
627 return {{first, errc{}}};
628 }
629
630 // We're formatting 0.
631 __glibcxx_assert(value == 0);
632 const auto orig_first = first;
633 const bool sign = __builtin_signbit(value);
634 int expected_output_length;
635 switch (fmt)
636 {
637 case chars_format::fixed:
638 case chars_format::scientific:
639 case chars_format::hex:
640 expected_output_length = sign + 1;
641 if (precision)
642 expected_output_length += strlen(".") + precision;
643 if (fmt == chars_format::scientific)
644 expected_output_length += strlen("e+00");
645 else if (fmt == chars_format::hex)
646 expected_output_length += strlen("p+0");
647 if (last - first < expected_output_length)
648 return {{last, errc::value_too_large}};
649
650 if (sign)
651 *first++ = '-';
652 *first++ = '0';
653 if (precision)
654 {
655 *first++ = '.';
656 memset(first, '0', precision);
657 first += precision;
658 }
659 if (fmt == chars_format::scientific)
660 {
661 memcpy(first, "e+00", 4);
662 first += 4;
663 }
664 else if (fmt == chars_format::hex)
665 {
666 memcpy(first, "p+0", 3);
667 first += 3;
668 }
669 break;
670
671 case chars_format::general:
672 default: // case chars_format{}:
673 expected_output_length = sign + 1;
674 if (last - first < expected_output_length)
675 return {{last, errc::value_too_large}};
676
677 if (sign)
678 *first++ = '-';
679 *first++ = '0';
680 break;
681 }
682 __glibcxx_assert(first - orig_first == expected_output_length);
683 return {{first, errc{}}};
684 }
685
686 // This subroutine of the floating-point to_chars overloads performs
687 // hexadecimal formatting.
688 template<typename T>
689 static to_chars_result
__floating_to_chars_hex(char * first,char * const last,const T value,const optional<int> precision)690 __floating_to_chars_hex(char* first, char* const last, const T value,
691 const optional<int> precision)
692 {
693 if (precision.has_value() && precision.value() < 0) [[unlikely]]
694 // A negative precision argument is treated as if it were omitted.
695 return __floating_to_chars_hex(first, last, value, nullopt);
696
697 __glibcxx_requires_valid_range(first, last);
698
699 constexpr int mantissa_bits = floating_type_traits<T>::mantissa_bits;
700 constexpr bool has_implicit_leading_bit
701 = floating_type_traits<T>::has_implicit_leading_bit;
702 constexpr int exponent_bits = floating_type_traits<T>::exponent_bits;
703 constexpr int exponent_bias = (1u << (exponent_bits - 1)) - 1;
704 using mantissa_t = typename floating_type_traits<T>::mantissa_t;
705 constexpr int mantissa_t_width = sizeof(mantissa_t) * __CHAR_BIT__;
706
707 if (auto result = __handle_special_value(first, last, value,
708 chars_format::hex,
709 precision.value_or(0)))
710 return *result;
711
712 // Extract the sign, mantissa and exponent from the value.
713 const auto [ieee_mantissa, biased_exponent, sign] = get_ieee_repr(value);
714 const bool is_normal_number = (biased_exponent != 0);
715
716 // Calculate the unbiased exponent.
717 const int32_t unbiased_exponent = (is_normal_number
718 ? biased_exponent - exponent_bias
719 : 1 - exponent_bias);
720
721 // Shift the mantissa so that its bitwidth is a multiple of 4.
722 constexpr unsigned rounded_mantissa_bits = (mantissa_bits + 3) / 4 * 4;
723 static_assert(mantissa_t_width >= rounded_mantissa_bits);
724 mantissa_t effective_mantissa
725 = ieee_mantissa << (rounded_mantissa_bits - mantissa_bits);
726 if (is_normal_number)
727 {
728 if constexpr (has_implicit_leading_bit)
729 // Restore the mantissa's implicit leading bit.
730 effective_mantissa |= mantissa_t{1} << rounded_mantissa_bits;
731 else
732 // The explicit mantissa bit should already be set.
733 __glibcxx_assert(effective_mantissa & (mantissa_t{1} << (mantissa_bits
734 - 1u)));
735 }
736
737 // Compute the shortest precision needed to print this value exactly,
738 // disregarding trailing zeros.
739 constexpr int full_hex_precision = (has_implicit_leading_bit
740 ? (mantissa_bits + 3) / 4
741 // With an explicit leading bit, we
742 // use the four leading nibbles as the
743 // hexit before the decimal point.
744 : (mantissa_bits - 4 + 3) / 4);
745 const int trailing_zeros = __countr_zero(effective_mantissa) / 4;
746 const int shortest_full_precision = full_hex_precision - trailing_zeros;
747 __glibcxx_assert(shortest_full_precision >= 0);
748
749 int written_exponent = unbiased_exponent;
750 const int effective_precision = precision.value_or(shortest_full_precision);
751 if (effective_precision < shortest_full_precision)
752 {
753 // When limiting the precision, we need to determine how to round the
754 // least significant printed hexit. The following branchless
755 // bit-level-parallel technique computes whether to round up the
756 // mantissa bit at index N (according to round-to-nearest rules) when
757 // dropping N bits of precision, for each index N in the bit vector.
758 // This technique is borrowed from the MSVC implementation.
759 using bitvec = mantissa_t;
760 const bitvec round_bit = effective_mantissa << 1;
761 const bitvec has_tail_bits = round_bit - 1;
762 const bitvec lsb_bit = effective_mantissa;
763 const bitvec should_round = round_bit & (has_tail_bits | lsb_bit);
764
765 const int dropped_bits = 4*(full_hex_precision - effective_precision);
766 // Mask out the dropped nibbles.
767 effective_mantissa >>= dropped_bits;
768 effective_mantissa <<= dropped_bits;
769 if (should_round & (mantissa_t{1} << dropped_bits))
770 {
771 // Round up the least significant nibble.
772 effective_mantissa += mantissa_t{1} << dropped_bits;
773 // Check and adjust for overflow of the leading nibble. When the
774 // type has an implicit leading bit, then the leading nibble
775 // before rounding is either 0 or 1, so it can't overflow.
776 if constexpr (!has_implicit_leading_bit)
777 {
778 // The only supported floating-point type with explicit
779 // leading mantissa bit is LDK_FLOAT80, i.e. x86 80-bit
780 // extended precision, and so we hardcode the below overflow
781 // check+adjustment for this type.
782 static_assert(mantissa_t_width == 64
783 && rounded_mantissa_bits == 64);
784 if (effective_mantissa == 0)
785 {
786 // We rounded up the least significant nibble and the
787 // mantissa overflowed, e.g f.fcp+10 with precision=1
788 // became 10.0p+10. Absorb this extra hexit into the
789 // exponent to obtain 1.0p+14.
790 effective_mantissa
791 = mantissa_t{1} << (rounded_mantissa_bits - 4);
792 written_exponent += 4;
793 }
794 }
795 }
796 }
797
798 // Compute the leading hexit and mask it out from the mantissa.
799 char leading_hexit;
800 if constexpr (has_implicit_leading_bit)
801 {
802 const unsigned nibble = effective_mantissa >> rounded_mantissa_bits;
803 __glibcxx_assert(nibble <= 2);
804 leading_hexit = '0' + nibble;
805 effective_mantissa &= ~(mantissa_t{0b11} << rounded_mantissa_bits);
806 }
807 else
808 {
809 const unsigned nibble = effective_mantissa >> (rounded_mantissa_bits-4);
810 __glibcxx_assert(nibble < 16);
811 leading_hexit = "0123456789abcdef"[nibble];
812 effective_mantissa &= ~(mantissa_t{0b1111} << (rounded_mantissa_bits-4));
813 written_exponent -= 3;
814 }
815
816 // Now before we start writing the string, determine the total length of
817 // the output string and perform a single bounds check.
818 int expected_output_length = sign + 1;
819 if (effective_precision != 0)
820 expected_output_length += strlen(".") + effective_precision;
821 const int abs_written_exponent = abs(written_exponent);
822 expected_output_length += (abs_written_exponent >= 10000 ? strlen("p+ddddd")
823 : abs_written_exponent >= 1000 ? strlen("p+dddd")
824 : abs_written_exponent >= 100 ? strlen("p+ddd")
825 : abs_written_exponent >= 10 ? strlen("p+dd")
826 : strlen("p+d"));
827 if (last - first < expected_output_length)
828 return {last, errc::value_too_large};
829
830 const auto saved_first = first;
831 // Write the negative sign and the leading hexit.
832 if (sign)
833 *first++ = '-';
834 *first++ = leading_hexit;
835
836 if (effective_precision > 0)
837 {
838 *first++ = '.';
839 int written_hexits = 0;
840 // Extract and mask out the leading nibble after the decimal point,
841 // write its corresponding hexit, and repeat until the mantissa is
842 // empty.
843 int nibble_offset = rounded_mantissa_bits;
844 if constexpr (!has_implicit_leading_bit)
845 // We already printed the entire leading hexit.
846 nibble_offset -= 4;
847 while (effective_mantissa != 0)
848 {
849 nibble_offset -= 4;
850 const unsigned nibble = effective_mantissa >> nibble_offset;
851 __glibcxx_assert(nibble < 16);
852 *first++ = "0123456789abcdef"[nibble];
853 ++written_hexits;
854 effective_mantissa &= ~(mantissa_t{0b1111} << nibble_offset);
855 }
856 __glibcxx_assert(nibble_offset >= 0);
857 __glibcxx_assert(written_hexits <= effective_precision);
858 // Since the mantissa is now empty, every hexit hereafter must be '0'.
859 if (int remaining_hexits = effective_precision - written_hexits)
860 {
861 memset(first, '0', remaining_hexits);
862 first += remaining_hexits;
863 }
864 }
865
866 // Finally, write the exponent.
867 *first++ = 'p';
868 if (written_exponent >= 0)
869 *first++ = '+';
870 const to_chars_result result = to_chars(first, last, written_exponent);
871 __glibcxx_assert(result.ec == errc{}
872 && result.ptr == saved_first + expected_output_length);
873 return result;
874 }
875
876 namespace
877 {
878 #pragma GCC diagnostic push
879 #pragma GCC diagnostic ignored "-Wabi"
880 template<typename T, typename... Extra>
881 inline int
sprintf_ld(char * buffer,const char * format_string,T value,Extra...args)882 sprintf_ld(char* buffer, const char* format_string, T value, Extra... args)
883 {
884 int len;
885
886 #if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
887 const int saved_rounding_mode = fegetround();
888 if (saved_rounding_mode != FE_TONEAREST)
889 fesetround(FE_TONEAREST); // We want round-to-nearest behavior.
890 #endif
891
892 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
893 if constexpr (is_same_v<T, __ieee128>)
894 len = __sprintfieee128(buffer, format_string, args..., value);
895 else
896 #endif
897 len = sprintf(buffer, format_string, args..., value);
898
899 #if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
900 if (saved_rounding_mode != FE_TONEAREST)
901 fesetround(saved_rounding_mode);
902 #endif
903
904 return len;
905 }
906 #pragma GCC diagnostic pop
907 }
908
909 template<typename T>
910 static to_chars_result
__floating_to_chars_shortest(char * first,char * const last,const T value,chars_format fmt)911 __floating_to_chars_shortest(char* first, char* const last, const T value,
912 chars_format fmt)
913 {
914 if (fmt == chars_format::hex)
915 return __floating_to_chars_hex(first, last, value, nullopt);
916
917 __glibcxx_assert(fmt == chars_format::fixed
918 || fmt == chars_format::scientific
919 || fmt == chars_format::general
920 || fmt == chars_format{});
921 __glibcxx_requires_valid_range(first, last);
922
923 if (auto result = __handle_special_value(first, last, value, fmt, 0))
924 return *result;
925
926 const auto fd = floating_to_shortest_scientific(value);
927 const int mantissa_length = get_mantissa_length(fd);
928 const int scientific_exponent = fd.exponent + mantissa_length - 1;
929
930 if (fmt == chars_format::general)
931 {
932 // Resolve the 'general' formatting mode as per the specification of
933 // the 'g' printf output specifier. Since there is no precision
934 // argument, the default precision of the 'g' specifier, 6, applies.
935 if (scientific_exponent >= -4 && scientific_exponent < 6)
936 fmt = chars_format::fixed;
937 else
938 fmt = chars_format::scientific;
939 }
940 else if (fmt == chars_format{})
941 {
942 // The 'plain' formatting mode resolves to 'scientific' if it yields
943 // the shorter string, and resolves to 'fixed' otherwise. The
944 // following lower and upper bounds on the exponent characterize when
945 // to prefer 'fixed' over 'scientific'.
946 int lower_bound = -(mantissa_length + 3);
947 int upper_bound = 5;
948 if (mantissa_length == 1)
949 // The decimal point in scientific notation will be omitted in this
950 // case; tighten the bounds appropriately.
951 ++lower_bound, --upper_bound;
952
953 if (fd.exponent >= lower_bound && fd.exponent <= upper_bound)
954 fmt = chars_format::fixed;
955 else
956 fmt = chars_format::scientific;
957 }
958
959 if (fmt == chars_format::scientific)
960 {
961 // Calculate the total length of the output string, perform a bounds
962 // check, and then defer to Ryu's to_chars subroutine.
963 int expected_output_length = fd.sign + mantissa_length;
964 if (mantissa_length > 1)
965 expected_output_length += strlen(".");
966 const int abs_exponent = abs(scientific_exponent);
967 expected_output_length += (abs_exponent >= 1000 ? strlen("e+dddd")
968 : abs_exponent >= 100 ? strlen("e+ddd")
969 : strlen("e+dd"));
970 if (last - first < expected_output_length)
971 return {last, errc::value_too_large};
972
973 const int output_length = ryu::to_chars(fd, first);
974 __glibcxx_assert(output_length == expected_output_length);
975 return {first + output_length, errc{}};
976 }
977 else if (fmt == chars_format::fixed && fd.exponent >= 0)
978 {
979 // The Ryu exponent is positive, and so this number's shortest
980 // representation is a whole number, to be formatted in fixed instead
981 // of scientific notation "as if by std::printf". This means we may
982 // need to print more digits of the IEEE mantissa than what the
983 // shortest scientific form given by Ryu provides.
984 //
985 // For instance, the exactly representable number
986 // 12300000000000001048576.0 has as its shortest scientific
987 // representation 123e+22, so in this case fd.mantissa is 123 and
988 // fd.exponent is 22, which doesn't have enough information to format
989 // the number exactly. So we defer to Ryu's d2fixed_buffered_n with
990 // precision=0 to format the number in the general case here.
991
992 // To that end, first compute the output length and perform a bounds
993 // check.
994 int expected_output_length = fd.sign + mantissa_length + fd.exponent;
995 if (is_rounded_up_pow10_p<T>(fd))
996 --expected_output_length;
997 if (last - first < expected_output_length)
998 return {last, errc::value_too_large};
999
1000 // Optimization: if the shortest representation fits inside the IEEE
1001 // mantissa, then the number is certainly exactly-representable and
1002 // its shortest scientific form must be equal to its exact form. So
1003 // we can write the value in fixed form exactly via fd.mantissa and
1004 // fd.exponent.
1005 //
1006 // Taking log2 of both sides of the desired condition
1007 // fd.mantissa * 10^fd.exponent < 2^mantissa_bits
1008 // we get
1009 // log2 fd.mantissa + fd.exponent * log2 10 < mantissa_bits
1010 // where log2 10 is slightly smaller than 10/3=3.333...
1011 //
1012 // After adding some wiggle room due to rounding we get the condition
1013 // value_fits_inside_mantissa_p below.
1014 const int log2_mantissa = __bit_width(fd.mantissa) - 1;
1015 const bool value_fits_inside_mantissa_p
1016 = (log2_mantissa + (fd.exponent*10 + 2) / 3
1017 < floating_type_traits<T>::mantissa_bits - 2);
1018 if (value_fits_inside_mantissa_p)
1019 {
1020 // Print the small exactly-representable number in fixed form by
1021 // writing out fd.mantissa followed by fd.exponent many 0s.
1022 if (fd.sign)
1023 *first++ = '-';
1024 to_chars_result result = to_chars(first, last, fd.mantissa);
1025 __glibcxx_assert(result.ec == errc{});
1026 memset(result.ptr, '0', fd.exponent);
1027 result.ptr += fd.exponent;
1028 const int output_length = fd.sign + (result.ptr - first);
1029 __glibcxx_assert(output_length == expected_output_length);
1030 return result;
1031 }
1032 else if constexpr (is_same_v<T, long double>
1033 || is_same_v<T, F128_type>)
1034 {
1035 // We can't use d2fixed_buffered_n for types larger than double,
1036 // so we instead format larger types through sprintf.
1037 // TODO: We currently go through an intermediate buffer in order
1038 // to accommodate the mandatory null terminator of sprintf, but we
1039 // can avoid this if we use sprintf to write all but the last
1040 // digit, and carefully compute and write the last digit
1041 // ourselves.
1042 char buffer[expected_output_length+1];
1043 const int output_length = sprintf_ld(buffer, "%.0Lf", value);
1044 __glibcxx_assert(output_length == expected_output_length);
1045 memcpy(first, buffer, output_length);
1046 return {first + output_length, errc{}};
1047 }
1048 else
1049 {
1050 // Otherwise, the number is too big, so defer to d2fixed_buffered_n.
1051 const int output_length = ryu::d2fixed_buffered_n(value, 0, first);
1052 __glibcxx_assert(output_length == expected_output_length);
1053 return {first + output_length, errc{}};
1054 }
1055 }
1056 else if (fmt == chars_format::fixed && fd.exponent < 0)
1057 {
1058 // The Ryu exponent is negative, so fd.mantissa definitely contains
1059 // all of the whole part of the number, and therefore fd.mantissa and
1060 // fd.exponent contain all of the information needed to format the
1061 // number in fixed notation "as if by std::printf" (with precision
1062 // equal to -fd.exponent).
1063 const int whole_digits = max<int>(mantissa_length + fd.exponent, 1);
1064 const int expected_output_length
1065 = fd.sign + whole_digits + strlen(".") + -fd.exponent;
1066 if (last - first < expected_output_length)
1067 return {last, errc::value_too_large};
1068 if (mantissa_length <= -fd.exponent)
1069 {
1070 // The magnitude of the number is less than one. Format the
1071 // number appropriately.
1072 const auto orig_first = first;
1073 if (fd.sign)
1074 *first++ = '-';
1075 *first++ = '0';
1076 *first++ = '.';
1077 const int leading_zeros = -fd.exponent - mantissa_length;
1078 memset(first, '0', leading_zeros);
1079 first += leading_zeros;
1080 const to_chars_result result = to_chars(first, last, fd.mantissa);
1081 const int output_length = result.ptr - orig_first;
1082 __glibcxx_assert(output_length == expected_output_length
1083 && result.ec == errc{});
1084 return result;
1085 }
1086 else
1087 {
1088 // The magnitude of the number is at least one.
1089 const auto orig_first = first;
1090 if (fd.sign)
1091 *first++ = '-';
1092 to_chars_result result = to_chars(first, last, fd.mantissa);
1093 __glibcxx_assert(result.ec == errc{});
1094 // Make space for and write the decimal point in the correct spot.
1095 memmove(&result.ptr[fd.exponent+1], &result.ptr[fd.exponent],
1096 -fd.exponent);
1097 result.ptr[fd.exponent] = '.';
1098 const int output_length = result.ptr + 1 - orig_first;
1099 __glibcxx_assert(output_length == expected_output_length);
1100 ++result.ptr;
1101 return result;
1102 }
1103 }
1104
1105 __glibcxx_assert(false);
1106 }
1107
1108 template<typename T>
1109 static to_chars_result
__floating_to_chars_precision(char * first,char * const last,const T value,chars_format fmt,const int precision)1110 __floating_to_chars_precision(char* first, char* const last, const T value,
1111 chars_format fmt, const int precision)
1112 {
1113 if (fmt == chars_format::hex)
1114 return __floating_to_chars_hex(first, last, value, precision);
1115
1116 if (precision < 0) [[unlikely]]
1117 // A negative precision argument is treated as if it were omitted, in
1118 // which case the default precision of 6 applies, as per the printf
1119 // specification.
1120 return __floating_to_chars_precision(first, last, value, fmt, 6);
1121
1122 __glibcxx_assert(fmt == chars_format::fixed
1123 || fmt == chars_format::scientific
1124 || fmt == chars_format::general);
1125 __glibcxx_requires_valid_range(first, last);
1126
1127 if (auto result = __handle_special_value(first, last, value,
1128 fmt, precision))
1129 return *result;
1130
1131 constexpr int mantissa_bits = floating_type_traits<T>::mantissa_bits;
1132 constexpr int exponent_bits = floating_type_traits<T>::exponent_bits;
1133 constexpr int exponent_bias = (1u << (exponent_bits - 1)) - 1;
1134
1135 // Extract the sign and exponent from the value.
1136 const auto [mantissa, biased_exponent, sign] = get_ieee_repr(value);
1137 const bool is_normal_number = (biased_exponent != 0);
1138
1139 // Calculate the unbiased exponent.
1140 const int32_t unbiased_exponent = (is_normal_number
1141 ? biased_exponent - exponent_bias
1142 : 1 - exponent_bias);
1143
1144 // Obtain trunc(log2(abs(value))), which is just the unbiased exponent.
1145 const int floor_log2_value = unbiased_exponent;
1146 // This is within +-1 of log10(abs(value)). Note that log10 2 is 0.3010..
1147 const int approx_log10_value = (floor_log2_value >= 0
1148 ? (floor_log2_value*301 + 999)/1000
1149 : (floor_log2_value*301 - 999)/1000);
1150
1151 // Compute (an upper bound of) the number's effective precision when it is
1152 // formatted in scientific and fixed notation. Beyond this precision all
1153 // digits are definitely zero, and this fact allows us to bound the sizes
1154 // of any local output buffers that we may need to use. TODO: Consider
1155 // the number of trailing zero bits in the mantissa to obtain finer upper
1156 // bounds.
1157 // ???: Using "mantissa_bits + 1" instead of just "mantissa_bits" in the
1158 // bounds below is necessary only for __ibm128, it seems. Even though the
1159 // type has 105 bits of precision, printf may output 106 fractional digits
1160 // on some inputs, e.g. 0x1.bcd19f5d720d12a3513e3301028p+0.
1161 const int max_eff_scientific_precision
1162 = (floor_log2_value >= 0
1163 ? max(mantissa_bits + 1, approx_log10_value + 1)
1164 : -(7*floor_log2_value + 9)/10 + 2 + mantissa_bits + 1);
1165 __glibcxx_assert(max_eff_scientific_precision > 0);
1166
1167 const int max_eff_fixed_precision
1168 = (floor_log2_value >= 0
1169 ? mantissa_bits + 1
1170 : -floor_log2_value + mantissa_bits + 1);
1171 __glibcxx_assert(max_eff_fixed_precision > 0);
1172
1173 // Ryu doesn't support formatting floating-point types larger than double
1174 // with an explicit precision, so instead we just go through printf.
1175 if constexpr (is_same_v<T, long double> || is_same_v<T, F128_type>)
1176 {
1177 int effective_precision;
1178 const char* output_specifier;
1179 if (fmt == chars_format::scientific)
1180 {
1181 effective_precision = min(precision, max_eff_scientific_precision);
1182 output_specifier = "%.*Le";
1183 }
1184 else if (fmt == chars_format::fixed)
1185 {
1186 effective_precision = min(precision, max_eff_fixed_precision);
1187 output_specifier = "%.*Lf";
1188 }
1189 else if (fmt == chars_format::general)
1190 {
1191 effective_precision = min(precision, max_eff_scientific_precision);
1192 output_specifier = "%.*Lg";
1193 }
1194 const int excess_precision = (fmt != chars_format::general
1195 ? precision - effective_precision : 0);
1196
1197 // Since the output of printf is locale-sensitive, we need to be able
1198 // to handle a radix point that's different from '.'.
1199 char radix[6] = {'.', '\0', '\0', '\0', '\0', '\0'};
1200 #ifdef RADIXCHAR
1201 if (effective_precision > 0)
1202 // ???: Can nl_langinfo() ever return null?
1203 if (const char* const radix_ptr = nl_langinfo(RADIXCHAR))
1204 {
1205 strncpy(radix, radix_ptr, sizeof(radix)-1);
1206 // We accept only radix points which are at most 4 bytes (one
1207 // UTF-8 character) wide.
1208 __glibcxx_assert(radix[4] == '\0');
1209 }
1210 #endif
1211
1212 // Compute straightforward upper bounds on the output length.
1213 int output_length_upper_bound;
1214 if (fmt == chars_format::scientific || fmt == chars_format::general)
1215 output_length_upper_bound = (strlen("-d") + sizeof(radix)
1216 + effective_precision
1217 + strlen("e+dddd"));
1218 else if (fmt == chars_format::fixed)
1219 {
1220 if (approx_log10_value >= 0)
1221 output_length_upper_bound = sign + approx_log10_value + 1;
1222 else
1223 output_length_upper_bound = sign + strlen("0");
1224 output_length_upper_bound += sizeof(radix) + effective_precision;
1225 }
1226
1227 // Do the sprintf into the local buffer.
1228 char buffer[output_length_upper_bound+1];
1229 int output_length
1230 = sprintf_ld(buffer, output_specifier, value, effective_precision);
1231 __glibcxx_assert(output_length <= output_length_upper_bound);
1232
1233 if (effective_precision > 0)
1234 // We need to replace a radix that is different from '.' with '.'.
1235 if (const string_view radix_sv = {radix}; radix_sv != ".")
1236 {
1237 const string_view buffer_sv = {buffer, (size_t)output_length};
1238 const size_t radix_index = buffer_sv.find(radix_sv);
1239 if (radix_index != string_view::npos)
1240 {
1241 buffer[radix_index] = '.';
1242 if (radix_sv.length() > 1)
1243 {
1244 memmove(&buffer[radix_index + 1],
1245 &buffer[radix_index + radix_sv.length()],
1246 output_length - radix_index - radix_sv.length());
1247 output_length -= radix_sv.length() - 1;
1248 }
1249 }
1250 }
1251
1252 // Copy the string from the buffer over to the output range.
1253 if (last - first < output_length + excess_precision)
1254 return {last, errc::value_too_large};
1255 memcpy(first, buffer, output_length);
1256 first += output_length;
1257
1258 // Add the excess 0s to the result.
1259 if (excess_precision > 0)
1260 {
1261 if (fmt == chars_format::scientific)
1262 {
1263 char* const significand_end
1264 = (output_length >= 6 && first[-6] == 'e' ? &first[-6]
1265 : first[-5] == 'e' ? &first[-5]
1266 : &first[-4]);
1267 __glibcxx_assert(*significand_end == 'e');
1268 memmove(significand_end + excess_precision, significand_end,
1269 first - significand_end);
1270 memset(significand_end, '0', excess_precision);
1271 first += excess_precision;
1272 }
1273 else if (fmt == chars_format::fixed)
1274 {
1275 memset(first, '0', excess_precision);
1276 first += excess_precision;
1277 }
1278 }
1279 return {first, errc{}};
1280 }
1281 else if (fmt == chars_format::scientific)
1282 {
1283 const int effective_precision
1284 = min(precision, max_eff_scientific_precision);
1285 const int excess_precision = precision - effective_precision;
1286
1287 // We can easily compute the output length exactly whenever the
1288 // scientific exponent is far enough away from +-100. But if it's
1289 // near +-100, then our log2 approximation is too coarse (and doesn't
1290 // consider precision-dependent rounding) in order to accurately
1291 // distinguish between a scientific exponent of +-100 and +-99.
1292 const bool scientific_exponent_near_100_p
1293 = abs(abs(floor_log2_value) - 332) <= 4;
1294
1295 // Compute an upper bound on the output length. TODO: Maybe also
1296 // consider a lower bound on the output length.
1297 int output_length_upper_bound = sign + strlen("d");
1298 if (effective_precision > 0)
1299 output_length_upper_bound += strlen(".") + effective_precision;
1300 if (scientific_exponent_near_100_p
1301 || (floor_log2_value >= 332 || floor_log2_value <= -333))
1302 output_length_upper_bound += strlen("e+ddd");
1303 else
1304 output_length_upper_bound += strlen("e+dd");
1305
1306 int output_length;
1307 if (last - first >= output_length_upper_bound + excess_precision)
1308 {
1309 // The result will definitely fit into the output range, so we can
1310 // write directly into it.
1311 output_length = ryu::d2exp_buffered_n(value, effective_precision,
1312 first, nullptr);
1313 __glibcxx_assert(output_length == output_length_upper_bound
1314 || (scientific_exponent_near_100_p
1315 && (output_length
1316 == output_length_upper_bound - 1)));
1317 }
1318 else if (scientific_exponent_near_100_p)
1319 {
1320 // Write the result of d2exp_buffered_n into an intermediate
1321 // buffer, do a bounds check, and copy the result into the output
1322 // range.
1323 char buffer[output_length_upper_bound];
1324 output_length = ryu::d2exp_buffered_n(value, effective_precision,
1325 buffer, nullptr);
1326 __glibcxx_assert(output_length == output_length_upper_bound - 1
1327 || output_length == output_length_upper_bound);
1328 if (last - first < output_length + excess_precision)
1329 return {last, errc::value_too_large};
1330 memcpy(first, buffer, output_length);
1331 }
1332 else
1333 // If the scientific exponent is not near 100, then the upper bound
1334 // is actually the exact length, and so the result will definitely
1335 // not fit into the output range.
1336 return {last, errc::value_too_large};
1337 first += output_length;
1338 if (excess_precision > 0)
1339 {
1340 // Splice the excess zeros into the result.
1341 char* const significand_end = (first[-5] == 'e'
1342 ? &first[-5] : &first[-4]);
1343 __glibcxx_assert(*significand_end == 'e');
1344 memmove(significand_end + excess_precision, significand_end,
1345 first - significand_end);
1346 memset(significand_end, '0', excess_precision);
1347 first += excess_precision;
1348 }
1349 return {first, errc{}};
1350 }
1351 else if (fmt == chars_format::fixed)
1352 {
1353 const int effective_precision
1354 = min(precision, max_eff_fixed_precision);
1355 const int excess_precision = precision - effective_precision;
1356
1357 // Compute an upper bound on the output length. TODO: Maybe also
1358 // consider a lower bound on the output length.
1359 int output_length_upper_bound;
1360 if (approx_log10_value >= 0)
1361 output_length_upper_bound = sign + approx_log10_value + 1;
1362 else
1363 output_length_upper_bound = sign + strlen("0");
1364 if (effective_precision > 0)
1365 output_length_upper_bound += strlen(".") + effective_precision;
1366
1367 int output_length;
1368 if (last - first >= output_length_upper_bound + excess_precision)
1369 {
1370 // The result will definitely fit into the output range, so we can
1371 // write directly into it.
1372 output_length = ryu::d2fixed_buffered_n(value, effective_precision,
1373 first);
1374 __glibcxx_assert(output_length <= output_length_upper_bound);
1375 }
1376 else
1377 {
1378 // Write the result of d2fixed_buffered_n into an intermediate
1379 // buffer, do a bounds check, and copy the result into the output
1380 // range.
1381 char buffer[output_length_upper_bound];
1382 output_length = ryu::d2fixed_buffered_n(value, effective_precision,
1383 buffer);
1384 __glibcxx_assert(output_length <= output_length_upper_bound);
1385 if (last - first < output_length + excess_precision)
1386 return {last, errc::value_too_large};
1387 memcpy(first, buffer, output_length);
1388 }
1389 first += output_length;
1390 if (excess_precision > 0)
1391 {
1392 // Append the excess zeros into the result.
1393 memset(first, '0', excess_precision);
1394 first += excess_precision;
1395 }
1396 return {first, errc{}};
1397 }
1398 else if (fmt == chars_format::general)
1399 {
1400 // Handle the 'general' formatting mode as per C11 printf's %g output
1401 // specifier. Since Ryu doesn't do zero-trimming, we always write to
1402 // an intermediate buffer and manually perform zero-trimming there
1403 // before copying the result over to the output range.
1404 int effective_precision
1405 = min(precision, max_eff_scientific_precision + 1);
1406 const int output_length_upper_bound
1407 = strlen("-d.") + effective_precision + strlen("e+ddd");
1408 // The four bytes of headroom is to avoid needing to do a memmove when
1409 // rewriting a scientific form such as 1.00e-2 into the equivalent
1410 // fixed form 0.001.
1411 char buffer[4 + output_length_upper_bound];
1412
1413 // 7.21.6.1/8: "Let P equal ... 1 if the precision is zero."
1414 if (effective_precision == 0)
1415 effective_precision = 1;
1416
1417 // Perform a trial formatting in scientific form, and obtain the
1418 // scientific exponent.
1419 int scientific_exponent;
1420 char* buffer_start = buffer + 4;
1421 int output_length
1422 = ryu::d2exp_buffered_n(value, effective_precision - 1,
1423 buffer_start, &scientific_exponent);
1424 __glibcxx_assert(output_length <= output_length_upper_bound);
1425
1426 // 7.21.6.1/8: "Then, if a conversion with style E would have an
1427 // exponent of X:
1428 // if P > X >= -4, the conversion is with style f and
1429 // precision P - (X + 1).
1430 // otherwise, the conversion is with style e and precision P - 1."
1431 const bool resolve_to_fixed_form
1432 = (scientific_exponent >= -4
1433 && scientific_exponent < effective_precision);
1434 if (resolve_to_fixed_form)
1435 {
1436 // Rather than invoking d2fixed_buffered_n to reformat the number
1437 // for us from scratch, we can just rewrite the scientific form
1438 // into fixed form in-place. This is safe to do because whenever
1439 // %g resolves to %f, the fixed form will be no larger than the
1440 // corresponding scientific form, and it will also contain the
1441 // same significant digits as the scientific form.
1442 fmt = chars_format::fixed;
1443 if (scientific_exponent < 0)
1444 {
1445 // e.g. buffer_start == "-1.234e-04"
1446 char* leading_digit = &buffer_start[sign];
1447 leading_digit[1] = leading_digit[0];
1448 // buffer_start == "-11234e-04"
1449 buffer_start -= -scientific_exponent;
1450 __glibcxx_assert(buffer_start >= buffer);
1451 // buffer_start == "????-11234e-04"
1452 char* head = buffer_start;
1453 if (sign)
1454 *head++ = '-';
1455 *head++ = '0';
1456 *head++ = '.';
1457 memset(head, '0', -scientific_exponent - 1);
1458 // buffer_start == "-0.00011234e-04"
1459
1460 // Now drop the exponent suffix, and add the leading zeros to
1461 // the output length.
1462 output_length -= strlen("e-0d");
1463 output_length += -scientific_exponent;
1464 if (effective_precision - 1 == 0)
1465 // The scientific form had no decimal point, but the fixed
1466 // form now does.
1467 output_length += strlen(".");
1468 }
1469 else if (effective_precision == 1)
1470 {
1471 // The scientific exponent must be 0, so the fixed form
1472 // coincides with the scientific form (minus the exponent
1473 // suffix).
1474 __glibcxx_assert(scientific_exponent == 0);
1475 output_length -= strlen("e+dd");
1476 }
1477 else
1478 {
1479 // We are dealing with a scientific form which has a
1480 // non-empty fractional part and a nonnegative exponent,
1481 // e.g. buffer_start == "1.234e+02".
1482 __glibcxx_assert(effective_precision >= 1);
1483 char* const decimal_point = &buffer_start[sign + 1];
1484 __glibcxx_assert(*decimal_point == '.');
1485 memmove(decimal_point, decimal_point+1,
1486 scientific_exponent);
1487 // buffer_start == "123.4e+02"
1488 decimal_point[scientific_exponent] = '.';
1489 if (scientific_exponent >= 100)
1490 output_length -= strlen("e+ddd");
1491 else
1492 output_length -= strlen("e+dd");
1493 if (effective_precision - 1 == scientific_exponent)
1494 output_length -= strlen(".");
1495 }
1496 effective_precision -= 1 + scientific_exponent;
1497
1498 __glibcxx_assert(output_length <= output_length_upper_bound);
1499 }
1500 else
1501 {
1502 // We're sticking to the scientific form, so keep the output as-is.
1503 fmt = chars_format::scientific;
1504 effective_precision = effective_precision - 1;
1505 }
1506
1507 // 7.21.6.1/8: "Finally ... any any trailing zeros are removed from
1508 // the fractional portion of the result and the decimal-point
1509 // character is removed if there is no fractional portion remaining."
1510 if (effective_precision > 0)
1511 {
1512 char* decimal_point = nullptr;
1513 if (fmt == chars_format::scientific)
1514 decimal_point = &buffer_start[sign + 1];
1515 else if (fmt == chars_format::fixed)
1516 decimal_point
1517 = &buffer_start[output_length] - effective_precision - 1;
1518 __glibcxx_assert(*decimal_point == '.');
1519
1520 char* const fractional_part_start = decimal_point + 1;
1521 char* fractional_part_end = nullptr;
1522 if (fmt == chars_format::scientific)
1523 {
1524 fractional_part_end = (buffer_start[output_length-5] == 'e'
1525 ? &buffer_start[output_length-5]
1526 : &buffer_start[output_length-4]);
1527 __glibcxx_assert(*fractional_part_end == 'e');
1528 }
1529 else if (fmt == chars_format::fixed)
1530 fractional_part_end = &buffer_start[output_length];
1531
1532 const string_view fractional_part
1533 = {fractional_part_start, (size_t)(fractional_part_end
1534 - fractional_part_start) };
1535 const size_t last_nonzero_digit_pos
1536 = fractional_part.find_last_not_of('0');
1537
1538 char* trim_start;
1539 if (last_nonzero_digit_pos == string_view::npos)
1540 trim_start = decimal_point;
1541 else
1542 trim_start = &fractional_part_start[last_nonzero_digit_pos] + 1;
1543 if (fmt == chars_format::scientific)
1544 memmove(trim_start, fractional_part_end,
1545 &buffer_start[output_length] - fractional_part_end);
1546 output_length -= fractional_part_end - trim_start;
1547 }
1548
1549 if (last - first < output_length)
1550 return {last, errc::value_too_large};
1551
1552 memcpy(first, buffer_start, output_length);
1553 return {first + output_length, errc{}};
1554 }
1555
1556 __glibcxx_assert(false);
1557 }
1558
1559 // Define the overloads for float.
1560 to_chars_result
to_chars(char * first,char * last,float value)1561 to_chars(char* first, char* last, float value) noexcept
1562 { return __floating_to_chars_shortest(first, last, value, chars_format{}); }
1563
1564 to_chars_result
to_chars(char * first,char * last,float value,chars_format fmt)1565 to_chars(char* first, char* last, float value, chars_format fmt) noexcept
1566 { return __floating_to_chars_shortest(first, last, value, fmt); }
1567
1568 to_chars_result
to_chars(char * first,char * last,float value,chars_format fmt,int precision)1569 to_chars(char* first, char* last, float value, chars_format fmt,
1570 int precision) noexcept
1571 { return __floating_to_chars_precision(first, last, value, fmt, precision); }
1572
1573 // Define the overloads for double.
1574 to_chars_result
to_chars(char * first,char * last,double value)1575 to_chars(char* first, char* last, double value) noexcept
1576 { return __floating_to_chars_shortest(first, last, value, chars_format{}); }
1577
1578 to_chars_result
to_chars(char * first,char * last,double value,chars_format fmt)1579 to_chars(char* first, char* last, double value, chars_format fmt) noexcept
1580 { return __floating_to_chars_shortest(first, last, value, fmt); }
1581
1582 to_chars_result
to_chars(char * first,char * last,double value,chars_format fmt,int precision)1583 to_chars(char* first, char* last, double value, chars_format fmt,
1584 int precision) noexcept
1585 { return __floating_to_chars_precision(first, last, value, fmt, precision); }
1586
1587 // Define the overloads for long double.
1588 to_chars_result
to_chars(char * first,char * last,long double value)1589 to_chars(char* first, char* last, long double value) noexcept
1590 {
1591 if constexpr (LONG_DOUBLE_KIND == LDK_BINARY64
1592 || LONG_DOUBLE_KIND == LDK_UNSUPPORTED)
1593 return __floating_to_chars_shortest(first, last, static_cast<double>(value),
1594 chars_format{});
1595 else
1596 return __floating_to_chars_shortest(first, last, value, chars_format{});
1597 }
1598
1599 to_chars_result
to_chars(char * first,char * last,long double value,chars_format fmt)1600 to_chars(char* first, char* last, long double value, chars_format fmt) noexcept
1601 {
1602 if constexpr (LONG_DOUBLE_KIND == LDK_BINARY64
1603 || LONG_DOUBLE_KIND == LDK_UNSUPPORTED)
1604 return __floating_to_chars_shortest(first, last, static_cast<double>(value),
1605 fmt);
1606 else
1607 return __floating_to_chars_shortest(first, last, value, fmt);
1608 }
1609
1610 to_chars_result
to_chars(char * first,char * last,long double value,chars_format fmt,int precision)1611 to_chars(char* first, char* last, long double value, chars_format fmt,
1612 int precision) noexcept
1613 {
1614 if constexpr (LONG_DOUBLE_KIND == LDK_BINARY64
1615 || LONG_DOUBLE_KIND == LDK_UNSUPPORTED)
1616 return __floating_to_chars_precision(first, last, static_cast<double>(value),
1617 fmt,
1618 precision);
1619 else
1620 return __floating_to_chars_precision(first, last, value, fmt, precision);
1621 }
1622
1623 #ifdef FLOAT128_TO_CHARS
1624 to_chars_result
to_chars(char * first,char * last,__float128 value)1625 to_chars(char* first, char* last, __float128 value) noexcept
1626 {
1627 return __floating_to_chars_shortest(first, last, value, chars_format{});
1628 }
1629
1630 to_chars_result
to_chars(char * first,char * last,__float128 value,chars_format fmt)1631 to_chars(char* first, char* last, __float128 value, chars_format fmt) noexcept
1632 {
1633 return __floating_to_chars_shortest(first, last, value, fmt);
1634 }
1635
1636 to_chars_result
to_chars(char * first,char * last,__float128 value,chars_format fmt,int precision)1637 to_chars(char* first, char* last, __float128 value, chars_format fmt,
1638 int precision) noexcept
1639 {
1640 return __floating_to_chars_precision(first, last, value, fmt, precision);
1641 }
1642 #endif
1643
1644 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
1645 // Map the -mlong-double-64 long double overloads to the double overloads.
1646 extern "C" to_chars_result
1647 _ZSt8to_charsPcS_e(char* first, char* last, double value) noexcept
1648 __attribute__((alias ("_ZSt8to_charsPcS_d")));
1649
1650 extern "C" to_chars_result
1651 _ZSt8to_charsPcS_eSt12chars_format(char* first, char* last, double value,
1652 chars_format fmt) noexcept
1653 __attribute__((alias ("_ZSt8to_charsPcS_dSt12chars_format")));
1654
1655 extern "C" to_chars_result
1656 _ZSt8to_charsPcS_eSt12chars_formati(char* first, char* last, double value,
1657 chars_format fmt, int precision) noexcept
1658 __attribute__((alias ("_ZSt8to_charsPcS_dSt12chars_formati")));
1659 #endif
1660
1661 _GLIBCXX_END_NAMESPACE_VERSION
1662 } // namespace std
1663
1664 #endif // _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
1665