~jsvoboda/helenos/dnsr

« back to all changes in this revision

Viewing changes to uspace/lib/c/generic/double_to_str.c

  • Committer: Jiri Svoboda
  • Date: 2012-11-11 21:31:03 UTC
  • mfrom: (1527.1.178 mainline)
  • Revision ID: jiri@wiwaxia-20121111213103-314bmkettwvlwj97
MergeĀ mainlineĀ changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2012 Adam Hraska
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * - Redistributions of source code must retain the above copyright
 
10
 *   notice, this list of conditions and the following disclaimer.
 
11
 * - Redistributions in binary form must reproduce the above copyright
 
12
 *   notice, this list of conditions and the following disclaimer in the
 
13
 *   documentation and/or other materials provided with the distribution.
 
14
 * - The name of the author may not be used to endorse or promote products
 
15
 *   derived from this software without specific prior written permission.
 
16
 *
 
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 */
 
28
 
 
29
#include <double_to_str.h>
 
30
 
 
31
#include "private/power_of_ten.h"
 
32
#include <ieee_double.h>
 
33
 
 
34
#include <stdint.h>
 
35
#include <assert.h>
 
36
#include <bool.h>
 
37
 
 
38
/*
 
39
 * Floating point numbers are converted from their binary representation
 
40
 * into a decimal string using the algorithm described in:
 
41
 *   Printing floating-point numbers quickly and accurately with integers
 
42
 *   Loitsch, 2010
 
43
 */
 
44
 
 
45
/** The computation assumes a significand of 64 bits. */
 
46
static const int significand_width = 64;
 
47
 
 
48
/* Scale exponents to interval [alpha, gamma] to simplify conversion. */
 
49
static const int alpha = -59;
 
50
static const int gamma = -32;
 
51
 
 
52
 
 
53
/** Returns true if the most-significant bit of num.significand is set. */
 
54
static bool is_normalized(fp_num_t num)
 
55
{
 
56
        assert(8*sizeof(num.significand) == significand_width);
 
57
 
 
58
        /* Normalized == most significant bit of the significand is set. */
 
59
        return (num.significand & (1ULL << (significand_width - 1))) != 0;
 
60
}
 
61
 
 
62
/** Returns a normalized num with the MSbit set. */
 
63
static fp_num_t normalize(fp_num_t num)
 
64
{
 
65
        const uint64_t top10bits = 0xffc0000000000000ULL;
 
66
 
 
67
        /* num usually comes from ieee_double with top 10 bits zero. */
 
68
        while (0 == (num.significand & top10bits)) {
 
69
                num.significand <<= 10;
 
70
                num.exponent -= 10;
 
71
        }
 
72
 
 
73
        while (!is_normalized(num)) {
 
74
                num.significand <<= 1;
 
75
                --num.exponent;
 
76
        }
 
77
 
 
78
        return num;
 
79
}
 
80
 
 
81
 
 
82
/** Returns x * y with an error of less than 0.5 ulp. */
 
83
static fp_num_t multiply(fp_num_t x, fp_num_t y)
 
84
{
 
85
        assert(/* is_normalized(x) && */ is_normalized(y));
 
86
        
 
87
        const uint32_t low_bits = -1;
 
88
 
 
89
        uint64_t a, b, c, d;
 
90
        a = x.significand >> 32;
 
91
        b = x.significand & low_bits;
 
92
        c = y.significand >> 32;
 
93
        d = y.significand & low_bits;
 
94
 
 
95
        uint64_t bd, ad, bc, ac;
 
96
        bd = b * d;
 
97
        ad = a * d;
 
98
        
 
99
        bc = b * c;
 
100
        ac = a * c;
 
101
 
 
102
        /* Denote 32 bit parts of x a y as: x == a b, y == c d. Then:
 
103
         *        a  b    
 
104
         *  *     c  d
 
105
         *  ----------
 
106
         *       ad bd .. multiplication of 32bit parts results in 64bit parts
 
107
         *  + ac bc
 
108
         *  ----------
 
109
         *       [b|d] .. Depicts 64 bit intermediate results and how
 
110
         *     [a|d]      the 32 bit parts of these results overlap and
 
111
         *     [b|c]      contribute to the final result.
 
112
         *  +[a|c]
 
113
         *  ----------
 
114
         *   [ret]
 
115
         *  [tmp]
 
116
         */
 
117
        uint64_t tmp = (bd >> 32) + (ad & low_bits) + (bc & low_bits);
 
118
 
 
119
        /* Round upwards. */
 
120
        tmp += 1U << 31;
 
121
 
 
122
        fp_num_t ret;
 
123
        ret.significand = ac + (bc >> 32) + (ad >> 32) + (tmp >> 32);
 
124
        ret.exponent = x.exponent + y.exponent + significand_width;
 
125
        
 
126
        return ret;                     
 
127
}
 
