File Coverage

generic_128.c
Criterion Covered Total %
statement 0 140 0.0
branch 0 70 0.0
condition n/a
subroutine n/a
pod n/a
total 0 210 0.0


line stmt bran cond sub pod time code
1             // Copyright 2018 Ulf Adams
2             //
3             // The contents of this file may be used under the terms of the Apache License,
4             // Version 2.0.
5             //
6             // (See accompanying file LICENSE-Apache or copy at
7             // http://www.apache.org/licenses/LICENSE-2.0)
8             //
9             // Alternatively, the contents of this file may be used under the terms of
10             // the Boost Software License, Version 1.0.
11             // (See accompanying file LICENSE-Boost or copy at
12             // https://www.boost.org/LICENSE_1_0.txt)
13             //
14             // Unless required by applicable law or agreed to in writing, this software
15             // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16             // KIND, either express or implied.
17              
18             // Runtime compiler options:
19             // -DRYU_DEBUG Generate verbose debugging output to stdout.
20              
21             /* Sisyphus has applied some superficial changes to this file because perl has *
22             * not always honored "C99 mode". The location of the headers, relative to
23             * this file has also been changed */
24              
25             #include "ryu_headers/ryu_generic_128.h"
26              
27             #include
28             #include
29             #include
30             #include
31             #include
32              
33             #include "ryu_headers/generic_128.h"
34              
35             #ifdef RYU_DEBUG
36             #include
37             #include
38             static char* s(uint128_t v) {
39             int len = decimalLength(v);
40             char* b = (char*) malloc((len + 1) * sizeof(char));
41             for (int i = 0; i < len; i++) {
42             const uint32_t c = (uint32_t) (v % 10);
43             v /= 10;
44             b[len - 1 - i] = (char) ('0' + c);
45             }
46             b[len] = 0;
47             return b;
48             }
49             #endif
50              
51             #define ONE ((uint128_t) 1)
52              
53             #define FLOAT_MANTISSA_BITS 23
54             #define FLOAT_EXPONENT_BITS 8
55              
56 0           struct floating_decimal_128 float_to_fd128(float f) {
57 0           uint32_t bits = 0;
58 0           memcpy(&bits, &f, sizeof(float));
59 0           return generic_binary_to_decimal(bits, FLOAT_MANTISSA_BITS, FLOAT_EXPONENT_BITS, false);
60             }
61              
62             #define DOUBLE_MANTISSA_BITS 52
63             #define DOUBLE_EXPONENT_BITS 11
64              
65 0           struct floating_decimal_128 double_to_fd128(double d) {
66 0           uint64_t bits = 0;
67 0           memcpy(&bits, &d, sizeof(double));
68 0           return generic_binary_to_decimal(bits, DOUBLE_MANTISSA_BITS, DOUBLE_EXPONENT_BITS, false);
69             }
70              
71             #define LONG_DOUBLE_MANTISSA_BITS 64
72             #define LONG_DOUBLE_EXPONENT_BITS 15
73              
74 0           struct floating_decimal_128 long_double_to_fd128(long double d) {
75 0           uint128_t bits = 0;
76 0           memcpy(&bits, &d, sizeof(long double));
77             #ifdef RYU_DEBUG
78             // For some odd reason, this ends up with noise in the top 48 bits. We can
79             // clear out those bits with the following line; this is not required, the
80             // conversion routine should ignore those bits, but the debug output can be
81             // confusing if they aren't 0s.
82             bits &= (ONE << 80) - 1;
83             #endif
84 0           return generic_binary_to_decimal(bits, LONG_DOUBLE_MANTISSA_BITS, LONG_DOUBLE_EXPONENT_BITS, true);
85             }
86              
87 0           struct floating_decimal_128 generic_binary_to_decimal(
88             const uint128_t bits, const uint32_t mantissaBits, const uint32_t exponentBits, const bool explicitLeadingBit) {
89             #ifdef RYU_DEBUG
90             printf("IN=");
91             for (int32_t bit = 127; bit >= 0; --bit) {
92             printf("%u", (uint32_t) ((bits >> bit) & 1));
93             }
94             printf("\n");
95             #endif
96              
97 0           const uint32_t bias = (1u << (exponentBits - 1)) - 1;
98 0           const bool ieeeSign = ((bits >> (mantissaBits + exponentBits)) & 1) != 0;
99 0           const uint128_t ieeeMantissa = bits & ((ONE << mantissaBits) - 1);
100 0           const uint32_t ieeeExponent = (uint32_t) ((bits >> mantissaBits) & ((ONE << exponentBits) - 1u));
101              
102 0 0         if (ieeeExponent == 0 && ieeeMantissa == 0) {
    0          
103             struct floating_decimal_128 fd;
104 0           fd.mantissa = 0;
105 0           fd.exponent = 0;
106 0           fd.sign = ieeeSign;
107 0           return fd;
108             }
109 0 0         if (ieeeExponent == ((1u << exponentBits) - 1u)) {
110             struct floating_decimal_128 fd;
111 0 0         fd.mantissa = explicitLeadingBit ? ieeeMantissa & ((ONE << (mantissaBits - 1)) - 1) : ieeeMantissa;
112 0           fd.exponent = FD128_EXCEPTIONAL_EXPONENT;
113 0           fd.sign = ieeeSign;
114 0           return fd;
115             }
116              
117             int32_t e2;
118             uint128_t m2;
119             // We subtract 2 in all cases so that the bounds computation has 2 additional bits.
120 0 0         if (explicitLeadingBit) {
121             // mantissaBits includes the explicit leading bit, so we need to correct for that here.
122 0 0         if (ieeeExponent == 0) {
123 0           e2 = 1 - bias - mantissaBits + 1 - 2;
124             } else {
125 0           e2 = ieeeExponent - bias - mantissaBits + 1 - 2;
126             }
127 0           m2 = ieeeMantissa;
128             } else {
129 0 0         if (ieeeExponent == 0) {
130 0           e2 = 1 - bias - mantissaBits - 2;
131 0           m2 = ieeeMantissa;
132             } else {
133 0           e2 = ieeeExponent - bias - mantissaBits - 2;
134 0           m2 = (ONE << mantissaBits) | ieeeMantissa;
135             }
136             }
137 0           const bool even = (m2 & 1) == 0;
138 0           const bool acceptBounds = even;
139              
140             #ifdef RYU_DEBUG
141             printf("-> %s %s * 2^%d\n", ieeeSign ? "-" : "+", s(m2), e2 + 2);
142             #endif
143              
144             // Step 2: Determine the interval of legal decimal representations.
145 0           const uint128_t mv = 4 * m2;
146             // Implicit bool -> int conversion. True is 1, false is 0.
147 0           const uint32_t mmShift =
148 0 0         (ieeeMantissa != (explicitLeadingBit ? ONE << (mantissaBits - 1) : 0))
149 0 0         || (ieeeExponent == 0);
    0          
150              
151             // Step 3: Convert to a decimal power base using 128-bit arithmetic.
152             uint128_t vr, vp, vm;
153             int32_t e10;
154 0           bool vmIsTrailingZeros = false;
155 0           bool vrIsTrailingZeros = false;
156 0 0         if (e2 >= 0) {
157             // I tried special-casing q == 0, but there was no effect on performance.
158             // This expression is slightly faster than max(0, log10Pow2(e2) - 1).
159 0           const uint32_t q = log10Pow2(e2) - (e2 > 3);
160 0           e10 = q;
161 0           const int32_t k = FLOAT_128_POW5_INV_BITCOUNT + pow5bits(q) - 1;
162 0           const int32_t i = -e2 + q + k;
163             uint64_t pow5[4];
164 0           generic_computeInvPow5(q, pow5);
165 0           vr = mulShift(4 * m2, pow5, i);
166 0           vp = mulShift(4 * m2 + 2, pow5, i);
167 0           vm = mulShift(4 * m2 - 1 - mmShift, pow5, i);
168             #ifdef RYU_DEBUG
169             printf("%s * 2^%d / 10^%d\n", s(mv), e2, q);
170             printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
171             #endif
172             // floor(log_5(2^128)) = 55, this is very conservative
173 0 0         if (q <= 55) {
174             // Only one of mp, mv, and mm can be a multiple of 5, if any.
175 0 0         if (mv % 5 == 0) {
176 0           vrIsTrailingZeros = multipleOfPowerOf5(mv, q - 1);
177 0 0         } else if (acceptBounds) {
178             // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
179             // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
180             // <=> true && pow5Factor(mm) >= q, since e2 >= q.
181 0           vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
182             } else {
183             // Same as min(e2 + 1, pow5Factor(mp)) >= q.
184 0           vp -= multipleOfPowerOf5(mv + 2, q);
185             }
186             }
187             } else {
188             // This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
189 0           const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
190 0           e10 = q + e2;
191 0           const int32_t i = -e2 - q;
192 0           const int32_t k = pow5bits(i) - FLOAT_128_POW5_BITCOUNT;
193 0           const int32_t j = q - k;
194             uint64_t pow5[4];
195 0           generic_computePow5(i, pow5);
196 0           vr = mulShift(4 * m2, pow5, j);
197 0           vp = mulShift(4 * m2 + 2, pow5, j);
198 0           vm = mulShift(4 * m2 - 1 - mmShift, pow5, j);
199             #ifdef RYU_DEBUG
200             printf("%s * 5^%d / 10^%d\n", s(mv), -e2, q);
201             printf("%d %d %d %d\n", q, i, k, j);
202             printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
203             #endif
204 0 0         if (q <= 1) {
205             // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
206             // mv = 4 m2, so it always has at least two trailing 0 bits.
207 0           vrIsTrailingZeros = true;
208 0 0         if (acceptBounds) {
209             // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
210 0           vmIsTrailingZeros = mmShift == 1;
211             } else {
212             // mp = mv + 2, so it always has at least one trailing 0 bit.
213 0           --vp;
214             }
215 0 0         } else if (q < 127) { // TODO(ulfjack): Use a tighter bound here.
216             // We need to compute min(ntz(mv), pow5Factor(mv) - e2) >= q-1
217             // <=> ntz(mv) >= q-1 && pow5Factor(mv) - e2 >= q-1
218             // <=> ntz(mv) >= q-1 (e2 is negative and -e2 >= q)
219             // <=> (mv & ((1 << (q-1)) - 1)) == 0
220             // We also need to make sure that the left shift does not overflow.
221 0           vrIsTrailingZeros = multipleOfPowerOf2(mv, q - 1);
222             #ifdef RYU_DEBUG
223             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
224             #endif
225             }
226             }
227             #ifdef RYU_DEBUG
228             printf("e10=%d\n", e10);
229             printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
230             printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
231             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
232             #endif
233              
234             // Step 4: Find the shortest decimal representation in the interval of legal representations.
235 0           uint32_t removed = 0;
236 0           uint8_t lastRemovedDigit = 0;
237             uint128_t output;
238              
239 0 0         while (vp / 10 > vm / 10) {
240 0           vmIsTrailingZeros &= vm % 10 == 0;
241 0           vrIsTrailingZeros &= lastRemovedDigit == 0;
242 0           lastRemovedDigit = (uint8_t) (vr % 10);
243 0           vr /= 10;
244 0           vp /= 10;
245 0           vm /= 10;
246 0           ++removed;
247             }
248             #ifdef RYU_DEBUG
249             printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
250             printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
251             #endif
252 0 0         if (vmIsTrailingZeros) {
253 0 0         while (vm % 10 == 0) {
254 0           vrIsTrailingZeros &= lastRemovedDigit == 0;
255 0           lastRemovedDigit = (uint8_t) (vr % 10);
256 0           vr /= 10;
257 0           vp /= 10;
258 0           vm /= 10;
259 0           ++removed;
260             }
261             }
262             #ifdef RYU_DEBUG
263             printf("%s %d\n", s(vr), lastRemovedDigit);
264             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
265             #endif
266 0 0         if (vrIsTrailingZeros && (lastRemovedDigit == 5) && (vr % 2 == 0)) {
    0          
    0          
267             // Round even if the exact numbers is .....50..0.
268 0           lastRemovedDigit = 4;
269             }
270             // We need to take vr+1 if vr is outside bounds or we need to round up.
271 0           output = vr +
272 0 0         ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || (lastRemovedDigit >= 5));
    0          
    0          
    0          
