| 1 |
/* ldtoa.c provide long double floating point string conversion routines. |
| 2 |
|
| 3 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. */ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "system.h" |
| 29 |
#include "ansidecl.h" |
| 30 |
|
| 31 |
#define LIBNAME "m2pim" |
| 32 |
|
| 33 |
#include "gm2-libs-host.h" |
| 34 |
#include "m2rts.h" |
| 35 |
|
| 36 |
# ifdef __cplusplus |
| 37 |
extern "C" { |
| 38 |
# endif |
| 39 |
|
| 40 |
#define MAX_FP_DIGITS 500 |
| 41 |
|
| 42 |
typedef enum Mode { maxsignicant, decimaldigits } Mode; |
| 43 |
|
| 44 |
extern int dtoa_calcmaxsig (char *p, int ndigits); |
| 45 |
extern int dtoa_calcdecimal (char *p, int str_size, int ndigits); |
| 46 |
extern bool dtoa_calcsign (char *p, int str_size); |
| 47 |
|
| 48 |
/* maxsignicant: return a string containing max(1,ndigits) |
| 49 |
significant digits. The return string contains the string |
| 50 |
produced by snprintf. decimaldigits: return a string produced by |
| 51 |
fcvt. The string will contain ndigits past the decimal point |
| 52 |
(ndigits may be negative). */ |
| 53 |
|
| 54 |
long double |
| 55 |
ldtoa_strtold (void *s, bool *error) |
| 56 |
{ |
| 57 |
char *endp; |
| 58 |
long double d; |
| 59 |
|
| 60 |
errno = 0; |
| 61 |
#if defined(HAVE_STRTOLD) |
| 62 |
d = strtold (reinterpret_cast <char *> (s), &endp); |
| 63 |
#else |
| 64 |
/* fall back to using strtod. */ |
| 65 |
d = (long double)strtod (reinterpret_cast <char *> (s), &endp); |
| 66 |
#endif |
| 67 |
if (endp != NULL && (*endp == '\0')) |
| 68 |
*error = (errno != 0); |
| 69 |
else |
| 70 |
*error = true; |
| 71 |
return d; |
| 72 |
} |
| 73 |
|
| 74 |
char * |
| 75 |
ldtoa_ldtoa (long double d, int mode, int ndigits, int *decpt, bool *sign) |
| 76 |
{ |
| 77 |
char format[50]; |
| 78 |
char *p; |
| 79 |
int r; |
| 80 |
switch (mode) |
| 81 |
{ |
| 82 |
|
| 83 |
case maxsignicant: |
| 84 |
ndigits += 20; /* enough for exponent. */ |
| 85 |
p = (char *) malloc (ndigits); |
| 86 |
snprintf (format, 50, "%s%d%s", "%.", ndigits - 20, "LE"); |
| 87 |
snprintf (p, ndigits, format, d); |
| 88 |
*sign = dtoa_calcsign (p, ndigits); |
| 89 |
*decpt = dtoa_calcmaxsig (p, ndigits); |
| 90 |
return p; |
| 91 |
case decimaldigits: |
| 92 |
p = (char *) malloc (MAX_FP_DIGITS + 20); |
| 93 |
snprintf (format, 50, "%s%d%s", "%.", MAX_FP_DIGITS, "LE"); |
| 94 |
snprintf (p, MAX_FP_DIGITS + 20, format, d); |
| 95 |
*sign = dtoa_calcsign (p, MAX_FP_DIGITS + 20); |
| 96 |
*decpt = dtoa_calcdecimal (p, MAX_FP_DIGITS + 20, ndigits); |
| 97 |
return p; |
| 98 |
default: |
| 99 |
abort (); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/* GNU Modula-2 hooks */ |
| 104 |
|
| 105 |
void |
| 106 |
_M2_ldtoa_init (int, char **, char **) |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
_M2_ldtoa_finish (int, char **, char **) |
| 112 |
{ |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
_M2_ldtoa_dep (void) |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
# ifdef __cplusplus |
| 121 |
} |
| 122 |
|
| 123 |
extern "C" void __attribute__((__constructor__)) |
| 124 |
_M2_ldtoa_ctor (void) |
| 125 |
{ |
| 126 |
M2RTS_RegisterModule_Cstr ("ldtoa", LIBNAME, _M2_ldtoa_init, |
| 127 |
_M2_ldtoa_finish, _M2_ldtoa_dep); |
| 128 |
} |
| 129 |
|
| 130 |
#else |
| 131 |
void |
| 132 |
_M2_ldtoa_ctor (void) |
| 133 |
{ |
| 134 |
} |
| 135 |
|
| 136 |
# endif |