aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/fpconv/fpconv.c
blob: f8b601a55d9e0a5ae8a45d3eed4ffa78c47ce726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <stdbool.h>
#include <string.h>
#include <sys/param.h>

#include "fpconv.h"
#include "powers.h"

#define fracmask  0x000FFFFFFFFFFFFFU
#define expmask   0x7FF0000000000000U
#define hiddenbit 0x0010000000000000U
#define signmask  0x8000000000000000U
#define expbias   (1023 + 52)

#define absv(n) ((n) < 0 ? -(n) : (n))
#define minv(a, b) ((a) < (b) ? (a) : (b))

static uint64_t tens[] = {
		10000000000000000000U, 1000000000000000000U, 100000000000000000U,
		10000000000000000U, 1000000000000000U, 100000000000000U,
		10000000000000U, 1000000000000U, 100000000000U,
		10000000000U, 1000000000U, 100000000U,
		10000000U, 1000000U, 100000U,
		10000U, 1000U, 100U,
		10U, 1U
};

static inline uint64_t get_dbits (double d) {
	union {
		double dbl;
		uint64_t i;
	} dbl_bits = {d};

	return dbl_bits.i;
}

static Fp build_fp (double d) {
	uint64_t bits = get_dbits (d);

	Fp fp;
	fp.frac = bits & fracmask;
	fp.exp = (bits & expmask) >> 52u;

	if (fp.exp) {
		fp.frac += hiddenbit;
		fp.exp -= expbias;

	}
	else {
		fp.exp = -expbias + 1;
	}

	return fp;
}

static void normalize (Fp *fp) {
	while ((fp->frac & hiddenbit) == 0) {
		fp->frac <<= 1u;
		fp->exp--;
	}

	const unsigned int shift = 64 - 52 - 1;
	fp->frac <<= shift;
	fp->exp -= shift;
}

static void get_normalized_boundaries (Fp *fp, Fp *lower, Fp *upper) {
	upper->frac = (fp->frac << 1u) + 1u;
	upper->exp = fp->exp - 1u;

	while ((upper->frac & (hiddenbit << 1u)) == 0) {
		upper->frac <<= 1u;
		upper->exp--;
	}

	const unsigned int u_shift = 64 - 52 - 2;

	upper->frac <<= u_shift;
	upper->exp = upper->exp - u_shift;


	unsigned int l_shift = fp->frac == hiddenbit ? 2u : 1u;

	lower->frac = (fp->frac << l_shift) - 1;
	lower->exp = fp->exp - l_shift;


	lower->frac <<= lower->exp - upper->exp;
	lower->exp = upper->exp;
}

static Fp multiply (Fp *a, Fp *b) {
	const uint64_t lomask = 0x00000000FFFFFFFFu;

	uint64_t ah_bl = (a->frac >> 32u) * (b->frac & lomask);
	uint64_t al_bh = (a->frac & lomask) * (b->frac >> 32u);
	uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask);
	uint64_t ah_bh = (a->frac >> 32u) * (b->frac >> 32u);

	uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32u);
	/* round up */
	tmp += 1U << 31u;

	Fp fp = {
			ah_bh + (ah_bl >> 32u) + (al_bh >> 32u) + (tmp >> 32u),
			a->exp + b->exp + 64u
	};

	return fp;
}

static void round_digit (char *digits, int ndigits, uint64_t delta, uint64_t rem, uint64_t kappa, uint64_t frac) {
	while (rem < frac && delta - rem >= kappa &&
		   (rem + kappa < frac || frac - rem > rem + kappa - frac)) {

		digits[ndigits - 1]--;
		rem += kappa;
	}
}