128
 
 
129
 
 
130
/** Returns a - b. Both must have the same exponent. */
 
131
static fp_num_t subtract(fp_num_t a, fp_num_t b)
 
132
{
 
133
        assert(a.exponent == b.exponent);
 
134
        assert(a.significand >= b.significand);
 
135
 
 
136
        fp_num_t result;
 
137
        
 
138
        result.significand = a.significand - b.significand;
 
139
        result.exponent = a.exponent;
 
140
 
 
141
        return result;
 
142
}
 
143
 
 
144
 
 
145
/** Returns the interval [low, high] of numbers that convert to binary val. */
 
146
static void get_normalized_bounds(ieee_double_t val, fp_num_t *high, 
 
147
        fp_num_t *low, fp_num_t *val_dist)
 
148
{
 
149
        /* 
 
150
         * Only works if val comes directly from extract_ieee_double without
 
151
         * being manipulated in any way (eg it must not be normalized). 
 
152
         */
 
153
        assert(!is_normalized(val.pos_val));
 
154
 
 
155
        high->significand = (val.pos_val.significand << 1) + 1;
 
156
        high->exponent = val.pos_val.exponent - 1;
 
157
 
 
158
        /* val_dist = high - val */
 
159
        val_dist->significand = 1;
 
160
        val_dist->exponent = val.pos_val.exponent - 1;
 
161
 
 
162
        /* Distance from both lower and upper bound is the same. */
 
163
        if (!val.is_accuracy_step) {
 
164
                low->significand = (val.pos_val.significand << 1) - 1;
 
165
                low->exponent = val.pos_val.exponent - 1;
 
166
        } else {
 
167
                low->significand = (val.pos_val.significand << 2) - 1;
 
168
                low->exponent = val.pos_val.exponent - 2;
 
169
        }
 
170
 
 
171
        *high = normalize(*high);
 
172
 
 
173
        /* 
 
174
         * Lower bound may not be normalized if subtracting 1 unit
 
175
         * reset the most-significant bit to 0. 
 
176
         */
 
177
        low->significand = low->significand << (low->exponent - high->exponent);
 
178
        low->exponent = high->exponent;
 
179
 
 
180
        val_dist->significand = 
 
181
                val_dist->significand << (val_dist->exponent - high->exponent);
 
182
        val_dist->exponent = high->exponent;
 
183
}
 
184
 
 
185
/** Determines the interval of numbers that have the binary representation 
 
186
 *  of val.
 
187
 * 
 
188
 * Numbers in the range [scaled_upper_bound - bounds_delta, scaled_upper_bound]
 
189
 * have the same double binary representation as val. 
 
190
 *
 
191
 * Bounds are scaled by 10^scale so that alpha <= exponent <= gamma.
 
192
 * Moreover, scaled_upper_bound is normalized.
 
193
 *
 
194
 * val_dist is the scaled distance from val to the upper bound, ie
 
195
 * val_dist == (upper_bound - val) * 10^scale
 
196
 */
 
197
static void calc_scaled_bounds(ieee_double_t val, fp_num_t *scaled_upper_bound, 
 
198
        fp_num_t *bounds_delta, fp_num_t *val_dist, int *scale)
 
