Arcane  v3.16.8.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
dtoa.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2// Tencent is pleased to support the open source community by making RapidJSON available.
3//
4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
5//
6// Licensed under the MIT License (the "License"); you may not use this file except
7// in compliance with the License. You may obtain a copy of the License at
8//
9// http://opensource.org/licenses/MIT
10//
11// Unless required by applicable law or agreed to in writing, software distributed
12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13// CONDITIONS OF ANY KIND, either express or implied. See the License for the
14// specific language governing permissions and limitations under the License.
15
16// This is a C++ header-only implementation of Grisu2 algorithm from the publication:
17// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
18// integers." ACM Sigplan Notices 45.6 (2010): 233-243.
19
20#ifndef RAPIDJSON_DTOA_
21#define RAPIDJSON_DTOA_
22
23#include "itoa.h" // GetDigitsLut()
24#include "diyfp.h"
25#include "ieee754.h"
26
28namespace internal {
29
30#ifdef __GNUC__
31RAPIDJSON_DIAG_PUSH
32RAPIDJSON_DIAG_OFF(effc++)
33RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
34#endif
35
36inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
37 while (rest < wp_w && delta - rest >= ten_kappa &&
38 (rest + ten_kappa < wp_w ||
39 wp_w - rest > rest + ten_kappa - wp_w)) {
40 buffer[len - 1]--;
41 rest += ten_kappa;
42 }
43}
44
45inline int CountDecimalDigit32(uint32_t n) {
46 // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
47 if (n < 10) return 1;
48 if (n < 100) return 2;
49 if (n < 1000) return 3;
50 if (n < 10000) return 4;
51 if (n < 100000) return 5;
52 if (n < 1000000) return 6;
53 if (n < 10000000) return 7;
54 if (n < 100000000) return 8;
55 // Will not reach 10 digits in DigitGen()
56 //if (n < 1000000000) return 9;
57 //return 10;
58 return 9;
59}
60
61inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
62 static const uint64_t kPow10[] = { 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL, 10000000ULL, 100000000ULL,
63 1000000000ULL, 10000000000ULL, 100000000000ULL, 1000000000000ULL,
64 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL,
65 10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL,
66 10000000000000000000ULL };
67 const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
68 const DiyFp wp_w = Mp - W;
69 uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
70 uint64_t p2 = Mp.f & (one.f - 1);
71 int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
72 *len = 0;
73
74 while (kappa > 0) {
75 uint32_t d = 0;
76 switch (kappa) {
77 case 9: d = p1 / 100000000; p1 %= 100000000; break;
78 case 8: d = p1 / 10000000; p1 %= 10000000; break;
79 case 7: d = p1 / 1000000; p1 %= 1000000; break;
80 case 6: d = p1 / 100000; p1 %= 100000; break;
81 case 5: d = p1 / 10000; p1 %= 10000; break;
82 case 4: d = p1 / 1000; p1 %= 1000; break;
83 case 3: d = p1 / 100; p1 %= 100; break;
84 case 2: d = p1 / 10; p1 %= 10; break;
85 case 1: d = p1; p1 = 0; break;
86 default:;
87 }
88 if (d || *len)
89 buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
90 kappa--;
91 uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
92 if (tmp <= delta) {
93 *K += kappa;
94 GrisuRound(buffer, *len, delta, tmp, kPow10[kappa] << -one.e, wp_w.f);
95 return;
96 }
97 }
98
99 // kappa = 0
100 for (;;) {
101 p2 *= 10;
102 delta *= 10;
103 char d = static_cast<char>(p2 >> -one.e);
104 if (d || *len)
105 buffer[(*len)++] = static_cast<char>('0' + d);
106 p2 &= one.f - 1;
107 kappa--;
108 if (p2 < delta) {
109 *K += kappa;
110 int index = -kappa;
111 GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 20 ? kPow10[index] : 0));
112 return;
113 }
114 }
115}
116
117inline void Grisu2(double value, char* buffer, int* length, int* K) {
118 const DiyFp v(value);
119 DiyFp w_m, w_p;
120 v.NormalizedBoundaries(&w_m, &w_p);
121
122 const DiyFp c_mk = GetCachedPower(w_p.e, K);
123 const DiyFp W = v.Normalize() * c_mk;
124 DiyFp Wp = w_p * c_mk;
125 DiyFp Wm = w_m * c_mk;
126 Wm.f++;
127 Wp.f--;
128 DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
129}
130
131inline char* WriteExponent(int K, char* buffer) {
132 if (K < 0) {
133 *buffer++ = '-';
134 K = -K;
135 }
136
137 if (K >= 100) {
138 *buffer++ = static_cast<char>('0' + static_cast<char>(K / 100));
139 K %= 100;
140 const char* d = GetDigitsLut() + K * 2;
141 *buffer++ = d[0];
142 *buffer++ = d[1];
143 }
144 else if (K >= 10) {
145 const char* d = GetDigitsLut() + K * 2;
146 *buffer++ = d[0];
147 *buffer++ = d[1];
148 }
149 else
150 *buffer++ = static_cast<char>('0' + static_cast<char>(K));
151
152 return buffer;
153}
154
155inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
156 const int kk = length + k; // 10^(kk-1) <= v < 10^kk
157
158 if (0 <= k && kk <= 21) {
159 // 1234e7 -> 12340000000
160 for (int i = length; i < kk; i++)
161 buffer[i] = '0';
162 buffer[kk] = '.';
163 buffer[kk + 1] = '0';
164 return &buffer[kk + 2];
165 }
166 else if (0 < kk && kk <= 21) {
167 // 1234e-2 -> 12.34
168 std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
169 buffer[kk] = '.';
170 if (0 > k + maxDecimalPlaces) {
171 // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
172 // Remove extra trailing zeros (at least one) after truncation.
173 for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)
174 if (buffer[i] != '0')
175 return &buffer[i + 1];
176 return &buffer[kk + 2]; // Reserve one zero
177 }
178 else
179 return &buffer[length + 1];
180 }
181 else if (-6 < kk && kk <= 0) {
182 // 1234e-6 -> 0.001234
183 const int offset = 2 - kk;
184 std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
185 buffer[0] = '0';
186 buffer[1] = '.';
187 for (int i = 2; i < offset; i++)
188 buffer[i] = '0';
189 if (length - kk > maxDecimalPlaces) {
190 // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
191 // Remove extra trailing zeros (at least one) after truncation.
192 for (int i = maxDecimalPlaces + 1; i > 2; i--)
193 if (buffer[i] != '0')
194 return &buffer[i + 1];
195 return &buffer[3]; // Reserve one zero
196 }
197 else
198 return &buffer[length + offset];
199 }
200 else if (kk < -maxDecimalPlaces) {
201 // Truncate to zero
202 buffer[0] = '0';
203 buffer[1] = '.';
204 buffer[2] = '0';
205 return &buffer[3];
206 }
207 else if (length == 1) {
208 // 1e30
209 buffer[1] = 'e';
210 return WriteExponent(kk - 1, &buffer[2]);
211 }
212 else {
213 // 1234e30 -> 1.234e33
214 std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
215 buffer[1] = '.';
216 buffer[length + 1] = 'e';
217 return WriteExponent(kk - 1, &buffer[0 + length + 2]);
218 }
219}
220
221inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
222 RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
223 Double d(value);
224 if (d.IsZero()) {
225 if (d.Sign())
226 *buffer++ = '-'; // -0.0, Issue #289
227 buffer[0] = '0';
228 buffer[1] = '.';
229 buffer[2] = '0';
230 return &buffer[3];
231 }
232 else {
233 if (value < 0) {
234 *buffer++ = '-';
235 value = -value;
236 }
237 int length, K;
238 Grisu2(value, buffer, &length, &K);
239 return Prettify(buffer, length, K, maxDecimalPlaces);
240 }
241}
242
243#ifdef __GNUC__
244RAPIDJSON_DIAG_POP
245#endif
246
247} // namespace internal
249
250#endif // RAPIDJSON_DTOA_
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:438
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:122
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:125
Integer len(const char *s)
Retourne la longueur de la chaîne s.