123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732 |
- #ifndef BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP
- #define BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP
- #include <boost/json/detail/ryu/ryu.hpp>
- #include <cstdlib>
- #include <cstring>
- #ifdef RYU_DEBUG
- #include <stdio.h>
- #endif
- #if defined(__SIZEOF_INT128__) && !defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS)
- #define BOOST_JSON_RYU_HAS_UINT128
- #elif defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64)
- #define BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS
- #endif
- #include <boost/json/detail/ryu/detail/common.hpp>
- #include <boost/json/detail/ryu/detail/digit_table.hpp>
- #include <boost/json/detail/ryu/detail/d2s.hpp>
- #include <boost/json/detail/ryu/detail/d2s_intrinsics.hpp>
- namespace boost {
- namespace json {
- namespace detail {
- namespace ryu {
- namespace detail {
- #if defined(BOOST_JSON_RYU_HAS_UINT128)
- inline
- std::uint64_t
- mulShift(
- const std::uint64_t m,
- const std::uint64_t* const mul,
- const std::int32_t j) noexcept
- {
- const uint128_t b0 = ((uint128_t) m) * mul[0];
- const uint128_t b2 = ((uint128_t) m) * mul[1];
- return (std::uint64_t) (((b0 >> 64) + b2) >> (j - 64));
- }
- inline
- uint64_t
- mulShiftAll(
- const std::uint64_t m,
- const std::uint64_t* const mul,
- std::int32_t const j,
- std::uint64_t* const vp,
- std::uint64_t* const vm,
- const std::uint32_t mmShift) noexcept
- {
- *vp = mulShift(4 * m + 2, mul, j);
- *vm = mulShift(4 * m - 1 - mmShift, mul, j);
- return mulShift(4 * m, mul, j);
- }
- #elif defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
- inline
- std::uint64_t
- mulShift(
- const std::uint64_t m,
- const std::uint64_t* const mul,
- const std::int32_t j) noexcept
- {
-
- std::uint64_t high1;
- std::uint64_t const low1 = umul128(m, mul[1], &high1);
- std::uint64_t high0;
- umul128(m, mul[0], &high0);
- std::uint64_t const sum = high0 + low1;
- if (sum < high0)
- ++high1;
- return shiftright128(sum, high1, j - 64);
- }
- inline
- std::uint64_t
- mulShiftAll(
- const std::uint64_t m,
- const std::uint64_t* const mul,
- const std::int32_t j,
- std::uint64_t* const vp,
- std::uint64_t* const vm,
- const std::uint32_t mmShift) noexcept
- {
- *vp = mulShift(4 * m + 2, mul, j);
- *vm = mulShift(4 * m - 1 - mmShift, mul, j);
- return mulShift(4 * m, mul, j);
- }
- #else
- inline
- std::uint64_t
- mulShiftAll(
- std::uint64_t m,
- const std::uint64_t* const mul,
- const std::int32_t j,
- std::uint64_t* const vp,
- std::uint64_t* const vm,
- const std::uint32_t mmShift)
- {
- m <<= 1;
-
- std::uint64_t tmp;
- std::uint64_t const lo = umul128(m, mul[0], &tmp);
- std::uint64_t hi;
- std::uint64_t const mid = tmp + umul128(m, mul[1], &hi);
- hi += mid < tmp;
- const std::uint64_t lo2 = lo + mul[0];
- const std::uint64_t mid2 = mid + mul[1] + (lo2 < lo);
- const std::uint64_t hi2 = hi + (mid2 < mid);
- *vp = shiftright128(mid2, hi2, (std::uint32_t)(j - 64 - 1));
- if (mmShift == 1)
- {
- const std::uint64_t lo3 = lo - mul[0];
- const std::uint64_t mid3 = mid - mul[1] - (lo3 > lo);
- const std::uint64_t hi3 = hi - (mid3 > mid);
- *vm = shiftright128(mid3, hi3, (std::uint32_t)(j - 64 - 1));
- }
- else
- {
- const std::uint64_t lo3 = lo + lo;
- const std::uint64_t mid3 = mid + mid + (lo3 < lo);
- const std::uint64_t hi3 = hi + hi + (mid3 < mid);
- const std::uint64_t lo4 = lo3 - mul[0];
- const std::uint64_t mid4 = mid3 - mul[1] - (lo4 > lo3);
- const std::uint64_t hi4 = hi3 - (mid4 > mid3);
- *vm = shiftright128(mid4, hi4, (std::uint32_t)(j - 64));
- }
- return shiftright128(mid, hi, (std::uint32_t)(j - 64 - 1));
- }
- #endif
- inline
- std::uint32_t
- decimalLength17(
- const std::uint64_t v)
- {
-
-
-
-
- BOOST_ASSERT(v < 100000000000000000L);
- if (v >= 10000000000000000L) { return 17; }
- if (v >= 1000000000000000L) { return 16; }
- if (v >= 100000000000000L) { return 15; }
- if (v >= 10000000000000L) { return 14; }
- if (v >= 1000000000000L) { return 13; }
- if (v >= 100000000000L) { return 12; }
- if (v >= 10000000000L) { return 11; }
- if (v >= 1000000000L) { return 10; }
- if (v >= 100000000L) { return 9; }
- if (v >= 10000000L) { return 8; }
- if (v >= 1000000L) { return 7; }
- if (v >= 100000L) { return 6; }
- if (v >= 10000L) { return 5; }
- if (v >= 1000L) { return 4; }
- if (v >= 100L) { return 3; }
- if (v >= 10L) { return 2; }
- return 1;
- }
- struct floating_decimal_64
- {
- std::uint64_t mantissa;
-
-
- std::int32_t exponent;
- };
- inline
- floating_decimal_64
- d2d(
- const std::uint64_t ieeeMantissa,
- const std::uint32_t ieeeExponent)
- {
- std::int32_t e2;
- std::uint64_t m2;
- if (ieeeExponent == 0)
- {
-
- e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
- m2 = ieeeMantissa;
- }
- else
- {
- e2 = (std::int32_t)ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
- m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
- }
- const bool even = (m2 & 1) == 0;
- const bool acceptBounds = even;
- #ifdef RYU_DEBUG
- printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
- #endif
-
- const std::uint64_t mv = 4 * m2;
-
- const std::uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
-
-
-
-
- std::uint64_t vr, vp, vm;
- std::int32_t e10;
- bool vmIsTrailingZeros = false;
- bool vrIsTrailingZeros = false;
- if (e2 >= 0) {
-
-
- const std::uint32_t q = log10Pow2(e2) - (e2 > 3);
- e10 = (std::int32_t)q;
- const std::int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t)q) - 1;
- const std::int32_t i = -e2 + (std::int32_t)q + k;
- #if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE)
- uint64_t pow5[2];
- double_computeInvPow5(q, pow5);
- vr = mulShiftAll(m2, pow5, i, &vp, &vm, mmShift);
- #else
- vr = mulShiftAll(m2, DOUBLE_POW5_INV_SPLIT()[q], i, &vp, &vm, mmShift);
- #endif
- #ifdef RYU_DEBUG
- printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
- #endif
- if (q <= 21)
- {
-
-
-
- const std::uint32_t mvMod5 = ((std::uint32_t)mv) - 5 * ((std::uint32_t)div5(mv));
- if (mvMod5 == 0)
- {
- vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
- }
- else if (acceptBounds)
- {
-
-
-
- vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
- }
- else
- {
-
- vp -= multipleOfPowerOf5(mv + 2, q);
- }
- }
- }
- else
- {
-
- const std::uint32_t q = log10Pow5(-e2) - (-e2 > 1);
- e10 = (std::int32_t)q + e2;
- const std::int32_t i = -e2 - (std::int32_t)q;
- const std::int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
- const std::int32_t j = (std::int32_t)q - k;
- #if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE)
- std::uint64_t pow5[2];
- double_computePow5(i, pow5);
- vr = mulShiftAll(m2, pow5, j, &vp, &vm, mmShift);
- #else
- vr = mulShiftAll(m2, DOUBLE_POW5_SPLIT()[i], j, &vp, &vm, mmShift);
- #endif
- #ifdef RYU_DEBUG
- printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
- printf("%u %d %d %d\n", q, i, k, j);
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
- #endif
- if (q <= 1)
- {
-
-
- vrIsTrailingZeros = true;
- if (acceptBounds)
- {
-
- vmIsTrailingZeros = mmShift == 1;
- }
- else
- {
-
- --vp;
- }
- }
- else if (q < 63)
- {
-
-
-
-
-
- vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
- #ifdef RYU_DEBUG
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
- #endif
- }
- }
- #ifdef RYU_DEBUG
- printf("e10=%d\n", e10);
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
- printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
- #endif
-
- std::int32_t removed = 0;
- std::uint8_t lastRemovedDigit = 0;
- std::uint64_t output;
-
- if (vmIsTrailingZeros || vrIsTrailingZeros)
- {
-
- for (;;)
- {
- const std::uint64_t vpDiv10 = div10(vp);
- const std::uint64_t vmDiv10 = div10(vm);
- if (vpDiv10 <= vmDiv10)
- break;
- const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10);
- const std::uint64_t vrDiv10 = div10(vr);
- const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
- vmIsTrailingZeros &= vmMod10 == 0;
- vrIsTrailingZeros &= lastRemovedDigit == 0;
- lastRemovedDigit = (uint8_t)vrMod10;
- vr = vrDiv10;
- vp = vpDiv10;
- vm = vmDiv10;
- ++removed;
- }
- #ifdef RYU_DEBUG
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
- printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
- #endif
- if (vmIsTrailingZeros)
- {
- for (;;)
- {
- const std::uint64_t vmDiv10 = div10(vm);
- const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10);
- if (vmMod10 != 0)
- break;
- const std::uint64_t vpDiv10 = div10(vp);
- const std::uint64_t vrDiv10 = div10(vr);
- const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
- vrIsTrailingZeros &= lastRemovedDigit == 0;
- lastRemovedDigit = (uint8_t)vrMod10;
- vr = vrDiv10;
- vp = vpDiv10;
- vm = vmDiv10;
- ++removed;
- }
- }
- #ifdef RYU_DEBUG
- printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
- #endif
- if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0)
- {
-
- lastRemovedDigit = 4;
- }
-
- output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
- }
- else
- {
-
- bool roundUp = false;
- const std::uint64_t vpDiv100 = div100(vp);
- const std::uint64_t vmDiv100 = div100(vm);
- if (vpDiv100 > vmDiv100)
- {
-
- const std::uint64_t vrDiv100 = div100(vr);
- const std::uint32_t vrMod100 = ((std::uint32_t)vr) - 100 * ((std::uint32_t)vrDiv100);
- roundUp = vrMod100 >= 50;
- vr = vrDiv100;
- vp = vpDiv100;
- vm = vmDiv100;
- removed += 2;
- }
-
-
-
-
- for (;;)
- {
- const std::uint64_t vpDiv10 = div10(vp);
- const std::uint64_t vmDiv10 = div10(vm);
- if (vpDiv10 <= vmDiv10)
- break;
- const std::uint64_t vrDiv10 = div10(vr);
- const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
- roundUp = vrMod10 >= 5;
- vr = vrDiv10;
- vp = vpDiv10;
- vm = vmDiv10;
- ++removed;
- }
- #ifdef RYU_DEBUG
- printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
- #endif
-
- output = vr + (vr == vm || roundUp);
- }
- const std::int32_t exp = e10 + removed;
- #ifdef RYU_DEBUG
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
- printf("O=%" PRIu64 "\n", output);
- printf("EXP=%d\n", exp);
- #endif
- floating_decimal_64 fd;
- fd.exponent = exp;
- fd.mantissa = output;
- return fd;
- }
- inline
- int
- to_chars(
- const floating_decimal_64 v,
- const bool sign,
- char* const result)
- {
-
- int index = 0;
- if (sign)
- result[index++] = '-';
- std::uint64_t output = v.mantissa;
- std::uint32_t const olength = decimalLength17(output);
- #ifdef RYU_DEBUG
- printf("DIGITS=%" PRIu64 "\n", v.mantissa);
- printf("OLEN=%u\n", olength);
- printf("EXP=%u\n", v.exponent + olength);
- #endif
-
-
-
-
-
-
-
- std::uint32_t i = 0;
-
-
-
-
- if ((output >> 32) != 0)
- {
-
- std::uint64_t const q = div1e8(output);
- std::uint32_t output2 = ((std::uint32_t)output) - 100000000 * ((std::uint32_t)q);
- output = q;
- const std::uint32_t c = output2 % 10000;
- output2 /= 10000;
- const std::uint32_t d = output2 % 10000;
- const std::uint32_t c0 = (c % 100) << 1;
- const std::uint32_t c1 = (c / 100) << 1;
- const std::uint32_t d0 = (d % 100) << 1;
- const std::uint32_t d1 = (d / 100) << 1;
- std::memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2);
- std::memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2);
- std::memcpy(result + index + olength - i - 5, DIGIT_TABLE() + d0, 2);
- std::memcpy(result + index + olength - i - 7, DIGIT_TABLE() + d1, 2);
- i += 8;
- }
- uint32_t output2 = (std::uint32_t)output;
- while (output2 >= 10000)
- {
- #ifdef __clang__
- const uint32_t c = output2 - 10000 * (output2 / 10000);
- #else
- const uint32_t c = output2 % 10000;
- #endif
- output2 /= 10000;
- const uint32_t c0 = (c % 100) << 1;
- const uint32_t c1 = (c / 100) << 1;
- memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2);
- memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2);
- i += 4;
- }
- if (output2 >= 100) {
- const uint32_t c = (output2 % 100) << 1;
- output2 /= 100;
- memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c, 2);
- i += 2;
- }
- if (output2 >= 10) {
- const uint32_t c = output2 << 1;
-
- result[index + olength - i] = DIGIT_TABLE()[c + 1];
- result[index] = DIGIT_TABLE()[c];
- }
- else {
- result[index] = (char)('0' + output2);
- }
-
- if (olength > 1) {
- result[index + 1] = '.';
- index += olength + 1;
- }
- else {
- ++index;
- }
-
- result[index++] = 'E';
- int32_t exp = v.exponent + (int32_t)olength - 1;
- if (exp < 0) {
- result[index++] = '-';
- exp = -exp;
- }
- if (exp >= 100) {
- const int32_t c = exp % 10;
- memcpy(result + index, DIGIT_TABLE() + 2 * (exp / 10), 2);
- result[index + 2] = (char)('0' + c);
- index += 3;
- }
- else if (exp >= 10) {
- memcpy(result + index, DIGIT_TABLE() + 2 * exp, 2);
- index += 2;
- }
- else {
- result[index++] = (char)('0' + exp);
- }
- return index;
- }
- static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
- floating_decimal_64* const v) {
- const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
- const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
- if (e2 > 0) {
-
-
- return false;
- }
- if (e2 < -52) {
-
- return false;
- }
-
-
- const uint64_t mask = (1ull << -e2) - 1;
- const uint64_t fraction = m2 & mask;
- if (fraction != 0) {
- return false;
- }
-
-
-
- v->mantissa = m2 >> -e2;
- v->exponent = 0;
- return true;
- }
- }
- int
- d2s_buffered_n(
- double f,
- char* result,
- bool allow_infinity_and_nan) noexcept
- {
- using namespace detail;
-
- std::uint64_t const bits = double_to_bits(f);
- #ifdef RYU_DEBUG
- printf("IN=");
- for (std::int32_t bit = 63; bit >= 0; --bit) {
- printf("%d", (int)((bits >> bit) & 1));
- }
- printf("\n");
- #endif
-
- const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
- const std::uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
- const std::uint32_t ieeeExponent = (std::uint32_t)((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
-
- if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
-
- if (allow_infinity_and_nan)
- return copy_special_str(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0);
- else
- return copy_special_str_conforming(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0);
- }
- floating_decimal_64 v;
- const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
- if (isSmallInt) {
-
-
-
-
- for (;;) {
- std::uint64_t const q = div10(v.mantissa);
- std::uint32_t const r = ((std::uint32_t) v.mantissa) - 10 * ((std::uint32_t) q);
- if (r != 0)
- break;
- v.mantissa = q;
- ++v.exponent;
- }
- }
- else {
- v = d2d(ieeeMantissa, ieeeExponent);
- }
- return to_chars(v, ieeeSign, result);
- }
- }
- }
- }
- }
- #endif
|