| 1 | // std::from_chars implementation for floating-point types -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2020-2025 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 | // |
| 26 | // ISO C++ 14882:2017 |
| 27 | // 23.2.9 Primitive numeric input conversion [utility.from.chars] |
| 28 | // |
| 29 | |
| 30 | // Prefer to use std::pmr::string if possible, which requires the cxx11 ABI. |
| 31 | #define _GLIBCXX_USE_CXX11_ABI 1 |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <array> |
| 35 | #include <charconv> |
| 36 | #include <bit> |
| 37 | #include <iterator> |
| 38 | #include <limits> |
| 39 | #include <string> |
| 40 | #include <memory_resource> |
| 41 | #include <cfenv> |
| 42 | #include <cfloat> |
| 43 | #include <cmath> |
| 44 | #include <cstdint> |
| 45 | #include <cstdlib> |
| 46 | #include <cstring> |
| 47 | #include <locale.h> |
| 48 | #include <bits/functexcept.h> |
| 49 | #if _GLIBCXX_HAVE_XLOCALE_H |
| 50 | # include <xlocale.h> |
| 51 | #endif |
| 52 | |
| 53 | #if _GLIBCXX_HAVE_USELOCALE |
| 54 | // FIXME: This should be reimplemented so it doesn't use strtod and newlocale. |
| 55 | // That will avoid the need for any memory allocation, meaning that the |
| 56 | // non-conforming errc::not_enough_memory result cannot happen. |
| 57 | # define USE_STRTOD_FOR_FROM_CHARS 1 |
| 58 | #endif |
| 59 | |
| 60 | #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 61 | #ifndef __LONG_DOUBLE_IBM128__ |
| 62 | #error "floating_from_chars.cc must be compiled with -mabi=ibmlongdouble" |
| 63 | #endif |
| 64 | // strtold for __ieee128 |
| 65 | extern "C" __ieee128 __strtoieee128(const char*, char**); |
| 66 | #elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \ |
| 67 | && defined(__GLIBC_PREREQ) && defined(USE_STRTOD_FOR_FROM_CHARS) |
| 68 | #define USE_STRTOF128_FOR_FROM_CHARS 1 |
| 69 | extern "C" _Float128 __strtof128(const char*, char**) |
| 70 | __asm ("strtof128") |
| 71 | #ifndef _GLIBCXX_HAVE_FLOAT128_MATH |
| 72 | __attribute__((__weak__)) |
| 73 | #endif |
| 74 | ; |
| 75 | #endif |
| 76 | |
| 77 | #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \ |
| 78 | && __SIZE_WIDTH__ >= 32 |
| 79 | # define USE_LIB_FAST_FLOAT 1 |
| 80 | #endif |
| 81 | |
| 82 | #if USE_LIB_FAST_FLOAT |
| 83 | # define FASTFLOAT_DEBUG_ASSERT __glibcxx_assert |
| 84 | namespace |
| 85 | { |
| 86 | # include "fast_float/fast_float.h" |
| 87 | |
| 88 | namespace fast_float |
| 89 | { |
| 90 | |
| 91 | // Wrappers around float for std::{,b}float16_t promoted to float. |
| 92 | struct floating_type_float16_t |
| 93 | { |
| 94 | float* x; |
| 95 | uint16_t bits; |
| 96 | }; |
| 97 | struct floating_type_bfloat16_t |
| 98 | { |
| 99 | float* x; |
| 100 | uint16_t bits; |
| 101 | }; |
| 102 | |
| 103 | template<> |
| 104 | constexpr int |
| 105 | binary_format<floating_type_float16_t>::mantissa_explicit_bits() |
| 106 | { return 10; } |
| 107 | |
| 108 | template<> |
| 109 | constexpr int |
| 110 | binary_format<floating_type_bfloat16_t>::mantissa_explicit_bits() |
| 111 | { return 7; } |
| 112 | |
| 113 | // 10 bits of stored mantissa, pow(5,q) <= 0x4p+10 implies q <= 5 |
| 114 | template<> |
| 115 | constexpr int |
| 116 | binary_format<floating_type_float16_t>::max_exponent_round_to_even() |
| 117 | { return 5; } |
| 118 | |
| 119 | // 7 bits of stored mantissa, pow(5,q) <= 0x4p+7 implies q <= 3 |
| 120 | template<> |
| 121 | constexpr int |
| 122 | binary_format<floating_type_bfloat16_t>::max_exponent_round_to_even() |
| 123 | { return 3; } |
| 124 | |
| 125 | // 10 bits of stored mantissa, pow(5,-q) < 0x1p+64 / 0x1p+11 implies q >= -22 |
| 126 | template<> |
| 127 | constexpr int |
| 128 | binary_format<floating_type_float16_t>::min_exponent_round_to_even() |
| 129 | { return -22; } |
| 130 | |
| 131 | // 7 bits of stored mantissa, pow(5,-q) < 0x1p+64 / 0x1p+8 implies q >= -24 |
| 132 | template<> |
| 133 | constexpr int |
| 134 | binary_format<floating_type_bfloat16_t>::min_exponent_round_to_even() |
| 135 | { return -24; } |
| 136 | |
| 137 | template<> |
| 138 | constexpr int |
| 139 | binary_format<floating_type_float16_t>::minimum_exponent() |
| 140 | { return -15; } |
| 141 | |
| 142 | template<> |
| 143 | constexpr int |
| 144 | binary_format<floating_type_bfloat16_t>::minimum_exponent() |
| 145 | { return -127; } |
| 146 | |
| 147 | template<> |
| 148 | constexpr int |
| 149 | binary_format<floating_type_float16_t>::infinite_power() |
| 150 | { return 0x1F; } |
| 151 | |
| 152 | template<> |
| 153 | constexpr int |
| 154 | binary_format<floating_type_bfloat16_t>::infinite_power() |
| 155 | { return 0xFF; } |
| 156 | |
| 157 | template<> |
| 158 | constexpr int |
| 159 | binary_format<floating_type_float16_t>::sign_index() |
| 160 | { return 15; } |
| 161 | |
| 162 | template<> |
| 163 | constexpr int |
| 164 | binary_format<floating_type_bfloat16_t>::sign_index() |
| 165 | { return 15; } |
| 166 | |
| 167 | template<> |
| 168 | constexpr int |
| 169 | binary_format<floating_type_float16_t>::largest_power_of_ten() |
| 170 | { return 4; } |
| 171 | |
| 172 | template<> |
| 173 | constexpr int |
| 174 | binary_format<floating_type_bfloat16_t>::largest_power_of_ten() |
| 175 | { return 38; } |
| 176 | |
| 177 | template<> |
| 178 | constexpr int |
| 179 | binary_format<floating_type_float16_t>::smallest_power_of_ten() |
| 180 | { return -27; } |
| 181 | |
| 182 | template<> |
| 183 | constexpr int |
| 184 | binary_format<floating_type_bfloat16_t>::smallest_power_of_ten() |
| 185 | { return -60; } |
| 186 | |
| 187 | template<> |
| 188 | constexpr size_t |
| 189 | binary_format<floating_type_float16_t>::max_digits() |
| 190 | { return 22; } |
| 191 | |
| 192 | template<> |
| 193 | constexpr size_t |
| 194 | binary_format<floating_type_bfloat16_t>::max_digits() |
| 195 | { return 98; } |
| 196 | |
| 197 | // negative_digit_comp converts adjusted_mantissa to the (originally only) |
| 198 | // floating type and immediately back with slight tweaks (e.g. explicit |
| 199 | // leading bit instead of implicit for normals). |
| 200 | // Avoid going through the floating point type. |
| 201 | template<> |
| 202 | fastfloat_really_inline void |
| 203 | to_float<floating_type_float16_t>(bool negative, adjusted_mantissa am, |
| 204 | floating_type_float16_t &value) |
| 205 | { |
| 206 | constexpr int mantissa_bits |
| 207 | = binary_format<floating_type_float16_t>::mantissa_explicit_bits(); |
| 208 | value.bits = (am.mantissa |
| 209 | | (uint16_t(am.power2) << mantissa_bits) |
| 210 | | (negative ? 0x8000 : 0)); |
| 211 | } |
| 212 | |
| 213 | template<> |
| 214 | fastfloat_really_inline void |
| 215 | to_float<floating_type_bfloat16_t>(bool negative, adjusted_mantissa am, |
| 216 | floating_type_bfloat16_t &value) |
| 217 | { |
| 218 | constexpr int mantissa_bits |
| 219 | = binary_format<floating_type_bfloat16_t>::mantissa_explicit_bits(); |
| 220 | value.bits = (am.mantissa |
| 221 | | (uint16_t(am.power2) << mantissa_bits) |
| 222 | | (negative ? 0x8000 : 0)); |
| 223 | } |
| 224 | |
| 225 | template <> |
| 226 | fastfloat_really_inline adjusted_mantissa |
| 227 | to_extended<floating_type_float16_t>(floating_type_float16_t value) noexcept |
| 228 | { |
| 229 | adjusted_mantissa am; |
| 230 | constexpr int mantissa_bits |
| 231 | = binary_format<floating_type_float16_t>::mantissa_explicit_bits(); |
| 232 | int32_t bias |
| 233 | = (mantissa_bits |
| 234 | - binary_format<floating_type_float16_t>::minimum_exponent()); |
| 235 | constexpr uint16_t exponent_mask = 0x7C00; |
| 236 | constexpr uint16_t mantissa_mask = 0x03FF; |
| 237 | constexpr uint16_t hidden_bit_mask = 0x0400; |
| 238 | if ((value.bits & exponent_mask) == 0) { |
| 239 | // denormal |
| 240 | am.power2 = 1 - bias; |
| 241 | am.mantissa = value.bits & mantissa_mask; |
| 242 | } else { |
| 243 | // normal |
| 244 | am.power2 = int32_t((value.bits & exponent_mask) >> mantissa_bits); |
| 245 | am.power2 -= bias; |
| 246 | am.mantissa = (value.bits & mantissa_mask) | hidden_bit_mask; |
| 247 | } |
| 248 | return am; |
| 249 | } |
| 250 | |
| 251 | template <> |
| 252 | fastfloat_really_inline adjusted_mantissa |
| 253 | to_extended<floating_type_bfloat16_t>(floating_type_bfloat16_t value) noexcept |
| 254 | { |
| 255 | adjusted_mantissa am; |
| 256 | constexpr int mantissa_bits |
| 257 | = binary_format<floating_type_bfloat16_t>::mantissa_explicit_bits(); |
| 258 | int32_t bias |
| 259 | = (mantissa_bits |
| 260 | - binary_format<floating_type_bfloat16_t>::minimum_exponent()); |
| 261 | constexpr uint16_t exponent_mask = 0x7F80; |
| 262 | constexpr uint16_t mantissa_mask = 0x007F; |
| 263 | constexpr uint16_t hidden_bit_mask = 0x0080; |
| 264 | if ((value.bits & exponent_mask) == 0) { |
| 265 | // denormal |
| 266 | am.power2 = 1 - bias; |
| 267 | am.mantissa = value.bits & mantissa_mask; |
| 268 | } else { |
| 269 | // normal |
| 270 | am.power2 = int32_t((value.bits & exponent_mask) >> mantissa_bits); |
| 271 | am.power2 -= bias; |
| 272 | am.mantissa = (value.bits & mantissa_mask) | hidden_bit_mask; |
| 273 | } |
| 274 | return am; |
| 275 | } |
| 276 | |
| 277 | // Like fast_float.h from_chars_advanced, but for 16-bit float. |
| 278 | template<typename T> |
| 279 | from_chars_result |
| 280 | from_chars_16(const char* first, const char* last, T &value, |
| 281 | chars_format fmt) noexcept |
| 282 | { |
| 283 | parse_options options{fmt}; |
| 284 | |
| 285 | from_chars_result answer; |
| 286 | if (first == last) |
| 287 | { |
| 288 | answer.ec = std::errc::invalid_argument; |
| 289 | answer.ptr = first; |
| 290 | return answer; |
| 291 | } |
| 292 | |
| 293 | parsed_number_string pns = parse_number_string(first, last, options); |
| 294 | if (!pns.valid) |
| 295 | return detail::parse_infnan(first, last, *value.x); |
| 296 | |
| 297 | answer.ec = std::errc(); |
| 298 | answer.ptr = pns.lastmatch; |
| 299 | |
| 300 | adjusted_mantissa am |
| 301 | = compute_float<binary_format<T>>(pns.exponent, pns.mantissa); |
| 302 | if (pns.too_many_digits && am.power2 >= 0) |
| 303 | { |
| 304 | if (am != compute_float<binary_format<T>>(pns.exponent, |
| 305 | pns.mantissa + 1)) |
| 306 | am = compute_error<binary_format<T>>(pns.exponent, pns.mantissa); |
| 307 | } |
| 308 | |
| 309 | // If we called compute_float<binary_format<T>>(pns.exponent, pns.mantissa) |
| 310 | // and we have an invalid power (am.power2 < 0), |
| 311 | // then we need to go the long way around again. This is very uncommon. |
| 312 | if (am.power2 < 0) |
| 313 | am = digit_comp<T>(pns, am); |
| 314 | |
| 315 | if ((pns.mantissa != 0 && am.mantissa == 0 && am.power2 == 0) |
| 316 | || am.power2 == binary_format<T>::infinite_power()) |
| 317 | { |
| 318 | // In case of over/underflow, return result_out_of_range and don't |
| 319 | // modify value, as per [charconv.from.chars]/1. Note that LWG 3081 wants |
| 320 | // to modify value in this case too. |
| 321 | answer.ec = std::errc::result_out_of_range; |
| 322 | return answer; |
| 323 | } |
| 324 | |
| 325 | // Transform the {,b}float16_t to float32_t before to_float. |
| 326 | if constexpr (std::is_same_v<T, floating_type_float16_t>) |
| 327 | { |
| 328 | if (am.power2 == 0) |
| 329 | { |
| 330 | if (am.mantissa) |
| 331 | { |
| 332 | int n = (std::numeric_limits<unsigned int>::digits |
| 333 | - __builtin_clz (am.mantissa)) - 1; |
| 334 | am.mantissa &= ~(static_cast<decltype(am.mantissa)>(1) << n); |
| 335 | am.mantissa <<= (binary_format<float>::mantissa_explicit_bits() |
| 336 | - n); |
| 337 | am.power2 = n + 0x67; |
| 338 | } |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | am.mantissa <<= 13; |
| 343 | am.power2 += 0x70; |
| 344 | } |
| 345 | } |
| 346 | else |
| 347 | am.mantissa <<= 16; |
| 348 | to_float(pns.negative, am, *value.x); |
| 349 | return answer; |
| 350 | } |
| 351 | } // fast_float |
| 352 | |
| 353 | } // anon namespace |
| 354 | #endif |
| 355 | |
| 356 | namespace std _GLIBCXX_VISIBILITY(default) |
| 357 | { |
| 358 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 359 | |
| 360 | namespace |
| 361 | { |
| 362 | #if USE_STRTOD_FOR_FROM_CHARS |
| 363 | // A memory resource with a static buffer that can be used for small |
| 364 | // allocations. At most one allocation using the freestore can be done |
| 365 | // if the static buffer is insufficient. The callers below only require |
| 366 | // a single allocation, so there's no need for anything more complex. |
| 367 | struct buffer_resource : pmr::memory_resource |
| 368 | { |
| 369 | ~buffer_resource() { if (m_ptr) operator delete(m_ptr, m_bytes); } |
| 370 | |
| 371 | void* |
| 372 | do_allocate(size_t bytes, size_t alignment [[maybe_unused]]) override |
| 373 | { |
| 374 | // Allocate from the buffer if it will fit. |
| 375 | if (m_bytes < sizeof(m_buf) && (m_bytes + bytes) <= sizeof(m_buf)) |
| 376 | return m_buf + std::__exchange(m_bytes, m_bytes + bytes); |
| 377 | |
| 378 | __glibcxx_assert(m_ptr == nullptr); |
| 379 | |
| 380 | m_ptr = operator new(bytes); |
| 381 | m_bytes = bytes; |
| 382 | return m_ptr; |
| 383 | } |
| 384 | |
| 385 | void |
| 386 | do_deallocate(void*, size_t, size_t) noexcept override |
| 387 | { /* like pmr::monotonic_buffer_resource, do nothing here */ } |
| 388 | |
| 389 | bool |
| 390 | do_is_equal(const pmr::memory_resource& other) const noexcept override |
| 391 | { return &other == this; } |
| 392 | |
| 393 | static constexpr int guaranteed_capacity() { return sizeof(m_buf); } |
| 394 | |
| 395 | private: |
| 396 | char m_buf[512]; |
| 397 | size_t m_bytes = 0; |
| 398 | void* m_ptr = nullptr; |
| 399 | }; |
| 400 | |
| 401 | #if _GLIBCXX_USE_CXX11_ABI |
| 402 | using buffered_string = std::pmr::string; |
| 403 | #else |
| 404 | using buffered_string = std::string; |
| 405 | #endif |
| 406 | |
| 407 | inline bool valid_fmt(chars_format fmt) |
| 408 | { |
| 409 | return fmt != chars_format{} |
| 410 | && ((fmt & chars_format::general) == fmt |
| 411 | || (fmt & chars_format::hex) == fmt); |
| 412 | } |
| 413 | |
| 414 | constexpr char hex_digits[] = "abcdefABCDEF0123456789"; |
| 415 | constexpr auto dec_digits = hex_digits + 12; |
| 416 | |
| 417 | // Find initial portion of [first, last) containing a floating-point number. |
| 418 | // The string `digits` is either `dec_digits` or `hex_digits` |
| 419 | // and `exp` is "eE", "pP" or NULL. |
| 420 | const char* |
| 421 | find_end_of_float(const char* first, const char* last, const char* digits, |
| 422 | const char *exp) |
| 423 | { |
| 424 | while (first < last && strchr(digits, *first) != nullptr) |
| 425 | ++first; |
| 426 | if (first < last && *first == '.') |
| 427 | { |
| 428 | ++first; |
| 429 | while (first < last && strchr(digits, *first)) |
| 430 | ++first; |
| 431 | } |
| 432 | if (first < last && exp != nullptr && (*first == exp[0] || *first == exp[1])) |
| 433 | { |
| 434 | ++first; |
| 435 | if (first < last && (*first == '-' || *first == '+')) |
| 436 | ++first; |
| 437 | while (first < last && strchr(dec_digits, *first) != nullptr) |
| 438 | ++first; |
| 439 | } |
| 440 | return first; |
| 441 | } |
| 442 | |
| 443 | // Determine the prefix of [first, last) that matches the pattern |
| 444 | // corresponding to `fmt`. |
| 445 | // Returns a NTBS containing the pattern, using `buf` to allocate |
| 446 | // additional storage if needed. |
| 447 | // Returns a nullptr if a valid pattern is not present. |
| 448 | const char* |
| 449 | pattern(const char* const first, const char* last, |
| 450 | chars_format& fmt, buffered_string& buf) |
| 451 | { |
| 452 | // fmt has the value of one of the enumerators of chars_format. |
| 453 | __glibcxx_assert(valid_fmt(fmt)); |
| 454 | |
| 455 | string_view res; |
| 456 | |
| 457 | if (first == last || *first == '+') [[unlikely]] |
| 458 | return nullptr; |
| 459 | |
| 460 | const int neg = (*first == '-'); |
| 461 | |
| 462 | if (std::memchr("iInN", (unsigned char)first[neg], 4)) |
| 463 | { |
| 464 | ptrdiff_t len = last - first; |
| 465 | if (len < (3 + neg)) |
| 466 | return nullptr; |
| 467 | |
| 468 | // possible infinity or NaN, let strtod decide |
| 469 | if (first[neg] == 'i' || first[neg] == 'I') |
| 470 | { |
| 471 | // Need at most 9 chars for "-INFINITY", ignore anything after it. |
| 472 | len = std::min(len, ptrdiff_t(neg + 8)); |
| 473 | } |
| 474 | else if (len > (neg + 3) && first[neg + 3] == '(') |
| 475 | { |
| 476 | // Look for end of "NAN(n-char-sequence)" |
| 477 | if (void* p = std::memchr(const_cast<char*>(first)+4, ')', len-4)) |
| 478 | len = static_cast<char*>(p) + 1 - first; |
| 479 | #ifndef __cpp_exceptions |
| 480 | if (len > buffer_resource::guaranteed_capacity()) |
| 481 | { |
| 482 | // The character sequence is too large for the buffer. |
| 483 | // Allocation failure could terminate the process, |
| 484 | // so just return an error via the fmt parameter. |
| 485 | fmt = chars_format{}; |
| 486 | return nullptr; |
| 487 | } |
| 488 | #endif |
| 489 | } |
| 490 | else // Only need 4 chars for "-NAN" |
| 491 | len = neg + 3; |
| 492 | |
| 493 | buf.assign(first, 0, len); |
| 494 | // prevent make_result correcting for "0x" |
| 495 | fmt = chars_format::general; |
| 496 | return buf.c_str(); |
| 497 | } |
| 498 | |
| 499 | const char* digits; |
| 500 | char* ptr; |
| 501 | |
| 502 | // Assign [first,last) to a std::string to get a NTBS that can be used |
| 503 | // with strspn, strtod etc. |
| 504 | // If the string would be longer than the fixed buffer inside the |
| 505 | // buffer_resource type use find_end_of_float to try to reduce how |
| 506 | // much memory is needed, to reduce the chance of std::bad_alloc. |
| 507 | |
| 508 | if (fmt == chars_format::hex) |
| 509 | { |
| 510 | digits = hex_digits; |
| 511 | |
| 512 | if ((last - first + 2) > buffer_resource::guaranteed_capacity()) |
| 513 | { |
| 514 | last = find_end_of_float(first + neg, last, digits, "pP"); |
| 515 | #ifndef __cpp_exceptions |
| 516 | if ((last - first + 2) > buffer_resource::guaranteed_capacity()) |
| 517 | { |
| 518 | // The character sequence is still too large for the buffer. |
| 519 | // Allocation failure could terminate the process, |
| 520 | // so just return an error via the fmt parameter. |
| 521 | fmt = chars_format{}; |
| 522 | return nullptr; |
| 523 | } |
| 524 | #endif |
| 525 | } |
| 526 | |
| 527 | buf = "-0x" + !neg; |
| 528 | buf.append(first + neg, last); |
| 529 | ptr = buf.data() + neg + 2; |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | digits = dec_digits; |
| 534 | |
| 535 | if ((last - first) > buffer_resource::guaranteed_capacity()) |
| 536 | { |
| 537 | last = find_end_of_float(first + neg, last, digits, |
| 538 | fmt == chars_format::fixed ? nullptr : "eE"); |
| 539 | #ifndef __cpp_exceptions |
| 540 | if ((last - first) > buffer_resource::guaranteed_capacity()) |
| 541 | { |
| 542 | // The character sequence is still too large for the buffer. |
| 543 | // Allocation failure could terminate the process, |
| 544 | // so just return an error via the fmt parameter. |
| 545 | fmt = chars_format{}; |
| 546 | return nullptr; |
| 547 | } |
| 548 | #endif |
| 549 | } |
| 550 | buf.assign(first, last); |
| 551 | ptr = buf.data() + neg; |
| 552 | } |
| 553 | |
| 554 | // "A non-empty sequence of decimal digits" or |
| 555 | // "A non-empty sequence of hexadecimal digits" |
| 556 | size_t len = std::strspn(ptr, digits); |
| 557 | // "possibly containing a radix character," |
| 558 | if (ptr[len] == '.') |
| 559 | { |
| 560 | const size_t len2 = std::strspn(ptr + len + 1, digits); |
| 561 | if (len + len2) |
| 562 | ptr += len + 1 + len2; |
| 563 | else |
| 564 | return nullptr; |
| 565 | } |
| 566 | else if (len == 0) [[unlikely]] |
| 567 | return nullptr; |
| 568 | else |
| 569 | ptr += len; |
| 570 | |
| 571 | if (fmt == chars_format::fixed) |
| 572 | { |
| 573 | // Truncate the string to stop strtod parsing past this point. |
| 574 | *ptr = '\0'; |
| 575 | } |
| 576 | else if (fmt == chars_format::scientific) |
| 577 | { |
| 578 | // Check for required exponent part which starts with 'e' or 'E' |
| 579 | if (*ptr != 'e' && *ptr != 'E') |
| 580 | return nullptr; |
| 581 | // then an optional plus or minus sign |
| 582 | const int sign = (ptr[1] == '-' || ptr[1] == '+'); |
| 583 | // then a nonempty sequence of decimal digits |
| 584 | if (!std::memchr(dec_digits, (unsigned char)ptr[1+sign], 10)) |
| 585 | return nullptr; |
| 586 | } |
| 587 | else if (fmt == chars_format::general) |
| 588 | { |
| 589 | if (*ptr == 'x' || *ptr == 'X') |
| 590 | *ptr = '\0'; |
| 591 | } |
| 592 | |
| 593 | return buf.c_str(); |
| 594 | } |
| 595 | |
| 596 | // RAII type to change and restore the locale. |
| 597 | struct auto_locale |
| 598 | { |
| 599 | #if _GLIBCXX_HAVE_USELOCALE |
| 600 | // When we have uselocale we can change the current thread's locale. |
| 601 | const locale_t loc; |
| 602 | locale_t orig; |
| 603 | |
| 604 | auto_locale() |
| 605 | : loc(::newlocale(LC_ALL_MASK, "C", (locale_t)0)) |
| 606 | { |
| 607 | if (loc) |
| 608 | orig = ::uselocale(loc); |
| 609 | else |
| 610 | ec = errc{errno}; |
| 611 | } |
| 612 | |
| 613 | ~auto_locale() |
| 614 | { |
| 615 | if (loc) |
| 616 | { |
| 617 | ::uselocale(orig); |
| 618 | ::freelocale(loc); |
| 619 | } |
| 620 | } |
| 621 | #else |
| 622 | // Otherwise, we can't change the locale and so strtod can't be used. |
| 623 | auto_locale() = delete; |
| 624 | #endif |
| 625 | |
| 626 | explicit operator bool() const noexcept { return ec == errc{}; } |
| 627 | |
| 628 | errc ec{}; |
| 629 | |
| 630 | auto_locale(const auto_locale&) = delete; |
| 631 | auto_locale& operator=(const auto_locale&) = delete; |
| 632 | }; |
| 633 | |
| 634 | // RAII type to change and restore the floating-point environment. |
| 635 | struct auto_ferounding |
| 636 | { |
| 637 | #if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST) |
| 638 | const int rounding = std::fegetround(); |
| 639 | |
| 640 | auto_ferounding() |
| 641 | { |
| 642 | if (rounding != FE_TONEAREST) |
| 643 | std::fesetround(FE_TONEAREST); |
| 644 | } |
| 645 | |
| 646 | ~auto_ferounding() |
| 647 | { |
| 648 | if (rounding != FE_TONEAREST) |
| 649 | std::fesetround(rounding); |
| 650 | } |
| 651 | #else |
| 652 | auto_ferounding() = default; |
| 653 | #endif |
| 654 | |
| 655 | auto_ferounding(const auto_ferounding&) = delete; |
| 656 | auto_ferounding& operator=(const auto_ferounding&) = delete; |
| 657 | }; |
| 658 | |
| 659 | // Convert the NTBS `str` to a floating-point value of type `T`. |
| 660 | // If `str` cannot be converted, `value` is unchanged and `0` is returned. |
| 661 | // Otherwise, let N be the number of characters consumed from `str`. |
| 662 | // On success `value` is set to the converted value and N is returned. |
| 663 | // If the converted value is out of range, `value` is unchanged and |
| 664 | // -N is returned. |
| 665 | template<typename T> |
| 666 | ptrdiff_t |
| 667 | from_chars_impl(const char* str, T& value, errc& ec) noexcept |
| 668 | { |
| 669 | auto_locale loc; |
| 670 | |
| 671 | if (loc) |
| 672 | { |
| 673 | auto_ferounding rounding; |
| 674 | const int save_errno = errno; |
| 675 | errno = 0; |
| 676 | char* endptr; |
| 677 | T tmpval; |
| 678 | #if _GLIBCXX_USE_C99_STDLIB |
| 679 | if constexpr (is_same_v<T, float>) |
| 680 | tmpval = std::strtof(str, &endptr); |
| 681 | else if constexpr (is_same_v<T, double>) |
| 682 | tmpval = std::strtod(str, &endptr); |
| 683 | else if constexpr (is_same_v<T, long double>) |
| 684 | tmpval = std::strtold(str, &endptr); |
| 685 | # ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 686 | else if constexpr (is_same_v<T, __ieee128>) |
| 687 | tmpval = __strtoieee128(str, &endptr); |
| 688 | # elif defined(USE_STRTOF128_FOR_FROM_CHARS) |
| 689 | else if constexpr (is_same_v<T, _Float128>) |
| 690 | { |
| 691 | #ifndef _GLIBCXX_HAVE_FLOAT128_MATH |
| 692 | if (&__strtof128 == nullptr) |
| 693 | tmpval = _Float128(std::strtold(str, &endptr)); |
| 694 | else |
| 695 | #endif |
| 696 | tmpval = __strtof128(str, &endptr); |
| 697 | } |
| 698 | # endif |
| 699 | #else |
| 700 | tmpval = std::strtod(str, &endptr); |
| 701 | #endif |
| 702 | const int conv_errno = std::__exchange(errno, save_errno); |
| 703 | |
| 704 | const ptrdiff_t n = endptr - str; |
| 705 | if (conv_errno == ERANGE) [[unlikely]] |
| 706 | { |
| 707 | if (__builtin_isinf(tmpval)) // overflow |
| 708 | ec = errc::result_out_of_range; |
| 709 | else if (tmpval == 0) // underflow (LWG 3081 wants to set value = tmpval here) |
| 710 | ec = errc::result_out_of_range; |
| 711 | else // denormal value |
| 712 | { |
| 713 | value = tmpval; |
| 714 | ec = errc(); |
| 715 | } |
| 716 | } |
| 717 | else if (n) |
| 718 | { |
| 719 | value = tmpval; |
| 720 | ec = errc(); |
| 721 | } |
| 722 | return n; |
| 723 | } |
| 724 | else |
| 725 | ec = loc.ec; |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | inline from_chars_result |
| 731 | make_result(const char* str, ptrdiff_t n, chars_format fmt, errc ec) noexcept |
| 732 | { |
| 733 | from_chars_result result = { str, ec }; |
| 734 | if (n != 0) |
| 735 | { |
| 736 | if (fmt == chars_format::hex) |
| 737 | n -= 2; // correct for the "0x" inserted into the pattern |
| 738 | result.ptr += n; |
| 739 | } |
| 740 | else if (fmt == chars_format{}) [[unlikely]] |
| 741 | { |
| 742 | // FIXME: the standard does not allow this result. |
| 743 | ec = errc::not_enough_memory; |
| 744 | } |
| 745 | return result; |
| 746 | } |
| 747 | |
| 748 | #if ! _GLIBCXX_USE_CXX11_ABI |
| 749 | inline bool |
| 750 | reserve_string(std::string& s) noexcept |
| 751 | { |
| 752 | __try |
| 753 | { |
| 754 | s.reserve(buffer_resource::guaranteed_capacity()); |
| 755 | } |
| 756 | __catch (const std::bad_alloc&) |
| 757 | { |
| 758 | return false; |
| 759 | } |
| 760 | return true; |
| 761 | } |
| 762 | #endif |
| 763 | |
| 764 | template<typename T> |
| 765 | from_chars_result |
| 766 | from_chars_strtod(const char* first, const char* last, T& value, |
| 767 | chars_format fmt) noexcept |
| 768 | { |
| 769 | errc ec = errc::invalid_argument; |
| 770 | #if _GLIBCXX_USE_CXX11_ABI |
| 771 | buffer_resource mr; |
| 772 | pmr::string buf(&mr); |
| 773 | #else |
| 774 | string buf; |
| 775 | if (!reserve_string(buf)) |
| 776 | return make_result(first, 0, {}, ec); |
| 777 | #endif |
| 778 | size_t len = 0; |
| 779 | __try |
| 780 | { |
| 781 | if (const char* pat = pattern(first, last, fmt, buf)) [[likely]] |
| 782 | len = from_chars_impl(pat, value, ec); |
| 783 | } |
| 784 | __catch (const std::bad_alloc&) |
| 785 | { |
| 786 | fmt = chars_format{}; |
| 787 | } |
| 788 | return make_result(first, len, fmt, ec); |
| 789 | } |
| 790 | #endif // USE_STRTOD_FOR_FROM_CHARS |
| 791 | |
| 792 | #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 |
| 793 | // Return true iff [FIRST,LAST) begins with PREFIX, ignoring case. |
| 794 | // PREFIX is assumed to not contain any uppercase letters. |
| 795 | bool |
| 796 | starts_with_ci(const char* first, const char* last, string_view prefix) |
| 797 | { |
| 798 | __glibcxx_requires_valid_range(first, last); |
| 799 | |
| 800 | // A lookup table that maps uppercase letters to lowercase and |
| 801 | // is otherwise the identity mapping. |
| 802 | static constexpr auto upper_to_lower_table = [] { |
| 803 | constexpr unsigned char lower_letters[27] = "abcdefghijklmnopqrstuvwxyz"; |
| 804 | constexpr unsigned char upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 805 | std::array<unsigned char, (1u << __CHAR_BIT__)> table = {}; |
| 806 | for (unsigned i = 0; i < table.size(); ++i) |
| 807 | table[i] = i; |
| 808 | for (unsigned i = 0; i < 26; ++i) |
| 809 | table[upper_letters[i]] = lower_letters[i]; |
| 810 | return table; |
| 811 | }(); |
| 812 | |
| 813 | if (last - first < static_cast<ptrdiff_t>(prefix.length())) |
| 814 | return false; |
| 815 | |
| 816 | for (const unsigned char pch : prefix) |
| 817 | { |
| 818 | // __glibcxx_assert(pch == upper_to_lower_table[pch]); |
| 819 | const unsigned char ch = *first; |
| 820 | if (ch != pch && upper_to_lower_table[ch] != pch) |
| 821 | return false; |
| 822 | ++first; |
| 823 | } |
| 824 | |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | // An implementation of hexadecimal float parsing for binary32/64. |
| 829 | template<typename T> |
| 830 | from_chars_result |
| 831 | __floating_from_chars_hex(const char* first, const char* last, T& value) |
| 832 | { |
| 833 | using uint_t = conditional_t<is_same_v<T, float>, uint32_t, |
| 834 | conditional_t<is_same_v<T, double>, uint64_t, |
| 835 | uint16_t>>; |
| 836 | #if USE_LIB_FAST_FLOAT |
| 837 | constexpr int mantissa_bits |
| 838 | = fast_float::binary_format<T>::mantissa_explicit_bits(); |
| 839 | constexpr int exponent_bits |
| 840 | = is_same_v<T, double> ? 11 |
| 841 | : is_same_v<T, fast_float::floating_type_float16_t> ? 5 : 8; |
| 842 | #else |
| 843 | constexpr int mantissa_bits = is_same_v<T, float> ? 23 : 52; |
| 844 | constexpr int exponent_bits = is_same_v<T, float> ? 8 : 11; |
| 845 | #endif |
| 846 | constexpr int exponent_bias = (1 << (exponent_bits - 1)) - 1; |
| 847 | |
| 848 | __glibcxx_requires_valid_range(first, last); |
| 849 | if (first == last) |
| 850 | return {first, errc::invalid_argument}; |
| 851 | |
| 852 | // Consume the sign bit. |
| 853 | const char* const orig_first = first; |
| 854 | bool sign_bit = false; |
| 855 | if (*first == '-') |
| 856 | { |
| 857 | sign_bit = true; |
| 858 | ++first; |
| 859 | } |
| 860 | |
| 861 | // Handle "inf", "infinity", "NaN" and variants thereof. |
| 862 | if (first != last) |
| 863 | if (*first == 'i' || *first == 'I' || *first == 'n' || *first == 'N') [[unlikely]] |
| 864 | { |
| 865 | if (starts_with_ci(first, last, "inf"sv)) |
| 866 | { |
| 867 | first += strlen("inf"); |
| 868 | if (starts_with_ci(first, last, "inity"sv)) |
| 869 | first += strlen("inity"); |
| 870 | |
| 871 | if constexpr (is_same_v<T, float> || is_same_v<T, double>) |
| 872 | { |
| 873 | uint_t result = 0; |
| 874 | result |= sign_bit; |
| 875 | result <<= exponent_bits; |
| 876 | result |= (1ull << exponent_bits) - 1; |
| 877 | result <<= mantissa_bits; |
| 878 | memcpy(&value, &result, sizeof(result)); |
| 879 | } |
| 880 | else |
| 881 | { |
| 882 | // float +/-Inf. |
| 883 | uint32_t result = 0x7F800000 | (sign_bit ? 0x80000000U : 0); |
| 884 | memcpy(value.x, &result, sizeof(result)); |
| 885 | } |
| 886 | |
| 887 | return {first, errc{}}; |
| 888 | } |
| 889 | else if (starts_with_ci(first, last, "nan")) |
| 890 | { |
| 891 | first += strlen("nan"); |
| 892 | |
| 893 | if (first != last && *first == '(') |
| 894 | { |
| 895 | // Tentatively consume the '(' as we look for an optional |
| 896 | // n-char-sequence followed by a ')'. |
| 897 | const char* const fallback_first = first; |
| 898 | for (;;) |
| 899 | { |
| 900 | ++first; |
| 901 | if (first == last) |
| 902 | { |
| 903 | first = fallback_first; |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | char ch = *first; |
| 908 | if (ch == ')') |
| 909 | { |
| 910 | ++first; |
| 911 | break; |
| 912 | } |
| 913 | else if (ch == '_' |
| 914 | || __detail::__from_chars_alnum_to_val(ch) < 127) |
| 915 | continue; |
| 916 | else |
| 917 | { |
| 918 | first = fallback_first; |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | // We make the implementation-defined decision of ignoring the |
| 925 | // sign bit and the n-char-sequence when assembling the NaN. |
| 926 | if constexpr (is_same_v<T, float> || is_same_v<T, double>) |
| 927 | { |
| 928 | uint_t result = 0; |
| 929 | result <<= exponent_bits; |
| 930 | result |= (1ull << exponent_bits) - 1; |
| 931 | result <<= mantissa_bits; |
| 932 | result |= (1ull << (mantissa_bits - 1)) | 1; |
| 933 | memcpy(&value, &result, sizeof(result)); |
| 934 | } |
| 935 | else |
| 936 | { |
| 937 | // float qNaN. |
| 938 | uint32_t result = 0x7FC00001; |
| 939 | memcpy(value.x, &result, sizeof(result)); |
| 940 | } |
| 941 | |
| 942 | return {first, errc{}}; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | // Consume all insignificant leading zeros in the whole part of the |
| 947 | // mantissa. |
| 948 | bool seen_hexit = false; |
| 949 | while (first != last && *first == '0') |
| 950 | { |
| 951 | seen_hexit = true; |
| 952 | ++first; |
| 953 | } |
| 954 | |
| 955 | // Now consume the rest of the written mantissa, populating MANTISSA with |
| 956 | // the first MANTISSA_BITS+k significant bits of the written mantissa, where |
| 957 | // 1 <= k <= 4 is the bit width of the leading significant written hexit. |
| 958 | // |
| 959 | // Examples: |
| 960 | // After parsing "1.2f3", MANTISSA is 0x12f30000000000 (bit_width=52+1). |
| 961 | // After parsing ".0000f0e", MANTISSA is 0xf0e00000000000 (bit_width=52+4). |
| 962 | // After parsing ".1234567890abcd8", MANTISSA is 0x1234567890abcd (bit_width=52+1) |
| 963 | // and MIDPOINT_BIT is true (and NONZERO_TAIL is false). |
| 964 | uint_t mantissa = 0; |
| 965 | int mantissa_idx = mantissa_bits; // The current bit index into MANTISSA |
| 966 | // into which we'll write the next hexit. |
| 967 | int exponent_adjustment = 0; // How much we'd have to adjust the written |
| 968 | // exponent in order to represent the mantissa |
| 969 | // in scientific form h.hhhhhhhhhhhhh. |
| 970 | bool midpoint_bit = false; // Whether the MANTISSA_BITS+k+1 significant |
| 971 | // bit is set in the written mantissa. |
| 972 | bool nonzero_tail = false; // Whether some bit thereafter is set in the |
| 973 | // written mantissa. |
| 974 | bool seen_decimal_point = false; |
| 975 | for (; first != last; ++first) |
| 976 | { |
| 977 | char ch = *first; |
| 978 | if (ch == '.' && !seen_decimal_point) |
| 979 | { |
| 980 | seen_decimal_point = true; |
| 981 | continue; |
| 982 | } |
| 983 | |
| 984 | int hexit = __detail::__from_chars_alnum_to_val(ch); |
| 985 | if (hexit >= 16) |
| 986 | break; |
| 987 | seen_hexit = true; |
| 988 | |
| 989 | if (!seen_decimal_point && mantissa != 0) |
| 990 | exponent_adjustment += 4; |
| 991 | else if (seen_decimal_point && mantissa == 0) |
| 992 | { |
| 993 | exponent_adjustment -= 4; |
| 994 | if (hexit == 0x0) |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | if (mantissa_idx >= 0) |
| 999 | mantissa |= uint_t(hexit) << mantissa_idx; |
| 1000 | else if (mantissa_idx >= -4) |
| 1001 | { |
| 1002 | if constexpr (is_same_v<T, float> |
| 1003 | #if USE_LIB_FAST_FLOAT |
| 1004 | || is_same_v<T, |
| 1005 | fast_float::floating_type_bfloat16_t> |
| 1006 | #endif |
| 1007 | ) |
| 1008 | { |
| 1009 | __glibcxx_assert(mantissa_idx == -1); |
| 1010 | mantissa |= hexit >> 1; |
| 1011 | midpoint_bit = (hexit & 0b0001) != 0; |
| 1012 | } |
| 1013 | else if constexpr (is_same_v<T, double>) |
| 1014 | { |
| 1015 | __glibcxx_assert(mantissa_idx == -4); |
| 1016 | midpoint_bit = (hexit & 0b1000) != 0; |
| 1017 | nonzero_tail = (hexit & 0b0111) != 0; |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | __glibcxx_assert(mantissa_idx == -2); |
| 1022 | mantissa |= hexit >> 2; |
| 1023 | midpoint_bit = (hexit & 0b0010) != 0; |
| 1024 | nonzero_tail = (hexit & 0b0001) != 0; |
| 1025 | } |
| 1026 | } |
| 1027 | else |
| 1028 | nonzero_tail |= (hexit != 0x0); |
| 1029 | |
| 1030 | mantissa_idx -= 4; |
| 1031 | } |
| 1032 | if (mantissa != 0) |
| 1033 | __glibcxx_assert(__bit_width(mantissa) >= mantissa_bits + 1 |
| 1034 | && __bit_width(mantissa) <= mantissa_bits + 4); |
| 1035 | else |
| 1036 | __glibcxx_assert(!midpoint_bit && !nonzero_tail); |
| 1037 | |
| 1038 | if (!seen_hexit) |
| 1039 | // If we haven't seen any hexit at this point, the parse failed. |
| 1040 | return {orig_first, errc::invalid_argument}; |
| 1041 | |
| 1042 | // Parse the written exponent. |
| 1043 | int written_exponent = 0; |
| 1044 | if (first != last && (*first == 'p' || *first == 'P')) |
| 1045 | { |
| 1046 | // Tentatively consume the 'p' and try to parse a decimal number. |
| 1047 | const char* const fallback_first = first; |
| 1048 | ++first; |
| 1049 | if (first != last && *first == '+') |
| 1050 | ++first; |
| 1051 | from_chars_result fcr = from_chars(first, last, written_exponent, 10); |
| 1052 | if (fcr.ptr == first) |
| 1053 | // The parse failed, so undo consuming the 'p' and carry on as if the |
| 1054 | // exponent was omitted (i.e. is 0). |
| 1055 | first = fallback_first; |
| 1056 | else |
| 1057 | { |
| 1058 | first = fcr.ptr; |
| 1059 | if (mantissa != 0 && fcr.ec == errc::result_out_of_range) |
| 1060 | // Punt on very large exponents for now. FIXME |
| 1061 | return {first, errc::result_out_of_range}; |
| 1062 | } |
| 1063 | } |
| 1064 | int biased_exponent = written_exponent + exponent_bias; |
| 1065 | if (exponent_adjustment != 0) |
| 1066 | // The mantissa wasn't written in scientific form. Adjust the exponent |
| 1067 | // so that we may assume scientific form. |
| 1068 | // |
| 1069 | // Examples; |
| 1070 | // For input "a.bcp5", EXPONENT_ADJUSTMENT would be 0 since this |
| 1071 | // written mantissa is already in scientific form. |
| 1072 | // For input "ab.cp5", EXPONENT_ADJUSTMENT would be 4 since the |
| 1073 | // scientific form is "a.bcp9". |
| 1074 | // For input 0.0abcp5", EXPONENT_ADJUSTMENT would be -8 since the |
| 1075 | // scientific form is "a.bcp-3". |
| 1076 | biased_exponent += exponent_adjustment; |
| 1077 | |
| 1078 | // Shifts the mantissa to the right by AMOUNT while updating |
| 1079 | // BIASED_EXPONENT, MIDPOINT_BIT and NONZERO_TAIL accordingly. |
| 1080 | auto shift_mantissa = [&] (int amount) { |
| 1081 | __glibcxx_assert(amount >= 0); |
| 1082 | if (amount > mantissa_bits + 1) |
| 1083 | { |
| 1084 | // Shifting the mantissa by an amount greater than its precision. |
| 1085 | nonzero_tail |= midpoint_bit; |
| 1086 | nonzero_tail |= mantissa != 0; |
| 1087 | midpoint_bit = false; |
| 1088 | mantissa = 0; |
| 1089 | biased_exponent += amount; |
| 1090 | } |
| 1091 | else if (amount != 0) |
| 1092 | { |
| 1093 | nonzero_tail |= midpoint_bit; |
| 1094 | nonzero_tail |= (mantissa & ((1ull << (amount - 1)) - 1)) != 0; |
| 1095 | midpoint_bit = (mantissa & (1ull << (amount - 1))) != 0; |
| 1096 | mantissa >>= amount; |
| 1097 | biased_exponent += amount; |
| 1098 | } |
| 1099 | }; |
| 1100 | |
| 1101 | if (mantissa != 0) |
| 1102 | { |
| 1103 | // If the leading hexit is not '1', shift MANTISSA to make it so. |
| 1104 | // This normalizes input like "4.08p0" into "1.02p2". |
| 1105 | const unsigned leading_hexit = mantissa >> mantissa_bits; |
| 1106 | const int leading_hexit_width = __bit_width(leading_hexit); // FIXME: optimize? |
| 1107 | __glibcxx_assert(leading_hexit_width >= 1 && leading_hexit_width <= 4); |
| 1108 | shift_mantissa(leading_hexit_width - 1); |
| 1109 | // After this adjustment, we can assume the leading hexit is '1'. |
| 1110 | __glibcxx_assert((mantissa >> mantissa_bits) == 0x1); |
| 1111 | } |
| 1112 | |
| 1113 | if (biased_exponent <= 0) |
| 1114 | { |
| 1115 | // This number is too small to be represented as a normal number, so |
| 1116 | // try for a subnormal number by shifting the mantissa sufficiently. |
| 1117 | // We need to shift by 1 more than -BIASED_EXPONENT because the leading |
| 1118 | // mantissa bit is omitted in the representation of a normal number but |
| 1119 | // not in a subnormal number. |
| 1120 | shift_mantissa(-biased_exponent + 1); |
| 1121 | __glibcxx_assert(!(mantissa & (1ull << mantissa_bits))); |
| 1122 | __glibcxx_assert(biased_exponent == 1); |
| 1123 | biased_exponent = 0; |
| 1124 | } |
| 1125 | |
| 1126 | // Perform round-to-nearest, tie-to-even rounding according to |
| 1127 | // MIDPOINT_BIT and NONZERO_TAIL. |
| 1128 | if (midpoint_bit && (nonzero_tail || (mantissa % 2) != 0)) |
| 1129 | { |
| 1130 | // Rounding away from zero. |
| 1131 | ++mantissa; |
| 1132 | midpoint_bit = false; |
| 1133 | nonzero_tail = false; |
| 1134 | |
| 1135 | // Deal with a couple of corner cases after rounding. |
| 1136 | if (mantissa == (1ull << mantissa_bits)) |
| 1137 | { |
| 1138 | // We rounded the subnormal number 1.fffffffffffff...p-1023 |
| 1139 | // up to the normal number 1p-1022. |
| 1140 | __glibcxx_assert(biased_exponent == 0); |
| 1141 | ++biased_exponent; |
| 1142 | } |
| 1143 | else if (mantissa & (1ull << (mantissa_bits + 1))) |
| 1144 | { |
| 1145 | // We rounded the normal number 1.fffffffffffff8pN (with maximal |
| 1146 | // mantissa) up to to 1p(N+1). |
| 1147 | mantissa >>= 1; |
| 1148 | ++biased_exponent; |
| 1149 | } |
| 1150 | } |
| 1151 | else |
| 1152 | { |
| 1153 | // Rounding toward zero. |
| 1154 | |
| 1155 | if (mantissa == 0 && (midpoint_bit || nonzero_tail)) |
| 1156 | { |
| 1157 | // A nonzero number that rounds to zero is unrepresentable. |
| 1158 | __glibcxx_assert(biased_exponent == 0); |
| 1159 | return {first, errc::result_out_of_range}; |
| 1160 | } |
| 1161 | |
| 1162 | midpoint_bit = false; |
| 1163 | nonzero_tail = false; |
| 1164 | } |
| 1165 | |
| 1166 | if (mantissa != 0 && biased_exponent >= (1 << exponent_bits) - 1) |
| 1167 | // The exponent of this number is too large to be representable. |
| 1168 | return {first, errc::result_out_of_range}; |
| 1169 | |
| 1170 | uint_t result = 0; |
| 1171 | if (mantissa == 0) |
| 1172 | { |
| 1173 | // Assemble a (possibly signed) zero. |
| 1174 | if (sign_bit) |
| 1175 | result |= 1ull << (exponent_bits + mantissa_bits); |
| 1176 | } |
| 1177 | else |
| 1178 | { |
| 1179 | // Assemble a nonzero normal or subnormal value. |
| 1180 | result |= sign_bit; |
| 1181 | result <<= exponent_bits; |
| 1182 | result |= biased_exponent; |
| 1183 | result <<= mantissa_bits; |
| 1184 | result |= mantissa & ((1ull << mantissa_bits) - 1); |
| 1185 | // The implicit leading mantissa bit is set iff the number is normal. |
| 1186 | __glibcxx_assert(((mantissa & (1ull << mantissa_bits)) != 0) |
| 1187 | == (biased_exponent != 0)); |
| 1188 | } |
| 1189 | if constexpr (is_same_v<T, float> || is_same_v<T, double>) |
| 1190 | memcpy(&value, &result, sizeof(result)); |
| 1191 | #if USE_LIB_FAST_FLOAT |
| 1192 | else if constexpr (is_same_v<T, fast_float::floating_type_bfloat16_t>) |
| 1193 | { |
| 1194 | uint32_t res = uint32_t{result} << 16; |
| 1195 | memcpy(value.x, &res, sizeof(res)); |
| 1196 | } |
| 1197 | else |
| 1198 | { |
| 1199 | // Otherwise float16_t which needs to be converted to float32_t. |
| 1200 | uint32_t res; |
| 1201 | if ((result & 0x7FFF) == 0) |
| 1202 | res = uint32_t{result} << 16; // +/-0.0f16 |
| 1203 | else if ((result & 0x7C00) == 0) |
| 1204 | { // denormal |
| 1205 | unsigned n = (std::numeric_limits<unsigned int>::digits |
| 1206 | - __builtin_clz (result & 0x3FF) - 1); |
| 1207 | res = uint32_t{result} & 0x3FF & ~(uint32_t{1} << n); |
| 1208 | res <<= 23 - n; |
| 1209 | res |= (((uint32_t{n} + 0x67) << 23) |
| 1210 | | ((uint32_t{result} & 0x8000) << 16)); |
| 1211 | } |
| 1212 | else |
| 1213 | res = (((uint32_t{result} & 0x3FF) << 13) |
| 1214 | | ((((uint32_t{result} >> 10) & 0x1F) + 0x70) << 23) |
| 1215 | | ((uint32_t{result} & 0x8000) << 16)); |
| 1216 | memcpy(value.x, &res, sizeof(res)); |
| 1217 | } |
| 1218 | #endif |
| 1219 | |
| 1220 | return {first, errc{}}; |
| 1221 | } |
| 1222 | #endif // _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 |
| 1223 | |
| 1224 | } // namespace |
| 1225 | |
| 1226 | #if USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS |
| 1227 | |
| 1228 | from_chars_result |
| 1229 | from_chars(const char* first, const char* last, float& value, |
| 1230 | chars_format fmt) noexcept |
| 1231 | { |
| 1232 | #if USE_LIB_FAST_FLOAT |
| 1233 | if (fmt == chars_format::hex) |
| 1234 | return __floating_from_chars_hex(first, last, value); |
| 1235 | else |
| 1236 | return fast_float::from_chars(first, last, value, fmt); |
| 1237 | #else |
| 1238 | return from_chars_strtod(first, last, value, fmt); |
| 1239 | #endif |
| 1240 | } |
| 1241 | |
| 1242 | from_chars_result |
| 1243 | from_chars(const char* first, const char* last, double& value, |
| 1244 | chars_format fmt) noexcept |
| 1245 | { |
| 1246 | #if USE_LIB_FAST_FLOAT |
| 1247 | if (fmt == chars_format::hex) |
| 1248 | return __floating_from_chars_hex(first, last, value); |
| 1249 | else |
| 1250 | return fast_float::from_chars(first, last, value, fmt); |
| 1251 | #else |
| 1252 | return from_chars_strtod(first, last, value, fmt); |
| 1253 | #endif |
| 1254 | } |
| 1255 | |
| 1256 | from_chars_result |
| 1257 | from_chars(const char* first, const char* last, long double& value, |
| 1258 | chars_format fmt) noexcept |
| 1259 | { |
| 1260 | #if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__ || !defined USE_STRTOD_FOR_FROM_CHARS |
| 1261 | // Either long double is the same as double, or we can't use strtold. |
| 1262 | // In the latter case, this might give an incorrect result (e.g. values |
| 1263 | // out of range of double give an error, even if they fit in long double). |
| 1264 | double dbl_value; |
| 1265 | from_chars_result result; |
| 1266 | if (fmt == chars_format::hex) |
| 1267 | result = __floating_from_chars_hex(first, last, dbl_value); |
| 1268 | else |
| 1269 | result = fast_float::from_chars(first, last, dbl_value, fmt); |
| 1270 | if (result.ec == errc{}) |
| 1271 | value = dbl_value; |
| 1272 | return result; |
| 1273 | #else |
| 1274 | return from_chars_strtod(first, last, value, fmt); |
| 1275 | #endif |
| 1276 | } |
| 1277 | |
| 1278 | #if USE_LIB_FAST_FLOAT |
| 1279 | // Entrypoints for 16-bit floats. |
| 1280 | [[gnu::cold]] from_chars_result |
| 1281 | __from_chars_float16_t(const char* first, const char* last, float& value, |
| 1282 | chars_format fmt) noexcept |
| 1283 | { |
| 1284 | struct fast_float::floating_type_float16_t val{ &value, 0 }; |
| 1285 | if (fmt == chars_format::hex) |
| 1286 | return __floating_from_chars_hex(first, last, val); |
| 1287 | else |
| 1288 | return fast_float::from_chars_16(first, last, val, fmt); |
| 1289 | } |
| 1290 | |
| 1291 | [[gnu::cold]] from_chars_result |
| 1292 | __from_chars_bfloat16_t(const char* first, const char* last, float& value, |
| 1293 | chars_format fmt) noexcept |
| 1294 | { |
| 1295 | struct fast_float::floating_type_bfloat16_t val{ &value, 0 }; |
| 1296 | if (fmt == chars_format::hex) |
| 1297 | return __floating_from_chars_hex(first, last, val); |
| 1298 | else |
| 1299 | return fast_float::from_chars_16(first, last, val, fmt); |
| 1300 | } |
| 1301 | #endif |
| 1302 | |
| 1303 | #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT |
| 1304 | // Make std::from_chars for 64-bit long double an alias for the overload |
| 1305 | // for double. |
| 1306 | extern "C" from_chars_result |
| 1307 | _ZSt10from_charsPKcS0_ReSt12chars_format(const char* first, const char* last, |
| 1308 | long double& value, |
| 1309 | chars_format fmt) noexcept |
| 1310 | __attribute__((alias ("_ZSt10from_charsPKcS0_RdSt12chars_format"))); |
| 1311 | #endif |
| 1312 | |
| 1313 | #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 1314 | from_chars_result |
| 1315 | from_chars(const char* first, const char* last, __ieee128& value, |
| 1316 | chars_format fmt) noexcept |
| 1317 | { |
| 1318 | // fast_float doesn't support IEEE binary128 format, but we can use strtold. |
| 1319 | return from_chars_strtod(first, last, value, fmt); |
| 1320 | } |
| 1321 | |
| 1322 | extern "C" from_chars_result |
| 1323 | _ZSt10from_charsPKcS0_RDF128_St12chars_format(const char* first, |
| 1324 | const char* last, |
| 1325 | __ieee128& value, |
| 1326 | chars_format fmt) noexcept |
| 1327 | __attribute__((alias ("_ZSt10from_charsPKcS0_Ru9__ieee128St12chars_format"))); |
| 1328 | #elif defined(USE_STRTOF128_FOR_FROM_CHARS) |
| 1329 | // Overload for _Float128 is not defined inline in <charconv>, define it here. |
| 1330 | from_chars_result |
| 1331 | from_chars(const char* first, const char* last, _Float128& value, |
| 1332 | chars_format fmt) noexcept |
| 1333 | { |
| 1334 | // fast_float doesn't support IEEE binary128 format, but we can use strtold. |
| 1335 | return from_chars_strtod(first, last, value, fmt); |
| 1336 | } |
| 1337 | #endif |
| 1338 | |
| 1339 | #endif // USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS |
| 1340 | |
| 1341 | _GLIBCXX_END_NAMESPACE_VERSION |
| 1342 | } // namespace std |