• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 // This is a C++ header-only implementation of Grisu2 algorithm from the publication:
16 // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
17 // integers." ACM Sigplan Notices 45.6 (2010): 233-243.
18 
19 #ifndef RAPIDJSON_DTOA_
20 #define RAPIDJSON_DTOA_
21 
22 #include "itoa.h" // GetDigitsLut()
23 #include "diyfp.h"
24 #include "ieee754.h"
25 
26 RAPIDJSON_NAMESPACE_BEGIN
27 namespace internal {
28 
29 #ifdef __GNUC__
30 RAPIDJSON_DIAG_PUSH
31 RAPIDJSON_DIAG_OFF(effc++)
32 #endif
33 
GrisuRound(char * buffer,int len,uint64_t delta,uint64_t rest,uint64_t ten_kappa,uint64_t wp_w)34 inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
35     while (rest < wp_w && delta - rest >= ten_kappa &&
36            (rest + ten_kappa < wp_w ||  /// closer
37             wp_w - rest > rest + ten_kappa - wp_w)) {
38         buffer[len - 1]--;
39         rest += ten_kappa;
40     }
41 }
42 
CountDecimalDigit32(uint32_t n)43 inline unsigned CountDecimalDigit32(uint32_t n) {
44     // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
45     if (n < 10) return 1;
46     if (n < 100) return 2;
47     if (n < 1000) return 3;
48     if (n < 10000) return 4;
49     if (n < 100000) return 5;
50     if (n < 1000000) return 6;
51     if (n < 10000000) return 7;
52     if (n < 100000000) return 8;
53     // Will not reach 10 digits in DigitGen()
54     //if (n < 1000000000) return 9;
55     //return 10;
56     return 9;
57 }
58 
DigitGen(const DiyFp & W,const DiyFp & Mp,uint64_t delta,char * buffer,int * len,int * K)59 inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
60     static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
61     const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
62     const DiyFp wp_w = Mp - W;
63     uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
64     uint64_t p2 = Mp.f & (one.f - 1);
65     unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
66     *len = 0;
67 
68     while (kappa > 0) {
69         uint32_t d = 0;
70         switch (kappa) {
71             case  9: d = p1 /  100000000; p1 %=  100000000; break;
72             case  8: d = p1 /   10000000; p1 %=   10000000; break;
73             case  7: d = p1 /    1000000; p1 %=    1000000; break;
74             case  6: d = p1 /     100000; p1 %=     100000; break;
75             case  5: d = p1 /      10000; p1 %=      10000; break;
76             case  4: d = p1 /       1000; p1 %=       1000; break;
77             case  3: d = p1 /        100; p1 %=        100; break;
78             case  2: d = p1 /         10; p1 %=         10; break;
79             case  1: d = p1;              p1 =           0; break;
80             default:;
81         }
82         if (d || *len)
83             buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
84         kappa--;
85         uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
86         if (tmp <= delta) {
87             *K += kappa;
88             GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
89             return;
90         }
91     }
92 
93     // kappa = 0
94     for (;;) {
95         p2 *= 10;
96         delta *= 10;
97         char d = static_cast<char>(p2 >> -one.e);
98         if (d || *len)
99             buffer[(*len)++] = static_cast<char>('0' + d);
100         p2 &= one.f - 1;
101         kappa--;
102         if (p2 < delta) {
103             *K += kappa;
104             GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-static_cast<int>(kappa)]);
105             return;
106         }
107     }
108 }
109 
Grisu2(double value,char * buffer,int * length,int * K)110 inline void Grisu2(double value, char* buffer, int* length, int* K) {
111     const DiyFp v(value);
112     DiyFp w_m, w_p;
113     v.NormalizedBoundaries(&w_m, &w_p);
114 
115     const DiyFp c_mk = GetCachedPower(w_p.e, K);
116     const DiyFp W = v.Normalize() * c_mk;
117     DiyFp Wp = w_p * c_mk;
118     DiyFp Wm = w_m * c_mk;
119     Wm.f++;
120     Wp.f--;
121     DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
122 }
123 
WriteExponent(int K,char * buffer)124 inline char* WriteExponent(int K, char* buffer) {
125     if (K < 0) {
126         *buffer++ = '-';
127         K = -K;
128     }
129 
130     if (K >= 100) {
131         *buffer++ = static_cast<char>('0' + static_cast<char>(K / 100));
132         K %= 100;
133         const char* d = GetDigitsLut() + K * 2;
134         *buffer++ = d[0];
135         *buffer++ = d[1];
136     }
137     else if (K >= 10) {
138         const char* d = GetDigitsLut() + K * 2;
139         *buffer++ = d[0];
140         *buffer++ = d[1];
141     }
142     else
143         *buffer++ = static_cast<char>('0' + static_cast<char>(K));
144 
145     return buffer;
146 }
147 
Prettify(char * buffer,int length,int k)148 inline char* Prettify(char* buffer, int length, int k) {
149     const int kk = length + k;  // 10^(kk-1) <= v < 10^kk
150 
151     if (length <= kk && kk <= 21) {
152         // 1234e7 -> 12340000000
153         for (int i = length; i < kk; i++)
154             buffer[i] = '0';
155         buffer[kk] = '.';
156         buffer[kk + 1] = '0';
157         return &buffer[kk + 2];
158     }
159     else if (0 < kk && kk <= 21) {
160         // 1234e-2 -> 12.34
161         std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
162         buffer[kk] = '.';
163         return &buffer[length + 1];
164     }
165     else if (-6 < kk && kk <= 0) {
166         // 1234e-6 -> 0.001234
167         const int offset = 2 - kk;
168         std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
169         buffer[0] = '0';
170         buffer[1] = '.';
171         for (int i = 2; i < offset; i++)
172             buffer[i] = '0';
173         return &buffer[length + offset];
174     }
175     else if (length == 1) {
176         // 1e30
177         buffer[1] = 'e';
178         return WriteExponent(kk - 1, &buffer[2]);
179     }
180     else {
181         // 1234e30 -> 1.234e33
182         std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
183         buffer[1] = '.';
184         buffer[length + 1] = 'e';
185         return WriteExponent(kk - 1, &buffer[0 + length + 2]);
186     }
187 }
188 
dtoa(double value,char * buffer)189 inline char* dtoa(double value, char* buffer) {
190     Double d(value);
191     if (d.IsZero()) {
192         if (d.Sign())
193             *buffer++ = '-';     // -0.0, Issue #289
194         buffer[0] = '0';
195         buffer[1] = '.';
196         buffer[2] = '0';
197         return &buffer[3];
198     }
199     else {
200         if (value < 0) {
201             *buffer++ = '-';
202             value = -value;
203         }
204         int length, K;
205         Grisu2(value, buffer, &length, &K);
206         return Prettify(buffer, length, K);
207     }
208 }
209 
210 #ifdef __GNUC__
211 RAPIDJSON_DIAG_POP
212 #endif
213 
214 } // namespace internal
215 RAPIDJSON_NAMESPACE_END
216 
217 #endif // RAPIDJSON_DTOA_
218