| 1 |
#ifndef FASTFLOAT_FLOAT_COMMON_H |
| 2 |
#define FASTFLOAT_FLOAT_COMMON_H |
| 3 |
|
| 4 |
#include <cfloat> |
| 5 |
#include <cstdint> |
| 6 |
#include <cassert> |
| 7 |
#include <cstring> |
| 8 |
#include <limits> |
| 9 |
#include <type_traits> |
| 10 |
#include <system_error> |
| 11 |
#ifdef __has_include |
| 12 |
#if __has_include(<stdfloat>) && (__cplusplus > 202002L || (defined(_MSVC_LANG) && (_MSVC_LANG > 202002L))) |
| 13 |
#include <stdfloat> |
| 14 |
#endif |
| 15 |
#endif |
| 16 |
#include "constexpr_feature_detect.h" |
| 17 |
|
| 18 |
#define FASTFLOAT_VERSION_MAJOR 8 |
| 19 |
#define FASTFLOAT_VERSION_MINOR 1 |
| 20 |
#define FASTFLOAT_VERSION_PATCH 0 |
| 21 |
|
| 22 |
#define FASTFLOAT_STRINGIZE_IMPL(x) #x |
| 23 |
#define FASTFLOAT_STRINGIZE(x) FASTFLOAT_STRINGIZE_IMPL(x) |
| 24 |
|
| 25 |
#define FASTFLOAT_VERSION_STR \ |
| 26 |
FASTFLOAT_STRINGIZE(FASTFLOAT_VERSION_MAJOR) \ |
| 27 |
"." FASTFLOAT_STRINGIZE(FASTFLOAT_VERSION_MINOR) "." FASTFLOAT_STRINGIZE( \ |
| 28 |
FASTFLOAT_VERSION_PATCH) |
| 29 |
|
| 30 |
#define FASTFLOAT_VERSION \ |
| 31 |
(FASTFLOAT_VERSION_MAJOR * 10000 + FASTFLOAT_VERSION_MINOR * 100 + \ |
| 32 |
FASTFLOAT_VERSION_PATCH) |
| 33 |
|
| 34 |
namespace fast_float { |
| 35 |
|
| 36 |
enum class chars_format : uint64_t; |
| 37 |
|
| 38 |
namespace detail { |
| 39 |
constexpr chars_format basic_json_fmt = chars_format(1 << 5); |
| 40 |
constexpr chars_format basic_fortran_fmt = chars_format(1 << 6); |
| 41 |
} // namespace detail |
| 42 |
|
| 43 |
enum class chars_format : uint64_t { |
| 44 |
scientific = 1 << 0, |
| 45 |
fixed = 1 << 2, |
| 46 |
hex = 1 << 3, |
| 47 |
no_infnan = 1 << 4, |
| 48 |
// RFC 8259: https://datatracker.ietf.org/doc/html/rfc8259#section-6 |
| 49 |
json = uint64_t(detail::basic_json_fmt) | fixed | scientific | no_infnan, |
| 50 |
// Extension of RFC 8259 where, e.g., "inf" and "nan" are allowed. |
| 51 |
json_or_infnan = uint64_t(detail::basic_json_fmt) | fixed | scientific, |
| 52 |
fortran = uint64_t(detail::basic_fortran_fmt) | fixed | scientific, |
| 53 |
general = fixed | scientific, |
| 54 |
allow_leading_plus = 1 << 7, |
| 55 |
skip_white_space = 1 << 8, |
| 56 |
}; |
| 57 |
|
| 58 |
template <typename UC> struct from_chars_result_t { |
| 59 |
UC const *ptr; |
| 60 |
std::errc ec; |
| 61 |
|
| 62 |
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2497r0.html |
| 63 |
constexpr explicit operator bool() const noexcept { |
| 64 |
return ec == std::errc(); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
using from_chars_result = from_chars_result_t<char>; |
| 69 |
|
| 70 |
template <typename UC> struct parse_options_t { |
| 71 |
constexpr explicit parse_options_t(chars_format fmt = chars_format::general, |
| 72 |
UC dot = UC('.'), int b = 10) |
| 73 |
: format(fmt), decimal_point(dot), base(b) {} |
| 74 |
|
| 75 |
/** Which number formats are accepted */ |
| 76 |
chars_format format; |
| 77 |
/** The character used as decimal point */ |
| 78 |
UC decimal_point; |
| 79 |
/** The base used for integers */ |
| 80 |
int base; |
| 81 |
}; |
| 82 |
|
| 83 |
using parse_options = parse_options_t<char>; |
| 84 |
|
| 85 |
} // namespace fast_float |
| 86 |
|
| 87 |
#if FASTFLOAT_HAS_BIT_CAST |
| 88 |
#include <bit> |
| 89 |
#endif |
| 90 |
|
| 91 |
#if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 92 |
defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) || \ |
| 93 |
defined(__MINGW64__) || defined(__s390x__) || \ |
| 94 |
(defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || \ |
| 95 |
defined(__PPC64LE__)) || \ |
| 96 |
defined(__loongarch64) || (defined(__riscv) && __riscv_xlen == 64)) |
| 97 |
#define FASTFLOAT_64BIT 1 |
| 98 |
#elif (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 99 |
defined(__arm__) || defined(_M_ARM) || defined(__ppc__) || \ |
| 100 |
defined(__MINGW32__) || defined(__EMSCRIPTEN__) || \ |
| 101 |
(defined(__riscv) && __riscv_xlen == 32)) |
| 102 |
#define FASTFLOAT_32BIT 1 |
| 103 |
#else |
| 104 |
// Need to check incrementally, since SIZE_MAX is a size_t, avoid overflow. |
| 105 |
// We can never tell the register width, but the SIZE_MAX is a good |
| 106 |
// approximation. UINTPTR_MAX and INTPTR_MAX are optional, so avoid them for max |
| 107 |
// portability. |
| 108 |
#if SIZE_MAX == 0xffff |
| 109 |
#error Unknown platform (16-bit, unsupported) |
| 110 |
#elif SIZE_MAX == 0xffffffff |
| 111 |
#define FASTFLOAT_32BIT 1 |
| 112 |
#elif SIZE_MAX == 0xffffffffffffffff |
| 113 |
#define FASTFLOAT_64BIT 1 |
| 114 |
#else |
| 115 |
#error Unknown platform (not 32-bit, not 64-bit?) |
| 116 |
#endif |
| 117 |
#endif |
| 118 |
|
| 119 |
#if ((defined(_WIN32) || defined(_WIN64)) && !defined(__clang__)) || \ |
| 120 |
(defined(_M_ARM64) && !defined(__MINGW32__)) |
| 121 |
#include <intrin.h> |
| 122 |
#endif |
| 123 |
|
| 124 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 125 |
#define FASTFLOAT_VISUAL_STUDIO 1 |
| 126 |
#endif |
| 127 |
|
| 128 |
#if defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__ |
| 129 |
#define FASTFLOAT_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 130 |
#elif defined _WIN32 |
| 131 |
#define FASTFLOAT_IS_BIG_ENDIAN 0 |
| 132 |
#else |
| 133 |
#if defined(__APPLE__) || defined(__FreeBSD__) |
| 134 |
#include <machine/endian.h> |
| 135 |
#elif defined(sun) || defined(__sun) |
| 136 |
#include <sys/byteorder.h> |
| 137 |
#elif defined(__MVS__) |
| 138 |
#include <sys/endian.h> |
| 139 |
#else |
| 140 |
#ifdef __has_include |
| 141 |
#if __has_include(<endian.h>) |
| 142 |
#include <endian.h> |
| 143 |
#endif //__has_include(<endian.h>) |
| 144 |
#endif //__has_include |
| 145 |
#endif |
| 146 |
# |
| 147 |
#ifndef __BYTE_ORDER__ |
| 148 |
// safe choice |
| 149 |
#define FASTFLOAT_IS_BIG_ENDIAN 0 |
| 150 |
#endif |
| 151 |
# |
| 152 |
#ifndef __ORDER_LITTLE_ENDIAN__ |
| 153 |
// safe choice |
| 154 |
#define FASTFLOAT_IS_BIG_ENDIAN 0 |
| 155 |
#endif |
| 156 |
# |
| 157 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 158 |
#define FASTFLOAT_IS_BIG_ENDIAN 0 |
| 159 |
#else |
| 160 |
#define FASTFLOAT_IS_BIG_ENDIAN 1 |
| 161 |
#endif |
| 162 |
#endif |
| 163 |
|
| 164 |
#if defined(__SSE2__) || (defined(FASTFLOAT_VISUAL_STUDIO) && \ |
| 165 |
(defined(_M_AMD64) || defined(_M_X64) || \ |
| 166 |
(defined(_M_IX86_FP) && _M_IX86_FP == 2))) |
| 167 |
#define FASTFLOAT_SSE2 1 |
| 168 |
#endif |
| 169 |
|
| 170 |
#if defined(__aarch64__) || defined(_M_ARM64) |
| 171 |
#define FASTFLOAT_NEON 1 |
| 172 |
#endif |
| 173 |
|
| 174 |
#if defined(FASTFLOAT_SSE2) || defined(FASTFLOAT_NEON) |
| 175 |
#define FASTFLOAT_HAS_SIMD 1 |
| 176 |
#endif |
| 177 |
|
| 178 |
#if defined(__GNUC__) |
| 179 |
// disable -Wcast-align=strict (GCC only) |
| 180 |
#define FASTFLOAT_SIMD_DISABLE_WARNINGS \ |
| 181 |
_Pragma("GCC diagnostic push") \ |
| 182 |
_Pragma("GCC diagnostic ignored \"-Wcast-align\"") |
| 183 |
#else |
| 184 |
#define FASTFLOAT_SIMD_DISABLE_WARNINGS |
| 185 |
#endif |
| 186 |
|
| 187 |
#if defined(__GNUC__) |
| 188 |
#define FASTFLOAT_SIMD_RESTORE_WARNINGS _Pragma("GCC diagnostic pop") |
| 189 |
#else |
| 190 |
#define FASTFLOAT_SIMD_RESTORE_WARNINGS |
| 191 |
#endif |
| 192 |
|
| 193 |
#ifdef FASTFLOAT_VISUAL_STUDIO |
| 194 |
#define fastfloat_really_inline __forceinline |
| 195 |
#else |
| 196 |
#define fastfloat_really_inline inline __attribute__((always_inline)) |
| 197 |
#endif |
| 198 |
|
| 199 |
#ifndef FASTFLOAT_ASSERT |
| 200 |
#define FASTFLOAT_ASSERT(x) \ |
| 201 |
{ ((void)(x)); } |
| 202 |
#endif |
| 203 |
|
| 204 |
#ifndef FASTFLOAT_DEBUG_ASSERT |
| 205 |
#define FASTFLOAT_DEBUG_ASSERT(x) \ |
| 206 |
{ ((void)(x)); } |
| 207 |
#endif |
| 208 |
|
| 209 |
// rust style `try!()` macro, or `?` operator |
| 210 |
#define FASTFLOAT_TRY(x) \ |
| 211 |
{ \ |
| 212 |
if (!(x)) \ |
| 213 |
return false; \ |
| 214 |
} |
| 215 |
|
| 216 |
#define FASTFLOAT_ENABLE_IF(...) \ |
| 217 |
typename std::enable_if<(__VA_ARGS__), int>::type |
| 218 |
|
| 219 |
namespace fast_float { |
| 220 |
|
| 221 |
fastfloat_really_inline constexpr bool cpp20_and_in_constexpr() { |
| 222 |
#if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED |
| 223 |
return std::is_constant_evaluated(); |
| 224 |
#else |
| 225 |
return false; |
| 226 |
#endif |
| 227 |
} |
| 228 |
|
| 229 |
template <typename T> |
| 230 |
struct is_supported_float_type |
| 231 |
: std::integral_constant< |
| 232 |
bool, std::is_same<T, double>::value || std::is_same<T, float>::value |
| 233 |
#ifdef __STDCPP_FLOAT64_T__ |
| 234 |
|| std::is_same<T, std::float64_t>::value |
| 235 |
#endif |
| 236 |
#ifdef __STDCPP_FLOAT32_T__ |
| 237 |
|| std::is_same<T, std::float32_t>::value |
| 238 |
#endif |
| 239 |
#ifdef __STDCPP_FLOAT16_T__ |
| 240 |
|| std::is_same<T, std::float16_t>::value |
| 241 |
#endif |
| 242 |
#ifdef __STDCPP_BFLOAT16_T__ |
| 243 |
|| std::is_same<T, std::bfloat16_t>::value |
| 244 |
#endif |
| 245 |
> { |
| 246 |
}; |
| 247 |
|
| 248 |
template <typename T> |
| 249 |
using equiv_uint_t = typename std::conditional< |
| 250 |
sizeof(T) == 1, uint8_t, |
| 251 |
typename std::conditional< |
| 252 |
sizeof(T) == 2, uint16_t, |
| 253 |
typename std::conditional<sizeof(T) == 4, uint32_t, |
| 254 |
uint64_t>::type>::type>::type; |
| 255 |
|
| 256 |
template <typename T> struct is_supported_integer_type : std::is_integral<T> {}; |
| 257 |
|
| 258 |
template <typename UC> |
| 259 |
struct is_supported_char_type |
| 260 |
: std::integral_constant<bool, std::is_same<UC, char>::value || |
| 261 |
std::is_same<UC, wchar_t>::value || |
| 262 |
std::is_same<UC, char16_t>::value || |
| 263 |
std::is_same<UC, char32_t>::value |
| 264 |
#ifdef __cpp_char8_t |
| 265 |
|| std::is_same<UC, char8_t>::value |
| 266 |
#endif |
| 267 |
> { |
| 268 |
}; |
| 269 |
|
| 270 |
// Compares two ASCII strings in a case insensitive manner. |
| 271 |
template <typename UC> |
| 272 |
inline FASTFLOAT_CONSTEXPR14 bool |
| 273 |
fastfloat_strncasecmp(UC const *actual_mixedcase, UC const *expected_lowercase, |
| 274 |
size_t length) { |
| 275 |
for (size_t i = 0; i < length; ++i) { |
| 276 |
UC const actual = actual_mixedcase[i]; |
| 277 |
if ((actual < 256 ? actual | 32 : actual) != expected_lowercase[i]) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
} |
| 281 |
return true; |
| 282 |
} |
| 283 |
|
| 284 |
#ifndef FLT_EVAL_METHOD |
| 285 |
#error "FLT_EVAL_METHOD should be defined, please include cfloat." |
| 286 |
#endif |
| 287 |
|
| 288 |
// a pointer and a length to a contiguous block of memory |
| 289 |
template <typename T> struct span { |
| 290 |
T const *ptr; |
| 291 |
size_t length; |
| 292 |
|
| 293 |
constexpr span(T const *_ptr, size_t _length) : ptr(_ptr), length(_length) {} |
| 294 |
|
| 295 |
constexpr span() : ptr(nullptr), length(0) {} |
| 296 |
|
| 297 |
constexpr size_t len() const noexcept { return length; } |
| 298 |
|
| 299 |
FASTFLOAT_CONSTEXPR14 const T &operator[](size_t index) const noexcept { |
| 300 |
FASTFLOAT_DEBUG_ASSERT(index < length); |
| 301 |
return ptr[index]; |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
struct value128 { |
| 306 |
uint64_t low; |
| 307 |
uint64_t high; |
| 308 |
|
| 309 |
constexpr value128(uint64_t _low, uint64_t _high) : low(_low), high(_high) {} |
| 310 |
|
| 311 |
constexpr value128() : low(0), high(0) {} |
| 312 |
}; |
| 313 |
|
| 314 |
/* Helper C++14 constexpr generic implementation of leading_zeroes */ |
| 315 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int |
| 316 |
leading_zeroes_generic(uint64_t input_num, int last_bit = 0) { |
| 317 |
if (input_num & uint64_t(0xffffffff00000000)) { |
| 318 |
input_num >>= 32; |
| 319 |
last_bit |= 32; |
| 320 |
} |
| 321 |
if (input_num & uint64_t(0xffff0000)) { |
| 322 |
input_num >>= 16; |
| 323 |
last_bit |= 16; |
| 324 |
} |
| 325 |
if (input_num & uint64_t(0xff00)) { |
| 326 |
input_num >>= 8; |
| 327 |
last_bit |= 8; |
| 328 |
} |
| 329 |
if (input_num & uint64_t(0xf0)) { |
| 330 |
input_num >>= 4; |
| 331 |
last_bit |= 4; |
| 332 |
} |
| 333 |
if (input_num & uint64_t(0xc)) { |
| 334 |
input_num >>= 2; |
| 335 |
last_bit |= 2; |
| 336 |
} |
| 337 |
if (input_num & uint64_t(0x2)) { /* input_num >>= 1; */ |
| 338 |
last_bit |= 1; |
| 339 |
} |
| 340 |
return 63 - last_bit; |
| 341 |
} |
| 342 |
|
| 343 |
/* result might be undefined when input_num is zero */ |
| 344 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int |
| 345 |
leading_zeroes(uint64_t input_num) { |
| 346 |
assert(input_num > 0); |
| 347 |
if (cpp20_and_in_constexpr()) { |
| 348 |
return leading_zeroes_generic(input_num); |
| 349 |
} |
| 350 |
#ifdef FASTFLOAT_VISUAL_STUDIO |
| 351 |
#if defined(_M_X64) || defined(_M_ARM64) |
| 352 |
unsigned long leading_zero = 0; |
| 353 |
// Search the mask data from most significant bit (MSB) |
| 354 |
// to least significant bit (LSB) for a set bit (1). |
| 355 |
_BitScanReverse64(&leading_zero, input_num); |
| 356 |
return (int)(63 - leading_zero); |
| 357 |
#else |
| 358 |
return leading_zeroes_generic(input_num); |
| 359 |
#endif |
| 360 |
#else |
| 361 |
return __builtin_clzll(input_num); |
| 362 |
#endif |
| 363 |
} |
| 364 |
|
| 365 |
// slow emulation routine for 32-bit |
| 366 |
fastfloat_really_inline constexpr uint64_t emulu(uint32_t x, uint32_t y) { |
| 367 |
return x * (uint64_t)y; |
| 368 |
} |
| 369 |
|
| 370 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t |
| 371 |
umul128_generic(uint64_t ab, uint64_t cd, uint64_t *hi) { |
| 372 |
uint64_t ad = emulu((uint32_t)(ab >> 32), (uint32_t)cd); |
| 373 |
uint64_t bd = emulu((uint32_t)ab, (uint32_t)cd); |
| 374 |
uint64_t adbc = ad + emulu((uint32_t)ab, (uint32_t)(cd >> 32)); |
| 375 |
uint64_t adbc_carry = (uint64_t)(adbc < ad); |
| 376 |
uint64_t lo = bd + (adbc << 32); |
| 377 |
*hi = emulu((uint32_t)(ab >> 32), (uint32_t)(cd >> 32)) + (adbc >> 32) + |
| 378 |
(adbc_carry << 32) + (uint64_t)(lo < bd); |
| 379 |
return lo; |
| 380 |
} |
| 381 |
|
| 382 |
#ifdef FASTFLOAT_32BIT |
| 383 |
|
| 384 |
// slow emulation routine for 32-bit |
| 385 |
#if !defined(__MINGW64__) |
| 386 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t _umul128(uint64_t ab, |
| 387 |
uint64_t cd, |
| 388 |
uint64_t *hi) { |
| 389 |
return umul128_generic(ab, cd, hi); |
| 390 |
} |
| 391 |
#endif // !__MINGW64__ |
| 392 |
|
| 393 |
#endif // FASTFLOAT_32BIT |
| 394 |
|
| 395 |
// compute 64-bit a*b |
| 396 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 value128 |
| 397 |
full_multiplication(uint64_t a, uint64_t b) { |
| 398 |
if (cpp20_and_in_constexpr()) { |
| 399 |
value128 answer; |
| 400 |
answer.low = umul128_generic(a, b, &answer.high); |
| 401 |
return answer; |
| 402 |
} |
| 403 |
value128 answer; |
| 404 |
#if defined(_M_ARM64) && !defined(__MINGW32__) |
| 405 |
// ARM64 has native support for 64-bit multiplications, no need to emulate |
| 406 |
// But MinGW on ARM64 doesn't have native support for 64-bit multiplications |
| 407 |
answer.high = __umulh(a, b); |
| 408 |
answer.low = a * b; |
| 409 |
#elif defined(FASTFLOAT_32BIT) || \ |
| 410 |
(defined(_WIN64) && !defined(__clang__) && !defined(_M_ARM64)) |
| 411 |
answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64 |
| 412 |
#elif defined(FASTFLOAT_64BIT) && defined(__SIZEOF_INT128__) |
| 413 |
__uint128_t r = ((__uint128_t)a) * b; |
| 414 |
answer.low = uint64_t(r); |
| 415 |
answer.high = uint64_t(r >> 64); |
| 416 |
#else |
| 417 |
answer.low = umul128_generic(a, b, &answer.high); |
| 418 |
#endif |
| 419 |
return answer; |
| 420 |
} |
| 421 |
|
| 422 |
struct adjusted_mantissa { |
| 423 |
uint64_t mantissa{0}; |
| 424 |
int32_t power2{0}; // a negative value indicates an invalid result |
| 425 |
adjusted_mantissa() = default; |
| 426 |
|
| 427 |
constexpr bool operator==(adjusted_mantissa const &o) const { |
| 428 |
return mantissa == o.mantissa && power2 == o.power2; |
| 429 |
} |
| 430 |
|
| 431 |
constexpr bool operator!=(adjusted_mantissa const &o) const { |
| 432 |
return mantissa != o.mantissa || power2 != o.power2; |
| 433 |
} |
| 434 |
}; |
| 435 |
|
| 436 |
// Bias so we can get the real exponent with an invalid adjusted_mantissa. |
| 437 |
constexpr static int32_t invalid_am_bias = -0x8000; |
| 438 |
|
| 439 |
// used for binary_format_lookup_tables<T>::max_mantissa |
| 440 |
constexpr uint64_t constant_55555 = 5 * 5 * 5 * 5 * 5; |
| 441 |
|
| 442 |
template <typename T, typename U = void> struct binary_format_lookup_tables; |
| 443 |
|
| 444 |
template <typename T> struct binary_format : binary_format_lookup_tables<T> { |
| 445 |
using equiv_uint = equiv_uint_t<T>; |
| 446 |
|
| 447 |
static constexpr int mantissa_explicit_bits(); |
| 448 |
static constexpr int minimum_exponent(); |
| 449 |
static constexpr int infinite_power(); |
| 450 |
static constexpr int sign_index(); |
| 451 |
static constexpr int |
| 452 |
min_exponent_fast_path(); // used when fegetround() == FE_TONEAREST |
| 453 |
static constexpr int max_exponent_fast_path(); |
| 454 |
static constexpr int max_exponent_round_to_even(); |
| 455 |
static constexpr int min_exponent_round_to_even(); |
| 456 |
static constexpr uint64_t max_mantissa_fast_path(int64_t power); |
| 457 |
static constexpr uint64_t |
| 458 |
max_mantissa_fast_path(); // used when fegetround() == FE_TONEAREST |
| 459 |
static constexpr int largest_power_of_ten(); |
| 460 |
static constexpr int smallest_power_of_ten(); |
| 461 |
static constexpr T exact_power_of_ten(int64_t power); |
| 462 |
static constexpr size_t max_digits(); |
| 463 |
static constexpr equiv_uint exponent_mask(); |
| 464 |
static constexpr equiv_uint mantissa_mask(); |
| 465 |
static constexpr equiv_uint hidden_bit_mask(); |
| 466 |
}; |
| 467 |
|
| 468 |
template <typename U> struct binary_format_lookup_tables<double, U> { |
| 469 |
static constexpr double powers_of_ten[] = { |
| 470 |
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, |
| 471 |
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22}; |
| 472 |
|
| 473 |
// Largest integer value v so that (5**index * v) <= 1<<53. |
| 474 |
// 0x20000000000000 == 1 << 53 |
| 475 |
static constexpr uint64_t max_mantissa[] = { |
| 476 |
0x20000000000000, |
| 477 |
0x20000000000000 / 5, |
| 478 |
0x20000000000000 / (5 * 5), |
| 479 |
0x20000000000000 / (5 * 5 * 5), |
| 480 |
0x20000000000000 / (5 * 5 * 5 * 5), |
| 481 |
0x20000000000000 / (constant_55555), |
| 482 |
0x20000000000000 / (constant_55555 * 5), |
| 483 |
0x20000000000000 / (constant_55555 * 5 * 5), |
| 484 |
0x20000000000000 / (constant_55555 * 5 * 5 * 5), |
| 485 |
0x20000000000000 / (constant_55555 * 5 * 5 * 5 * 5), |
| 486 |
0x20000000000000 / (constant_55555 * constant_55555), |
| 487 |
0x20000000000000 / (constant_55555 * constant_55555 * 5), |
| 488 |
0x20000000000000 / (constant_55555 * constant_55555 * 5 * 5), |
| 489 |
0x20000000000000 / (constant_55555 * constant_55555 * 5 * 5 * 5), |
| 490 |
0x20000000000000 / (constant_55555 * constant_55555 * constant_55555), |
| 491 |
0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 * 5), |
| 492 |
0x20000000000000 / |
| 493 |
(constant_55555 * constant_55555 * constant_55555 * 5 * 5), |
| 494 |
0x20000000000000 / |
| 495 |
(constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5), |
| 496 |
0x20000000000000 / |
| 497 |
(constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5 * 5), |
| 498 |
0x20000000000000 / |
| 499 |
(constant_55555 * constant_55555 * constant_55555 * constant_55555), |
| 500 |
0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 * |
| 501 |
constant_55555 * 5), |
| 502 |
0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 * |
| 503 |
constant_55555 * 5 * 5), |
| 504 |
0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 * |
| 505 |
constant_55555 * 5 * 5 * 5), |
| 506 |
0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 * |
| 507 |
constant_55555 * 5 * 5 * 5 * 5)}; |
| 508 |
}; |
| 509 |
|
| 510 |
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE |
| 511 |
|
| 512 |
template <typename U> |
| 513 |
constexpr double binary_format_lookup_tables<double, U>::powers_of_ten[]; |
| 514 |
|
| 515 |
template <typename U> |
| 516 |
constexpr uint64_t binary_format_lookup_tables<double, U>::max_mantissa[]; |
| 517 |
|
| 518 |
#endif |
| 519 |
|
| 520 |
template <typename U> struct binary_format_lookup_tables<float, U> { |
| 521 |
static constexpr float powers_of_ten[] = {1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, |
| 522 |
1e6f, 1e7f, 1e8f, 1e9f, 1e10f}; |
| 523 |
|
| 524 |
// Largest integer value v so that (5**index * v) <= 1<<24. |
| 525 |
// 0x1000000 == 1<<24 |
| 526 |
static constexpr uint64_t max_mantissa[] = { |
| 527 |
0x1000000, |
| 528 |
0x1000000 / 5, |
| 529 |
0x1000000 / (5 * 5), |
| 530 |
0x1000000 / (5 * 5 * 5), |
| 531 |
0x1000000 / (5 * 5 * 5 * 5), |
| 532 |
0x1000000 / (constant_55555), |
| 533 |
0x1000000 / (constant_55555 * 5), |
| 534 |
0x1000000 / (constant_55555 * 5 * 5), |
| 535 |
0x1000000 / (constant_55555 * 5 * 5 * 5), |
| 536 |
0x1000000 / (constant_55555 * 5 * 5 * 5 * 5), |
| 537 |
0x1000000 / (constant_55555 * constant_55555), |
| 538 |
0x1000000 / (constant_55555 * constant_55555 * 5)}; |
| 539 |
}; |
| 540 |
|
| 541 |
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE |
| 542 |
|
| 543 |
template <typename U> |
| 544 |
constexpr float binary_format_lookup_tables<float, U>::powers_of_ten[]; |
| 545 |
|
| 546 |
template <typename U> |
| 547 |
constexpr uint64_t binary_format_lookup_tables<float, U>::max_mantissa[]; |
| 548 |
|
| 549 |
#endif |
| 550 |
|
| 551 |
template <> |
| 552 |
inline constexpr int binary_format<double>::min_exponent_fast_path() { |
| 553 |
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) |
| 554 |
return 0; |
| 555 |
#else |
| 556 |
return -22; |
| 557 |
#endif |
| 558 |
} |
| 559 |
|
| 560 |
template <> |
| 561 |
inline constexpr int binary_format<float>::min_exponent_fast_path() { |
| 562 |
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) |
| 563 |
return 0; |
| 564 |
#else |
| 565 |
return -10; |
| 566 |
#endif |
| 567 |
} |
| 568 |
|
| 569 |
template <> |
| 570 |
inline constexpr int binary_format<double>::mantissa_explicit_bits() { |
| 571 |
return 52; |
| 572 |
} |
| 573 |
|
| 574 |
template <> |
| 575 |
inline constexpr int binary_format<float>::mantissa_explicit_bits() { |
| 576 |
return 23; |
| 577 |
} |
| 578 |
|
| 579 |
template <> |
| 580 |
inline constexpr int binary_format<double>::max_exponent_round_to_even() { |
| 581 |
return 23; |
| 582 |
} |
| 583 |
|
| 584 |
template <> |
| 585 |
inline constexpr int binary_format<float>::max_exponent_round_to_even() { |
| 586 |
return 10; |
| 587 |
} |
| 588 |
|
| 589 |
template <> |
| 590 |
inline constexpr int binary_format<double>::min_exponent_round_to_even() { |
| 591 |
return -4; |
| 592 |
} |
| 593 |
|
| 594 |
template <> |
| 595 |
inline constexpr int binary_format<float>::min_exponent_round_to_even() { |
| 596 |
return -17; |
| 597 |
} |
| 598 |
|
| 599 |
template <> inline constexpr int binary_format<double>::minimum_exponent() { |
| 600 |
return -1023; |
| 601 |
} |
| 602 |
|
| 603 |
template <> inline constexpr int binary_format<float>::minimum_exponent() { |
| 604 |
return -127; |
| 605 |
} |
| 606 |
|
| 607 |
template <> inline constexpr int binary_format<double>::infinite_power() { |
| 608 |
return 0x7FF; |
| 609 |
} |
| 610 |
|
| 611 |
template <> inline constexpr int binary_format<float>::infinite_power() { |
| 612 |
return 0xFF; |
| 613 |
} |
| 614 |
|
| 615 |
template <> inline constexpr int binary_format<double>::sign_index() { |
| 616 |
return 63; |
| 617 |
} |
| 618 |
|
| 619 |
template <> inline constexpr int binary_format<float>::sign_index() { |
| 620 |
return 31; |
| 621 |
} |
| 622 |
|
| 623 |
template <> |
| 624 |
inline constexpr int binary_format<double>::max_exponent_fast_path() { |
| 625 |
return 22; |
| 626 |
} |
| 627 |
|
| 628 |
template <> |
| 629 |
inline constexpr int binary_format<float>::max_exponent_fast_path() { |
| 630 |
return 10; |
| 631 |
} |
| 632 |
|
| 633 |
template <> |
| 634 |
inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() { |
| 635 |
return uint64_t(2) << mantissa_explicit_bits(); |
| 636 |
} |
| 637 |
|
| 638 |
template <> |
| 639 |
inline constexpr uint64_t binary_format<float>::max_mantissa_fast_path() { |
| 640 |
return uint64_t(2) << mantissa_explicit_bits(); |
| 641 |
} |
| 642 |
|
| 643 |
// credit: Jakub Jelínek |
| 644 |
#ifdef __STDCPP_FLOAT16_T__ |
| 645 |
template <typename U> struct binary_format_lookup_tables<std::float16_t, U> { |
| 646 |
static constexpr std::float16_t powers_of_ten[] = {1e0f16, 1e1f16, 1e2f16, |
| 647 |
1e3f16, 1e4f16}; |
| 648 |
|
| 649 |
// Largest integer value v so that (5**index * v) <= 1<<11. |
| 650 |
// 0x800 == 1<<11 |
| 651 |
static constexpr uint64_t max_mantissa[] = {0x800, |
| 652 |
0x800 / 5, |
| 653 |
0x800 / (5 * 5), |
| 654 |
0x800 / (5 * 5 * 5), |
| 655 |
0x800 / (5 * 5 * 5 * 5), |
| 656 |
0x800 / (constant_55555)}; |
| 657 |
}; |
| 658 |
|
| 659 |
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE |
| 660 |
|
| 661 |
template <typename U> |
| 662 |
constexpr std::float16_t |
| 663 |
binary_format_lookup_tables<std::float16_t, U>::powers_of_ten[]; |
| 664 |
|
| 665 |
template <typename U> |
| 666 |
constexpr uint64_t |
| 667 |
binary_format_lookup_tables<std::float16_t, U>::max_mantissa[]; |
| 668 |
|
| 669 |
#endif |
| 670 |
|
| 671 |
template <> |
| 672 |
inline constexpr std::float16_t |
| 673 |
binary_format<std::float16_t>::exact_power_of_ten(int64_t power) { |
| 674 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 675 |
return (void)powers_of_ten[0], powers_of_ten[power]; |
| 676 |
} |
| 677 |
|
| 678 |
template <> |
| 679 |
inline constexpr binary_format<std::float16_t>::equiv_uint |
| 680 |
binary_format<std::float16_t>::exponent_mask() { |
| 681 |
return 0x7C00; |
| 682 |
} |
| 683 |
|
| 684 |
template <> |
| 685 |
inline constexpr binary_format<std::float16_t>::equiv_uint |
| 686 |
binary_format<std::float16_t>::mantissa_mask() { |
| 687 |
return 0x03FF; |
| 688 |
} |
| 689 |
|
| 690 |
template <> |
| 691 |
inline constexpr binary_format<std::float16_t>::equiv_uint |
| 692 |
binary_format<std::float16_t>::hidden_bit_mask() { |
| 693 |
return 0x0400; |
| 694 |
} |
| 695 |
|
| 696 |
template <> |
| 697 |
inline constexpr int binary_format<std::float16_t>::max_exponent_fast_path() { |
| 698 |
return 4; |
| 699 |
} |
| 700 |
|
| 701 |
template <> |
| 702 |
inline constexpr int binary_format<std::float16_t>::mantissa_explicit_bits() { |
| 703 |
return 10; |
| 704 |
} |
| 705 |
|
| 706 |
template <> |
| 707 |
inline constexpr uint64_t |
| 708 |
binary_format<std::float16_t>::max_mantissa_fast_path() { |
| 709 |
return uint64_t(2) << mantissa_explicit_bits(); |
| 710 |
} |
| 711 |
|
| 712 |
template <> |
| 713 |
inline constexpr uint64_t |
| 714 |
binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) { |
| 715 |
// caller is responsible to ensure that |
| 716 |
// power >= 0 && power <= 4 |
| 717 |
// |
| 718 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 719 |
return (void)max_mantissa[0], max_mantissa[power]; |
| 720 |
} |
| 721 |
|
| 722 |
template <> |
| 723 |
inline constexpr int binary_format<std::float16_t>::min_exponent_fast_path() { |
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
template <> |
| 728 |
inline constexpr int |
| 729 |
binary_format<std::float16_t>::max_exponent_round_to_even() { |
| 730 |
return 5; |
| 731 |
} |
| 732 |
|
| 733 |
template <> |
| 734 |
inline constexpr int |
| 735 |
binary_format<std::float16_t>::min_exponent_round_to_even() { |
| 736 |
return -22; |
| 737 |
} |
| 738 |
|
| 739 |
template <> |
| 740 |
inline constexpr int binary_format<std::float16_t>::minimum_exponent() { |
| 741 |
return -15; |
| 742 |
} |
| 743 |
|
| 744 |
template <> |
| 745 |
inline constexpr int binary_format<std::float16_t>::infinite_power() { |
| 746 |
return 0x1F; |
| 747 |
} |
| 748 |
|
| 749 |
template <> inline constexpr int binary_format<std::float16_t>::sign_index() { |
| 750 |
return 15; |
| 751 |
} |
| 752 |
|
| 753 |
template <> |
| 754 |
inline constexpr int binary_format<std::float16_t>::largest_power_of_ten() { |
| 755 |
return 4; |
| 756 |
} |
| 757 |
|
| 758 |
template <> |
| 759 |
inline constexpr int binary_format<std::float16_t>::smallest_power_of_ten() { |
| 760 |
return -27; |
| 761 |
} |
| 762 |
|
| 763 |
template <> |
| 764 |
inline constexpr size_t binary_format<std::float16_t>::max_digits() { |
| 765 |
return 22; |
| 766 |
} |
| 767 |
#endif // __STDCPP_FLOAT16_T__ |
| 768 |
|
| 769 |
// credit: Jakub Jelínek |
| 770 |
#ifdef __STDCPP_BFLOAT16_T__ |
| 771 |
template <typename U> struct binary_format_lookup_tables<std::bfloat16_t, U> { |
| 772 |
static constexpr std::bfloat16_t powers_of_ten[] = {1e0bf16, 1e1bf16, 1e2bf16, |
| 773 |
1e3bf16}; |
| 774 |
|
| 775 |
// Largest integer value v so that (5**index * v) <= 1<<8. |
| 776 |
// 0x100 == 1<<8 |
| 777 |
static constexpr uint64_t max_mantissa[] = {0x100, 0x100 / 5, 0x100 / (5 * 5), |
| 778 |
0x100 / (5 * 5 * 5), |
| 779 |
0x100 / (5 * 5 * 5 * 5)}; |
| 780 |
}; |
| 781 |
|
| 782 |
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE |
| 783 |
|
| 784 |
template <typename U> |
| 785 |
constexpr std::bfloat16_t |
| 786 |
binary_format_lookup_tables<std::bfloat16_t, U>::powers_of_ten[]; |
| 787 |
|
| 788 |
template <typename U> |
| 789 |
constexpr uint64_t |
| 790 |
binary_format_lookup_tables<std::bfloat16_t, U>::max_mantissa[]; |
| 791 |
|
| 792 |
#endif |
| 793 |
|
| 794 |
template <> |
| 795 |
inline constexpr std::bfloat16_t |
| 796 |
binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) { |
| 797 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 798 |
return (void)powers_of_ten[0], powers_of_ten[power]; |
| 799 |
} |
| 800 |
|
| 801 |
template <> |
| 802 |
inline constexpr int binary_format<std::bfloat16_t>::max_exponent_fast_path() { |
| 803 |
return 3; |
| 804 |
} |
| 805 |
|
| 806 |
template <> |
| 807 |
inline constexpr binary_format<std::bfloat16_t>::equiv_uint |
| 808 |
binary_format<std::bfloat16_t>::exponent_mask() { |
| 809 |
return 0x7F80; |
| 810 |
} |
| 811 |
|
| 812 |
template <> |
| 813 |
inline constexpr binary_format<std::bfloat16_t>::equiv_uint |
| 814 |
binary_format<std::bfloat16_t>::mantissa_mask() { |
| 815 |
return 0x007F; |
| 816 |
} |
| 817 |
|
| 818 |
template <> |
| 819 |
inline constexpr binary_format<std::bfloat16_t>::equiv_uint |
| 820 |
binary_format<std::bfloat16_t>::hidden_bit_mask() { |
| 821 |
return 0x0080; |
| 822 |
} |
| 823 |
|
| 824 |
template <> |
| 825 |
inline constexpr int binary_format<std::bfloat16_t>::mantissa_explicit_bits() { |
| 826 |
return 7; |
| 827 |
} |
| 828 |
|
| 829 |
template <> |
| 830 |
inline constexpr uint64_t |
| 831 |
binary_format<std::bfloat16_t>::max_mantissa_fast_path() { |
| 832 |
return uint64_t(2) << mantissa_explicit_bits(); |
| 833 |
} |
| 834 |
|
| 835 |
template <> |
| 836 |
inline constexpr uint64_t |
| 837 |
binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) { |
| 838 |
// caller is responsible to ensure that |
| 839 |
// power >= 0 && power <= 3 |
| 840 |
// |
| 841 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 842 |
return (void)max_mantissa[0], max_mantissa[power]; |
| 843 |
} |
| 844 |
|
| 845 |
template <> |
| 846 |
inline constexpr int binary_format<std::bfloat16_t>::min_exponent_fast_path() { |
| 847 |
return 0; |
| 848 |
} |
| 849 |
|
| 850 |
template <> |
| 851 |
inline constexpr int |
| 852 |
binary_format<std::bfloat16_t>::max_exponent_round_to_even() { |
| 853 |
return 3; |
| 854 |
} |
| 855 |
|
| 856 |
template <> |
| 857 |
inline constexpr int |
| 858 |
binary_format<std::bfloat16_t>::min_exponent_round_to_even() { |
| 859 |
return -24; |
| 860 |
} |
| 861 |
|
| 862 |
template <> |
| 863 |
inline constexpr int binary_format<std::bfloat16_t>::minimum_exponent() { |
| 864 |
return -127; |
| 865 |
} |
| 866 |
|
| 867 |
template <> |
| 868 |
inline constexpr int binary_format<std::bfloat16_t>::infinite_power() { |
| 869 |
return 0xFF; |
| 870 |
} |
| 871 |
|
| 872 |
template <> inline constexpr int binary_format<std::bfloat16_t>::sign_index() { |
| 873 |
return 15; |
| 874 |
} |
| 875 |
|
| 876 |
template <> |
| 877 |
inline constexpr int binary_format<std::bfloat16_t>::largest_power_of_ten() { |
| 878 |
return 38; |
| 879 |
} |
| 880 |
|
| 881 |
template <> |
| 882 |
inline constexpr int binary_format<std::bfloat16_t>::smallest_power_of_ten() { |
| 883 |
return -60; |
| 884 |
} |
| 885 |
|
| 886 |
template <> |
| 887 |
inline constexpr size_t binary_format<std::bfloat16_t>::max_digits() { |
| 888 |
return 98; |
| 889 |
} |
| 890 |
#endif // __STDCPP_BFLOAT16_T__ |
| 891 |
|
| 892 |
template <> |
| 893 |
inline constexpr uint64_t |
| 894 |
binary_format<double>::max_mantissa_fast_path(int64_t power) { |
| 895 |
// caller is responsible to ensure that |
| 896 |
// power >= 0 && power <= 22 |
| 897 |
// |
| 898 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 899 |
return (void)max_mantissa[0], max_mantissa[power]; |
| 900 |
} |
| 901 |
|
| 902 |
template <> |
| 903 |
inline constexpr uint64_t |
| 904 |
binary_format<float>::max_mantissa_fast_path(int64_t power) { |
| 905 |
// caller is responsible to ensure that |
| 906 |
// power >= 0 && power <= 10 |
| 907 |
// |
| 908 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 909 |
return (void)max_mantissa[0], max_mantissa[power]; |
| 910 |
} |
| 911 |
|
| 912 |
template <> |
| 913 |
inline constexpr double |
| 914 |
binary_format<double>::exact_power_of_ten(int64_t power) { |
| 915 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 916 |
return (void)powers_of_ten[0], powers_of_ten[power]; |
| 917 |
} |
| 918 |
|
| 919 |
template <> |
| 920 |
inline constexpr float binary_format<float>::exact_power_of_ten(int64_t power) { |
| 921 |
// Work around clang bug https://godbolt.org/z/zedh7rrhc |
| 922 |
return (void)powers_of_ten[0], powers_of_ten[power]; |
| 923 |
} |
| 924 |
|
| 925 |
template <> inline constexpr int binary_format<double>::largest_power_of_ten() { |
| 926 |
return 308; |
| 927 |
} |
| 928 |
|
| 929 |
template <> inline constexpr int binary_format<float>::largest_power_of_ten() { |
| 930 |
return 38; |
| 931 |
} |
| 932 |
|
| 933 |
template <> |
| 934 |
inline constexpr int binary_format<double>::smallest_power_of_ten() { |
| 935 |
return -342; |
| 936 |
} |
| 937 |
|
| 938 |
template <> inline constexpr int binary_format<float>::smallest_power_of_ten() { |
| 939 |
return -64; |
| 940 |
} |
| 941 |
|
| 942 |
template <> inline constexpr size_t binary_format<double>::max_digits() { |
| 943 |
return 769; |
| 944 |
} |
| 945 |
|
| 946 |
template <> inline constexpr size_t binary_format<float>::max_digits() { |
| 947 |
return 114; |
| 948 |
} |
| 949 |
|
| 950 |
template <> |
| 951 |
inline constexpr binary_format<float>::equiv_uint |
| 952 |
binary_format<float>::exponent_mask() { |
| 953 |
return 0x7F800000; |
| 954 |
} |
| 955 |
|
| 956 |
template <> |
| 957 |
inline constexpr binary_format<double>::equiv_uint |
| 958 |
binary_format<double>::exponent_mask() { |
| 959 |
return 0x7FF0000000000000; |
| 960 |
} |
| 961 |
|
| 962 |
template <> |
| 963 |
inline constexpr binary_format<float>::equiv_uint |
| 964 |
binary_format<float>::mantissa_mask() { |
| 965 |
return 0x007FFFFF; |
| 966 |
} |
| 967 |
|
| 968 |
template <> |
| 969 |
inline constexpr binary_format<double>::equiv_uint |
| 970 |
binary_format<double>::mantissa_mask() { |
| 971 |
return 0x000FFFFFFFFFFFFF; |
| 972 |
} |
| 973 |
|
| 974 |
template <> |
| 975 |
inline constexpr binary_format<float>::equiv_uint |
| 976 |
binary_format<float>::hidden_bit_mask() { |
| 977 |
return 0x00800000; |
| 978 |
} |
| 979 |
|
| 980 |
template <> |
| 981 |
inline constexpr binary_format<double>::equiv_uint |
| 982 |
binary_format<double>::hidden_bit_mask() { |
| 983 |
return 0x0010000000000000; |
| 984 |
} |
| 985 |
|
| 986 |
template <typename T> |
| 987 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void |
| 988 |
to_float(bool negative, adjusted_mantissa am, T &value) { |
| 989 |
using equiv_uint = equiv_uint_t<T>; |
| 990 |
equiv_uint word = equiv_uint(am.mantissa); |
| 991 |
word = equiv_uint(word | equiv_uint(am.power2) |
| 992 |
<< binary_format<T>::mantissa_explicit_bits()); |
| 993 |
word = |
| 994 |
equiv_uint(word | equiv_uint(negative) << binary_format<T>::sign_index()); |
| 995 |
#if FASTFLOAT_HAS_BIT_CAST |
| 996 |
value = std::bit_cast<T>(word); |
| 997 |
#else |
| 998 |
::memcpy(&value, &word, sizeof(T)); |
| 999 |
#endif |
| 1000 |
} |
| 1001 |
|
| 1002 |
template <typename = void> struct space_lut { |
| 1003 |
static constexpr bool value[] = { |
| 1004 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1005 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1006 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1007 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1008 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1009 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1010 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1011 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1012 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1013 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1014 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 1015 |
}; |
| 1016 |
|
| 1017 |
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE |
| 1018 |
|
| 1019 |
template <typename T> constexpr bool space_lut<T>::value[]; |
| 1020 |
|
| 1021 |
#endif |
| 1022 |
|
| 1023 |
template <typename UC> constexpr bool is_space(UC c) { |
| 1024 |
return c < 256 && space_lut<>::value[uint8_t(c)]; |
| 1025 |
} |
| 1026 |
|
| 1027 |
template <typename UC> static constexpr uint64_t int_cmp_zeros() { |
| 1028 |
static_assert((sizeof(UC) == 1) || (sizeof(UC) == 2) || (sizeof(UC) == 4), |
| 1029 |
"Unsupported character size"); |
| 1030 |
return (sizeof(UC) == 1) ? 0x3030303030303030 |
| 1031 |
: (sizeof(UC) == 2) |
| 1032 |
? (uint64_t(UC('0')) << 48 | uint64_t(UC('0')) << 32 | |
| 1033 |
uint64_t(UC('0')) << 16 | UC('0')) |
| 1034 |
: (uint64_t(UC('0')) << 32 | UC('0')); |
| 1035 |
} |
| 1036 |
|
| 1037 |
template <typename UC> static constexpr int int_cmp_len() { |
| 1038 |
return sizeof(uint64_t) / sizeof(UC); |
| 1039 |
} |
| 1040 |
|
| 1041 |
template <typename UC> constexpr UC const *str_const_nan(); |
| 1042 |
|
| 1043 |
template <> constexpr char const *str_const_nan<char>() { return "nan"; } |
| 1044 |
|
| 1045 |
template <> constexpr wchar_t const *str_const_nan<wchar_t>() { return L"nan"; } |
| 1046 |
|
| 1047 |
template <> constexpr char16_t const *str_const_nan<char16_t>() { |
| 1048 |
return u"nan"; |
| 1049 |
} |
| 1050 |
|
| 1051 |
template <> constexpr char32_t const *str_const_nan<char32_t>() { |
| 1052 |
return U"nan"; |
| 1053 |
} |
| 1054 |
|
| 1055 |
#ifdef __cpp_char8_t |
| 1056 |
template <> constexpr char8_t const *str_const_nan<char8_t>() { |
| 1057 |
return u8"nan"; |
| 1058 |
} |
| 1059 |
#endif |
| 1060 |
|
| 1061 |
template <typename UC> constexpr UC const *str_const_inf(); |
| 1062 |
|
| 1063 |
template <> constexpr char const *str_const_inf<char>() { return "infinity"; } |
| 1064 |
|
| 1065 |
template <> constexpr wchar_t const *str_const_inf<wchar_t>() { |
| 1066 |
return L"infinity"; |
| 1067 |
} |
| 1068 |
|
| 1069 |
template <> constexpr char16_t const *str_const_inf<char16_t>() { |
| 1070 |
return u"infinity"; |
| 1071 |
} |
| 1072 |
|
| 1073 |
template <> constexpr char32_t const *str_const_inf<char32_t>() { |
| 1074 |
return U"infinity"; |
| 1075 |
} |
| 1076 |
|
| 1077 |
#ifdef __cpp_char8_t |
| 1078 |
template <> constexpr char8_t const *str_const_inf<char8_t>() { |
| 1079 |
return u8"infinity"; |
| 1080 |
} |
| 1081 |
#endif |
| 1082 |
|
| 1083 |
template <typename = void> struct int_luts { |
| 1084 |
static constexpr uint8_t chdigit[] = { |
| 1085 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1086 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1087 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1088 |
255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, |
| 1089 |
255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 1090 |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
| 1091 |
35, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, |
| 1092 |
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, |
| 1093 |
33, 34, 35, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1094 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1095 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1096 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1097 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1098 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1099 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1100 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1101 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 1102 |
255}; |
| 1103 |
|
| 1104 |
static constexpr size_t maxdigits_u64[] = { |
| 1105 |
64, 41, 32, 28, 25, 23, 22, 21, 20, 19, 18, 18, 17, 17, 16, 16, 16, 16, |
| 1106 |
15, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13}; |
| 1107 |
|
| 1108 |
static constexpr uint64_t min_safe_u64[] = { |
| 1109 |
9223372036854775808ull, 12157665459056928801ull, 4611686018427387904, |
| 1110 |
7450580596923828125, 4738381338321616896, 3909821048582988049, |
| 1111 |
9223372036854775808ull, 12157665459056928801ull, 10000000000000000000ull, |
| 1112 |
5559917313492231481, 2218611106740436992, 8650415919381337933, |
| 1113 |
2177953337809371136, 6568408355712890625, 1152921504606846976, |
| 1114 |
2862423051509815793, 6746640616477458432, 15181127029874798299ull, |
| 1115 |
1638400000000000000, 3243919932521508681, 6221821273427820544, |
| 1116 |
11592836324538749809ull, 876488338465357824, 1490116119384765625, |
| 1117 |
2481152873203736576, 4052555153018976267, 6502111422497947648, |
| 1118 |
10260628712958602189ull, 15943230000000000000ull, 787662783788549761, |
| 1119 |
1152921504606846976, 1667889514952984961, 2386420683693101056, |
| 1120 |
3379220508056640625, 4738381338321616896}; |
| 1121 |
}; |
| 1122 |
|
| 1123 |
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE |
| 1124 |
|
| 1125 |
template <typename T> constexpr uint8_t int_luts<T>::chdigit[]; |
| 1126 |
|
| 1127 |
template <typename T> constexpr size_t int_luts<T>::maxdigits_u64[]; |
| 1128 |
|
| 1129 |
template <typename T> constexpr uint64_t int_luts<T>::min_safe_u64[]; |
| 1130 |
|
| 1131 |
#endif |
| 1132 |
|
| 1133 |
template <typename UC> |
| 1134 |
fastfloat_really_inline constexpr uint8_t ch_to_digit(UC c) { |
| 1135 |
// wchar_t and char can be signed, so we need to be careful. |
| 1136 |
using UnsignedUC = typename std::make_unsigned<UC>::type; |
| 1137 |
return int_luts<>::chdigit[static_cast<unsigned char>( |
| 1138 |
static_cast<UnsignedUC>(c) & |
| 1139 |
static_cast<UnsignedUC>( |
| 1140 |
-((static_cast<UnsignedUC>(c) & ~0xFFull) == 0)))]; |
| 1141 |
} |
| 1142 |
|
| 1143 |
fastfloat_really_inline constexpr size_t max_digits_u64(int base) { |
| 1144 |
return int_luts<>::maxdigits_u64[base - 2]; |
| 1145 |
} |
| 1146 |
|
| 1147 |
// If a u64 is exactly max_digits_u64() in length, this is |
| 1148 |
// the value below which it has definitely overflowed. |
| 1149 |
fastfloat_really_inline constexpr uint64_t min_safe_u64(int base) { |
| 1150 |
return int_luts<>::min_safe_u64[base - 2]; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static_assert(std::is_same<equiv_uint_t<double>, uint64_t>::value, |
| 1154 |
"equiv_uint should be uint64_t for double"); |
| 1155 |
static_assert(std::numeric_limits<double>::is_iec559, |
| 1156 |
"double must fulfill the requirements of IEC 559 (IEEE 754)"); |
| 1157 |
|
| 1158 |
static_assert(std::is_same<equiv_uint_t<float>, uint32_t>::value, |
| 1159 |
"equiv_uint should be uint32_t for float"); |
| 1160 |
static_assert(std::numeric_limits<float>::is_iec559, |
| 1161 |
"float must fulfill the requirements of IEC 559 (IEEE 754)"); |
| 1162 |
|
| 1163 |
#ifdef __STDCPP_FLOAT64_T__ |
| 1164 |
static_assert(std::is_same<equiv_uint_t<std::float64_t>, uint64_t>::value, |
| 1165 |
"equiv_uint should be uint64_t for std::float64_t"); |
| 1166 |
static_assert( |
| 1167 |
std::numeric_limits<std::float64_t>::is_iec559, |
| 1168 |
"std::float64_t must fulfill the requirements of IEC 559 (IEEE 754)"); |
| 1169 |
#endif // __STDCPP_FLOAT64_T__ |
| 1170 |
|
| 1171 |
#ifdef __STDCPP_FLOAT32_T__ |
| 1172 |
static_assert(std::is_same<equiv_uint_t<std::float32_t>, uint32_t>::value, |
| 1173 |
"equiv_uint should be uint32_t for std::float32_t"); |
| 1174 |
static_assert( |
| 1175 |
std::numeric_limits<std::float32_t>::is_iec559, |
| 1176 |
"std::float32_t must fulfill the requirements of IEC 559 (IEEE 754)"); |
| 1177 |
#endif // __STDCPP_FLOAT32_T__ |
| 1178 |
|
| 1179 |
#ifdef __STDCPP_FLOAT16_T__ |
| 1180 |
static_assert( |
| 1181 |
std::is_same<binary_format<std::float16_t>::equiv_uint, uint16_t>::value, |
| 1182 |
"equiv_uint should be uint16_t for std::float16_t"); |
| 1183 |
static_assert( |
| 1184 |
std::numeric_limits<std::float16_t>::is_iec559, |
| 1185 |
"std::float16_t must fulfill the requirements of IEC 559 (IEEE 754)"); |
| 1186 |
#endif // __STDCPP_FLOAT16_T__ |
| 1187 |
|
| 1188 |
#ifdef __STDCPP_BFLOAT16_T__ |
| 1189 |
static_assert( |
| 1190 |
std::is_same<binary_format<std::bfloat16_t>::equiv_uint, uint16_t>::value, |
| 1191 |
"equiv_uint should be uint16_t for std::bfloat16_t"); |
| 1192 |
static_assert( |
| 1193 |
std::numeric_limits<std::bfloat16_t>::is_iec559, |
| 1194 |
"std::bfloat16_t must fulfill the requirements of IEC 559 (IEEE 754)"); |
| 1195 |
#endif // __STDCPP_BFLOAT16_T__ |
| 1196 |
|
| 1197 |
constexpr chars_format operator~(chars_format rhs) noexcept { |
| 1198 |
using int_type = std::underlying_type<chars_format>::type; |
| 1199 |
return static_cast<chars_format>(~static_cast<int_type>(rhs)); |
| 1200 |
} |
| 1201 |
|
| 1202 |
constexpr chars_format operator&(chars_format lhs, chars_format rhs) noexcept { |
| 1203 |
using int_type = std::underlying_type<chars_format>::type; |
| 1204 |
return static_cast<chars_format>(static_cast<int_type>(lhs) & |
| 1205 |
static_cast<int_type>(rhs)); |
| 1206 |
} |
| 1207 |
|
| 1208 |
constexpr chars_format operator|(chars_format lhs, chars_format rhs) noexcept { |
| 1209 |
using int_type = std::underlying_type<chars_format>::type; |
| 1210 |
return static_cast<chars_format>(static_cast<int_type>(lhs) | |
| 1211 |
static_cast<int_type>(rhs)); |
| 1212 |
} |
| 1213 |
|
| 1214 |
constexpr chars_format operator^(chars_format lhs, chars_format rhs) noexcept { |
| 1215 |
using int_type = std::underlying_type<chars_format>::type; |
| 1216 |
return static_cast<chars_format>(static_cast<int_type>(lhs) ^ |
| 1217 |
static_cast<int_type>(rhs)); |
| 1218 |
} |
| 1219 |
|
| 1220 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 chars_format & |
| 1221 |
operator&=(chars_format &lhs, chars_format rhs) noexcept { |
| 1222 |
return lhs = (lhs & rhs); |
| 1223 |
} |
| 1224 |
|
| 1225 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 chars_format & |
| 1226 |
operator|=(chars_format &lhs, chars_format rhs) noexcept { |
| 1227 |
return lhs = (lhs | rhs); |
| 1228 |
} |
| 1229 |
|
| 1230 |
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 chars_format & |
| 1231 |
operator^=(chars_format &lhs, chars_format rhs) noexcept { |
| 1232 |
return lhs = (lhs ^ rhs); |
| 1233 |
} |
| 1234 |
|
| 1235 |
namespace detail { |
| 1236 |
// adjust for deprecated feature macros |
| 1237 |
constexpr chars_format adjust_for_feature_macros(chars_format fmt) { |
| 1238 |
return fmt |
| 1239 |
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS |
| 1240 |
| chars_format::allow_leading_plus |
| 1241 |
#endif |
| 1242 |
#ifdef FASTFLOAT_SKIP_WHITE_SPACE |
| 1243 |
| chars_format::skip_white_space |
| 1244 |
#endif |
| 1245 |
; |
| 1246 |
} |
| 1247 |
} // namespace detail |
| 1248 |
|
| 1249 |
} // namespace fast_float |
| 1250 |
|
| 1251 |
#endif |