199
{
 
200
        fp_num_t upper_bound, lower_bound;
 
201
 
 
202
        get_normalized_bounds(val, &upper_bound, &lower_bound, val_dist);
 
203
 
 
204
        assert(upper_bound.exponent == lower_bound.exponent);
 
205
        assert(is_normalized(upper_bound));
 
206
        assert(normalize(val.pos_val).exponent == upper_bound.exponent);
 
207
 
 
208
        /* 
 
209
         * Find such a cached normalized power of 10 that if multiplied
 
210
         * by upper_bound the binary exponent of upper_bound almost vanishes, 
 
211
         * ie:
 
212
         *   upper_scaled := upper_bound * 10^scale
 
213
         *   alpha <= upper_scaled.exponent <= gamma
 
214
         *   alpha <= upper_bound.exponent + pow_10.exponent + 64 <= gamma
 
215
         */
 
216
        fp_num_t scaling_power_of_10;
 
217
        int lower_bin_exp = alpha - upper_bound.exponent - significand_width;
 
218
 
 
219
        get_power_of_ten(lower_bin_exp, &scaling_power_of_10, scale);
 
220
 
 
221
        int scale_exp = scaling_power_of_10.exponent;
 
222
        assert(alpha <= upper_bound.exponent + scale_exp + significand_width);
 
223
        assert(upper_bound.exponent + scale_exp + significand_width <= gamma);
 
224
 
 
225
        fp_num_t upper_scaled = multiply(upper_bound, scaling_power_of_10);
 
226
        fp_num_t lower_scaled = multiply(lower_bound, scaling_power_of_10);
 
227
        *val_dist = multiply(*val_dist, scaling_power_of_10);
 
228
 
 
229
        assert(alpha <= upper_scaled.exponent && upper_scaled.exponent <= gamma);
 
230
 
 
231
        /* 
 
232
         * Any value between lower and upper bound would be represented
 
233
         * in binary as the double val originated from. The bounds were
 
234
         * however scaled by an imprecise power of 10 (error less than 
 
235
         * 1 ulp) so the scaled bounds have an error of less than 1 ulp. 
 
236
         * Conservatively round the lower bound up and the upper bound 
 
237
         * down by 1 ulp just to be on the safe side. It avoids pronouncing
 
238
         * produced decimal digits as correct if such a decimal number
 
239
         * is close to the bounds to within 1 ulp. 
 
240
         */
 
241
        upper_scaled.significand -= 1;
 
242
        lower_scaled.significand += 1;
 
243
 
 
244
        *bounds_delta = subtract(upper_scaled, lower_scaled);
 
245
        *scaled_upper_bound = upper_scaled;
 
246
}
 
247
 
 
248
 
 
249
/** Rounds the last digit of buf so that it is closest to the converted number.*/
 
250
static void round_last_digit(uint64_t rest, uint64_t w_dist, uint64_t delta,
 
251
        uint64_t digit_val_diff, char *buf, int len)
 
252
{
 
253
        /*
 
254
         *  | <------- delta -------> |
 
255
         *  |    | <---- w_dist ----> |
 
256
         *  |    |       | <- rest -> |
 
257
         *  |    |       |            |
 
258
         *  |    |       ` buffer     |
 
259
         *  |    ` w                  ` upper
 
260
         *  ` lower
 
261
         *
 
262
         * delta = upper - lower .. conservative/safe interval
 
263
         * w_dist = upper - w    
 
264
         * upper = "number represented by digits in buf" + rest
 
265
         * 
 
266
         * Changing buf[len - 1] changes the value represented by buf 
 
267
         * by digit_val_diff * scaling, where scaling is shared by
 
268
         * all parameters. 
 
269
         *
 
270
         */
 
271
 
 
272
        /* Current number in buf is greater than the double being converted */
 
273
        bool cur_greater_w = rest < w_dist;
 
274
        /* Rounding down by one would keep buf in between bounds (in safe rng). */
 
275
        bool next_in_val_rng = cur_greater_w && (rest + digit_val_diff < delta);
 
276
        /* Rounding down by one would bring buf closer to the processed number. */
 
277
        bool next_closer = next_in_val_rng 
 
278
                && (rest + digit_val_diff < w_dist || rest - w_dist < w_dist - rest);
 
279
 
 
280
        /* Of the shortest strings pick the one that is closest to the actual 
 
281
           floating point number. */
 
282
        while (next_closer) {
 
283
                assert('0' < buf[len - 1]);
 
284
                assert(0 < digit_val_diff);
 
285
 
 
286
                --buf[len - 1];
 
287
                rest += digit_val_diff;
 
288
 
 
289
                cur_greater_w = rest < w_dist;
 
290
                next_in_val_rng = cur_greater_w && (rest + digit_val_diff < delta);
 
291
                next_closer = next_in_val_rng 
 
292
                        && (rest + digit_val_diff < w_dist || rest - w_dist < w_dist - rest);
 
293
        }
 
294
}
 
