lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Compatibility functions for floating point formatting, reentrant versions. |
| 2 | Copyright (C) 1995-2015 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 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <float.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <ctype.h> |
| 24 | #include <math.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/param.h> |
| 27 | #include <math_ldbl_opt.h> |
| 28 | |
| 29 | #ifndef FLOAT_TYPE |
| 30 | # define FLOAT_TYPE double |
| 31 | # define FUNC_PREFIX |
| 32 | # define FLOAT_FMT_FLAG |
| 33 | # define FLOAT_NAME_EXT |
| 34 | # define FLOAT_MIN_10_EXP DBL_MIN_10_EXP |
| 35 | # if DBL_MANT_DIG == 53 |
| 36 | # define NDIGIT_MAX 17 |
| 37 | # elif DBL_MANT_DIG == 24 |
| 38 | # define NDIGIT_MAX 9 |
| 39 | # elif DBL_MANT_DIG == 56 |
| 40 | # define NDIGIT_MAX 18 |
| 41 | # else |
| 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" |
| 45 | # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0))) |
| 46 | # endif |
| 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 |
| 58 | #else |
| 59 | # define LONG_DOUBLE_CVT |
| 60 | #endif |
| 61 | |
| 62 | #define APPEND(a, b) APPEND2 (a, b) |
| 63 | #define APPEND2(a, b) a##b |
| 64 | #define __APPEND(a, b) __APPEND2 (a, b) |
| 65 | #define __APPEND2(a, b) __##a##b |
| 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) |
| 70 | #define EXP APPEND(exp, FLOAT_NAME_EXT) |
| 71 | |
| 72 | |
| 73 | int |
| 74 | __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len) |
| 75 | FLOAT_TYPE value; |
| 76 | int ndigit, *decpt, *sign; |
| 77 | char *buf; |
| 78 | size_t len; |
| 79 | { |
| 80 | ssize_t n; |
| 81 | ssize_t i; |
| 82 | int left; |
| 83 | |
| 84 | if (buf == NULL) |
| 85 | { |
| 86 | __set_errno (EINVAL); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | left = 0; |
| 91 | if (isfinite (value)) |
| 92 | { |
| 93 | *sign = signbit (value) != 0; |
| 94 | if (*sign) |
| 95 | value = -value; |
| 96 | |
| 97 | if (ndigit < 0) |
| 98 | { |
| 99 | /* Rounding to the left of the decimal point. */ |
| 100 | while (ndigit < 0) |
| 101 | { |
| 102 | FLOAT_TYPE new_value = value * 0.1; |
| 103 | |
| 104 | if (new_value < 1.0) |
| 105 | { |
| 106 | ndigit = 0; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | value = new_value; |
| 111 | ++left; |
| 112 | ++ndigit; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | else |
| 117 | /* Value is Inf or NaN. */ |
| 118 | *sign = 0; |
| 119 | |
| 120 | n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX), |
| 121 | value); |
| 122 | /* Check for a too small buffer. */ |
| 123 | if (n >= (ssize_t) len) |
| 124 | return -1; |
| 125 | |
| 126 | i = 0; |
| 127 | while (i < n && isdigit (buf[i])) |
| 128 | ++i; |
| 129 | *decpt = i; |
| 130 | |
| 131 | if (i == 0) |
| 132 | /* Value is Inf or NaN. */ |
| 133 | return 0; |
| 134 | |
| 135 | if (i < n) |
| 136 | { |
| 137 | do |
| 138 | ++i; |
| 139 | while (i < n && !isdigit (buf[i])); |
| 140 | |
| 141 | if (*decpt == 1 && buf[0] == '0' && value != 0.0) |
| 142 | { |
| 143 | /* We must not have leading zeroes. Strip them all out and |
| 144 | adjust *DECPT if necessary. */ |
| 145 | --*decpt; |
| 146 | while (i < n && buf[i] == '0') |
| 147 | { |
| 148 | --*decpt; |
| 149 | ++i; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i); |
| 154 | buf[n - (i - MAX (*decpt, 0))] = '\0'; |
| 155 | } |
| 156 | |
| 157 | if (left) |
| 158 | { |
| 159 | *decpt += left; |
| 160 | if ((ssize_t) --len > n) |
| 161 | { |
| 162 | while (left-- > 0 && n < (ssize_t) len) |
| 163 | buf[n++] = '0'; |
| 164 | buf[n] = '\0'; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int |
| 172 | __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len) |
| 173 | FLOAT_TYPE value; |
| 174 | int ndigit, *decpt, *sign; |
| 175 | char *buf; |
| 176 | size_t len; |
| 177 | { |
| 178 | int exponent = 0; |
| 179 | |
| 180 | if (isfinite (value) && value != 0.0) |
| 181 | { |
| 182 | /* Slow code that doesn't require -lm functions. */ |
| 183 | FLOAT_TYPE d; |
| 184 | FLOAT_TYPE f = 1.0; |
| 185 | if (value < 0.0) |
| 186 | d = -value; |
| 187 | else |
| 188 | d = value; |
| 189 | /* For denormalized numbers the d < 1.0 case below won't work, |
| 190 | as f can overflow to +Inf. */ |
| 191 | if (d < FLOAT_MIN_10_NORM) |
| 192 | { |
| 193 | value /= FLOAT_MIN_10_NORM; |
| 194 | if (value < 0.0) |
| 195 | d = -value; |
| 196 | else |
| 197 | d = value; |
| 198 | exponent += FLOAT_MIN_10_EXP; |
| 199 | } |
| 200 | if (d < 1.0) |
| 201 | { |
| 202 | do |
| 203 | { |
| 204 | f *= 10.0; |
| 205 | --exponent; |
| 206 | } |
| 207 | while (d * f < 1.0); |
| 208 | |
| 209 | value *= f; |
| 210 | } |
| 211 | else if (d >= 10.0) |
| 212 | { |
| 213 | do |
| 214 | { |
| 215 | f *= 10; |
| 216 | ++exponent; |
| 217 | } |
| 218 | while (d >= f * 10.0); |
| 219 | |
| 220 | value /= f; |
| 221 | } |
| 222 | } |
| 223 | else if (value == 0.0) |
| 224 | /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0. |
| 225 | This could be changed to -1 if we want to return 0. */ |
| 226 | exponent = 0; |
| 227 | |
| 228 | if (ndigit <= 0 && len > 0) |
| 229 | { |
| 230 | buf[0] = '\0'; |
| 231 | *decpt = 1; |
| 232 | *sign = isfinite (value) ? signbit (value) != 0 : 0; |
| 233 | } |
| 234 | else |
| 235 | if (__APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1, |
| 236 | decpt, sign, buf, len)) |
| 237 | return -1; |
| 238 | |
| 239 | *decpt += exponent; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | #if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0) |
| 244 | # ifdef LONG_DOUBLE_CVT |
| 245 | # define cvt_symbol(symbol) \ |
| 246 | cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \ |
| 247 | APPEND (FUNC_PREFIX, symbol), GLIBC_2_4) |
| 248 | # define cvt_symbol_1(lib, local, symbol, version) \ |
| 249 | versioned_symbol (lib, local, symbol, version) |
| 250 | # else |
| 251 | # define cvt_symbol(symbol) \ |
| 252 | cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \ |
| 253 | APPEND (q, symbol), GLIBC_2_0); \ |
| 254 | weak_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol)) |
| 255 | # define cvt_symbol_1(lib, local, symbol, version) \ |
| 256 | compat_symbol (lib, local, symbol, version) |
| 257 | # endif |
| 258 | #else |
| 259 | # define cvt_symbol(symbol) \ |
| 260 | weak_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol)) |
| 261 | #endif |
| 262 | cvt_symbol(fcvt_r); |
| 263 | cvt_symbol(ecvt_r); |