1 /* Compatibility functions for floating point formatting, reentrant versions.
2    Copyright (C) 1995-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <math.h>
24 #include <stdlib.h>
25 #include <sys/param.h>
26 #include <math_ldbl_opt.h>
27 
28 #ifndef SNPRINTF
29 # define SNPRINTF __snprintf
30 #endif
31 
32 
33 #define APPEND(a, b) APPEND2 (a, b)
34 #define APPEND2(a, b) a##b
35 
36 int
__FCVT_R(FLOAT_TYPE value,int ndigit,int * decpt,int * sign,char * buf,size_t len)37 __FCVT_R (FLOAT_TYPE value, int ndigit, int *decpt, int *sign,
38 	  char *buf, size_t len)
39 {
40   ssize_t n;
41   ssize_t i;
42   int left;
43 
44   if (buf == NULL)
45     {
46       __set_errno (EINVAL);
47       return -1;
48     }
49 
50   left = 0;
51   if (isfinite (value))
52     {
53       *sign = signbit (value) != 0;
54       if (*sign)
55 	value = -value;
56 
57       if (ndigit < 0)
58 	{
59 	  /* Rounding to the left of the decimal point.  */
60 	  while (ndigit < 0)
61 	    {
62 	      FLOAT_TYPE new_value = value * 0.1;
63 
64 	      if (new_value < 1.0)
65 		{
66 		  ndigit = 0;
67 		  break;
68 		}
69 
70 	      value = new_value;
71 	      ++left;
72 	      ++ndigit;
73 	    }
74 	}
75     }
76   else
77     /* Value is Inf or NaN.  */
78     *sign = 0;
79 
80   n = SNPRINTF (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
81 		  value);
82   /* Check for a too small buffer.  */
83   if (n >= (ssize_t) len)
84     return -1;
85 
86   i = 0;
87   while (i < n && isdigit (buf[i]))
88     ++i;
89   *decpt = i;
90 
91   if (i == 0)
92     /* Value is Inf or NaN.  */
93     return 0;
94 
95   if (i < n)
96     {
97       do
98 	++i;
99       while (i < n && !isdigit (buf[i]));
100 
101       if (*decpt == 1 && buf[0] == '0' && value != 0.0)
102 	{
103 	  /* We must not have leading zeroes.  Strip them all out and
104 	     adjust *DECPT if necessary.  */
105 	  --*decpt;
106 	  while (i < n && buf[i] == '0')
107 	    {
108 	      --*decpt;
109 	      ++i;
110 	    }
111 	}
112 
113       memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
114       buf[n - (i - MAX (*decpt, 0))] = '\0';
115     }
116 
117   if (left)
118     {
119       *decpt += left;
120       if ((ssize_t) --len > n)
121 	{
122 	  while (left-- > 0 && n < (ssize_t) len)
123 	    buf[n++] = '0';
124 	  buf[n] = '\0';
125 	}
126     }
127 
128   return 0;
129 }
130 
131 int
__ECVT_R(FLOAT_TYPE value,int ndigit,int * decpt,int * sign,char * buf,size_t len)132 __ECVT_R (FLOAT_TYPE value, int ndigit, int *decpt, int *sign,
133 	  char *buf, size_t len)
134 {
135   int exponent = 0;
136 
137   if (isfinite (value) && value != 0.0)
138     {
139       /* Slow code that doesn't require -lm functions.  */
140       FLOAT_TYPE d;
141       FLOAT_TYPE f = 1.0;
142       if (value < 0.0)
143 	d = -value;
144       else
145 	d = value;
146       /* For denormalized numbers the d < 1.0 case below won't work,
147 	 as f can overflow to +Inf.  */
148       if (d < FLOAT_MIN_10_NORM)
149 	{
150 	  value /= FLOAT_MIN_10_NORM;
151 	  if (value < 0.0)
152 	    d = -value;
153 	  else
154 	    d = value;
155 	  exponent += FLOAT_MIN_10_EXP;
156 	}
157       if (d < 1.0)
158 	{
159 	  do
160 	    {
161 	      f *= 10.0;
162 	      --exponent;
163 	    }
164 	  while (d * f < 1.0);
165 
166 	  value *= f;
167 	}
168       else if (d >= 10.0)
169 	{
170 	  do
171 	    {
172 	      f *= 10;
173 	      ++exponent;
174 	    }
175 	  while (d >= f * 10.0);
176 
177 	  value /= f;
178 	}
179     }
180   else if (value == 0.0)
181     /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
182        This could be changed to -1 if we want to return 0.  */
183     exponent = 0;
184 
185   if (ndigit <= 0 && len > 0)
186     {
187       buf[0] = '\0';
188       *decpt = 1;
189       *sign = isfinite (value) ? signbit (value) != 0 : 0;
190     }
191   else
192     if (__FCVT_R (value, MIN (ndigit, NDIGIT_MAX) - 1,
193 		  decpt, sign, buf, len))
194       return -1;
195 
196   *decpt += exponent;
197   return 0;
198 }
199