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 #define _NEED_IO_FLOAT
37 
38 #include "dtoa.h"
39 
40 int
fcvtf_r(float invalue,int ndecimal,int * decpt,int * sign,char * buf,size_t len)41 fcvtf_r (float invalue,
42          int ndecimal,
43          int *decpt,
44          int *sign,
45          char *buf,
46          size_t len)
47 {
48     struct dtoa dtoa;
49     int ntrailing;      /* number of zeros to add after the value */
50     int ndigit;         /* numer of generated digits */
51     int dtoa_decimal = ndecimal;
52     char *digits = dtoa.digits;
53 
54     if (!finitef(invalue)) {
55         ndigit = 3;
56         ntrailing = 0;
57         *sign = invalue < 0;
58         *decpt = 0;
59         if (isnan(invalue))
60             digits = "nan";
61         else
62             digits = "inf";
63     } else {
64         /* ndecimal = digits after decimal point desired
65          * ndigit = digits actually generated
66          * dtoa.exp = exponent (position of decimal relative to first digit generated)
67          */
68         if (ndecimal < 0)
69             dtoa_decimal = 0;
70         ndigit = __ftoa_engine(invalue, &dtoa, FTOA_MAX_DIG, true, ndecimal);
71         *sign = !!(dtoa.flags & DTOA_MINUS);
72 
73         /*
74          * To compute the number of zeros added after the value, there are
75          * three cases:
76          *
77          * 1. all of the generated digits are left of the decimal
78          *    point (dtoa.exp >= ndigit). We need (dtoa.exp - ndigit)
79          *    digits left of the decimal and ndecimal right of the
80          *    decimal: (dtoa.exp - ndigit) + ndecimal
81          *
82          * 2. some of the generated digits are right
83          *    of the decimal point (dtoa.exp < ndigit). We need
84          *    ndecimal digits total, but we have (ndigit - dtoa.exp)
85          *    already, so: ndecimal - (ndigit - dtoa.exp).
86          *
87          * 3. all of the generated digits are right of the decimal point
88          *    We need fewer than ndecimal digits by the magnitude of
89          *    the exponent (which is negative in this case, so:
90          *    ndecimal - (-dtoa.exp - 1) - ndigit
91          *
92          * These all turn out to be the same computation. Kinda cool.
93          */
94         ntrailing = (dtoa.exp + 1 - ndigit) + dtoa_decimal;
95         /*
96          * If this value is negative, then we actually have *no* digits to
97          * generate. In this case, we return the empty string and set the
98          * exponent to the number of digits requested (as they're all
99          * zero)
100          */
101         if (ntrailing < 0) {
102             ntrailing = 0;
103             ndigit = 0;
104 
105             /*
106              * Adjust exponent to reflect the desired output of ndecimal
107              * zeros
108              */
109             dtoa.exp = -(dtoa_decimal + 1);
110         }
111         *decpt = dtoa.exp + 1;
112     }
113 
114     /* If we can't fit the whole value in the provided space,
115      * return an error
116      */
117     if ((size_t) (ndigit + ntrailing) >= len)
118         return -1;
119 
120     /* Value */
121     memcpy(buf, digits, ndigit);
122     len -= ndigit;
123     buf += ndigit;
124 
125     /* Trailing zeros */
126     memset(buf, '0', ntrailing);
127     buf += ntrailing;
128 
129     /* Null terminate */
130     buf[0] = '\0';
131     return 0;
132 }
133