273 0           const int32_t exp = e10 + removed;
274              
275             #ifdef RYU_DEBUG
276             printf("V+=%s\nV =%s\nV-=%s\n", s(vp), s(vr), s(vm));
277             printf("O=%s\n", s(output));
278             printf("EXP=%d\n", exp);
279             #endif
280              
281             struct floating_decimal_128 fd;
282 0           fd.mantissa = output;
283 0           fd.exponent = exp;
284 0           fd.sign = ieeeSign;
285 0           return fd;
286             }
287              
288 0           static inline int copy_special_str(char * const result, const struct floating_decimal_128 fd) {
289 0 0         if (fd.mantissa) {
290 0           memcpy(result, "NaN", 3);
291 0           return 3;
292             }
293 0 0         if (fd.sign) {
294 0           result[0] = '-';
295             }
296 0           memcpy(result + fd.sign, "Infinity", 8);
297 0           return fd.sign + 8;
298             }
299              
300 0           int generic_to_chars(const struct floating_decimal_128 v, char* const result) {
301 0 0         if (v.exponent == FD128_EXCEPTIONAL_EXPONENT) {
302 0           return copy_special_str(result, v);
303             }
304              
305             // Step 5: Print the decimal representation.
306 0           int index = 0;
307 0 0         if (v.sign) {
308 0           result[index++] = '-';
309             }
310              
311             uint32_t i;
312 0           uint128_t output = v.mantissa;
313 0           const uint32_t olength = decimalLength(output);
314              
315             #ifdef RYU_DEBUG
316             printf("DIGITS=%s\n", s(v.mantissa));
317             printf("OLEN=%u\n", olength);
318             printf("EXP=%u\n", v.exponent + olength);
319             #endif
320              
321 0 0         for (i = 0; i < olength - 1; ++i) {
322 0           const uint32_t c = (uint32_t) (output % 10);
323 0           output /= 10;
324 0           result[index + olength - i] = (char) ('0' + c);
325             }
326 0           result[index] = '0' + (uint32_t) (output % 10); // output should be < 10 by now.
327              
328             // Print decimal point if needed.
329 0 0         if (olength > 1) {
330 0           result[index + 1] = '.';
331 0           index += olength + 1;
332             } else {
333 0           ++index;
334             }
335              
336             // Print the exponent.
337 0           result[index++] = 'E';
338 0           int32_t exp = v.exponent + olength - 1;
339 0 0         if (exp < 0) {
340 0           result[index++] = '-';
341 0           exp = -exp;
342             }
343              
344 0           uint32_t elength = decimalLength(exp);
345 0 0         for (i = 0; i < elength; ++i) {
346 0           const uint32_t c = exp % 10;
347 0           exp /= 10;
348 0           result[index + elength - 1 - i] = (char) ('0' + c);
349             }
350 0           index += elength;
351 0           return index;
352             }