aarch64/fpu: Add vector variants of erf
[glibc.git] / misc / efgcvt_r-template.c
blob38330a85c94e26af57bb919593a3b79310c97682
1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
28 #ifndef SNPRINTF
29 # define SNPRINTF __snprintf
30 #endif
33 #define APPEND(a, b) APPEND2 (a, b)
34 #define APPEND2(a, b) a##b
36 int
37 __FCVT_R (FLOAT_TYPE value, int ndigit, int *decpt, int *sign,
38 char *buf, size_t len)
40 ssize_t n;
41 ssize_t i;
42 int left;
44 if (buf == NULL)
46 __set_errno (EINVAL);
47 return -1;
50 left = 0;
51 if (isfinite (value))
53 *sign = signbit (value) != 0;
54 if (*sign)
55 value = -value;
57 if (ndigit < 0)
59 /* Rounding to the left of the decimal point. */
60 while (ndigit < 0)
62 FLOAT_TYPE new_value = value * 0.1;
64 if (new_value < 1.0)
66 ndigit = 0;
67 break;
70 value = new_value;
71 ++left;
72 ++ndigit;
76 else
77 /* Value is Inf or NaN. */
78 *sign = 0;
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;
86 i = 0;
87 while (i < n && isdigit (buf[i]))
88 ++i;
89 *decpt = i;
91 if (i == 0)
92 /* Value is Inf or NaN. */
93 return 0;
95 if (i < n)
98 ++i;
99 while (i < n && !isdigit (buf[i]));
101 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
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')
108 --*decpt;
109 ++i;
113 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
114 buf[n - (i - MAX (*decpt, 0))] = '\0';
117 if (left)
119 *decpt += left;
120 if ((ssize_t) --len > n)
122 while (left-- > 0 && n < (ssize_t) len)
123 buf[n++] = '0';
124 buf[n] = '\0';
128 return 0;
132 __ECVT_R (FLOAT_TYPE value, int ndigit, int *decpt, int *sign,
133 char *buf, size_t len)
135 int exponent = 0;
137 if (isfinite (value) && value != 0.0)
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)
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;
157 if (d < 1.0)
161 f *= 10.0;
162 --exponent;
164 while (d * f < 1.0);
166 value *= f;
168 else if (d >= 10.0)
172 f *= 10;
173 ++exponent;
175 while (d >= f * 10.0);
177 value /= f;
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;
185 if (ndigit <= 0 && len > 0)
187 buf[0] = '\0';
188 *decpt = 1;
189 *sign = isfinite (value) ? signbit (value) != 0 : 0;
191 else
192 if (__FCVT_R (value, MIN (ndigit, NDIGIT_MAX) - 1,
193 decpt, sign, buf, len))
194 return -1;
196 *decpt += exponent;
197 return 0;