295
 
 
296
 
 
297
/** Generates the shortest accurate decimal string representation.
 
298
 *
 
299
 * Outputs (mostly) the shortest accurate string representation 
 
300
 * for the number scaled_upper - val_dist. Numbers in the interval
 
301
 * [scaled_upper - delta, scaled_upper] have the same binary
 
302
 * floating point representation and will therefore share the
 
303
 * shortest string representation (up to the rounding of the last
 
304
 * digit to bring the shortest string also the closest to the
 
305
 * actual number). 
 
306
 *
 
307
 * @param scaled_upper Scaled upper bound of numbers that have the
 
308
 *              same binary representation as the converted number.
 
309
 *              Scaled by 10^-scale so that alpha <= exponent <= gamma.
 
310
 * @param delta scaled_upper - delta is the lower bound of numbers
 
311
 *              that share the same binary representation in double.
 
312
 * @param val_dist scaled_upper - val_dist is the number whose
 
313
 *              decimal string we're generating.
 
314
 * @param scale Decimal scaling of the value to convert (ie scaled_upper).
 
315
 * @param buf   Buffer to store the string representation. Must be large 
 
316
 *              enough to store all digits and a null terminator. At most
 
317
 *              MAX_DOUBLE_STR_LEN digits will be written (not counting
 
318
 *              the null terminator).
 
319
 * @param buf_size Size of buf in bytes. 
 
320
 * @param dec_exponent Will be set to the decimal exponent of the number 
 
321
 *              string in buf.
 
322
 *
 
323
 * @return Number of digits; negative on failure (eg buffer too small).
 
324
 */
 
325
static int gen_dec_digits(fp_num_t scaled_upper, fp_num_t delta, 
 
326
        fp_num_t val_dist, int scale, char *buf, size_t buf_size, int *dec_exponent)
 
327
{
 
328
        /* 
 
329
         * The integral part of scaled_upper is 5 to 32 bits long while 
 
330
         * the remaining fractional part is 59 to 32 bits long because:
 
331
         * -59 == alpha <= scaled_upper.e <= gamma == -32
 
332
         *
 
333
         *  | <------- delta -------> |
 
334
         *  |    | <--- val_dist ---> |
 
335
         *  |    |    |<- remainder ->|
 
336
         *  |    |    |               |
 
337
         *  |    |    ` buffer        |
 
338
         *  |    ` val                ` upper
 
339
         *  ` lower
 
340
         *
 
341
         */ 
 
342
        assert(scaled_upper.significand != 0);
 
343
        assert(alpha <= scaled_upper.exponent && scaled_upper.exponent <= gamma);
 
344
        assert(scaled_upper.exponent == delta.exponent);
 
345
        assert(scaled_upper.exponent == val_dist.exponent);
 
346
        assert(val_dist.significand <= delta.significand);
 
347
 
 
348
        /* We'll produce at least one digit and a null terminator. */
 
349
        if (buf_size < 2) {
 
350
                return -1;
 
351
        }
 
352
 
 
353
        /* one is number 1 encoded with the same exponent as scaled_upper */
 
354
        fp_num_t one;
 
355
        one.significand = ((uint64_t) 1) << (-scaled_upper.exponent);
 
356
        one.exponent = scaled_upper.exponent;
 
357
 
 
358
        /*
 
359
         * Extract the integral part of scaled_upper. 
 
360
         *  upper / one == upper >> -one.e 
 
361
         */
 
362
        uint32_t int_part = (uint32_t)(scaled_upper.significand >> (-one.exponent));
 
363
        
 
364
        /* 
 
365
         * Fractional part of scaled_upper.
 
366
         *  upper % one == upper & (one.f - 1) 
 
367
         */
 
368
        uint64_t frac_part = scaled_upper.significand & (one.significand - 1);
 
369
 
 
370
        /*
 
371
         * The integral part of upper has at least 5 bits (64 + alpha) and 
 
372
         * at most 32 bits (64 + gamma). The integral part has at most 10 
 
373
         * decimal digits, so kappa <= 10. 
 
374
         */
 
375
        int kappa = 10;
 
376
        uint32_t div = 1000000000;
 
377
        size_t len = 0;
 
378
 
 
379
        /* Produce decimal digits for the integral part of upper. */
 
380
        while (kappa > 0) {
 
381
                int digit = int_part / div;
 
382
                int_part %= div;
 
383
 
 
384
                --kappa;
 
385
 
 
386
                /* Skip leading zeros. */
 
387
                if (digit != 0 || len != 0) {
 
388
                        /* Current length + new digit + null terminator <= buf_size */
 
389
                        if (len + 2 <= buf_size) {
 
390
                                buf[len] = '0' + digit;
 
391
                                ++len;
 
392
                        } else {
 
393
                                return -1;
 
394
                        }
 
395
                }
 
396
 
 
397
                /* 
 
398
                 * Difference between the so far produced decimal number and upper
 
399
                 * is calculated as: remaining_int_part * one + frac_part 
 
400
                 */
 
401
                uint64_t remainder = (((uint64_t)int_part) << -one.exponent) + frac_part;
 
402
 
 
403
                /* The produced decimal number would convert back to upper. */
 
404
                if (remainder <= delta.significand) {
 
405
                        assert(0 < len && len < buf_size);
 
406
                        *dec_exponent = kappa - scale;
 
407
                        buf[len] = '\0';
 
408
 
 
409
                        /* Of the shortest representations choose the numerically closest. */
 
410
                        round_last_digit(remainder, val_dist.significand, delta.significand,
 
411
                                (uint64_t)div << (-one.exponent), buf, len);
 
412
                        return len;
 
413
                }
 
414
 
 
415
                div /= 10;
 
416
        }
 
417
 
 
418
        /* Generate decimal digits for the fractional part of upper. */
 
419
        do {
 
420
                /*
 
421
                 * Does not overflow because at least 5 upper bits were
 
422
                 * taken by the integral part and are now unused in frac_part. 
 
423
                 */
 
424
                frac_part *= 10;
 
425
                delta.significand *= 10;
 
426
                val_dist.significand *= 10;
 
427
 
 
428
                /* frac_part / one */
 
429
                int digit = (int)(frac_part >> (-one.exponent));
 
430
                
 
431
                /* frac_part %= one */
 
432
                frac_part &= one.significand - 1;
 
433
 
 
434
                --kappa;
 
435
 
 
436
                /* Skip leading zeros. */
 
437
                if (digit == 0 && len == 0) {
 
438
                        continue;
 
439
                }
 
440
 
 
441
                /* Current length + new digit + null terminator <= buf_size */
 
442
                if (len + 2 <= buf_size) {
 
443
                        buf[len] = '0' + digit;
 
444
                        ++len;
 
445
                } else {
 
446
                        return -1;
 
447
                }
 
448
        } while (frac_part > delta.significand);
 
449
 
 
450
        assert(0 < len && len < buf_size);
 
451
 
 
452
        *dec_exponent = kappa - scale;
 
453
        buf[len] = '\0';
 
454
 
 
455
        /* Of the shortest representations choose the numerically closest one. */
 
456
        round_last_digit(frac_part, val_dist.significand, delta.significand, 
 
457
                one.significand, buf, len);
 
458
 
 
459
        return len;
 
460
}
 
