| 1 |
#ifndef FASTFLOAT_PARSE_NUMBER_H |
| 2 |
#define FASTFLOAT_PARSE_NUMBER_H |
| 3 |
|
| 4 |
#include "ascii_number.h" |
| 5 |
#include "decimal_to_binary.h" |
| 6 |
#include "digit_comparison.h" |
| 7 |
#include "float_common.h" |
| 8 |
|
| 9 |
#include <cmath> |
| 10 |
#include <cstring> |
| 11 |
#include <limits> |
| 12 |
#include <system_error> |
| 13 |
|
| 14 |
namespace fast_float { |
| 15 |
|
| 16 |
namespace detail { |
| 17 |
/** |
| 18 |
* Special case +inf, -inf, nan, infinity, -infinity. |
| 19 |
* The case comparisons could be made much faster given that we know that the |
| 20 |
* strings a null-free and fixed. |
| 21 |
**/ |
| 22 |
template <typename T, typename UC> |
| 23 |
from_chars_result_t<UC> |
| 24 |
FASTFLOAT_CONSTEXPR14 parse_infnan(UC const *first, UC const *last, |
| 25 |
T &value, chars_format fmt) noexcept { |
| 26 |
from_chars_result_t<UC> answer{}; |
| 27 |
answer.ptr = first; |
| 28 |
answer.ec = std::errc(); // be optimistic |
| 29 |
// assume first < last, so dereference without checks; |
| 30 |
bool const minusSign = (*first == UC('-')); |
| 31 |
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here |
| 32 |
if ((*first == UC('-')) || |
| 33 |
(uint64_t(fmt & chars_format::allow_leading_plus) && |
| 34 |
(*first == UC('+')))) { |
| 35 |
++first; |
| 36 |
} |
| 37 |
if (last - first >= 3) { |
| 38 |
if (fastfloat_strncasecmp(first, str_const_nan<UC>(), 3)) { |
| 39 |
answer.ptr = (first += 3); |
| 40 |
value = minusSign ? -std::numeric_limits<T>::quiet_NaN() |
| 41 |
: std::numeric_limits<T>::quiet_NaN(); |
| 42 |
// Check for possible nan(n-char-seq-opt), C++17 20.19.3.7, |
| 43 |
// C11 7.20.1.3.3. At least MSVC produces nan(ind) and nan(snan). |
| 44 |
if (first != last && *first == UC('(')) { |
| 45 |
for (UC const *ptr = first + 1; ptr != last; ++ptr) { |
| 46 |
if (*ptr == UC(')')) { |
| 47 |
answer.ptr = ptr + 1; // valid nan(n-char-seq-opt) |
| 48 |
break; |
| 49 |
} else if (!((UC('a') <= *ptr && *ptr <= UC('z')) || |
| 50 |
(UC('A') <= *ptr && *ptr <= UC('Z')) || |
| 51 |
(UC('0') <= *ptr && *ptr <= UC('9')) || *ptr == UC('_'))) |
| 52 |
break; // forbidden char, not nan(n-char-seq-opt) |
| 53 |
} |
| 54 |
} |
| 55 |
return answer; |
| 56 |
} |
| 57 |
if (fastfloat_strncasecmp(first, str_const_inf<UC>(), 3)) { |
| 58 |
if ((last - first >= 8) && |
| 59 |
fastfloat_strncasecmp(first + 3, str_const_inf<UC>() + 3, 5)) { |
| 60 |
answer.ptr = first + 8; |
| 61 |
} else { |
| 62 |
answer.ptr = first + 3; |
| 63 |
} |
| 64 |
value = minusSign ? -std::numeric_limits<T>::infinity() |
| 65 |
: std::numeric_limits<T>::infinity(); |
| 66 |
return answer; |
| 67 |
} |
| 68 |
} |
| 69 |
answer.ec = std::errc::invalid_argument; |
| 70 |
return answer; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns true if the floating-pointing rounding mode is to 'nearest'. |
| 75 |
* It is the default on most system. This function is meant to be inexpensive. |
| 76 |
* Credit : @mwalcott3 |
| 77 |
*/ |
| 78 |
fastfloat_really_inline bool rounds_to_nearest() noexcept { |
| 79 |
// https://lemire.me/blog/2020/06/26/gcc-not-nearest/ |
| 80 |
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) |
| 81 |
return false; |
| 82 |
#endif |
| 83 |
// See |
| 84 |
// A fast function to check your floating-point rounding mode |
| 85 |
// https://lemire.me/blog/2022/11/16/a-fast-function-to-check-your-floating-point-rounding-mode/ |
| 86 |
// |
| 87 |
// This function is meant to be equivalent to : |
| 88 |
// prior: #include <cfenv> |
| 89 |
// return fegetround() == FE_TONEAREST; |
| 90 |
// However, it is expected to be much faster than the fegetround() |
| 91 |
// function call. |
| 92 |
// |
| 93 |
// The volatile keyword prevents the compiler from computing the function |
| 94 |
// at compile-time. |
| 95 |
// There might be other ways to prevent compile-time optimizations (e.g., |
| 96 |
// asm). The value does not need to be std::numeric_limits<float>::min(), any |
| 97 |
// small value so that 1 + x should round to 1 would do (after accounting for |
| 98 |
// excess precision, as in 387 instructions). |
| 99 |
static float volatile fmin = std::numeric_limits<float>::min(); |
| 100 |
float fmini = fmin; // we copy it so that it gets loaded at most once. |
| 101 |
// |
| 102 |
// Explanation: |
| 103 |
// Only when fegetround() == FE_TONEAREST do we have that |
| 104 |
// fmin + 1.0f == 1.0f - fmin. |
| 105 |
// |
| 106 |
// FE_UPWARD: |
| 107 |
// fmin + 1.0f > 1 |
| 108 |
// 1.0f - fmin == 1 |
| 109 |
// |
| 110 |
// FE_DOWNWARD or FE_TOWARDZERO: |
| 111 |
// fmin + 1.0f == 1 |
| 112 |
// 1.0f - fmin < 1 |
| 113 |
// |
| 114 |
// Note: This may fail to be accurate if fast-math has been |
| 115 |
// enabled, as rounding conventions may not apply. |
| 116 |
#ifdef FASTFLOAT_VISUAL_STUDIO |
| 117 |
#pragma warning(push) |
| 118 |
// todo: is there a VS warning? |
| 119 |
// see |
| 120 |
// https://stackoverflow.com/questions/46079446/is-there-a-warning-for-floating-point-equality-checking-in-visual-studio-2013 |
| 121 |
#elif defined(__clang__) |
| 122 |
#pragma clang diagnostic push |
| 123 |
#pragma clang diagnostic ignored "-Wfloat-equal" |
| 124 |
#elif defined(__GNUC__) |
| 125 |
#pragma GCC diagnostic push |
| 126 |
#pragma GCC diagnostic ignored "-Wfloat-equal" |
| 127 |
#endif |
| 128 |
return (fmini + 1.0f == 1.0f - fmini); |
| 129 |
#ifdef FASTFLOAT_VISUAL_STUDIO |
| 130 |
#pragma warning(pop) |
| 131 |
#elif defined(__clang__) |
| 132 |
#pragma clang diagnostic pop |
| 133 |
#elif defined(__GNUC__) |
| 134 |
#pragma GCC diagnostic pop |
| 135 |
#endif |
| 136 |
} |
| 137 |
|
| 138 |
} // namespace detail |
| 139 |
|
| 140 |
template <typename T> struct from_chars_caller { |
| 141 |
template <typename UC> |
| 142 |
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC> |
| 143 |
call(UC const *first, UC const *last, T &value, |
| 144 |
parse_options_t<UC> options) noexcept { |
| 145 |
return from_chars_advanced(first, last, value, options); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
#ifdef __STDCPP_FLOAT32_T__ |
| 150 |
template <> struct from_chars_caller<std::float32_t> { |
| 151 |
template <typename UC> |
| 152 |
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC> |
| 153 |
call(UC const *first, UC const *last, std::float32_t &value, |
| 154 |
parse_options_t<UC> options) noexcept { |
| 155 |
// if std::float32_t is defined, and we are in C++23 mode; macro set for |
| 156 |
// float32; set value to float due to equivalence between float and |
| 157 |
// float32_t |
| 158 |
float val; |
| 159 |
auto ret = from_chars_advanced(first, last, val, options); |
| 160 |
value = val; |
| 161 |
return ret; |
| 162 |
} |
| 163 |
}; |
| 164 |
#endif |
| 165 |
|
| 166 |
#ifdef __STDCPP_FLOAT64_T__ |
| 167 |
template <> struct from_chars_caller<std::float64_t> { |
| 168 |
template <typename UC> |
| 169 |
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC> |
| 170 |
call(UC const *first, UC const *last, std::float64_t &value, |
| 171 |
parse_options_t<UC> options) noexcept { |
| 172 |
// if std::float64_t is defined, and we are in C++23 mode; macro set for |
| 173 |
// float64; set value as double due to equivalence between double and |
| 174 |
// float64_t |
| 175 |
double val; |
| 176 |
auto ret = from_chars_advanced(first, last, val, options); |
| 177 |
value = val; |
| 178 |
return ret; |
| 179 |
} |
| 180 |
}; |
| 181 |
#endif |
| 182 |
|
| 183 |
template <typename T, typename UC, typename> |
| 184 |
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC> |
| 185 |
from_chars(UC const *first, UC const *last, T &value, |
| 186 |
chars_format fmt /*= chars_format::general*/) noexcept { |
| 187 |
return from_chars_caller<T>::call(first, last, value, |
| 188 |
parse_options_t<UC>(fmt)); |
| 189 |
} |
| 190 |
|
| 191 |
template <typename T> |
| 192 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool |
| 193 |
clinger_fast_path_impl(uint64_t mantissa, int64_t exponent, bool is_negative, |
| 194 |
T &value) noexcept { |
| 195 |
// The implementation of the Clinger's fast path is convoluted because |
| 196 |
// we want round-to-nearest in all cases, irrespective of the rounding mode |
| 197 |
// selected on the thread. |
| 198 |
// We proceed optimistically, assuming that detail::rounds_to_nearest() |
| 199 |
// returns true. |
| 200 |
if (binary_format<T>::min_exponent_fast_path() <= exponent && |
| 201 |
exponent <= binary_format<T>::max_exponent_fast_path()) { |
| 202 |
// Unfortunately, the conventional Clinger's fast path is only possible |
| 203 |
// when the system rounds to the nearest float. |
| 204 |
// |
| 205 |
// We expect the next branch to almost always be selected. |
| 206 |
// We could check it first (before the previous branch), but |
| 207 |
// there might be performance advantages at having the check |
| 208 |
// be last. |
| 209 |
if (!cpp20_and_in_constexpr() && detail::rounds_to_nearest()) { |
| 210 |
// We have that fegetround() == FE_TONEAREST. |
| 211 |
// Next is Clinger's fast path. |
| 212 |
if (mantissa <= binary_format<T>::max_mantissa_fast_path()) { |
| 213 |
value = T(mantissa); |
| 214 |
if (exponent < 0) { |
| 215 |
value = value / binary_format<T>::exact_power_of_ten(-exponent); |
| 216 |
} else { |
| 217 |
value = value * binary_format<T>::exact_power_of_ten(exponent); |
| 218 |
} |
| 219 |
if (is_negative) { |
| 220 |
value = -value; |
| 221 |
} |
| 222 |
return true; |
| 223 |
} |
| 224 |
} else { |
| 225 |
// We do not have that fegetround() == FE_TONEAREST. |
| 226 |
// Next is a modified Clinger's fast path, inspired by Jakub Jelínek's |
| 227 |
// proposal |
| 228 |
if (exponent >= 0 && |
| 229 |
mantissa <= binary_format<T>::max_mantissa_fast_path(exponent)) { |
| 230 |
#if defined(__clang__) || defined(FASTFLOAT_32BIT) |
| 231 |
// Clang may map 0 to -0.0 when fegetround() == FE_DOWNWARD |
| 232 |
if (mantissa == 0) { |
| 233 |
value = is_negative ? T(-0.) : T(0.); |
| 234 |
return true; |
| 235 |
} |
| 236 |
#endif |
| 237 |
value = T(mantissa) * binary_format<T>::exact_power_of_ten(exponent); |
| 238 |
if (is_negative) { |
| 239 |
value = -value; |
| 240 |
} |
| 241 |
return true; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* This function overload takes parsed_number_string_t structure that is created |
| 250 |
* and populated either by from_chars_advanced function taking chars range and |
| 251 |
* parsing options or other parsing custom function implemented by user. |
| 252 |
*/ |
| 253 |
template <typename T, typename UC> |
| 254 |
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC> |
| 255 |
from_chars_advanced(parsed_number_string_t<UC> &pns, T &value) noexcept { |
| 256 |
static_assert(is_supported_float_type<T>::value, |
| 257 |
"only some floating-point types are supported"); |
| 258 |
static_assert(is_supported_char_type<UC>::value, |
| 259 |
"only char, wchar_t, char16_t and char32_t are supported"); |
| 260 |
|
| 261 |
from_chars_result_t<UC> answer; |
| 262 |
|
| 263 |
answer.ec = std::errc(); // be optimistic |
| 264 |
answer.ptr = pns.lastmatch; |
| 265 |
|
| 266 |
if (!pns.too_many_digits && |
| 267 |
clinger_fast_path_impl(pns.mantissa, pns.exponent, pns.negative, value)) |
| 268 |
return answer; |
| 269 |
|
| 270 |
adjusted_mantissa am = |
| 271 |
compute_float<binary_format<T>>(pns.exponent, pns.mantissa); |
| 272 |
if (pns.too_many_digits && am.power2 >= 0) { |
| 273 |
if (am != compute_float<binary_format<T>>(pns.exponent, pns.mantissa + 1)) { |
| 274 |
am = compute_error<binary_format<T>>(pns.exponent, pns.mantissa); |
| 275 |
} |
| 276 |
} |
| 277 |
// If we called compute_float<binary_format<T>>(pns.exponent, pns.mantissa) |
| 278 |
// and we have an invalid power (am.power2 < 0), then we need to go the long |
| 279 |
// way around again. This is very uncommon. |
| 280 |
if (am.power2 < 0) { |
| 281 |
am = digit_comp<T>(pns, am); |
| 282 |
} |
| 283 |
to_float(pns.negative, am, value); |
| 284 |
// Test for over/underflow. |
| 285 |
if ((pns.mantissa != 0 && am.mantissa == 0 && am.power2 == 0) || |
| 286 |
am.power2 == binary_format<T>::infinite_power()) { |
| 287 |
answer.ec = std::errc::result_out_of_range; |
| 288 |
} |
| 289 |
return answer; |
| 290 |
} |
| 291 |
|
| 292 |
template <typename T, typename UC> |
| 293 |
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC> |
| 294 |
from_chars_float_advanced(UC const *first, UC const *last, T &value, |
| 295 |
parse_options_t<UC> options) noexcept { |
| 296 |
|
| 297 |
static_assert(is_supported_float_type<T>::value, |
| 298 |
"only some floating-point types are supported"); |
| 299 |
static_assert(is_supported_char_type<UC>::value, |
| 300 |
"only char, wchar_t, char16_t and char32_t are supported"); |
| 301 |
|
| 302 |
chars_format const fmt = detail::adjust_for_feature_macros(options.format); |
| 303 |
|
| 304 |
from_chars_result_t<UC> answer; |
| 305 |
if (uint64_t(fmt & chars_format::skip_white_space)) { |
| 306 |
while ((first != last) && fast_float::is_space(*first)) { |
| 307 |
first++; |
| 308 |
} |
| 309 |
} |
| 310 |
if (first == last) { |
| 311 |
answer.ec = std::errc::invalid_argument; |
| 312 |
answer.ptr = first; |
| 313 |
return answer; |
| 314 |
} |
| 315 |
parsed_number_string_t<UC> pns = |
| 316 |
uint64_t(fmt & detail::basic_json_fmt) |
| 317 |
? parse_number_string<true, UC>(first, last, options) |
| 318 |
: parse_number_string<false, UC>(first, last, options); |
| 319 |
if (!pns.valid) { |
| 320 |
if (uint64_t(fmt & chars_format::no_infnan)) { |
| 321 |
answer.ec = std::errc::invalid_argument; |
| 322 |
answer.ptr = first; |
| 323 |
return answer; |
| 324 |
} else { |
| 325 |
return detail::parse_infnan(first, last, value, fmt); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// call overload that takes parsed_number_string_t directly. |
| 330 |
return from_chars_advanced(pns, value); |
| 331 |
} |
| 332 |
|
| 333 |
template <typename T, typename UC, typename> |
| 334 |
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC> |
| 335 |
from_chars(UC const *first, UC const *last, T &value, int base) noexcept { |
| 336 |
|
| 337 |
static_assert(is_supported_integer_type<T>::value, |
| 338 |
"only integer types are supported"); |
| 339 |
static_assert(is_supported_char_type<UC>::value, |
| 340 |
"only char, wchar_t, char16_t and char32_t are supported"); |
| 341 |
|
| 342 |
parse_options_t<UC> options; |
| 343 |
options.base = base; |
| 344 |
return from_chars_advanced(first, last, value, options); |
| 345 |
} |
| 346 |
|
| 347 |
FASTFLOAT_CONSTEXPR20 inline double |
| 348 |
integer_times_pow10(uint64_t mantissa, int decimal_exponent) noexcept { |
| 349 |
double value; |
| 350 |
if (clinger_fast_path_impl(mantissa, decimal_exponent, false, value)) |
| 351 |
return value; |
| 352 |
|
| 353 |
adjusted_mantissa am = |
| 354 |
compute_float<binary_format<double>>(decimal_exponent, mantissa); |
| 355 |
to_float(false, am, value); |
| 356 |
return value; |
| 357 |
} |
| 358 |
|
| 359 |
FASTFLOAT_CONSTEXPR20 inline double |
| 360 |
integer_times_pow10(int64_t mantissa, int decimal_exponent) noexcept { |
| 361 |
const bool is_negative = mantissa < 0; |
| 362 |
const uint64_t m = static_cast<uint64_t>(is_negative ? -mantissa : mantissa); |
| 363 |
|
| 364 |
double value; |
| 365 |
if (clinger_fast_path_impl(m, decimal_exponent, is_negative, value)) |
| 366 |
return value; |
| 367 |
|
| 368 |
adjusted_mantissa am = |
| 369 |
compute_float<binary_format<double>>(decimal_exponent, m); |
| 370 |
to_float(is_negative, am, value); |
| 371 |
return value; |
| 372 |
} |
| 373 |
|
| 374 |
// the following overloads are here to avoid surprising ambiguity for int, |
| 375 |
// unsigned, etc. |
| 376 |
template <typename Int> |
| 377 |
FASTFLOAT_CONSTEXPR20 inline typename std::enable_if< |
| 378 |
std::is_integral<Int>::value && !std::is_signed<Int>::value, double>::type |
| 379 |
integer_times_pow10(Int mantissa, int decimal_exponent) noexcept { |
| 380 |
return integer_times_pow10(static_cast<uint64_t>(mantissa), decimal_exponent); |
| 381 |
} |
| 382 |
|
| 383 |
template <typename Int> |
| 384 |
FASTFLOAT_CONSTEXPR20 inline typename std::enable_if< |
| 385 |
std::is_integral<Int>::value && std::is_signed<Int>::value, double>::type |
| 386 |
integer_times_pow10(Int mantissa, int decimal_exponent) noexcept { |
| 387 |
return integer_times_pow10(static_cast<int64_t>(mantissa), decimal_exponent); |
| 388 |
} |
| 389 |
|
| 390 |
template <typename T, typename UC> |
| 391 |
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC> |
| 392 |
from_chars_int_advanced(UC const *first, UC const *last, T &value, |
| 393 |
parse_options_t<UC> options) noexcept { |
| 394 |
|
| 395 |
static_assert(is_supported_integer_type<T>::value, |
| 396 |
"only integer types are supported"); |
| 397 |
static_assert(is_supported_char_type<UC>::value, |
| 398 |
"only char, wchar_t, char16_t and char32_t are supported"); |
| 399 |
|
| 400 |
chars_format const fmt = detail::adjust_for_feature_macros(options.format); |
| 401 |
int const base = options.base; |
| 402 |
|
| 403 |
from_chars_result_t<UC> answer; |
| 404 |
if (uint64_t(fmt & chars_format::skip_white_space)) { |
| 405 |
while ((first != last) && fast_float::is_space(*first)) { |
| 406 |
first++; |
| 407 |
} |
| 408 |
} |
| 409 |
if (first == last || base < 2 || base > 36) { |
| 410 |
answer.ec = std::errc::invalid_argument; |
| 411 |
answer.ptr = first; |
| 412 |
return answer; |
| 413 |
} |
| 414 |
|
| 415 |
return parse_int_string(first, last, value, options); |
| 416 |
} |
| 417 |
|
| 418 |
template <size_t TypeIx> struct from_chars_advanced_caller { |
| 419 |
static_assert(TypeIx > 0, "unsupported type"); |
| 420 |
}; |
| 421 |
|
| 422 |
template <> struct from_chars_advanced_caller<1> { |
| 423 |
template <typename T, typename UC> |
| 424 |
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC> |
| 425 |
call(UC const *first, UC const *last, T &value, |
| 426 |
parse_options_t<UC> options) noexcept { |
| 427 |
return from_chars_float_advanced(first, last, value, options); |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
template <> struct from_chars_advanced_caller<2> { |
| 432 |
template <typename T, typename UC> |
| 433 |
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC> |
| 434 |
call(UC const *first, UC const *last, T &value, |
| 435 |
parse_options_t<UC> options) noexcept { |
| 436 |
return from_chars_int_advanced(first, last, value, options); |
| 437 |
} |
| 438 |
}; |
| 439 |
|
| 440 |
template <typename T, typename UC> |
| 441 |
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC> |
| 442 |
from_chars_advanced(UC const *first, UC const *last, T &value, |
| 443 |
parse_options_t<UC> options) noexcept { |
| 444 |
return from_chars_advanced_caller< |
| 445 |
size_t(is_supported_float_type<T>::value) + |
| 446 |
2 * size_t(is_supported_integer_type<T>::value)>::call(first, last, value, |
| 447 |
options); |
| 448 |
} |
| 449 |
|
| 450 |
} // namespace fast_float |
| 451 |
|
| 452 |
#endif |