| 1 |
|
// Copyright 2018 Ulf Adams
|
1 |
|
// Copyright 2018 Ulf Adams
|
| 2 |
|
|
2 |
|
|
| 3 |
|
// The contents of this file may be used under the terms of the Apache License,
|
3 |
|
// The contents of this file may be used under the terms of the Apache License,
|
| 4 |
|
|
4 |
|
|
| 5 |
|
|
5 |
|
|
| 6 |
|
// (See accompanying file LICENSE-Apache or copy at
|
6 |
|
// (See accompanying file LICENSE-Apache or copy at
|
| 7 |
|
// http://www.apache.org/licenses/LICENSE-2.0)
|
7 |
|
// http://www.apache.org/licenses/LICENSE-2.0)
|
| 8 |
|
|
8 |
|
|
| 9 |
|
// Alternatively, the contents of this file may be used under the terms of
|
9 |
|
// Alternatively, the contents of this file may be used under the terms of
|
| 10 |
|
// the Boost Software License, Version 1.0.
|
10 |
|
// the Boost Software License, Version 1.0.
|
| 11 |
|
// (See accompanying file LICENSE-Boost or copy at
|
11 |
|
// (See accompanying file LICENSE-Boost or copy at
|
| 12 |
|
// https://www.boost.org/LICENSE_1_0.txt)
|
12 |
|
// https://www.boost.org/LICENSE_1_0.txt)
|
| 13 |
|
|
13 |
|
|
| 14 |
|
// Unless required by applicable law or agreed to in writing, this software
|
14 |
|
// Unless required by applicable law or agreed to in writing, this software
|
| 15 |
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15 |
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 16 |
|
// KIND, either express or implied.
|
16 |
|
// KIND, either express or implied.
|
| 17 |
|
|
17 |
|
|
| 18 |
|
// Runtime compiler options:
|
18 |
|
// Runtime compiler options:
|
| 19 |
|
// -DRYU_DEBUG Generate verbose debugging output to stdout.
|
19 |
|
// -DRYU_DEBUG Generate verbose debugging output to stdout.
|
| 20 |
|
|
20 |
|
|
| 21 |
|
// -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
|
21 |
|
// -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
|
| 22 |
|
// depending on your compiler.
|
22 |
|
// depending on your compiler.
|
| 23 |
|
|
23 |
|
|
| 24 |
|
// -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every
|
24 |
|
// -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every
|
| 25 |
|
// required power of 5, only store every 26th entry, and compute
|
25 |
|
// required power of 5, only store every 26th entry, and compute
|
| 26 |
|
// intermediate values with a multiplication. This reduces the lookup table
|
26 |
|
// intermediate values with a multiplication. This reduces the lookup table
|
| 27 |
|
// size by about 10x (only one case, and only double) at the cost of some
|
27 |
|
// size by about 10x (only one case, and only double) at the cost of some
|
| 28 |
|
// performance. Currently requires MSVC intrinsics.
|
28 |
|
// performance. Currently requires MSVC intrinsics.
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
This is a derivative work
|
31 |
|
This is a derivative work
|
| 32 |
|
|
32 |
|
|
| 33 |
|
|
33 |
|
|
| 34 |
|
#ifndef BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP
|
34 |
|
#ifndef BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP
|
| 35 |
|
#define BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP
|
35 |
|
#define BOOST_JSON_DETAIL_RYU_IMPL_D2S_IPP
|
| 36 |
|
|
36 |
|
|
| 37 |
|
#include <boost/json/detail/ryu/ryu.hpp>
|
37 |
|
#include <boost/json/detail/ryu/ryu.hpp>
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
|
42 |
|
|
| 43 |
|
|
43 |
|
|
| 44 |
|
|
44 |
|
|
| 45 |
|
// ABSL avoids uint128_t on Win32 even if __SIZEOF_INT128__ is defined.
|
45 |
|
// ABSL avoids uint128_t on Win32 even if __SIZEOF_INT128__ is defined.
|
| 46 |
|
// Let's do the same for now.
|
46 |
|
// Let's do the same for now.
|
| 47 |
|
#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS)
|
47 |
|
#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS)
|
| 48 |
|
#define BOOST_JSON_RYU_HAS_UINT128
|
48 |
|
#define BOOST_JSON_RYU_HAS_UINT128
|
| 49 |
|
#elif defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64)
|
49 |
|
#elif defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64)
|
| 50 |
|
#define BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS
|
50 |
|
#define BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS
|
| 51 |
|
|
51 |
|
|
| 52 |
|
|
52 |
|
|
| 53 |
|
#include <boost/json/detail/ryu/detail/common.hpp>
|
53 |
|
#include <boost/json/detail/ryu/detail/common.hpp>
|
| 54 |
|
#include <boost/json/detail/ryu/detail/digit_table.hpp>
|
54 |
|
#include <boost/json/detail/ryu/detail/digit_table.hpp>
|
| 55 |
|
#include <boost/json/detail/ryu/detail/d2s.hpp>
|
55 |
|
#include <boost/json/detail/ryu/detail/d2s.hpp>
|
| 56 |
|
#include <boost/json/detail/ryu/detail/d2s_intrinsics.hpp>
|
56 |
|
#include <boost/json/detail/ryu/detail/d2s_intrinsics.hpp>
|
| 57 |
|
|
57 |
|
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
|
60 |
|
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
|
63 |
|
|
| 64 |
|
|
64 |
|
|
| 65 |
|
// We need a 64x128-bit multiplication and a subsequent 128-bit shift.
|
65 |
|
// We need a 64x128-bit multiplication and a subsequent 128-bit shift.
|
| 66 |
|
|
66 |
|
|
| 67 |
|
// The 64-bit factor is variable and passed in, the 128-bit factor comes
|
67 |
|
// The 64-bit factor is variable and passed in, the 128-bit factor comes
|
| 68 |
|
// from a lookup table. We know that the 64-bit factor only has 55
|
68 |
|
// from a lookup table. We know that the 64-bit factor only has 55
|
| 69 |
|
// significant bits (i.e., the 9 topmost bits are zeros). The 128-bit
|
69 |
|
// significant bits (i.e., the 9 topmost bits are zeros). The 128-bit
|
| 70 |
|
// factor only has 124 significant bits (i.e., the 4 topmost bits are
|
70 |
|
// factor only has 124 significant bits (i.e., the 4 topmost bits are
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
// In principle, the multiplication result requires 55 + 124 = 179 bits to
|
73 |
|
// In principle, the multiplication result requires 55 + 124 = 179 bits to
|
| 74 |
|
// represent. However, we then shift this value to the right by j, which is
|
74 |
|
// represent. However, we then shift this value to the right by j, which is
|
| 75 |
|
// at least j >= 115, so the result is guaranteed to fit into 179 - 115 = 64
|
75 |
|
// at least j >= 115, so the result is guaranteed to fit into 179 - 115 = 64
|
| 76 |
|
// bits. This means that we only need the topmost 64 significant bits of
|
76 |
|
// bits. This means that we only need the topmost 64 significant bits of
|
| 77 |
|
// the 64x128-bit multiplication.
|
77 |
|
// the 64x128-bit multiplication.
|
| 78 |
|
|
78 |
|
|
| 79 |
|
// There are several ways to do this:
|
79 |
|
// There are several ways to do this:
|
| 80 |
|
// 1. Best case: the compiler exposes a 128-bit type.
|
80 |
|
// 1. Best case: the compiler exposes a 128-bit type.
|
| 81 |
|
// We perform two 64x64-bit multiplications, add the higher 64 bits of the
|
81 |
|
// We perform two 64x64-bit multiplications, add the higher 64 bits of the
|
| 82 |
|
// lower result to the higher result, and shift by j - 64 bits.
|
82 |
|
// lower result to the higher result, and shift by j - 64 bits.
|
| 83 |
|
|
83 |
|
|
| 84 |
|
// We explicitly cast from 64-bit to 128-bit, so the compiler can tell
|
84 |
|
// We explicitly cast from 64-bit to 128-bit, so the compiler can tell
|
| 85 |
|
// that these are only 64-bit inputs, and can map these to the best
|
85 |
|
// that these are only 64-bit inputs, and can map these to the best
|
| 86 |
|
// possible sequence of assembly instructions.
|
86 |
|
// possible sequence of assembly instructions.
|
| 87 |
|
// x64 machines happen to have matching assembly instructions for
|
87 |
|
// x64 machines happen to have matching assembly instructions for
|
| 88 |
|
// 64x64-bit multiplications and 128-bit shifts.
|
88 |
|
// 64x64-bit multiplications and 128-bit shifts.
|
| 89 |
|
|
89 |
|
|
| 90 |
|
// 2. Second best case: the compiler exposes intrinsics for the x64 assembly
|
90 |
|
// 2. Second best case: the compiler exposes intrinsics for the x64 assembly
|
| 91 |
|
// instructions mentioned in 1.
|
91 |
|
// instructions mentioned in 1.
|
| 92 |
|
|
92 |
|
|
| 93 |
|
// 3. We only have 64x64 bit instructions that return the lower 64 bits of
|
93 |
|
// 3. We only have 64x64 bit instructions that return the lower 64 bits of
|
| 94 |
|
// the result, i.e., we have to use plain C.
|
94 |
|
// the result, i.e., we have to use plain C.
|
| 95 |
|
// Our inputs are less than the full width, so we have three options:
|
95 |
|
// Our inputs are less than the full width, so we have three options:
|
| 96 |
|
// a. Ignore this fact and just implement the intrinsics manually.
|
96 |
|
// a. Ignore this fact and just implement the intrinsics manually.
|
| 97 |
|
// b. Split both into 31-bit pieces, which guarantees no internal overflow,
|
97 |
|
// b. Split both into 31-bit pieces, which guarantees no internal overflow,
|
| 98 |
|
// but requires extra work upfront (unless we change the lookup table).
|
98 |
|
// but requires extra work upfront (unless we change the lookup table).
|
| 99 |
|
// c. Split only the first factor into 31-bit pieces, which also guarantees
|
99 |
|
// c. Split only the first factor into 31-bit pieces, which also guarantees
|
| 100 |
|
// no internal overflow, but requires extra work since the intermediate
|
100 |
|
// no internal overflow, but requires extra work since the intermediate
|
| 101 |
|
// results are not perfectly aligned.
|
101 |
|
// results are not perfectly aligned.
|
| 102 |
|
#if defined(BOOST_JSON_RYU_HAS_UINT128)
|
102 |
|
#if defined(BOOST_JSON_RYU_HAS_UINT128)
|
| 103 |
|
|
103 |
|
|
| 104 |
|
// Best case: use 128-bit type.
|
104 |
|
// Best case: use 128-bit type.
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
const std::uint64_t* const mul,
|
109 |
|
const std::uint64_t* const mul,
|
| 110 |
|
const std::int32_t j) noexcept
|
110 |
|
const std::int32_t j) noexcept
|
| 111 |
|
|
111 |
|
|
| 112 |
|
const uint128_t b0 = ((uint128_t) m) * mul[0];
|
112 |
|
const uint128_t b0 = ((uint128_t) m) * mul[0];
|
| 113 |
|
const uint128_t b2 = ((uint128_t) m) * mul[1];
|
113 |
|
const uint128_t b2 = ((uint128_t) m) * mul[1];
|
| 114 |
|
return (std::uint64_t) (((b0 >> 64) + b2) >> (j - 64));
|
114 |
|
return (std::uint64_t) (((b0 >> 64) + b2) >> (j - 64));
|
| 115 |
|
|
115 |
|
|
| 116 |
|
|
116 |
|
|
| 117 |
|
|
117 |
|
|
| 118 |
|
|
118 |
|
|
| 119 |
|
|
119 |
|
|
| 120 |
|
|
120 |
|
|
| 121 |
|
const std::uint64_t* const mul,
|
121 |
|
const std::uint64_t* const mul,
|
| 122 |
|
|
122 |
|
|
| 123 |
|
|
123 |
|
|
| 124 |
|
|
124 |
|
|
| 125 |
|
const std::uint32_t mmShift) noexcept
|
125 |
|
const std::uint32_t mmShift) noexcept
|
| 126 |
|
|
126 |
|
|
| 127 |
|
|
127 |
|
|
| 128 |
|
// uint128_t b0 = ((uint128_t) m) * mul[0]; // 0
|
128 |
|
// uint128_t b0 = ((uint128_t) m) * mul[0]; // 0
|
| 129 |
|
// uint128_t b2 = ((uint128_t) m) * mul[1]; // 64
|
129 |
|
// uint128_t b2 = ((uint128_t) m) * mul[1]; // 64
|
| 130 |
|
|
130 |
|
|
| 131 |
|
// uint128_t hi = (b0 >> 64) + b2;
|
131 |
|
// uint128_t hi = (b0 >> 64) + b2;
|
| 132 |
|
// uint128_t lo = b0 & 0xffffffffffffffffull;
|
132 |
|
// uint128_t lo = b0 & 0xffffffffffffffffull;
|
| 133 |
|
// uint128_t factor = (((uint128_t) mul[1]) << 64) + mul[0];
|
133 |
|
// uint128_t factor = (((uint128_t) mul[1]) << 64) + mul[0];
|
| 134 |
|
// uint128_t vpLo = lo + (factor << 1);
|
134 |
|
// uint128_t vpLo = lo + (factor << 1);
|
| 135 |
|
// *vp = (std::uint64_t) ((hi + (vpLo >> 64)) >> (j - 64));
|
135 |
|
// *vp = (std::uint64_t) ((hi + (vpLo >> 64)) >> (j - 64));
|
| 136 |
|
// uint128_t vmLo = lo - (factor << mmShift);
|
136 |
|
// uint128_t vmLo = lo - (factor << mmShift);
|
| 137 |
|
// *vm = (std::uint64_t) ((hi + (vmLo >> 64) - (((uint128_t) 1ull) << 64)) >> (j - 64));
|
137 |
|
// *vm = (std::uint64_t) ((hi + (vmLo >> 64) - (((uint128_t) 1ull) << 64)) >> (j - 64));
|
| 138 |
|
// return (std::uint64_t) (hi >> (j - 64));
|
138 |
|
// return (std::uint64_t) (hi >> (j - 64));
|
| 139 |
|
*vp = mulShift(4 * m + 2, mul, j);
|
139 |
|
*vp = mulShift(4 * m + 2, mul, j);
|
| 140 |
|
*vm = mulShift(4 * m - 1 - mmShift, mul, j);
|
140 |
|
*vm = mulShift(4 * m - 1 - mmShift, mul, j);
|
| 141 |
|
return mulShift(4 * m, mul, j);
|
141 |
|
return mulShift(4 * m, mul, j);
|
| 142 |
|
|
142 |
|
|
| 143 |
|
|
143 |
|
|
| 144 |
|
#elif defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
|
144 |
|
#elif defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
|
| 145 |
|
|
145 |
|
|
| 146 |
|
|
146 |
|
|
| 147 |
|
|
147 |
|
|
| 148 |
|
|
148 |
|
|
| 149 |
|
|
149 |
|
|
| 150 |
|
const std::uint64_t* const mul,
|
150 |
|
const std::uint64_t* const mul,
|
| 151 |
|
const std::int32_t j) noexcept
|
151 |
|
const std::int32_t j) noexcept
|
| 152 |
|
|
152 |
|
|
| 153 |
|
|
153 |
|
|
| 154 |
|
std::uint64_t high1; // 128
|
154 |
|
std::uint64_t high1; // 128
|
| 155 |
|
std::uint64_t const low1 = umul128(m, mul[1], &high1); // 64
|
155 |
|
std::uint64_t const low1 = umul128(m, mul[1], &high1); // 64
|
| 156 |
|
std::uint64_t high0; // 64
|
156 |
|
std::uint64_t high0; // 64
|
| 157 |
|
umul128(m, mul[0], &high0); // 0
|
157 |
|
umul128(m, mul[0], &high0); // 0
|
| 158 |
|
std::uint64_t const sum = high0 + low1;
|
158 |
|
std::uint64_t const sum = high0 + low1;
|
| 159 |
|
|
159 |
|
|
| 160 |
|
++high1; // overflow into high1
|
160 |
|
++high1; // overflow into high1
|
| 161 |
|
return shiftright128(sum, high1, j - 64);
|
161 |
|
return shiftright128(sum, high1, j - 64);
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
|
166 |
|
|
| 167 |
|
|
167 |
|
|
| 168 |
|
const std::uint64_t* const mul,
|
168 |
|
const std::uint64_t* const mul,
|
| 169 |
|
|
169 |
|
|
| 170 |
|
|
170 |
|
|
| 171 |
|
|
171 |
|
|
| 172 |
|
const std::uint32_t mmShift) noexcept
|
172 |
|
const std::uint32_t mmShift) noexcept
|
| 173 |
|
|
173 |
|
|
| 174 |
|
*vp = mulShift(4 * m + 2, mul, j);
|
174 |
|
*vp = mulShift(4 * m + 2, mul, j);
|
| 175 |
|
*vm = mulShift(4 * m - 1 - mmShift, mul, j);
|
175 |
|
*vm = mulShift(4 * m - 1 - mmShift, mul, j);
|
| 176 |
|
return mulShift(4 * m, mul, j);
|
176 |
|
return mulShift(4 * m, mul, j);
|
| 177 |
|
|
177 |
|
|
| 178 |
|
|
178 |
|
|
| 179 |
|
#else // !defined(BOOST_JSON_RYU_HAS_UINT128) && !defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
|
179 |
|
#else // !defined(BOOST_JSON_RYU_HAS_UINT128) && !defined(BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS)
|
| 180 |
|
|
180 |
|
|
| 181 |
|
|
181 |
|
|
| 182 |
|
|
182 |
|
|
| 183 |
|
|
183 |
|
|
| 184 |
|
|
184 |
|
|
| 185 |
|
const std::uint64_t* const mul,
|
185 |
|
const std::uint64_t* const mul,
|
| 186 |
|
|
186 |
|
|
| 187 |
|
|
187 |
|
|
| 188 |
|
|
188 |
|
|
| 189 |
|
const std::uint32_t mmShift)
|
189 |
|
const std::uint32_t mmShift)
|
| 190 |
|
|
190 |
|
|
| 191 |
|
|
191 |
|
|
| 192 |
|
|
192 |
|
|
| 193 |
|
|
193 |
|
|
| 194 |
|
std::uint64_t const lo = umul128(m, mul[0], &tmp);
|
194 |
|
std::uint64_t const lo = umul128(m, mul[0], &tmp);
|
| 195 |
|
|
195 |
|
|
| 196 |
|
std::uint64_t const mid = tmp + umul128(m, mul[1], &hi);
|
196 |
|
std::uint64_t const mid = tmp + umul128(m, mul[1], &hi);
|
| 197 |
|
hi += mid < tmp; // overflow into hi
|
197 |
|
hi += mid < tmp; // overflow into hi
|
| 198 |
|
|
198 |
|
|
| 199 |
|
const std::uint64_t lo2 = lo + mul[0];
|
199 |
|
const std::uint64_t lo2 = lo + mul[0];
|
| 200 |
|
const std::uint64_t mid2 = mid + mul[1] + (lo2 < lo);
|
200 |
|
const std::uint64_t mid2 = mid + mul[1] + (lo2 < lo);
|
| 201 |
|
const std::uint64_t hi2 = hi + (mid2 < mid);
|
201 |
|
const std::uint64_t hi2 = hi + (mid2 < mid);
|
| 202 |
|
*vp = shiftright128(mid2, hi2, (std::uint32_t)(j - 64 - 1));
|
202 |
|
*vp = shiftright128(mid2, hi2, (std::uint32_t)(j - 64 - 1));
|
| 203 |
|
|
203 |
|
|
| 204 |
|
|
204 |
|
|
| 205 |
|
|
205 |
|
|
| 206 |
|
const std::uint64_t lo3 = lo - mul[0];
|
206 |
|
const std::uint64_t lo3 = lo - mul[0];
|
| 207 |
|
const std::uint64_t mid3 = mid - mul[1] - (lo3 > lo);
|
207 |
|
const std::uint64_t mid3 = mid - mul[1] - (lo3 > lo);
|
| 208 |
|
const std::uint64_t hi3 = hi - (mid3 > mid);
|
208 |
|
const std::uint64_t hi3 = hi - (mid3 > mid);
|
| 209 |
|
*vm = shiftright128(mid3, hi3, (std::uint32_t)(j - 64 - 1));
|
209 |
|
*vm = shiftright128(mid3, hi3, (std::uint32_t)(j - 64 - 1));
|
| 210 |
|
|
210 |
|
|
| 211 |
|
|
211 |
|
|
| 212 |
|
|
212 |
|
|
| 213 |
|
const std::uint64_t lo3 = lo + lo;
|
213 |
|
const std::uint64_t lo3 = lo + lo;
|
| 214 |
|
const std::uint64_t mid3 = mid + mid + (lo3 < lo);
|
214 |
|
const std::uint64_t mid3 = mid + mid + (lo3 < lo);
|
| 215 |
|
const std::uint64_t hi3 = hi + hi + (mid3 < mid);
|
215 |
|
const std::uint64_t hi3 = hi + hi + (mid3 < mid);
|
| 216 |
|
const std::uint64_t lo4 = lo3 - mul[0];
|
216 |
|
const std::uint64_t lo4 = lo3 - mul[0];
|
| 217 |
|
const std::uint64_t mid4 = mid3 - mul[1] - (lo4 > lo3);
|
217 |
|
const std::uint64_t mid4 = mid3 - mul[1] - (lo4 > lo3);
|
| 218 |
|
const std::uint64_t hi4 = hi3 - (mid4 > mid3);
|
218 |
|
const std::uint64_t hi4 = hi3 - (mid4 > mid3);
|
| 219 |
|
*vm = shiftright128(mid4, hi4, (std::uint32_t)(j - 64));
|
219 |
|
*vm = shiftright128(mid4, hi4, (std::uint32_t)(j - 64));
|
| 220 |
|
|
220 |
|
|
| 221 |
|
|
221 |
|
|
| 222 |
|
return shiftright128(mid, hi, (std::uint32_t)(j - 64 - 1));
|
222 |
|
return shiftright128(mid, hi, (std::uint32_t)(j - 64 - 1));
|
| 223 |
|
|
223 |
|
|
| 224 |
|
|
224 |
|
|
| 225 |
|
#endif // BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS
|
225 |
|
#endif // BOOST_JSON_RYU_HAS_64_BIT_INTRINSICS
|
| 226 |
|
|
226 |
|
|
| 227 |
|
|
227 |
|
|
| 228 |
|
|
228 |
|
|
| 229 |
|
|
229 |
|
|
| 230 |
|
|
230 |
|
|
| 231 |
|
|
231 |
|
|
| 232 |
|
// This is slightly faster than a loop.
|
232 |
|
// This is slightly faster than a loop.
|
| 233 |
|
// The average output length is 16.38 digits, so we check high-to-low.
|
233 |
|
// The average output length is 16.38 digits, so we check high-to-low.
|
| 234 |
|
// Function precondition: v is not an 18, 19, or 20-digit number.
|
234 |
|
// Function precondition: v is not an 18, 19, or 20-digit number.
|
| 235 |
|
// (17 digits are sufficient for round-tripping.)
|
235 |
|
// (17 digits are sufficient for round-tripping.)
|
| 236 |
|
BOOST_ASSERT(v < 100000000000000000L);
|
236 |
|
BOOST_ASSERT(v < 100000000000000000L);
|
| 237 |
|
if (v >= 10000000000000000L) { return 17; }
|
237 |
|
if (v >= 10000000000000000L) { return 17; }
|
| 238 |
|
if (v >= 1000000000000000L) { return 16; }
|
238 |
|
if (v >= 1000000000000000L) { return 16; }
|
| 239 |
|
if (v >= 100000000000000L) { return 15; }
|
239 |
|
if (v >= 100000000000000L) { return 15; }
|
| 240 |
|
if (v >= 10000000000000L) { return 14; }
|
240 |
|
if (v >= 10000000000000L) { return 14; }
|
| 241 |
|
if (v >= 1000000000000L) { return 13; }
|
241 |
|
if (v >= 1000000000000L) { return 13; }
|
| 242 |
|
if (v >= 100000000000L) { return 12; }
|
242 |
|
if (v >= 100000000000L) { return 12; }
|
| 243 |
|
if (v >= 10000000000L) { return 11; }
|
243 |
|
if (v >= 10000000000L) { return 11; }
|
| 244 |
|
if (v >= 1000000000L) { return 10; }
|
244 |
|
if (v >= 1000000000L) { return 10; }
|
| 245 |
|
if (v >= 100000000L) { return 9; }
|
245 |
|
if (v >= 100000000L) { return 9; }
|
| 246 |
|
if (v >= 10000000L) { return 8; }
|
246 |
|
if (v >= 10000000L) { return 8; }
|
| 247 |
|
if (v >= 1000000L) { return 7; }
|
247 |
|
if (v >= 1000000L) { return 7; }
|
| 248 |
|
if (v >= 100000L) { return 6; }
|
248 |
|
if (v >= 100000L) { return 6; }
|
| 249 |
|
if (v >= 10000L) { return 5; }
|
249 |
|
if (v >= 10000L) { return 5; }
|
| 250 |
|
if (v >= 1000L) { return 4; }
|
250 |
|
if (v >= 1000L) { return 4; }
|
| 251 |
|
if (v >= 100L) { return 3; }
|
251 |
|
if (v >= 100L) { return 3; }
|
| 252 |
|
if (v >= 10L) { return 2; }
|
252 |
|
if (v >= 10L) { return 2; }
|
| 253 |
|
|
253 |
|
|
| 254 |
|
|
254 |
|
|
| 255 |
|
|
255 |
|
|
| 256 |
|
// A floating decimal representing m * 10^e.
|
256 |
|
// A floating decimal representing m * 10^e.
|
| 257 |
|
struct floating_decimal_64
|
257 |
|
struct floating_decimal_64
|
| 258 |
|
|
258 |
|
|
| 259 |
|
|
259 |
|
|
| 260 |
|
// Decimal exponent's range is -324 to 308
|
260 |
|
// Decimal exponent's range is -324 to 308
|
| 261 |
|
// inclusive, and can fit in a short if needed.
|
261 |
|
// inclusive, and can fit in a short if needed.
|
| 262 |
|
|
262 |
|
|
| 263 |
|
|
263 |
|
|
| 264 |
|
|
264 |
|
|
| 265 |
|
|
265 |
|
|
| 266 |
|
|
266 |
|
|
| 267 |
|
|
267 |
|
|
| 268 |
|
const std::uint64_t ieeeMantissa,
|
268 |
|
const std::uint64_t ieeeMantissa,
|
| 269 |
|
const std::uint32_t ieeeExponent)
|
269 |
|
const std::uint32_t ieeeExponent)
|
| 270 |
|
|
270 |
|
|
| 271 |
|
|
271 |
|
|
| 272 |
|
|
272 |
|
|
| 273 |
|
|
273 |
|
|
| 274 |
|
|
274 |
|
|
| 275 |
|
// We subtract 2 so that the bounds computation has 2 additional bits.
|
275 |
|
// We subtract 2 so that the bounds computation has 2 additional bits.
|
| 276 |
|
e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
|
276 |
|
e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
|
| 277 |
|
|
277 |
|
|
| 278 |
|
|
278 |
|
|
| 279 |
|
|
279 |
|
|
| 280 |
|
|
280 |
|
|
| 281 |
|
e2 = (std::int32_t)ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
|
281 |
|
e2 = (std::int32_t)ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
|
| 282 |
|
m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
|
282 |
|
m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
|
| 283 |
|
|
283 |
|
|
| 284 |
|
const bool even = (m2 & 1) == 0;
|
284 |
|
const bool even = (m2 & 1) == 0;
|
| 285 |
|
const bool acceptBounds = even;
|
285 |
|
const bool acceptBounds = even;
|
| 286 |
|
|
286 |
|
|
| 287 |
|
|
287 |
|
|
| 288 |
|
printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
|
288 |
|
printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
|
| 289 |
|
|
289 |
|
|
| 290 |
|
|
290 |
|
|
| 291 |
|
// Step 2: Determine the interval of valid decimal representations.
|
291 |
|
// Step 2: Determine the interval of valid decimal representations.
|
| 292 |
|
const std::uint64_t mv = 4 * m2;
|
292 |
|
const std::uint64_t mv = 4 * m2;
|
| 293 |
|
// Implicit bool -> int conversion. True is 1, false is 0.
|
293 |
|
// Implicit bool -> int conversion. True is 1, false is 0.
|
| 294 |
|
const std::uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
|
294 |
|
const std::uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
|
| 295 |
|
// We would compute mp and mm like this:
|
295 |
|
// We would compute mp and mm like this:
|
| 296 |
|
// uint64_t mp = 4 * m2 + 2;
|
296 |
|
// uint64_t mp = 4 * m2 + 2;
|
| 297 |
|
// uint64_t mm = mv - 1 - mmShift;
|
297 |
|
// uint64_t mm = mv - 1 - mmShift;
|
| 298 |
|
|
298 |
|
|
| 299 |
|
// Step 3: Convert to a decimal power base using 128-bit arithmetic.
|
299 |
|
// Step 3: Convert to a decimal power base using 128-bit arithmetic.
|
| 300 |
|
std::uint64_t vr, vp, vm;
|
300 |
|
std::uint64_t vr, vp, vm;
|
| 301 |
|
|
301 |
|
|
| 302 |
|
bool vmIsTrailingZeros = false;
|
302 |
|
bool vmIsTrailingZeros = false;
|
| 303 |
|
bool vrIsTrailingZeros = false;
|
303 |
|
bool vrIsTrailingZeros = false;
|
| 304 |
|
|
304 |
|
|
| 305 |
|
// I tried special-casing q == 0, but there was no effect on performance.
|
305 |
|
// I tried special-casing q == 0, but there was no effect on performance.
|
| 306 |
|
// This expression is slightly faster than max(0, log10Pow2(e2) - 1).
|
306 |
|
// This expression is slightly faster than max(0, log10Pow2(e2) - 1).
|
| 307 |
|
const std::uint32_t q = log10Pow2(e2) - (e2 > 3);
|
307 |
|
const std::uint32_t q = log10Pow2(e2) - (e2 > 3);
|
| 308 |
|
|
308 |
|
|
| 309 |
|
const std::int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t)q) - 1;
|
309 |
|
const std::int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t)q) - 1;
|
| 310 |
|
const std::int32_t i = -e2 + (std::int32_t)q + k;
|
310 |
|
const std::int32_t i = -e2 + (std::int32_t)q + k;
|
| 311 |
|
#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE)
|
311 |
|
#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE)
|
| 312 |
|
|
312 |
|
|
| 313 |
|
double_computeInvPow5(q, pow5);
|
313 |
|
double_computeInvPow5(q, pow5);
|
| 314 |
|
vr = mulShiftAll(m2, pow5, i, &vp, &vm, mmShift);
|
314 |
|
vr = mulShiftAll(m2, pow5, i, &vp, &vm, mmShift);
|
| 315 |
|
|
315 |
|
|
| 316 |
|
vr = mulShiftAll(m2, DOUBLE_POW5_INV_SPLIT()[q], i, &vp, &vm, mmShift);
|
316 |
|
vr = mulShiftAll(m2, DOUBLE_POW5_INV_SPLIT()[q], i, &vp, &vm, mmShift);
|
| 317 |
|
|
317 |
|
|
| 318 |
|
|
318 |
|
|
| 319 |
|
printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
|
319 |
|
printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
|
| 320 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
320 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
| 321 |
|
|
321 |
|
|
| 322 |
|
|
322 |
|
|
| 323 |
|
|
323 |
|
|
| 324 |
|
// This should use q <= 22, but I think 21 is also safe. Smaller values
|
324 |
|
// This should use q <= 22, but I think 21 is also safe. Smaller values
|
| 325 |
|
// may still be safe, but it's more difficult to reason about them.
|
325 |
|
// may still be safe, but it's more difficult to reason about them.
|
| 326 |
|
// Only one of mp, mv, and mm can be a multiple of 5, if any.
|
326 |
|
// Only one of mp, mv, and mm can be a multiple of 5, if any.
|
| 327 |
|
const std::uint32_t mvMod5 = ((std::uint32_t)mv) - 5 * ((std::uint32_t)div5(mv));
|
327 |
|
const std::uint32_t mvMod5 = ((std::uint32_t)mv) - 5 * ((std::uint32_t)div5(mv));
|
| 328 |
|
|
328 |
|
|
| 329 |
|
|
329 |
|
|
| 330 |
|
vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
|
330 |
|
vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
|
| 331 |
|
|
331 |
|
|
| 332 |
|
|
332 |
|
|
| 333 |
|
|
333 |
|
|
| 334 |
|
// Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
|
334 |
|
// Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
|
| 335 |
|
// <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
|
335 |
|
// <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
|
| 336 |
|
// <=> true && pow5Factor(mm) >= q, since e2 >= q.
|
336 |
|
// <=> true && pow5Factor(mm) >= q, since e2 >= q.
|
| 337 |
|
vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
|
337 |
|
vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
|
| 338 |
|
|
338 |
|
|
| 339 |
|
|
339 |
|
|
| 340 |
|
|
340 |
|
|
| 341 |
|
// Same as min(e2 + 1, pow5Factor(mp)) >= q.
|
341 |
|
// Same as min(e2 + 1, pow5Factor(mp)) >= q.
|
| 342 |
|
vp -= multipleOfPowerOf5(mv + 2, q);
|
342 |
|
vp -= multipleOfPowerOf5(mv + 2, q);
|
| 343 |
|
|
343 |
|
|
| 344 |
|
|
344 |
|
|
| 345 |
|
|
345 |
|
|
| 346 |
|
|
346 |
|
|
| 347 |
|
|
347 |
|
|
| 348 |
|
// This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
|
348 |
|
// This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
|
| 349 |
|
const std::uint32_t q = log10Pow5(-e2) - (-e2 > 1);
|
349 |
|
const std::uint32_t q = log10Pow5(-e2) - (-e2 > 1);
|
| 350 |
|
e10 = (std::int32_t)q + e2;
|
350 |
|
e10 = (std::int32_t)q + e2;
|
| 351 |
|
const std::int32_t i = -e2 - (std::int32_t)q;
|
351 |
|
const std::int32_t i = -e2 - (std::int32_t)q;
|
| 352 |
|
const std::int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
|
352 |
|
const std::int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
|
| 353 |
|
const std::int32_t j = (std::int32_t)q - k;
|
353 |
|
const std::int32_t j = (std::int32_t)q - k;
|
| 354 |
|
#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE)
|
354 |
|
#if defined(BOOST_JSON_RYU_OPTIMIZE_SIZE)
|
| 355 |
|
|
355 |
|
|
| 356 |
|
double_computePow5(i, pow5);
|
356 |
|
double_computePow5(i, pow5);
|
| 357 |
|
vr = mulShiftAll(m2, pow5, j, &vp, &vm, mmShift);
|
357 |
|
vr = mulShiftAll(m2, pow5, j, &vp, &vm, mmShift);
|
| 358 |
|
|
358 |
|
|
| 359 |
|
vr = mulShiftAll(m2, DOUBLE_POW5_SPLIT()[i], j, &vp, &vm, mmShift);
|
359 |
|
vr = mulShiftAll(m2, DOUBLE_POW5_SPLIT()[i], j, &vp, &vm, mmShift);
|
| 360 |
|
|
360 |
|
|
| 361 |
|
|
361 |
|
|
| 362 |
|
printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
|
362 |
|
printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
|
| 363 |
|
printf("%u %d %d %d\n", q, i, k, j);
|
363 |
|
printf("%u %d %d %d\n", q, i, k, j);
|
| 364 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
364 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
| 365 |
|
|
365 |
|
|
| 366 |
|
|
366 |
|
|
| 367 |
|
|
367 |
|
|
| 368 |
|
// {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
|
368 |
|
// {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
|
| 369 |
|
// mv = 4 * m2, so it always has at least two trailing 0 bits.
|
369 |
|
// mv = 4 * m2, so it always has at least two trailing 0 bits.
|
| 370 |
|
vrIsTrailingZeros = true;
|
370 |
|
vrIsTrailingZeros = true;
|
| 371 |
|
|
371 |
|
|
| 372 |
|
|
372 |
|
|
| 373 |
|
// mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
|
373 |
|
// mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
|
| 374 |
|
vmIsTrailingZeros = mmShift == 1;
|
374 |
|
vmIsTrailingZeros = mmShift == 1;
|
| 375 |
|
|
375 |
|
|
| 376 |
|
|
376 |
|
|
| 377 |
|
|
377 |
|
|
| 378 |
|
// mp = mv + 2, so it always has at least one trailing 0 bit.
|
378 |
|
// mp = mv + 2, so it always has at least one trailing 0 bit.
|
| 379 |
|
|
379 |
|
|
| 380 |
|
|
380 |
|
|
| 381 |
|
|
381 |
|
|
| 382 |
|
|
382 |
|
|
| 383 |
|
|
383 |
|
|
| 384 |
|
// TODO(ulfjack): Use a tighter bound here.
|
384 |
|
// TODO(ulfjack): Use a tighter bound here.
|
| 385 |
|
// We want to know if the full product has at least q trailing zeros.
|
385 |
|
// We want to know if the full product has at least q trailing zeros.
|
| 386 |
|
// We need to compute min(p2(mv), p5(mv) - e2) >= q
|
386 |
|
// We need to compute min(p2(mv), p5(mv) - e2) >= q
|
| 387 |
|
// <=> p2(mv) >= q && p5(mv) - e2 >= q
|
387 |
|
// <=> p2(mv) >= q && p5(mv) - e2 >= q
|
| 388 |
|
// <=> p2(mv) >= q (because -e2 >= q)
|
388 |
|
// <=> p2(mv) >= q (because -e2 >= q)
|
| 389 |
|
vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
|
389 |
|
vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
|
| 390 |
|
|
390 |
|
|
| 391 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
391 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
| 392 |
|
|
392 |
|
|
| 393 |
|
|
393 |
|
|
| 394 |
|
|
394 |
|
|
| 395 |
|
|
395 |
|
|
| 396 |
|
|
396 |
|
|
| 397 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
397 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
| 398 |
|
printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
|
398 |
|
printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
|
| 399 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
399 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
| 400 |
|
|
400 |
|
|
| 401 |
|
|
401 |
|
|
| 402 |
|
// Step 4: Find the shortest decimal representation in the interval of valid representations.
|
402 |
|
// Step 4: Find the shortest decimal representation in the interval of valid representations.
|
| 403 |
|
std::int32_t removed = 0;
|
403 |
|
std::int32_t removed = 0;
|
| 404 |
|
std::uint8_t lastRemovedDigit = 0;
|
404 |
|
std::uint8_t lastRemovedDigit = 0;
|
| 405 |
|
|
405 |
|
|
| 406 |
|
// On average, we remove ~2 digits.
|
406 |
|
// On average, we remove ~2 digits.
|
| 407 |
|
if (vmIsTrailingZeros || vrIsTrailingZeros)
|
407 |
|
if (vmIsTrailingZeros || vrIsTrailingZeros)
|
| 408 |
|
|
408 |
|
|
| 409 |
|
// General case, which happens rarely (~0.7%).
|
409 |
|
// General case, which happens rarely (~0.7%).
|
| 410 |
|
|
410 |
|
|
| 411 |
|
|
411 |
|
|
| 412 |
|
const std::uint64_t vpDiv10 = div10(vp);
|
412 |
|
const std::uint64_t vpDiv10 = div10(vp);
|
| 413 |
|
const std::uint64_t vmDiv10 = div10(vm);
|
413 |
|
const std::uint64_t vmDiv10 = div10(vm);
|
| 414 |
|
|
414 |
|
|
| 415 |
|
|
415 |
|
|
| 416 |
|
const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10);
|
416 |
|
const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10);
|
| 417 |
|
const std::uint64_t vrDiv10 = div10(vr);
|
417 |
|
const std::uint64_t vrDiv10 = div10(vr);
|
| 418 |
|
const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
|
418 |
|
const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
|
| 419 |
|
vmIsTrailingZeros &= vmMod10 == 0;
|
419 |
|
vmIsTrailingZeros &= vmMod10 == 0;
|
| 420 |
|
vrIsTrailingZeros &= lastRemovedDigit == 0;
|
420 |
|
vrIsTrailingZeros &= lastRemovedDigit == 0;
|
| 421 |
|
lastRemovedDigit = (uint8_t)vrMod10;
|
421 |
|
lastRemovedDigit = (uint8_t)vrMod10;
|
| 422 |
|
|
422 |
|
|
| 423 |
|
|
423 |
|
|
| 424 |
|
|
424 |
|
|
| 425 |
|
|
425 |
|
|
| 426 |
|
|
426 |
|
|
| 427 |
|
|
427 |
|
|
| 428 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
428 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
| 429 |
|
printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
|
429 |
|
printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
|
| 430 |
|
|
430 |
|
|
| 431 |
|
|
431 |
|
|
| 432 |
|
|
432 |
|
|
| 433 |
|
|
433 |
|
|
| 434 |
|
|
434 |
|
|
| 435 |
|
const std::uint64_t vmDiv10 = div10(vm);
|
435 |
|
const std::uint64_t vmDiv10 = div10(vm);
|
| 436 |
|
const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10);
|
436 |
|
const std::uint32_t vmMod10 = ((std::uint32_t)vm) - 10 * ((std::uint32_t)vmDiv10);
|
| 437 |
|
|
437 |
|
|
| 438 |
|
|
438 |
|
|
| 439 |
|
const std::uint64_t vpDiv10 = div10(vp);
|
439 |
|
const std::uint64_t vpDiv10 = div10(vp);
|
| 440 |
|
const std::uint64_t vrDiv10 = div10(vr);
|
440 |
|
const std::uint64_t vrDiv10 = div10(vr);
|
| 441 |
|
const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
|
441 |
|
const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
|
| 442 |
|
vrIsTrailingZeros &= lastRemovedDigit == 0;
|
442 |
|
vrIsTrailingZeros &= lastRemovedDigit == 0;
|
| 443 |
|
lastRemovedDigit = (uint8_t)vrMod10;
|
443 |
|
lastRemovedDigit = (uint8_t)vrMod10;
|
| 444 |
|
|
444 |
|
|
| 445 |
|
|
445 |
|
|
| 446 |
|
|
446 |
|
|
| 447 |
|
|
447 |
|
|
| 448 |
|
|
448 |
|
|
| 449 |
|
|
449 |
|
|
| 450 |
|
|
450 |
|
|
| 451 |
|
printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
|
451 |
|
printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
|
| 452 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
452 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
| 453 |
|
|
453 |
|
|
| 454 |
|
if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0)
|
454 |
|
if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0)
|
| 455 |
|
|
455 |
|
|
| 456 |
|
// Round even if the exact number is .....50..0.
|
456 |
|
// Round even if the exact number is .....50..0.
|
| 457 |
|
|
457 |
|
|
| 458 |
|
|
458 |
|
|
| 459 |
|
// We need to take vr + 1 if vr is outside bounds or we need to round up.
|
459 |
|
// We need to take vr + 1 if vr is outside bounds or we need to round up.
|
| 460 |
|
output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
|
460 |
|
output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
|
| 461 |
|
|
461 |
|
|
| 462 |
|
|
462 |
|
|
| 463 |
|
|
463 |
|
|
| 464 |
|
// Specialized for the common case (~99.3%). Percentages below are relative to this.
|
464 |
|
// Specialized for the common case (~99.3%). Percentages below are relative to this.
|
| 465 |
|
|
465 |
|
|
| 466 |
|
const std::uint64_t vpDiv100 = div100(vp);
|
466 |
|
const std::uint64_t vpDiv100 = div100(vp);
|
| 467 |
|
const std::uint64_t vmDiv100 = div100(vm);
|
467 |
|
const std::uint64_t vmDiv100 = div100(vm);
|
| 468 |
|
|
468 |
|
|
| 469 |
|
|
469 |
|
|
| 470 |
|
// Optimization: remove two digits at a time (~86.2%).
|
470 |
|
// Optimization: remove two digits at a time (~86.2%).
|
| 471 |
|
const std::uint64_t vrDiv100 = div100(vr);
|
471 |
|
const std::uint64_t vrDiv100 = div100(vr);
|
| 472 |
|
const std::uint32_t vrMod100 = ((std::uint32_t)vr) - 100 * ((std::uint32_t)vrDiv100);
|
472 |
|
const std::uint32_t vrMod100 = ((std::uint32_t)vr) - 100 * ((std::uint32_t)vrDiv100);
|
| 473 |
|
roundUp = vrMod100 >= 50;
|
473 |
|
roundUp = vrMod100 >= 50;
|
| 474 |
|
|
474 |
|
|
| 475 |
|
|
475 |
|
|
| 476 |
|
|
476 |
|
|
| 477 |
|
|
477 |
|
|
| 478 |
|
|
478 |
|
|
| 479 |
|
// Loop iterations below (approximately), without optimization above:
|
479 |
|
// Loop iterations below (approximately), without optimization above:
|
| 480 |
|
// 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
|
480 |
|
// 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
|
| 481 |
|
// Loop iterations below (approximately), with optimization above:
|
481 |
|
// Loop iterations below (approximately), with optimization above:
|
| 482 |
|
// 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
|
482 |
|
// 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
|
| 483 |
|
|
483 |
|
|
| 484 |
|
|
484 |
|
|
| 485 |
|
const std::uint64_t vpDiv10 = div10(vp);
|
485 |
|
const std::uint64_t vpDiv10 = div10(vp);
|
| 486 |
|
const std::uint64_t vmDiv10 = div10(vm);
|
486 |
|
const std::uint64_t vmDiv10 = div10(vm);
|
| 487 |
|
|
487 |
|
|
| 488 |
|
|
488 |
|
|
| 489 |
|
const std::uint64_t vrDiv10 = div10(vr);
|
489 |
|
const std::uint64_t vrDiv10 = div10(vr);
|
| 490 |
|
const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
|
490 |
|
const std::uint32_t vrMod10 = ((std::uint32_t)vr) - 10 * ((std::uint32_t)vrDiv10);
|
| 491 |
|
|
491 |
|
|
| 492 |
|
|
492 |
|
|
| 493 |
|
|
493 |
|
|
| 494 |
|
|
494 |
|
|
| 495 |
|
|
495 |
|
|
| 496 |
|
|
496 |
|
|
| 497 |
|
|
497 |
|
|
| 498 |
|
printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
|
498 |
|
printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
|
| 499 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
499 |
|
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
|
| 500 |
|
|
500 |
|
|
| 501 |
|
// We need to take vr + 1 if vr is outside bounds or we need to round up.
|
501 |
|
// We need to take vr + 1 if vr is outside bounds or we need to round up.
|
| 502 |
|
output = vr + (vr == vm || roundUp);
|
502 |
|
output = vr + (vr == vm || roundUp);
|
| 503 |
|
|
503 |
|
|
| 504 |
|
const std::int32_t exp = e10 + removed;
|
504 |
|
const std::int32_t exp = e10 + removed;
|
| 505 |
|
|
505 |
|
|
| 506 |
|
|
506 |
|
|
| 507 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
507 |
|
printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
|
| 508 |
|
printf("O=%" PRIu64 "\n", output);
|
508 |
|
printf("O=%" PRIu64 "\n", output);
|
| 509 |
|
|
509 |
|
|
| 510 |
|
|
510 |
|
|
| 511 |
|
|
511 |
|
|
| 512 |
|
|
512 |
|
|
| 513 |
|
|
513 |
|
|
| 514 |
|
|
514 |
|
|
| 515 |
|
|
515 |
|
|
| 516 |
|
|
516 |
|
|
| 517 |
|
|
517 |
|
|
| 518 |
|
|
518 |
|
|
| 519 |
|
|
519 |
|
|
| 520 |
|
|
520 |
|
|
| 521 |
|
const floating_decimal_64 v,
|
521 |
|
const floating_decimal_64 v,
|
| 522 |
|
|
522 |
|
|
| 523 |
|
|
523 |
|
|
| 524 |
|
|
524 |
|
|
| 525 |
|
// Step 5: Print the decimal representation.
|
525 |
|
// Step 5: Print the decimal representation.
|
| 526 |
|
|
526 |
|
|
| 527 |
|
|
527 |
|
|
| 528 |
|
|
528 |
|
|
| 529 |
|
|
529 |
|
|
| 530 |
|
std::uint64_t output = v.mantissa;
|
530 |
|
std::uint64_t output = v.mantissa;
|
| 531 |
|
std::uint32_t const olength = decimalLength17(output);
|
531 |
|
std::uint32_t const olength = decimalLength17(output);
|
| 532 |
|
|
532 |
|
|
| 533 |
|
|
533 |
|
|
| 534 |
|
printf("DIGITS=%" PRIu64 "\n", v.mantissa);
|
534 |
|
printf("DIGITS=%" PRIu64 "\n", v.mantissa);
|
| 535 |
|
printf("OLEN=%u\n", olength);
|
535 |
|
printf("OLEN=%u\n", olength);
|
| 536 |
|
printf("EXP=%u\n", v.exponent + olength);
|
536 |
|
printf("EXP=%u\n", v.exponent + olength);
|
| 537 |
|
|
537 |
|
|
| 538 |
|
|
538 |
|
|
| 539 |
|
// Print the decimal digits.
|
539 |
|
// Print the decimal digits.
|
| 540 |
|
// The following code is equivalent to:
|
540 |
|
// The following code is equivalent to:
|
| 541 |
|
// for (uint32_t i = 0; i < olength - 1; ++i) {
|
541 |
|
// for (uint32_t i = 0; i < olength - 1; ++i) {
|
| 542 |
|
// const uint32_t c = output % 10; output /= 10;
|
542 |
|
// const uint32_t c = output % 10; output /= 10;
|
| 543 |
|
// result[index + olength - i] = (char) ('0' + c);
|
543 |
|
// result[index + olength - i] = (char) ('0' + c);
|
| 544 |
|
|
544 |
|
|
| 545 |
|
// result[index] = '0' + output % 10;
|
545 |
|
// result[index] = '0' + output % 10;
|
| 546 |
|
|
546 |
|
|
| 547 |
|
|
547 |
|
|
| 548 |
|
// We prefer 32-bit operations, even on 64-bit platforms.
|
548 |
|
// We prefer 32-bit operations, even on 64-bit platforms.
|
| 549 |
|
// We have at most 17 digits, and uint32_t can store 9 digits.
|
549 |
|
// We have at most 17 digits, and uint32_t can store 9 digits.
|
| 550 |
|
// If output doesn't fit into uint32_t, we cut off 8 digits,
|
550 |
|
// If output doesn't fit into uint32_t, we cut off 8 digits,
|
| 551 |
|
// so the rest will fit into uint32_t.
|
551 |
|
// so the rest will fit into uint32_t.
|
| 552 |
|
|
552 |
|
|
| 553 |
|
|
553 |
|
|
| 554 |
|
// Expensive 64-bit division.
|
554 |
|
// Expensive 64-bit division.
|
| 555 |
|
std::uint64_t const q = div1e8(output);
|
555 |
|
std::uint64_t const q = div1e8(output);
|
| 556 |
|
std::uint32_t output2 = ((std::uint32_t)output) - 100000000 * ((std::uint32_t)q);
|
556 |
|
std::uint32_t output2 = ((std::uint32_t)output) - 100000000 * ((std::uint32_t)q);
|
| 557 |
|
|
557 |
|
|
| 558 |
|
|
558 |
|
|
| 559 |
|
const std::uint32_t c = output2 % 10000;
|
559 |
|
const std::uint32_t c = output2 % 10000;
|
| 560 |
|
|
560 |
|
|
| 561 |
|
const std::uint32_t d = output2 % 10000;
|
561 |
|
const std::uint32_t d = output2 % 10000;
|
| 562 |
|
const std::uint32_t c0 = (c % 100) << 1;
|
562 |
|
const std::uint32_t c0 = (c % 100) << 1;
|
| 563 |
|
const std::uint32_t c1 = (c / 100) << 1;
|
563 |
|
const std::uint32_t c1 = (c / 100) << 1;
|
| 564 |
|
const std::uint32_t d0 = (d % 100) << 1;
|
564 |
|
const std::uint32_t d0 = (d % 100) << 1;
|
| 565 |
|
const std::uint32_t d1 = (d / 100) << 1;
|
565 |
|
const std::uint32_t d1 = (d / 100) << 1;
|
| 566 |
|
std::memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2);
|
566 |
|
std::memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2);
|
| 567 |
|
std::memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2);
|
567 |
|
std::memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2);
|
| 568 |
|
std::memcpy(result + index + olength - i - 5, DIGIT_TABLE() + d0, 2);
|
568 |
|
std::memcpy(result + index + olength - i - 5, DIGIT_TABLE() + d0, 2);
|
| 569 |
|
std::memcpy(result + index + olength - i - 7, DIGIT_TABLE() + d1, 2);
|
569 |
|
std::memcpy(result + index + olength - i - 7, DIGIT_TABLE() + d1, 2);
|
| 570 |
|
|
570 |
|
|
| 571 |
|
|
571 |
|
|
| 572 |
|
uint32_t output2 = (std::uint32_t)output;
|
572 |
|
uint32_t output2 = (std::uint32_t)output;
|
| 573 |
|
|
573 |
|
|
| 574 |
|
|
574 |
|
|
| 575 |
|
#ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
|
575 |
|
#ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
|
| 576 |
|
const uint32_t c = output2 - 10000 * (output2 / 10000);
|
576 |
|
const uint32_t c = output2 - 10000 * (output2 / 10000);
|
| 577 |
|
|
577 |
|
|
| 578 |
|
const uint32_t c = output2 % 10000;
|
578 |
|
const uint32_t c = output2 % 10000;
|
| 579 |
|
|
579 |
|
|
| 580 |
|
|
580 |
|
|
| 581 |
|
const uint32_t c0 = (c % 100) << 1;
|
581 |
|
const uint32_t c0 = (c % 100) << 1;
|
| 582 |
|
const uint32_t c1 = (c / 100) << 1;
|
582 |
|
const uint32_t c1 = (c / 100) << 1;
|
| 583 |
|
memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2);
|
583 |
|
memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c0, 2);
|
| 584 |
|
memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2);
|
584 |
|
memcpy(result + index + olength - i - 3, DIGIT_TABLE() + c1, 2);
|
| 585 |
|
|
585 |
|
|
| 586 |
|
|
586 |
|
|
| 587 |
|
|
587 |
|
|
| 588 |
|
const uint32_t c = (output2 % 100) << 1;
|
588 |
|
const uint32_t c = (output2 % 100) << 1;
|
| 589 |
|
|
589 |
|
|
| 590 |
|
memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c, 2);
|
590 |
|
memcpy(result + index + olength - i - 1, DIGIT_TABLE() + c, 2);
|
| 591 |
|
|
591 |
|
|
| 592 |
|
|
592 |
|
|
| 593 |
|
|
593 |
|
|
| 594 |
|
const uint32_t c = output2 << 1;
|
594 |
|
const uint32_t c = output2 << 1;
|
| 595 |
|
// We can't use memcpy here: the decimal dot goes between these two digits.
|
595 |
|
// We can't use memcpy here: the decimal dot goes between these two digits.
|
| 596 |
|
result[index + olength - i] = DIGIT_TABLE()[c + 1];
|
596 |
|
result[index + olength - i] = DIGIT_TABLE()[c + 1];
|
| 597 |
|
result[index] = DIGIT_TABLE()[c];
|
597 |
|
result[index] = DIGIT_TABLE()[c];
|
| 598 |
|
|
598 |
|
|
| 599 |
|
|
599 |
|
|
| 600 |
|
result[index] = (char)('0' + output2);
|
600 |
|
result[index] = (char)('0' + output2);
|
| 601 |
|
|
601 |
|
|
| 602 |
|
|
602 |
|
|
| 603 |
|
// Print decimal point if needed.
|
603 |
|
// Print decimal point if needed.
|
| 604 |
|
|
604 |
|
|
| 605 |
|
|
605 |
|
|
| 606 |
|
|
606 |
|
|
| 607 |
|
|
607 |
|
|
| 608 |
|
|
608 |
|
|
| 609 |
|
|
609 |
|
|
| 610 |
|
|
610 |
|
|
| 611 |
|
|
611 |
|
|
| 612 |
|
|
612 |
|
|
| 613 |
|
|
613 |
|
|
| 614 |
|
int32_t exp = v.exponent + (int32_t)olength - 1;
|
614 |
|
int32_t exp = v.exponent + (int32_t)olength - 1;
|
| 615 |
|
|
615 |
|
|
| 616 |
|
|
616 |
|
|
| 617 |
|
|
617 |
|
|
| 618 |
|
|
618 |
|
|
| 619 |
|
|
619 |
|
|
| 620 |
|
|
620 |
|
|
| 621 |
|
const int32_t c = exp % 10;
|
621 |
|
const int32_t c = exp % 10;
|
| 622 |
|
memcpy(result + index, DIGIT_TABLE() + 2 * (exp / 10), 2);
|
622 |
|
memcpy(result + index, DIGIT_TABLE() + 2 * (exp / 10), 2);
|
| 623 |
|
result[index + 2] = (char)('0' + c);
|
623 |
|
result[index + 2] = (char)('0' + c);
|
| 624 |
|
|
624 |
|
|
| 625 |
|
|
625 |
|
|
| 626 |
|
|
626 |
|
|
| 627 |
|
memcpy(result + index, DIGIT_TABLE() + 2 * exp, 2);
|
627 |
|
memcpy(result + index, DIGIT_TABLE() + 2 * exp, 2);
|
| 628 |
|
|
628 |
|
|
| 629 |
|
|
629 |
|
|
| 630 |
|
|
630 |
|
|
| 631 |
|
result[index++] = (char)('0' + exp);
|
631 |
|
result[index++] = (char)('0' + exp);
|
| 632 |
|
|
632 |
|
|
| 633 |
|
|
633 |
|
|
| 634 |
|
|
634 |
|
|
| 635 |
|
|
635 |
|
|
| 636 |
|
|
636 |
|
|
| 637 |
|
static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
|
637 |
|
static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
|
| 638 |
|
floating_decimal_64* const v) {
|
638 |
|
floating_decimal_64* const v) {
|
| 639 |
|
const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
|
639 |
|
const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
|
| 640 |
|
const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
|
640 |
|
const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
|
| 641 |
|
|
641 |
|
|
| 642 |
|
|
642 |
|
|
| 643 |
|
// f = m2 * 2^e2 >= 2^53 is an integer.
|
643 |
|
// f = m2 * 2^e2 >= 2^53 is an integer.
|
| 644 |
|
// Ignore this case for now.
|
644 |
|
// Ignore this case for now.
|
| 645 |
|
|
645 |
|
|
| 646 |
|
|
646 |
|
|
| 647 |
|
|
647 |
|
|
| 648 |
|
|
648 |
|
|
| 649 |
|
|
649 |
|
|
| 650 |
|
|
650 |
|
|
| 651 |
|
|
651 |
|
|
| 652 |
|
|
652 |
|
|
| 653 |
|
// Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
|
653 |
|
// Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
|
| 654 |
|
// Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
|
654 |
|
// Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
|
| 655 |
|
const uint64_t mask = (1ull << -e2) - 1;
|
655 |
|
const uint64_t mask = (1ull << -e2) - 1;
|
| 656 |
|
const uint64_t fraction = m2 & mask;
|
656 |
|
const uint64_t fraction = m2 & mask;
|
| 657 |
|
|
657 |
|
|
| 658 |
|
|
658 |
|
|
| 659 |
|
|
659 |
|
|
| 660 |
|
|
660 |
|
|
| 661 |
|
// f is an integer in the range [1, 2^53).
|
661 |
|
// f is an integer in the range [1, 2^53).
|
| 662 |
|
// Note: mantissa might contain trailing (decimal) 0's.
|
662 |
|
// Note: mantissa might contain trailing (decimal) 0's.
|
| 663 |
|
// Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
|
663 |
|
// Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
|
| 664 |
|
|
664 |
|
|
| 665 |
|
|
665 |
|
|
| 666 |
|
|
666 |
|
|
| 667 |
|
|
667 |
|
|
| 668 |
|
|
668 |
|
|
| 669 |
|
|
669 |
|
|
| 670 |
|
|
670 |
|
|
| 671 |
|
|
671 |
|
|
| 672 |
|
|
672 |
|
|
| 673 |
|
|
673 |
|
|
| 674 |
|
|
674 |
|
|
| 675 |
|
bool allow_infinity_and_nan) noexcept
|
675 |
|
bool allow_infinity_and_nan) noexcept
|
| 676 |
|
|
676 |
|
|
| 677 |
|
|
677 |
|
|
| 678 |
|
// Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
|
678 |
|
// Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
|
| 679 |
|
std::uint64_t const bits = double_to_bits(f);
|
679 |
|
std::uint64_t const bits = double_to_bits(f);
|
| 680 |
|
|
680 |
|
|
| 681 |
|
|
681 |
|
|
| 682 |
|
|
682 |
|
|
| 683 |
|
for (std::int32_t bit = 63; bit >= 0; --bit) {
|
683 |
|
for (std::int32_t bit = 63; bit >= 0; --bit) {
|
| 684 |
|
printf("%d", (int)((bits >> bit) & 1));
|
684 |
|
printf("%d", (int)((bits >> bit) & 1));
|
| 685 |
|
|
685 |
|
|
| 686 |
|
|
686 |
|
|
| 687 |
|
|
687 |
|
|
| 688 |
|
|
688 |
|
|
| 689 |
|
// Decode bits into sign, mantissa, and exponent.
|
689 |
|
// Decode bits into sign, mantissa, and exponent.
|
| 690 |
|
const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
|
690 |
|
const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
|
| 691 |
|
const std::uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
|
691 |
|
const std::uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
|
| 692 |
|
const std::uint32_t ieeeExponent = (std::uint32_t)((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
|
692 |
|
const std::uint32_t ieeeExponent = (std::uint32_t)((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
|
| 693 |
|
// Case distinction; exit early for the easy cases.
|
693 |
|
// Case distinction; exit early for the easy cases.
|
| 694 |
|
if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
|
694 |
|
if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
|
| 695 |
|
// We changed how special numbers are output by default
|
695 |
|
// We changed how special numbers are output by default
|
| 696 |
|
if (allow_infinity_and_nan)
|
696 |
|
if (allow_infinity_and_nan)
|
| 697 |
|
return copy_special_str(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0);
|
697 |
|
return copy_special_str(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0);
|
| 698 |
|
|
698 |
|
|
| 699 |
|
return copy_special_str_conforming(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0);
|
699 |
|
return copy_special_str_conforming(result, ieeeSign, ieeeExponent != 0, ieeeMantissa != 0);
|
| 700 |
|
|
700 |
|
|
| 701 |
|
|
701 |
|
|
| 702 |
|
|
702 |
|
|
| 703 |
|
|
703 |
|
|
| 704 |
|
const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
|
704 |
|
const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
|
| 705 |
|
|
705 |
|
|
| 706 |
|
// For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
|
706 |
|
// For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
|
| 707 |
|
// For scientific notation we need to move these zeros into the exponent.
|
707 |
|
// For scientific notation we need to move these zeros into the exponent.
|
| 708 |
|
// (This is not needed for fixed-point notation, so it might be beneficial to trim
|
708 |
|
// (This is not needed for fixed-point notation, so it might be beneficial to trim
|
| 709 |
|
// trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
|
709 |
|
// trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
|
| 710 |
|
|
710 |
|
|
| 711 |
|
std::uint64_t const q = div10(v.mantissa);
|
711 |
|
std::uint64_t const q = div10(v.mantissa);
|
| 712 |
|
std::uint32_t const r = ((std::uint32_t) v.mantissa) - 10 * ((std::uint32_t) q);
|
712 |
|
std::uint32_t const r = ((std::uint32_t) v.mantissa) - 10 * ((std::uint32_t) q);
|
| 713 |
|
|
713 |
|
|
| 714 |
|
|
714 |
|
|
| 715 |
|
|
715 |
|
|
| 716 |
|
|
716 |
|
|
| 717 |
|
|
717 |
|
|
| 718 |
|
|
718 |
|
|
| 719 |
|
|
719 |
|
|
| 720 |
|
v = d2d(ieeeMantissa, ieeeExponent);
|
720 |
|
v = d2d(ieeeMantissa, ieeeExponent);
|
| 721 |
|
|
721 |
|
|
| 722 |
|
|
722 |
|
|
| 723 |
|
return to_chars(v, ieeeSign, result);
|
723 |
|
return to_chars(v, ieeeSign, result);
|
| 724 |
|
|
724 |
|
|
| 725 |
|
|
725 |
|
|
| 726 |
|
|
726 |
|
|
| 727 |
|
|
727 |
|
|
| 728 |
|
|
728 |
|
|
| 729 |
|
|
729 |
|
|
| 730 |
|
|
730 |
|
|
| 731 |
|
|
731 |
|
|
| 732 |
|
|
732 |
|
|