461
 
 
462
/** Produce a string for 0.0 */
 
463
static int zero_to_str(char *buf, size_t buf_size, int *dec_exponent)
 
464
{
 
465
        if (2 <= buf_size) {
 
466
                buf[0] = '0';
 
467
                buf[1] = '\0';
 
468
                *dec_exponent = 0;
 
469
                return 1;
 
470
        } else {
 
471
                return -1;
 
472
        }
 
473
}
 
474
 
 
475
 
 
476
/** Converts a non-special double into its shortest accurate string 
 
477
 *  representation.
 
478
 *
 
479
 * Produces an accurate string representation, ie the string will 
 
480
 * convert back to the same binary double (eg via strtod). In the
 
481
 * vast majority of cases (99%) the string will be the shortest such
 
482
 * string that is also the closest to the value of any shortest
 
483
 * string representations. Therefore, no trailing zeros are ever
 
484
 * produced.
 
485
 *
 
486
 * Conceptually, the value is: buf * 10^dec_exponent
 
487
 *
 
488
 * Never outputs trailing zeros.
 
489
 *
 
490
 * @param ieee_val Binary double description to convert. Must be the product
 
491
 *                 of extract_ieee_double and it must not be a special number.
 
492
 * @param buf      Buffer to store the string representation. Must be large 
 
493
 *                 enough to store all digits and a null terminator. At most
 
494
 *                 MAX_DOUBLE_STR_LEN digits will be written (not counting
 
495
 *                 the null terminator).
 
496
 * @param buf_size Size of buf in bytes.
 
497
 * @param dec_exponent Will be set to the decimal exponent of the number 
 
498
 *                 string in buf.
 
499
 *
 
500
 * @return The number of printed digits. A negative value indicates
 
501
 *         an error: buf too small (or ieee_val.is_special).
 
502
 */
 
503
int double_to_short_str(ieee_double_t ieee_val, char *buf, size_t buf_size, 
 
504
        int *dec_exponent)
 
