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
|
// Copyright 2018 Ulf Adams
//
// The contents of this file may be used under the terms of the Apache License,
// Version 2.0.
//
// (See accompanying file LICENSE-Apache or copy at
// http://www.apache.org/licenses/LICENSE-2.0)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
// (See accompanying file LICENSE-Boost or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
// Runtime compiler options:
// -DRYU_DEBUG Generate verbose debugging output to stdout.
#ifdef RYU_DEBUG
static char* s(uint128_t v) {
int len = decimalLength(v);
char* b = (char*) malloc((len + 1) * sizeof(char));
for (int i = 0; i < len; i++) {
const uint32_t c = (uint32_t) (v % 10);
v /= 10;
b[len - 1 - i] = (char) ('0' + c);
}
b[len] = 0;
return b;
}
#endif
#define ONE ((uint128_t) 1)
struct floating_decimal_128 generic_binary_to_decimal(
const uint128_t ieeeMantissa, const uint32_t ieeeExponent, const bool ieeeSign,
const uint32_t mantissaBits, const uint32_t exponentBits, const bool explicitLeadingBit) {
#ifdef RYU_DEBUG
printf("IN=");
for (int32_t bit = 127; bit >= 0; --bit) {
printf("%u", (uint32_t) ((bits >> bit) & 1));
}
printf("\n");
#endif
const uint32_t bias = (1u << (exponentBits - 1)) - 1;
if (ieeeExponent == 0 && ieeeMantissa == 0) {
struct floating_decimal_128 fd;
fd.mantissa = 0;
fd.exponent = 0;
fd.sign = ieeeSign;
return fd;
}
if (ieeeExponent == ((1u << exponentBits) - 1u)) {
struct floating_decimal_128 fd;
fd.mantissa = explicitLeadingBit ? ieeeMantissa & ((ONE << (mantissaBits - 1)) - 1) : ieeeMantissa;
fd.exponent = FD128_EXCEPTIONAL_EXPONENT;
fd.sign = ieeeSign;
return fd;
}
int32_t e2;
uint128_t m2;
// We subtract 2 in all cases so that the bounds computation has 2 additional bits.
if (explicitLeadingBit) {
// mantissaBits includes the explicit leading bit, so we need to correct for that here.
if (ieeeExponent == 0) {
e2 = 1 - bias - mantissaBits + 1 - 2;
} else {
e2 = ieeeExponent - bias - mantissaBits + 1 - 2;
}
m2 = ieeeMantissa;
} else {
if (ieeeExponent == 0) {
e2 = 1 - bias - mantissaBits - 2;
m2 = ieeeMantissa;
} else {
e2 = ieeeExponent - bias - mantissaBits - 2;
m2 = (ONE << mantissaBits) | ieeeMantissa;
}
}
const bool even = (m2 & 1) == 0;
const bool acceptBounds = even;
#ifdef RYU_DEBUG
printf("-> %s %s * 2^%d\n", ieeeSign ? "-" : "+", s(m2), e2 + 2);
#endif
// Step 2: Determine the interval of legal decimal representations.
const uint128_t mv = 4 * m2;
// Implicit bool -> int conversion. True is 1, false is 0.
const uint32_t mmShift =
(ieeeMantissa != (explicitLeadingBit ? ONE << (mantissaBits - 1) : 0))
|| (ieeeExponent == 0);
// Step 3: Convert to a decimal power base using 128-bit arithmetic.
uint128_t vr, vp, vm;
int32_t e10;
bool vmIsTrailingZeros = false;
bool vrIsTrailingZeros = false;
if (e2 >= 0) {
// I tried special-casing q == 0, but there was no effect on performance.
// This expression is slightly faster than max(0, log10Pow2(e2) - 1).
const uint32_t q = log10Pow2(e2) - (e2 > 3);
e10 = q;
const int32_t k = FLOAT_128_POW5_INV_BITCOUNT + pow5bits(q) - 1;
const int32_t i = -e2 + q + k;
uint64_t pow5[4];
generic_computeInvPow5(q, pow5);
vr = mulShift(4 * m2, pow5, i);
vp = mulShift(4 * m2 + 2, pow5, i);
vm = mulShift(4 * m2 - 1 - mmShift, pow5, i);
#ifdef RYU_DEBUG
printf("%s * 2^%d / 10^%d\n", s(mv), e2, q);
printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
#endif
// floor(log_5(2^128)) = 55, this is very conservative
if (q <= 55) {
// Only one of mp, mv, and mm can be a multiple of 5, if any.
if (mv % 5 == 0) {
vrIsTrailingZeros = multipleOfPowerOf5(mv, q - 1);
} else if (acceptBounds) {
// Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
// <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
// <=> true && pow5Factor(mm) >= q, since e2 >= q.
vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
} else {
// Same as min(e2 + 1, pow5Factor(mp)) >= q.
vp -= multipleOfPowerOf5(mv + 2, q);
}
}
} else {
// This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
e10 = q + e2;
const int32_t i = -e2 - q;
const int32_t k = pow5bits(i) - FLOAT_128_POW5_BITCOUNT;
const int32_t j = q - k;
uint64_t pow5[4];
generic_computePow5(i, pow5);
vr = mulShift(4 * m2, pow5, j);
vp = mulShift(4 * m2 + 2, pow5, j);
vm = mulShift(4 * m2 - 1 - mmShift, pow5, j);
#ifdef RYU_DEBUG
printf("%s * 5^%d / 10^%d\n", s(mv), -e2, q);
printf("%d %d %d %d\n", q, i, k, j);
printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
#endif
if (q <= 1) {
// {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
// mv = 4 m2, so it always has at least two trailing 0 bits.
vrIsTrailingZeros = true;
if (acceptBounds) {
// mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
vmIsTrailingZeros = mmShift == 1;
} else {
// mp = mv + 2, so it always has at least one trailing 0 bit.
--vp;
}
} else if (q < 127) { // TODO(ulfjack): Use a tighter bound here.
// We need to compute min(ntz(mv), pow5Factor(mv) - e2) >= q-1
// <=> ntz(mv) >= q-1 && pow5Factor(mv) - e2 >= q-1
// <=> ntz(mv) >= q-1 (e2 is negative and -e2 >= q)
// <=> (mv & ((1 << (q-1)) - 1)) == 0
// We also need to make sure that the left shift does not overflow.
vrIsTrailingZeros = multipleOfPowerOf2(mv, q - 1);
#ifdef RYU_DEBUG
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
#endif
}
}
#ifdef RYU_DEBUG
printf("e10=%d\n", e10);
printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
#endif
// Step 4: Find the shortest decimal representation in the interval of legal representations.
uint32_t removed = 0;
uint8_t lastRemovedDigit = 0;
uint128_t output;
while (vp / 10 > vm / 10) {
vmIsTrailingZeros &= vm % 10 == 0;
vrIsTrailingZeros &= lastRemovedDigit == 0;
lastRemovedDigit = (uint8_t) (vr % 10);
vr /= 10;
vp /= 10;
vm /= 10;
++removed;
}
#ifdef RYU_DEBUG
printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
#endif
if (vmIsTrailingZeros) {
while (vm % 10 == 0) {
vrIsTrailingZeros &= lastRemovedDigit == 0;
lastRemovedDigit = (uint8_t) (vr % 10);
vr /= 10;
vp /= 10;
vm /= 10;
++removed;
}
}
#ifdef RYU_DEBUG
printf("%s %d\n", s(vr), lastRemovedDigit);
printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
#endif
if (vrIsTrailingZeros && (lastRemovedDigit == 5) && (vr % 2 == 0)) {
// Round even if the exact numbers is .....50..0.
lastRemovedDigit = 4;
}
// We need to take vr+1 if vr is outside bounds or we need to round up.
output = vr +
((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || (lastRemovedDigit >= 5));
const int32_t exp = e10 + removed;
#ifdef RYU_DEBUG
printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
printf("O=%s\n", s(output));
printf("EXP=%d\n", exp);
#endif
struct floating_decimal_128 fd;
fd.mantissa = output;
fd.exponent = exp;
fd.sign = ieeeSign;
return fd;
}
static inline int copy_special_str(char * const result, const struct floating_decimal_128 fd) {
if (fd.mantissa) {
memcpy(result, "NaN", 3);
return 3;
}
if (fd.sign) {
result[0] = '-';
}
memcpy(result + fd.sign, "Infinity", 8);
return fd.sign + 8;
}
int generic_to_chars(const struct floating_decimal_128 v, char* const result) {
if (v.exponent == FD128_EXCEPTIONAL_EXPONENT) {
return copy_special_str(result, v);
}
// Step 5: Print the decimal representation.
int index = 0;
if (v.sign) {
result[index++] = '-';
}
uint128_t output = v.mantissa;
const uint32_t olength = decimalLength(output);
#ifdef RYU_DEBUG
printf("DIGITS=%s\n", s(v.mantissa));
printf("OLEN=%u\n", olength);
printf("EXP=%u\n", v.exponent + olength);
#endif
for (uint32_t i = 0; i < olength - 1; ++i) {
const uint32_t c = (uint32_t) (output % 10);
output /= 10;
result[index + olength - i] = (char) ('0' + c);
}
result[index] = '0' + (uint32_t) (output % 10); // output should be < 10 by now.
// Print decimal point if needed.
if (olength > 1) {
result[index + 1] = '.';
index += olength + 1;
} else {
++index;
}
// Print the exponent.
result[index++] = 'e';
int32_t exp = v.exponent + olength - 1;
if (exp < 0) {
result[index++] = '-';
exp = -exp;
} else
result[index++] = '+';
uint32_t elength = decimalLength(exp);
if (elength == 1)
++elength;
for (uint32_t i = 0; i < elength; ++i) {
const uint32_t c = exp % 10;
exp /= 10;
result[index + elength - 1 - i] = (char) ('0' + c);
}
index += elength;
return index;
}
|