Debian Codegraph @

texk/web2c/mfluadir/otfcc/dep/extern/emyg-dtoa/emyg-dtoa.c

raw
1 /* emyg_dtoa.c
2 ** Copyright (C) 2015 Doug Currie
3 ** based on dtoa_milo.h
4 ** Copyright (C) 2014 Milo Yip
5 **
6 ** Permission is hereby granted, free of charge, to any person obtaining a copy
7 ** of this software and associated documentation files (the "Software"), to deal
8 ** in the Software without restriction, including without limitation the rights
9 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 ** copies of the Software, and to permit persons to whom the Software is
11 ** furnished to do so, subject to the following conditions:
12 **
13 ** The above copyright notice and this permission notice shall be included in
14 ** all copies or substantial portions of the Software.
15 **
16 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 ** THE SOFTWARE.
23 */
24
25 /* This code is a mostly mechanical translation of Milo Yip's C++ version of
26 ** Grisu2 to C. For algorithm information, see Loitsch, Florian. "Printing
27 ** floating-point numbers quickly and accurately with integers." ACM Sigplan
28 ** Notices 45.6 (2010): 233-243.
29 */
30
31 #include <assert.h>
32 #include <math.h>
33
34 #if defined(_MSC_VER)
35 #include <intrin.h>
36 #endif
37
38 #include <stdint.h>
39
40 #include <string.h>
41
42 #include "emyg-dtoa.h"
43
44 #define UINT64_C2(h, l) (((uint64_t)(h) << 32) | (uint64_t)(l))
45
46 typedef struct DiyFp_s {
47 uint64_t f;
48 int e;
49 } DiyFp;
50
51 static const int kDiySignificandSize = 64;
52 static const int kDpSignificandSize = 52;
53 static const int kDpExponentBias = 0x3FF + 52;
54 static const int kDpMinExponent = -0x3FF - 52;
55 static const uint64_t kDpExponentMask = UINT64_C2(0x7FF00000, 0x00000000);
56 static const uint64_t kDpSignificandMask = UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
57 static const uint64_t kDpHiddenBit = UINT64_C2(0x00100000, 0x00000000);
58
59 static inline DiyFp DiyFp_from_parts(uint64_t f, int e) {
60 DiyFp fp;
61 fp.f = f;
62 fp.e = e;
63 return fp;
64 }
65
66 static inline DiyFp DiyFp_from_double(double d) {
67 union {
68 double d;
69 uint64_t u64;
70 } u = {d};
71 DiyFp res;
72
73 int biased_e = (int)((u.u64 & kDpExponentMask) >> kDpSignificandSize);
74 uint64_t significand = (u.u64 & kDpSignificandMask);
75 if (biased_e != 0) {
76 res.f = significand + kDpHiddenBit;
77 res.e = biased_e - kDpExponentBias;
78 } else {
79 res.f = significand;
80 res.e = kDpMinExponent + 1;
81 }
82 return res;
83 }
84
85 static inline DiyFp DiyFp_subtract(const DiyFp lhs, const DiyFp rhs) {
86 assert(lhs.e == rhs.e);
87 assert(lhs.f >= rhs.f);
88 return DiyFp_from_parts(lhs.f - rhs.f, lhs.e);
89 }
90
91 static inline DiyFp DiyFp_multiply(const DiyFp lhs, const DiyFp rhs) {
92 #if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__clang__)
93 uint64_t h;
94 uint64_t l = _umul128(lhs.f, rhs.f, &h);
95 if (l & ((uint64_t)(1) << 63)) { // rounding
96 h++;
97 }
98 return DiyFp_from_parts(h, lhs.e + rhs.e + 64);
99 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) && !defined(_MSC_VER)
100 __extension__ typedef unsigned __int128 uint128_t;
101 //unsigned int128 p = (unsigned int128)(lhs.f) * (unsigned int128)(rhs.f);
102 uint128_t p = (uint128_t)(lhs.f) * (uint128_t)(rhs.f);
103 uint64_t h = p >> 64;
104 uint64_t l = (uint64_t)(p);
105 if (l & ((uint64_t)(1) << 63)) // rounding
106 h++;
107 return DiyFp_from_parts(h, lhs.e + rhs.e + 64);
108 #else
109 const uint64_t M32 = 0xFFFFFFFF;
110 const uint64_t a = lhs.f >> 32;
111 const uint64_t b = lhs.f & M32;
112 const uint64_t c = rhs.f >> 32;
113 const uint64_t d = rhs.f & M32;
114 const uint64_t ac = a * c;
115 const uint64_t bc = b * c;
116 const uint64_t ad = a * d;
117 const uint64_t bd = b * d;
118 uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
119 tmp += 1U << 31; /// mult_round
120 return DiyFp_from_parts(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), lhs.e + rhs.e + 64);
121 #endif
122 }
123
124 static inline DiyFp Normalize(const DiyFp lhs) {
125 #if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__clang__)
126 unsigned long index;
127 _BitScanReverse64(&index, lhs.f);
128 return DiyFp_from_parts(lhs.f << (63 - index), lhs.e - (63 - index));
129 #elif defined(__GNUC__) && !defined(_MSC_VER)
130 int s = __builtin_clzll(lhs.f);
131 return DiyFp_from_parts(lhs.f << s, lhs.e - s);
132 #else
133 DiyFp res = lhs;
134 while (!(res.f & kDpHiddenBit)) {
135 res.f <<= 1;
136 res.e--;
137 }
138 res.f <<= (kDiySignificandSize - kDpSignificandSize - 1);
139 res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 1);
140 return res;
141 #endif
142 }
143
144 static inline DiyFp NormalizeBoundary(const DiyFp lhs) {
145 #if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__clang__)
146 unsigned long index;
147 _BitScanReverse64(&index, lhs.f);
148 return DiyFp_from_parts(lhs.f << (63 - index), lhs.e - (63 - index));
149 #else
150 DiyFp res = lhs;
151 while (!(res.f & (kDpHiddenBit << 1))) {
152 res.f <<= 1;
153 res.e--;
154 }
155 res.f <<= (kDiySignificandSize - kDpSignificandSize - 2);
156 res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2);
157 return res;
158 #endif
159 }
160
161 static inline void NormalizedBoundaries(DiyFp lhs, DiyFp *minus, DiyFp *plus) {
162 DiyFp pl = NormalizeBoundary(DiyFp_from_parts((lhs.f << 1) + 1, lhs.e - 1));
163 DiyFp mi = (lhs.f == kDpHiddenBit) ? DiyFp_from_parts((lhs.f << 2) - 1, lhs.e - 2)
164 : DiyFp_from_parts((lhs.f << 1) - 1, lhs.e - 1);
165 mi.f <<= mi.e - pl.e;
166 mi.e = pl.e;
167 *plus = pl;
168 *minus = mi;
169 }
170
171 static inline DiyFp GetCachedPower(int e, int *K) {
172 // 10^-348, 10^-340, ..., 10^340
173 static const uint64_t kCachedPowers_F[] = {
174 UINT64_C2(0xfa8fd5a0, 0x081c0288), UINT64_C2(0xbaaee17f, 0xa23ebf76), UINT64_C2(0x8b16fb20, 0x3055ac76),
175 UINT64_C2(0xcf42894a, 0x5dce35ea), UINT64_C2(0x9a6bb0aa, 0x55653b2d), UINT64_C2(0xe61acf03, 0x3d1a45df),
176 UINT64_C2(0xab70fe17, 0xc79ac6ca), UINT64_C2(0xff77b1fc, 0xbebcdc4f), UINT64_C2(0xbe5691ef, 0x416bd60c),
177 UINT64_C2(0x8dd01fad, 0x907ffc3c), UINT64_C2(0xd3515c28, 0x31559a83), UINT64_C2(0x9d71ac8f, 0xada6c9b5),
178 UINT64_C2(0xea9c2277, 0x23ee8bcb), UINT64_C2(0xaecc4991, 0x4078536d), UINT64_C2(0x823c1279, 0x5db6ce57),
179 UINT64_C2(0xc2109436, 0x4dfb5637), UINT64_C2(0x9096ea6f, 0x3848984f), UINT64_C2(0xd77485cb, 0x25823ac7),
180 UINT64_C2(0xa086cfcd, 0x97bf97f4), UINT64_C2(0xef340a98, 0x172aace5), UINT64_C2(0xb23867fb, 0x2a35b28e),
181 UINT64_C2(0x84c8d4df, 0xd2c63f3b), UINT64_C2(0xc5dd4427, 0x1ad3cdba), UINT64_C2(0x936b9fce, 0xbb25c996),
182 UINT64_C2(0xdbac6c24, 0x7d62a584), UINT64_C2(0xa3ab6658, 0x0d5fdaf6), UINT64_C2(0xf3e2f893, 0xdec3f126),
183 UINT64_C2(0xb5b5ada8, 0xaaff80b8), UINT64_C2(0x87625f05, 0x6c7c4a8b), UINT64_C2(0xc9bcff60, 0x34c13053),
184 UINT64_C2(0x964e858c, 0x91ba2655), UINT64_C2(0xdff97724, 0x70297ebd), UINT64_C2(0xa6dfbd9f, 0xb8e5b88f),
185 UINT64_C2(0xf8a95fcf, 0x88747d94), UINT64_C2(0xb9447093, 0x8fa89bcf), UINT64_C2(0x8a08f0f8, 0xbf0f156b),
186 UINT64_C2(0xcdb02555, 0x653131b6), UINT64_C2(0x993fe2c6, 0xd07b7fac), UINT64_C2(0xe45c10c4, 0x2a2b3b06),
187 UINT64_C2(0xaa242499, 0x697392d3), UINT64_C2(0xfd87b5f2, 0x8300ca0e), UINT64_C2(0xbce50864, 0x92111aeb),
188 UINT64_C2(0x8cbccc09, 0x6f5088cc), UINT64_C2(0xd1b71758, 0xe219652c), UINT64_C2(0x9c400000, 0x00000000),
189 UINT64_C2(0xe8d4a510, 0x00000000), UINT64_C2(0xad78ebc5, 0xac620000), UINT64_C2(0x813f3978, 0xf8940984),
190 UINT64_C2(0xc097ce7b, 0xc90715b3), UINT64_C2(0x8f7e32ce, 0x7bea5c70), UINT64_C2(0xd5d238a4, 0xabe98068),
191 UINT64_C2(0x9f4f2726, 0x179a2245), UINT64_C2(0xed63a231, 0xd4c4fb27), UINT64_C2(0xb0de6538, 0x8cc8ada8),
192 UINT64_C2(0x83c7088e, 0x1aab65db), UINT64_C2(0xc45d1df9, 0x42711d9a), UINT64_C2(0x924d692c, 0xa61be758),
193 UINT64_C2(0xda01ee64, 0x1a708dea), UINT64_C2(0xa26da399, 0x9aef774a), UINT64_C2(0xf209787b, 0xb47d6b85),
194 UINT64_C2(0xb454e4a1, 0x79dd1877), UINT64_C2(0x865b8692, 0x5b9bc5c2), UINT64_C2(0xc83553c5, 0xc8965d3d),
195 UINT64_C2(0x952ab45c, 0xfa97a0b3), UINT64_C2(0xde469fbd, 0x99a05fe3), UINT64_C2(0xa59bc234, 0xdb398c25),
196 UINT64_C2(0xf6c69a72, 0xa3989f5c), UINT64_C2(0xb7dcbf53, 0x54e9bece), UINT64_C2(0x88fcf317, 0xf22241e2),
197 UINT64_C2(0xcc20ce9b, 0xd35c78a5), UINT64_C2(0x98165af3, 0x7b2153df), UINT64_C2(0xe2a0b5dc, 0x971f303a),
198 UINT64_C2(0xa8d9d153, 0x5ce3b396), UINT64_C2(0xfb9b7cd9, 0xa4a7443c), UINT64_C2(0xbb764c4c, 0xa7a44410),
199 UINT64_C2(0x8bab8eef, 0xb6409c1a), UINT64_C2(0xd01fef10, 0xa657842c), UINT64_C2(0x9b10a4e5, 0xe9913129),
200 UINT64_C2(0xe7109bfb, 0xa19c0c9d), UINT64_C2(0xac2820d9, 0x623bf429), UINT64_C2(0x80444b5e, 0x7aa7cf85),
201 UINT64_C2(0xbf21e440, 0x03acdd2d), UINT64_C2(0x8e679c2f, 0x5e44ff8f), UINT64_C2(0xd433179d, 0x9c8cb841),
202 UINT64_C2(0x9e19db92, 0xb4e31ba9), UINT64_C2(0xeb96bf6e, 0xbadf77d9), UINT64_C2(0xaf87023b, 0x9bf0ee6b)};
203 static const int16_t kCachedPowers_E[] = {
204 -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954, -927, -901, -874, -847,
205 -821, -794, -768, -741, -715, -688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
206 -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, -157, -130, -103, -77, -50,
207 -24, 3, 30, 56, 83, 109, 136, 162, 189, 216, 242, 269, 295, 322, 348,
208 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, 641, 667, 694, 720, 747,
209 774, 800, 827, 853, 880, 907, 933, 960, 986, 1013, 1039, 1066};
210
211 // int k = (int )(ceil((-61 - e) * 0.30102999566398114)) + 374;
212 double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive
213 int k = (int)(dk);
214 if (k != dk) k++;
215
216 unsigned index = (unsigned)((k >> 3) + 1);
217 *K = -(-348 + (int)(index << 3)); // decimal exponent no need lookup table
218
219 assert(index < sizeof(kCachedPowers_F) / sizeof(kCachedPowers_F[0]));
220 return DiyFp_from_parts(kCachedPowers_F[index], kCachedPowers_E[index]);
221 }
222
223 static inline void GrisuRound(char *buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
224 while (rest < wp_w && delta - rest >= ten_kappa && (rest + ten_kappa < wp_w || /// closer
225 wp_w - rest > rest + ten_kappa - wp_w)) {
226 buffer[len - 1]--;
227 rest += ten_kappa;
228 }
229 }
230
231 static inline unsigned CountDecimalDigit32(uint32_t n) {
232 // Simple pure C++ implementation was faster than __builtin_clz version in this situation.
233 if (n < 10) return 1;
234 if (n < 100) return 2;
235 if (n < 1000) return 3;
236 if (n < 10000) return 4;
237 if (n < 100000) return 5;
238 if (n < 1000000) return 6;
239 if (n < 10000000) return 7;
240 if (n < 100000000) return 8;
241 if (n < 1000000000) return 9;
242 return 10;
243 }
244
245 static inline void DigitGen(const DiyFp W, const DiyFp Mp, uint64_t delta, char *buffer, int *len, int *K) {
246 static const uint32_t kPow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
247 const DiyFp one = DiyFp_from_parts((uint64_t)(1) << -Mp.e, Mp.e);
248 const DiyFp wp_w = DiyFp_subtract(Mp, W);
249 uint32_t p1 = (uint32_t)(Mp.f >> -one.e);
250 uint64_t p2 = Mp.f & (one.f - 1);
251 int kappa = (int)(CountDecimalDigit32(p1));
252 *len = 0;
253
254 while (kappa > 0) {
255 uint32_t d = 0;
256 switch (kappa) {
257 case 10:
258 d = p1 / 1000000000;
259 p1 %= 1000000000;
260 break;
261 case 9:
262 d = p1 / 100000000;
263 p1 %= 100000000;
264 break;
265 case 8:
266 d = p1 / 10000000;
267 p1 %= 10000000;
268 break;
269 case 7:
270 d = p1 / 1000000;
271 p1 %= 1000000;
272 break;
273 case 6:
274 d = p1 / 100000;
275 p1 %= 100000;
276 break;
277 case 5:
278 d = p1 / 10000;
279 p1 %= 10000;
280 break;
281 case 4:
282 d = p1 / 1000;
283 p1 %= 1000;
284 break;
285 case 3:
286 d = p1 / 100;
287 p1 %= 100;
288 break;
289 case 2:
290 d = p1 / 10;
291 p1 %= 10;
292 break;
293 case 1:
294 d = p1;
295 p1 = 0;
296 break;
297 default:
298 #if defined(_MSC_VER)
299 __assume(0);
300 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
301 __builtin_unreachable();
302 #else
303 d = 0;
304 #endif
305 }
306 if (d || *len) buffer[(*len)++] = '0' + (char)(d);
307 kappa--;
308 uint64_t tmp = ((uint64_t)(p1) << -one.e) + p2;
309 if (tmp <= delta) {
310 *K += kappa;
311 GrisuRound(buffer, *len, delta, tmp, (uint64_t)(kPow10[kappa]) << -one.e, wp_w.f);
312 return;
313 }
314 }
315
316 // kappa = 0
317 for (;;) {
318 p2 *= 10;
319 delta *= 10;
320 char d = (char)(p2 >> -one.e);
321 if (d || *len) buffer[(*len)++] = '0' + d;
322 p2 &= one.f - 1;
323 kappa--;
324 if (p2 < delta) {
325 *K += kappa;
326 GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]);
327 return;
328 }
329 }
330 }
331
332 static inline void Grisu2(double value, char *buffer, int *length, int *K) {
333 const DiyFp v = DiyFp_from_double(value);
334 DiyFp w_m, w_p;
335 NormalizedBoundaries(v, &w_m, &w_p);
336
337 const DiyFp c_mk = GetCachedPower(w_p.e, K);
338 const DiyFp W = DiyFp_multiply(Normalize(v), c_mk);
339 DiyFp Wp = DiyFp_multiply(w_p, c_mk);
340 DiyFp Wm = DiyFp_multiply(w_m, c_mk);
341 Wm.f++;
342 Wp.f--;
343 DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K);
344 }
345
346 static inline const char *GetDigitsLut() {
347 static const char cDigitsLut[200] = {
348 '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
349 '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
350 '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
351 '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
352 '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
353 '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
354 '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
355 '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
356 '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
357 '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'};
358 return cDigitsLut;
359 }
360
361 static inline void WriteExponent(int K, char *buffer) {
362 if (K < 0) {
363 *buffer++ = '-';
364 K = -K;
365 }
366
367 if (K >= 100) {
368 *buffer++ = '0' + (char)(K / 100);
369 K %= 100;
370 const char *d = GetDigitsLut() + K * 2;
371 *buffer++ = d[0];
372 *buffer++ = d[1];
373 } else if (K >= 10) {
374 const char *d = GetDigitsLut() + K * 2;
375 *buffer++ = d[0];
376 *buffer++ = d[1];
377 } else
378 *buffer++ = '0' + (char)(K);
379
380 *buffer = '\0';
381 }
382
383 static inline void Prettify(char *buffer, int length, int k) {
384 const int kk = length + k; // 10^(kk-1) <= v < 10^kk
385
386 if (length <= kk && kk <= 21) {
387 // 1234e7 -> 12340000000
388 for (int i = length; i < kk; i++)
389 buffer[i] = '0';
390 buffer[kk] = '.';
391 buffer[kk + 1] = '0';
392 buffer[kk + 2] = '\0';
393 } else if (0 < kk && kk <= 21) {
394 // 1234e-2 -> 12.34
395 memmove(&buffer[kk + 1], &buffer[kk], length - kk);
396 buffer[kk] = '.';
397 buffer[length + 1] = '\0';
398 } else if (-6 < kk && kk <= 0) {
399 // 1234e-6 -> 0.001234
400 const int offset = 2 - kk;
401 memmove(&buffer[offset], &buffer[0], length);
402 buffer[0] = '0';
403 buffer[1] = '.';
404 for (int i = 2; i < offset; i++)
405 buffer[i] = '0';
406 buffer[length + offset] = '\0';
407 } else if (length == 1) {
408 // 1e30
409 buffer[1] = 'e';
410 WriteExponent(kk - 1, &buffer[2]);
411 } else {
412 // 1234e30 -> 1.234e33
413 memmove(&buffer[2], &buffer[1], length - 1);
414 buffer[1] = '.';
415 buffer[length + 1] = 'e';
416 WriteExponent(kk - 1, &buffer[0 + length + 2]);
417 }
418 }
419
420 void emyg_dtoa(double value, char *buffer) {
421 // Not handling NaN and inf
422 assert(!isnan(value));
423 assert(!isinf(value));
424
425 if (value == 0) {
426 buffer[0] = '0';
427 buffer[1] = '.';
428 buffer[2] = '0';
429 buffer[3] = '\0';
430 } else {
431 if (value < 0) {
432 *buffer++ = '-';
433 value = -value;
434 }
435 int length, K;
436 Grisu2(value, buffer, &length, &K);
437 Prettify(buffer, length, K);
438 }
439 }