505
{
 
506
        /* The whole computation assumes 64bit significand. */
 
507
        assert(sizeof(ieee_val.pos_val.significand) == sizeof(uint64_t));
 
508
 
 
509
        if (ieee_val.is_special) {
 
510
                return -1;
 
511
        }
 
512
 
 
513
        /* Zero cannot be normalized. Handle it here. */
 
514
        if (0 == ieee_val.pos_val.significand) {
 
515
                return zero_to_str(buf, buf_size, dec_exponent);
 
516
        }
 
517
 
 
518
        fp_num_t scaled_upper_bound;
 
519
        fp_num_t delta;
 
520
        fp_num_t val_dist;
 
521
        int scale;
 
522
 
 
523
        calc_scaled_bounds(ieee_val, &scaled_upper_bound, 
 
524
                &delta, &val_dist, &scale);
 
525
 
 
526
        int len = gen_dec_digits(scaled_upper_bound, delta, val_dist, scale, 
 
527
                buf, buf_size, dec_exponent);
 
528
 
 
529
        assert(len <= MAX_DOUBLE_STR_LEN);
 
530
        return len;
 
531
}
 
532
 
 
533
/** Generates a fixed number of decimal digits of w_scaled.
 
534
 *
 
535
 * double == w_scaled * 10^-scale, where alpha <= w_scaled.e <= gamma
 
536
 *
 
537
 * @param w_scaled Scaled number by 10^-scale so that
 
538
 *              alpha <= exponent <= gamma
 
539
 * @param scale Decimal scaling of the value to convert (ie w_scaled).
 
540
 * @param signif_d_cnt Maximum number of significant digits to output. 
 
541
 *              Negative if as many as possible are requested.
 
542
 * @param frac_d_cnt   Maximum number of fractional digits to output.
 
543
 *              Negative if as many as possible are requested.
 
544
 *              Eg. if 2 then 1.234 -> "1.23"; if 2 then 3e-9 -> "0".
 
545
 * @param buf   Buffer to store the string representation. Must be large 
 
546
 *              enough to store all digits and a null terminator. At most
 
547
 *              MAX_DOUBLE_STR_LEN digits will be written (not counting
 
548
 *              the null terminator).
 
549
 * @param buf_size Size of buf in bytes. 
 
550
 *
 
551
 * @return Number of digits; negative on failure (eg buffer too small).
 
552
 */
 
553
static int gen_fixed_dec_digits(fp_num_t w_scaled, int scale, int signif_d_cnt, 
 
554
        int frac_d_cnt, char *buf, size_t buf_size, int *dec_exponent)
 