static int generate_digits (Fp *fp, Fp *upper, Fp *lower, char *digits, int *K) {
	uint64_t wfrac = upper->frac - fp->frac;
	uint64_t delta = upper->frac - lower->frac;

	Fp one;
	one.frac = 1ULL << -upper->exp;
	one.exp = upper->exp;

	uint64_t part1 = upper->frac >> -one.exp;
	uint64_t part2 = upper->frac & (one.frac - 1);

	int idx = 0, kappa = 10;
	uint64_t *divp;
	/* 1000000000 */
	for (divp = tens + 10; kappa > 0; divp++) {

		uint64_t div = *divp;
		unsigned digit = part1 / div;

		if (digit || idx) {
			digits[idx++] = digit + '0';
		}

		part1 -= digit * div;
		kappa--;

		uint64_t tmp = (part1 << -one.exp) + part2;
		if (tmp <= delta) {
			*K += kappa;
			round_digit (digits, idx, delta, tmp, div << -one.exp, wfrac);

			return idx;
		}
	}

	/* 10 */
	uint64_t *unit = tens + 18;

	while (true) {
		part2 *= 10;
		delta *= 10;
		kappa--;

		unsigned digit = part2 >> -one.exp;
		if (digit || idx) {
			digits[idx++] = digit + '0';
		}

		part2 &= one.frac - 1;
		if (part2 < delta) {
			*K += kappa;
			round_digit (digits, idx, delta, part2, one.frac, wfrac * *unit);

			return idx;
		}

		unit--;
	}
}

static int grisu2 (double d, char *digits, int *K) {
	Fp w = build_fp (d);

	Fp lower, upper;
	get_normalized_boundaries (&w, &lower, &upper);

	normalize (&w);

	int k;
	Fp cp = find_cachedpow10 (upper.exp, &k);

	w = multiply (&w, &cp);
	upper = multiply (&upper, &cp);
	lower = multiply (&lower, &cp);

	lower.frac++;
	upper.frac--;

	*K = -k;

	return generate_digits (&w, &upper, &lower, digits, K);
}

static inline int emit_integer (char *digits, int ndigits,
								char *dest, int K, bool neg,
								unsigned precision)
{
	char *d = dest;

	memcpy (d, digits, ndigits);
	d += ndigits;
	memset (d, '0', K);
	d += K;

	precision = MIN(precision, FPCONV_BUFLEN - (ndigits + K + 1));

	if (precision) {
		*d++ = '.';
		memset (d, '0', precision);
		d += precision;
	}

	return d - dest;
}

static inline int emit_scientific_digits (char *digits, int ndigits,
									 char *dest, int K, bool neg,
									 unsigned precision, int exp)
{
	/* write decimal w/ scientific notation */
	ndigits = minv(ndigits, 18 - neg);

	int idx = 0;
	dest[idx++] = digits[0];

	if (ndigits > 1) {
		dest[idx++] = '.';
		memcpy(dest + idx, digits + 1, ndigits - 1);
		idx += ndigits - 1;
	}

	dest[idx++] = 'e';

	char sign = K + ndigits - 1 < 0 ? '-' : '+';
	dest[idx++] = sign;

	int cent = 0;

	if (exp > 99) {
		cent = exp / 100;
		dest[idx++] = cent + '0';
		exp -= cent * 100;
	}
	if (exp > 9) {
		int dec = exp / 10;
		dest[idx++] = dec + '0';
		exp -= dec * 10;

	}
	else if (cent) {
		dest[idx++] = '0';
	}

	dest[idx++] = exp % 10 + '0';

	return idx;
}

