]> git.ipfire.org Git - thirdparty/glibc.git/blame - misc/efgcvt_r.c
aarch64: add STO_AARCH64_VARIANT_PCS and DT_AARCH64_VARIANT_PCS
[thirdparty/glibc.git] / misc / efgcvt_r.c
CommitLineData
19361cb7 1/* Compatibility functions for floating point formatting, reentrant versions.
688903eb 2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
19361cb7
UD
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
41bdb6e2
AJ
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.
19361cb7
UD
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
41bdb6e2 13 Lesser General Public License for more details.
19361cb7 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
60478656
RM
18
19#include <errno.h>
1420df2c 20#include <float.h>
60478656
RM
21#include <stdio.h>
22#include <string.h>
23#include <ctype.h>
24#include <math.h>
1d8dc429 25#include <stdlib.h>
da2d1bc5 26#include <sys/param.h>
1d446ec6 27#include <math_ldbl_opt.h>
60478656 28
2064087b 29#ifndef FLOAT_TYPE
67994d6f
UD
30# define FLOAT_TYPE double
31# define FUNC_PREFIX
32# define FLOAT_FMT_FLAG
33# define FLOAT_NAME_EXT
ec8c29dc 34# define FLOAT_MIN_10_EXP DBL_MIN_10_EXP
b113c12c
UD
35# if DBL_MANT_DIG == 53
36# define NDIGIT_MAX 17
f9a2a636
UD
37# elif DBL_MANT_DIG == 24
38# define NDIGIT_MAX 9
39# elif DBL_MANT_DIG == 56
40# define NDIGIT_MAX 18
b113c12c 41# else
f9a2a636
UD
42/* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
43 compile time constant here, so we cannot use it. */
44# error "NDIGIT_MAX must be precomputed"
b113c12c
UD
45# define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
46# endif
ec8c29dc
UD
47# if DBL_MIN_10_EXP == -37
48# define FLOAT_MIN_10_NORM 1.0e-37
49# elif DBL_MIN_10_EXP == -307
50# define FLOAT_MIN_10_NORM 1.0e-307
51# elif DBL_MIN_10_EXP == -4931
52# define FLOAT_MIN_10_NORM 1.0e-4931
53# else
54/* libc can't depend on libm. */
55# error "FLOAT_MIN_10_NORM must be precomputed"
56# define FLOAT_MIN_10_NORM exp10 (DBL_MIN_10_EXP)
57# endif
c6251f03
RM
58#else
59# define LONG_DOUBLE_CVT
2064087b
RM
60#endif
61
62#define APPEND(a, b) APPEND2 (a, b)
63#define APPEND2(a, b) a##b
c6251f03
RM
64#define __APPEND(a, b) __APPEND2 (a, b)
65#define __APPEND2(a, b) __##a##b
2064087b
RM
66
67#define FLOOR APPEND(floor, FLOAT_NAME_EXT)
68#define FABS APPEND(fabs, FLOAT_NAME_EXT)
69#define LOG10 APPEND(log10, FLOAT_NAME_EXT)
1f205a47 70#define EXP APPEND(exp, FLOAT_NAME_EXT)
2064087b
RM
71
72
60478656 73int
80d9be81
JM
74__APPEND (FUNC_PREFIX, fcvt_r) (FLOAT_TYPE value, int ndigit, int *decpt,
75 int *sign, char *buf, size_t len)
60478656 76{
2a068d20
UD
77 ssize_t n;
78 ssize_t i;
1eb687d0 79 int left;
60478656
RM
80
81 if (buf == NULL)
82 {
c4029823 83 __set_errno (EINVAL);
60478656
RM
84 return -1;
85 }
86
1eb687d0 87 left = 0;
1f205a47
UD
88 if (isfinite (value))
89 {
90 *sign = signbit (value) != 0;
91 if (*sign)
92 value = -value;
da2d1bc5
UD
93
94 if (ndigit < 0)
95 {
96 /* Rounding to the left of the decimal point. */
1eb687d0
UD
97 while (ndigit < 0)
98 {
99 FLOAT_TYPE new_value = value * 0.1;
100
101 if (new_value < 1.0)
102 {
103 ndigit = 0;
104 break;
105 }
da2d1bc5 106
1eb687d0
UD
107 value = new_value;
108 ++left;
109 ++ndigit;
110 }
da2d1bc5 111 }
1f205a47 112 }
07b51ba5
UD
113 else
114 /* Value is Inf or NaN. */
115 *sign = 0;
116
67994d6f
UD
117 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
118 value);
b9d3d9f7 119 /* Check for a too small buffer. */
2a068d20 120 if (n >= (ssize_t) len)
60478656
RM
121 return -1;
122
123 i = 0;
124 while (i < n && isdigit (buf[i]))
125 ++i;
126 *decpt = i;
1f205a47
UD
127
128 if (i == 0)
07b51ba5
UD
129 /* Value is Inf or NaN. */
130 return 0;
1f205a47
UD
131
132 if (i < n)
133 {
134 do
135 ++i;
136 while (i < n && !isdigit (buf[i]));
da2d1bc5 137
b8ce6e3e 138 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
da2d1bc5
UD
139 {
140 /* We must not have leading zeroes. Strip them all out and
141 adjust *DECPT if necessary. */
142 --*decpt;
143 while (i < n && buf[i] == '0')
144 {
145 --*decpt;
146 ++i;
147 }
148 }
149
150 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
151 buf[n - (i - MAX (*decpt, 0))] = '\0';
1f205a47 152 }
60478656 153
1eb687d0
UD
154 if (left)
155 {
156 *decpt += left;
6dd67bd5 157 if ((ssize_t) --len > n)
1eb687d0 158 {
6dd67bd5 159 while (left-- > 0 && n < (ssize_t) len)
1eb687d0
UD
160 buf[n++] = '0';
161 buf[n] = '\0';
162 }
163 }
164
60478656
RM
165 return 0;
166}
167
168int
80d9be81
JM
169__APPEND (FUNC_PREFIX, ecvt_r) (FLOAT_TYPE value, int ndigit, int *decpt,
170 int *sign, char *buf, size_t len)
60478656 171{
1f205a47 172 int exponent = 0;
0676b5fd 173
1f205a47 174 if (isfinite (value) && value != 0.0)
4d585333 175 {
ff8ac383
UD
176 /* Slow code that doesn't require -lm functions. */
177 FLOAT_TYPE d;
178 FLOAT_TYPE f = 1.0;
179 if (value < 0.0)
180 d = -value;
1f205a47 181 else
ff8ac383 182 d = value;
ec8c29dc
UD
183 /* For denormalized numbers the d < 1.0 case below won't work,
184 as f can overflow to +Inf. */
185 if (d < FLOAT_MIN_10_NORM)
186 {
187 value /= FLOAT_MIN_10_NORM;
188 if (value < 0.0)
189 d = -value;
190 else
191 d = value;
192 exponent += FLOAT_MIN_10_EXP;
193 }
ff8ac383 194 if (d < 1.0)
1f205a47 195 {
ff8ac383 196 do
1f205a47 197 {
ff8ac383
UD
198 f *= 10.0;
199 --exponent;
1f205a47 200 }
ff8ac383
UD
201 while (d * f < 1.0);
202
203 value *= f;
204 }
205 else if (d >= 10.0)
206 {
207 do
1f205a47 208 {
ff8ac383
UD
209 f *= 10;
210 ++exponent;
1f205a47 211 }
ff8ac383
UD
212 while (d >= f * 10.0);
213
214 value /= f;
1f205a47 215 }
4d585333 216 }
da2d1bc5
UD
217 else if (value == 0.0)
218 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
219 This could be changed to -1 if we want to return 0. */
220 exponent = 0;
221
222 if (ndigit <= 0 && len > 0)
223 {
224 buf[0] = '\0';
225 *decpt = 1;
226 *sign = isfinite (value) ? signbit (value) != 0 : 0;
227 }
228 else
c6251f03
RM
229 if (__APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
230 decpt, sign, buf, len))
da2d1bc5 231 return -1;
4d585333 232
1f205a47
UD
233 *decpt += exponent;
234 return 0;
60478656 235}
c6251f03
RM
236
237#if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
238# ifdef LONG_DOUBLE_CVT
239# define cvt_symbol(symbol) \
240 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
241 APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
242# define cvt_symbol_1(lib, local, symbol, version) \
8ed3b643 243 libc_hidden_def (local) \
c6251f03
RM
244 versioned_symbol (lib, local, symbol, version)
245# else
246# define cvt_symbol(symbol) \
247 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
248 APPEND (q, symbol), GLIBC_2_0); \
498c1f6a 249 weak_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
c6251f03 250# define cvt_symbol_1(lib, local, symbol, version) \
8ed3b643 251 libc_hidden_def (local) \
c6251f03
RM
252 compat_symbol (lib, local, symbol, version)
253# endif
254#else
255# define cvt_symbol(symbol) \
8ed3b643
L
256 cvt_symbol_1 (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
257# define cvt_symbol_1(local, symbol) \
258 libc_hidden_def (local) \
259 weak_alias (local, symbol)
c6251f03
RM
260#endif
261cvt_symbol(fcvt_r);
262cvt_symbol(ecvt_r);