555
{
 
556
        /* We'll produce at least one digit and a null terminator. */
 
557
        if (0 == signif_d_cnt || buf_size < 2) {
 
558
                return -1;
 
559
        }
 
560
 
 
561
        /* 
 
562
         * The integral part of w_scaled is 5 to 32 bits long while the 
 
563
         * remaining fractional part is 59 to 32 bits long because:
 
564
         * -59 == alpha <= w_scaled.e <= gamma == -32
 
565
         * 
 
566
         * Therefore:
 
567
         *  | 5..32 bits | 32..59 bits | == w_scaled == w * 10^scale
 
568
         *  |  int_part  |  frac_part  |
 
569
         *  |0 0  ..  0 1|0 0   ..  0 0| == one == 1.0
 
570
         *  |      0     |0 0   ..  0 1| == w_err == 1 * 2^w_scaled.e  
 
571
        */
 
572
        assert(alpha <= w_scaled.exponent && w_scaled.exponent <= gamma);
 
573
        assert(0 != w_scaled.significand);
 
574
 
 
575
        /* 
 
576
         * Scaling the number being converted by 10^scale introduced
 
577
         * an error of less that 1 ulp. The actual value of w_scaled
 
578
         * could lie anywhere between w_scaled.signif +/- w_err. 
 
579
         * Scale the error locally as we scale the fractional part
 
580
         * of w_scaled.
 
581
         */
 
582
        uint64_t w_err = 1;
 
583
 
 
584
        /* one is number 1.0 encoded with the same exponent as w_scaled */
 
585
        fp_num_t one;
 
586
        one.significand = ((uint64_t) 1) << (-w_scaled.exponent);
 
587
        one.exponent = w_scaled.exponent;
 
588
 
 
589
        /* Extract the integral part of w_scaled. 
 
590
           w_scaled / one == w_scaled >> -one.e */
 
591
        uint32_t int_part = (uint32_t)(w_scaled.significand >> (-one.exponent));
 
592
 
 
593
        /* Fractional part of w_scaled.
 
594
           w_scaled % one == w_scaled & (one.f - 1) */
 
595
        uint64_t frac_part = w_scaled.significand & (one.significand - 1);
 
596
 
 
597
        size_t len = 0;
 
598
        /* 
 
599
         * The integral part of w_scaled has at least 5 bits (64 + alpha = 5) 
 
600
         * and at most 32 bits (64 + gamma = 32). The integral part has 
 
601
         * at most 10 decimal digits, so kappa <= 10. 
 
602
         */
 
603
        int kappa = 10;
 
604
        uint32_t div = 1000000000;
 
605
 
 
606
        int rem_signif_d_cnt = signif_d_cnt;
 
607
        int rem_frac_d_cnt = 
 
608
                (frac_d_cnt >= 0) ? (kappa - scale + frac_d_cnt) : INT_MAX;
 
609
 
 
610
        /* Produce decimal digits for the integral part of w_scaled. */
 
611
        while (kappa > 0 && rem_signif_d_cnt != 0 && rem_frac_d_cnt > 0) {
 
612
                int digit = int_part / div;
 
613
                int_part %= div;
 
614
 
 
615
                div /= 10;
 
616
                --kappa;
 
617
                --rem_frac_d_cnt;
 
618
 
 
619
                /* Skip leading zeros. */
 
620
                if (digit == 0 && len == 0) {
 
621
                        continue;
 
622
                }
 
623
 
 
624
                /* Current length + new digit + null terminator <= buf_size */
 
625
                if (len + 2 <= buf_size) {
 
626
                        buf[len] = '0' + digit;
 
627
                        ++len;
 
628
                        --rem_signif_d_cnt;
 
629
                } else {
 
630
                        return -1;
 
631
                }
 
632
        }
 
633
 
 
634
        /* Generate decimal digits for the fractional part of w_scaled. */
 
635
        while (w_err <= frac_part && rem_signif_d_cnt != 0 && rem_frac_d_cnt > 0) {
 
636
                /*
 
637
                 * Does not overflow because at least 5 upper bits were
 
638
                 * taken by the integral part and are now unused in frac_part. 
 
639
                 */
 
640
                frac_part *= 10;
 
641
                w_err *= 10;
 
642
 
 
643
                /* frac_part / one */
 
644
                int digit = (int)(frac_part >> (-one.exponent));
 
645
                
 
646
                /* frac_part %= one */
 
647
                frac_part &= one.significand - 1;
 
648
 
 
649
                --kappa;
 
650
                --rem_frac_d_cnt;
 
651
 
 
652
                /* Skip leading zeros. */
 
653
                if (digit == 0 && len == 0) {
 
654
                        continue;
 
655
                }
 
656
 
 
657
                /* Current length + new digit + null terminator <= buf_size */
 
658
                if (len + 2 <= buf_size) {
 
659
                        buf[len] = '0' + digit;
 
660
                        ++len;
 
661
                        --rem_signif_d_cnt;
 
662
                } else {
 
663
                        return -1;
 
664
                }
 
665
        };
 
666
 
 
667
        assert(/* 0 <= len && */ len < buf_size);
 
668
 
 
669
        if (0 < len) {
 
670
                *dec_exponent = kappa - scale;
 
671
                assert(frac_d_cnt < 0 || -frac_d_cnt <= *dec_exponent);
 
672
        } else {
 
673
                /* 
 
674
                 * The number of fractional digits was too limiting to produce 
 
675
                 * any digits. 
 
676
                 */
 
677
                assert(rem_frac_d_cnt <= 0 || w_scaled.significand == 0);
 
678
                *dec_exponent = 0;
 
679
                buf[0] = '0';
 
680
                len = 1;
 
681
        }
 
682
 
 
683
        if (len < buf_size) {
 
684
                buf[len] = '\0';
 
685
                assert(signif_d_cnt < 0 || (int)len <= signif_d_cnt);
 
686
                return len;
 
687
        } else {
 
688
                return -1;
 
689
        }
 
690
}
 