static inline int emit_fixed_digits (char *digits, int ndigits,
									 char *dest, int K, bool neg,
									 unsigned precision, int exp)
{
	int offset = ndigits - absv(K), to_print;
	/* fp < 1.0 -> write leading zero */
	if (K < 0) {
		if (offset <= 0) {
			if (precision) {
				if (-offset >= precision) {
					/* Just print 0.[0]{precision} */
					dest[0] = '0';
					dest[1] = '.';
					memset(dest + 2, '0', precision);

					return precision + 2;
				}

				to_print = MAX(ndigits - offset, precision);
			}
			else {
				to_print = ndigits - offset;
			}

			if (to_print <= FPCONV_BUFLEN - 3) {
				offset = -offset;
				dest[0] = '0';
				dest[1] = '.';
				memset(dest + 2, '0', offset);

				if (precision) {
					/* The case where offset > precision is covered previously */
					precision -= offset;

					if (precision <= ndigits) {
						/* Truncate or leave as is */
						memcpy(dest + offset + 2, digits, precision);

						return precision + 2 + offset;
					}
					else {
						/* Expand */
						memcpy(dest + offset + 2, digits, ndigits);
						precision -= ndigits;
						memset(dest + offset + 2 + ndigits, '0', precision);

						return ndigits + 2 + offset + precision;
					}
				}
				else {
					memcpy(dest + offset + 2, digits, ndigits);
				}

				return ndigits + 2 + offset;
			}
			else {
				return emit_scientific_digits (digits, ndigits, dest, K, neg, precision, exp);
			}
		}
		else {
			/*
			 * fp > 1.0, if offset > 0 then we have less digits than
			 * fp exponent, so we need to switch to scientific notation to
			 * display number at least more or less precisely
			 */
			if (offset > 0 && ndigits <= FPCONV_BUFLEN - 3) {
				char *d = dest;
				memcpy(d, digits, offset);
				d += offset;
				*d++ = '.';

				ndigits -= offset;

				if (precision) {
					if (ndigits >= precision) {
						/* Truncate or leave as is */
						memcpy(d, digits + offset, precision);
						d += precision;
					}
					else {
						/* Expand */
						memcpy(d, digits + offset, ndigits);
						precision -= ndigits;
						d += ndigits;

						/* Check if we have enough bufspace */
						if ((d - dest) + precision <= FPCONV_BUFLEN) {
							memset (d, '0', precision);
							d += precision;
						}
						else {
							memset (d, '0', FPCONV_BUFLEN - (d - dest));
							d += FPCONV_BUFLEN - (d - dest);
						}
					}
				}
				else {
					memcpy(d, digits + offset, ndigits);
					d += ndigits;
				}

				return d - dest;
			}
		}
	}

	return emit_scientific_digits (digits, ndigits, dest, K, neg, precision, exp);
}

static int emit_digits (char *digits, int ndigits, char *dest, int K, bool neg,
						unsigned precision, bool scientific)
{
	int exp = absv(K + ndigits - 1);

	/* write plain integer */
	if (K >= 0 && (exp < (ndigits + 7))) {
		return emit_integer (digits, ndigits, dest, K, neg, precision);
	}

	/* write decimal w/o scientific notation */
	if (!scientific || (K < 0 && (K > -7 || exp < 4))) {
		return emit_fixed_digits (digits, ndigits, dest, K, neg, precision, exp);
	}

	return emit_scientific_digits (digits, ndigits, dest, K, neg, precision, exp);
}

static int filter_special (double fp, char *dest, unsigned precision)
{
	int nchars = 3;
	char *d = dest;

	if (fp == 0.0) {
		if (get_dbits (fp) & signmask) {
			*d++ = '-';
			*d++ = '0';
		}
		else {
			*d++ = '0';
		}

		if (precision) {
			*d ++ = '.';
			memset (d, '0', precision);
		}

		return d - dest + precision;
	}

	uint64_t bits = get_dbits (fp);

	bool nan = (bits & expmask) == expmask;

	if (!nan) {
		return 0;
	}

	if (bits & fracmask) {
		dest[0] = 'n';
		dest[1] = 'a';
		dest[2] = 'n';
	}
	else {
		if (get_dbits (fp) & signmask) {
			dest[0] = '-';
			dest[1] = 'i';
			dest[2] = 'n';
			dest[3] = 'f';
			nchars = 4;
		}
		else {
			dest[0] = 'i';
			dest[1] = 'n';
			dest[2] = 'f';
		}
	}

	return nchars;
}

int
fpconv_dtoa (double d, char dest[FPCONV_BUFLEN],
			 unsigned precision, bool scientific)
{
	char digits[18];

	int str_len = 0;
	bool neg = false;

	if (precision > FPCONV_BUFLEN - 5) {
		precision = FPCONV_BUFLEN - 5;
	}

	int spec = filter_special (d, dest, precision);

	if (spec) {
		return spec;
	}

	if (get_dbits (d) & signmask) {
		dest[0] = '-';
		str_len++;
		neg = true;
	}

	int K = 0;
	int ndigits = grisu2 (d, digits, &K);

	str_len += emit_digits (digits, ndigits, dest + str_len, K, neg, precision,
			scientific);

	return str_len;
}