1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2022 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #if __SIZEOF_DOUBLE__ == 8
37 
38 #define _NEED_IO_FLOAT64
39 
40 #include "dtoa.h"
41 
42 int
fcvt_r(double invalue,int ndecimal,int * decpt,int * sign,char * buf,size_t len)43 fcvt_r (double invalue,
44         int ndecimal,
45         int *decpt,
46         int *sign,
47         char *buf,
48         size_t len)
49 {
50     struct dtoa dtoa;
51     int ntrailing;      /* number of zeros to add after the value */
52     int ndigit;         /* numer of generated digits */
53     int dtoa_decimal = ndecimal;
54     char *digits = dtoa.digits;
55 
56     if (!isfinite(invalue)) {
57         ndigit = 3;
58         ntrailing = 0;
59         *sign = invalue < 0;
60         *decpt = 0;
61         if (isnan(invalue))
62             digits = "nan";
63         else
64             digits = "inf";
65     } else {
66         /* ndecimal = digits after decimal point desired
67          * ndigit = digits actually generated
68          * dtoa.exp = exponent (position of decimal relative to first digit generated)
69          */
70         if (ndecimal < 0)
71             dtoa_decimal = 0;
72         ndigit = __dtoa_engine(invalue, &dtoa, DTOA_MAX_DIG, true, ndecimal);
73         *sign = !!(dtoa.flags & DTOA_MINUS);
74 
75         /*
76          * To compute the number of zeros added after the value, there are
77          * three cases:
78          *
79          * 1. all of the generated digits are left of the decimal
80          *    point (dtoa.exp >= ndigit). We need (dtoa.exp - ndigit)
81          *    digits left of the decimal and ndecimal right of the
82          *    decimal: (dtoa.exp - ndigit) + ndecimal
83          *
84          * 2. some of the generated digits are right
85          *    of the decimal point (dtoa.exp < ndigit). We need
86          *    ndecimal digits total, but we have (ndigit - dtoa.exp)
87          *    already, so: ndecimal - (ndigit - dtoa.exp).
88          *
89          * 3. all of the generated digits are right of the decimal point
90          *    We need fewer than ndecimal digits by the magnitude of
91          *    the exponent (which is negative in this case, so:
92          *    ndecimal - (-dtoa.exp - 1) - ndigit
93          *
94          * These all turn out to be the same computation. Kinda cool.
95          */
96         ntrailing = (dtoa.exp + 1 - ndigit) + dtoa_decimal;
97         /*
98          * If this value is negative, then we actually have *no* digits to
99          * generate. In this case, we return the empty string and set the
100          * exponent to the number of digits requested (as they're all
101          * zero)
102          */
103         if (ntrailing < 0) {
104             ntrailing = 0;
105             ndigit = 0;
106 
107             /*
108              * Adjust exponent to reflect the desired output of ndecimal
109              * zeros
110              */
111             dtoa.exp = -(dtoa_decimal + 1);
112         }
113         *decpt = dtoa.exp + 1;
114     }
115 
116     /* If we can't fit the whole value in the provided space,
117      * return an error
118      */
119     if ((size_t) (ndigit + ntrailing) >= len)
120         return -1;
121 
122     /* Value */
123     memcpy(buf, digits, ndigit);
124     buf += ndigit;
125 
126     /* Trailing zeros */
127     memset(buf, '0', ntrailing);
128     buf += ntrailing;
129 
130     /* Null terminate */
131     buf[0] = '\0';
132     return 0;
133 }
134 
135 #elif __SIZEOF_DOUBLE__ == 4
136 
137 #include "stdio_private.h"
138 
139 int
fcvt_r(double invalue,int ndecimal,int * decpt,int * sign,char * buf,size_t len)140 fcvt_r (double invalue,
141         int ndecimal,
142         int *decpt,
143         int *sign,
144         char *buf,
145         size_t len)
146 {
147     return fcvtf_r((float) invalue, ndecimal, decpt, sign, buf, len);
148 }
149 
150 #endif
151