691
 
 
692
 
 
693
/** Converts a non-special double into its string representation.
 
694
 *
 
695
 * Conceptually, the truncated double value is: buf * 10^dec_exponent
 
696
 *
 
697
 * Conversion errors are tracked, so all produced digits except the
 
698
 * last one are accurate. Garbage digits are never produced.
 
699
 * If the requested number of digits cannot be produced accurately 
 
700
 * due to conversion errors less digits are produced than requested 
 
701
 * and the last digit has an error of +/- 1 (so if '7' is the last
 
702
 * converted digit it might have been converted to any of '6'..'8'
 
703
 * had the conversion been completely precise). 
 
704
 *
 
705
 * If no error occurs at least one digit is output. 
 
706
 *
 
707
 * The conversion stops once the requested number of significant or 
 
708
 * fractional digits is reached or the conversion error is too large 
 
709
 * to generate any more digits (whichever happens first).
 
710
 *
 
711
 * Any digits following the first (most-significant) digit (this digit
 
712
 * included) are counted as significant digits; eg:
 
713
 *   1.4,    4 signif -> "1400" * 10^-3, ie 1.400
 
714
 *   1000.3, 1 signif -> "1" * 10^3      ie 1000
 
715
 *   0.003,  2 signif -> "30" * 10^-4    ie 0.003
 
716
 *   9.5     1 signif -> "9" * 10^0,     ie 9
 
717
 *
 
718
 * Any digits following the decimal point are counted as fractional digits.
 
719
 * This includes the zeros that would appear between the decimal point
 
720
 * and the first non-zero fractional digit. If fewer fractional digits
 
721
 * are requested than would allow to place the most-significant digit
 
722
 * a "0" is output. Eg:
 
723
 *   1.4,   3 frac -> "1400" * 10^-3,   ie 1.400
 
724
 *   12.34  4 frac -> "123400" * 10^-4, ie 12.3400
 
725
 *   3e-99  4 frac -> "0" * 10^0,       ie 0
 
726
 *   0.009  2 frac -> "0" * 10^-2,      ie 0
 
727
 *
 
728
 * @param ieee_val Binary double description to convert. Must be the product
 
729
 *                 of extract_ieee_double and it must not be a special number.
 
730
 * @param signif_d_cnt Maximum number of significant digits to produce.
 
731
 *                 The output is not rounded. 
 
732
 *                 Set to a negative value to generate as many digits
 
733
 *                 as accurately possible.
 
734
 * @param frac_d_cnt Maximum number of fractional digits to produce including
 
735
 *                 any zeros immediately trailing the decimal point. 
 
736
 *                 The output is not rounded. 
 
737
 *                 Set to a negative value to generate as many digits
 
738
 *                 as accurately possible.
 
739
 * @param buf      Buffer to store the string representation. Must be large 
 
740
 *                 enough to store all digits and a null terminator. At most
 
741
 *                 MAX_DOUBLE_STR_LEN digits will be written (not counting
 
742
 *                 the null terminator).
 
743
 * @param buf_size Size of buf in bytes.
 
744
 * @param dec_exponent Set to the decimal exponent of the number string 
 
745
 *                 in buf.
 
746
 *
 
747
 * @return The number of output digits. A negative value indicates
 
748
 *         an error: buf too small (or ieee_val.is_special, or 
 
749
 *         signif_d_cnt == 0).
 
750
 */
 
751
int double_to_fixed_str(ieee_double_t ieee_val, int signif_d_cnt,
 
752
        int frac_d_cnt, char *buf, size_t buf_size, int *dec_exponent)
 
753
{
 
754
        /* The whole computation assumes 64bit significand. */
 
755
        assert(sizeof(ieee_val.pos_val.significand) == sizeof(uint64_t));
 
756
 
 
757
        if (ieee_val.is_special) {
 
758
                return -1;
 
759
        }
 
760
 
 
761
        /* Zero cannot be normalized. Handle it here. */
 
762
        if (0 == ieee_val.pos_val.significand) {
 
763
                return zero_to_str(buf, buf_size, dec_exponent);
 
764
        }
 
765
 
 
766
        /* Normalize and scale. */
 
767
        fp_num_t w = normalize(ieee_val.pos_val);
 
768
 
 
769
        int lower_bin_exp = alpha - w.exponent - significand_width;
 
770
 
 
771
        int scale;
 
772
        fp_num_t scaling_power_of_10;
 
773
 
 
774
        get_power_of_ten(lower_bin_exp, &scaling_power_of_10, &scale);
 
775
 
 
776
        fp_num_t w_scaled = multiply(w, scaling_power_of_10);
 
777
 
 
778
        /* Produce decimal digits from the scaled number. */
 
779
        int len = gen_fixed_dec_digits(w_scaled, scale, signif_d_cnt, frac_d_cnt, 
 
780
                buf, buf_size, dec_exponent);
 
781
 
 
782
        assert(len <= MAX_DOUBLE_STR_LEN);
 
783
        return len;
 
784
}
 
785