1 /* ldtoa.c convert long double to ascii and visa versa.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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.
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/>. */
32 #define EXPORT(FUNC) m2pim ## _ldtoa_ ## FUNC
33 #define IMPORT(MODULE,FUNC) m2pim ## _ ## MODULE ## _ ## FUNC
34 #define M2EXPORT(FUNC) m2pim ## _M2_ldtoa_ ## FUNC
35 #define M2LIBNAME "m2pim"
37 #if defined(HAVE_STRINGS)
41 #if defined(HAVE_STRING)
45 #if defined(HAVE_STDDEF_H)
46 /* Obtain a definition for NULL. */
50 #if defined(HAVE_STDIO_H)
51 /* Obtain a definition for NULL. */
55 #if defined(HAVE_TIME_H)
56 /* Obtain a definition for NULL. */
60 #if defined(HAVE_STRING_H)
61 /* Obtain a definition for NULL. */
65 #if defined(HAVE_WCHAR_H)
66 /* Obtain a definition for NULL. */
70 #if defined(HAVE_STDLIB_H)
71 #if !defined(_ISOC99_SOURCE)
72 #define _ISOC99_SOURCE
77 #if defined(HAVE_ERRNO_H)
81 #if defined(HAVE_SYS_ERRNO_H)
82 #include <sys/errno.h>
85 #if defined(HAVE_STDLIB_H)
86 /* Obtain a prototype for free and malloc. */
91 #define NULL (void *)0
94 #define MAX_FP_DIGITS 500
96 typedef enum Mode
{ maxsignicant
, decimaldigits
} Mode
;
98 extern "C" int IMPORT(dtoa
,calcmaxsig
) (char *p
, int ndigits
);
99 extern "C" int IMPORT(dtoa
,calcdecimal
) (char *p
, int str_size
, int ndigits
);
100 extern "C" int IMPORT(dtoa
,calcsign
) (char *p
, int str_size
);
102 /* maxsignicant return a string containing max(1,ndigits) significant
103 digits. The return string contains the string produced by snprintf.
105 decimaldigits: return a string produced by fcvt. The string will
106 contain ndigits past the decimal point (ndigits may be negative). */
108 extern "C" long double
109 EXPORT(strtold
) (const char *s
, bool *error
)
114 #if defined(HAVE_ERRNO_H)
117 #if defined(HAVE_STRTOLD)
118 d
= strtold (s
, &endp
);
120 /* Fall back to using strtod. */
121 d
= (long double)strtod (s
, &endp
);
123 if (endp
!= NULL
&& (*endp
== '\0'))
124 #if defined(HAVE_ERRNO_H)
125 *error
= (errno
!= 0);
135 EXPORT(ldtoa
) (long double d
, int mode
, int ndigits
, int *decpt
, bool *sign
)
144 ndigits
+= 20; /* Enough for exponent. */
145 p
= (char *) malloc (ndigits
);
146 snprintf (format
, 50, "%s%d%s", "%.", ndigits
- 20, "LE");
147 snprintf (p
, ndigits
, format
, d
);
148 *sign
= IMPORT(dtoa
,calcsign
) (p
, ndigits
);
149 *decpt
= IMPORT(dtoa
,calcmaxsig
) (p
, ndigits
);
152 p
= (char *) malloc (MAX_FP_DIGITS
+ 20);
153 snprintf (format
, 50, "%s%d%s", "%.", MAX_FP_DIGITS
, "LE");
154 snprintf (p
, MAX_FP_DIGITS
+ 20, format
, d
);
155 *sign
= IMPORT(dtoa
,calcsign
) (p
, MAX_FP_DIGITS
+ 20);
156 *decpt
= IMPORT(dtoa
,calcdecimal
) (p
, MAX_FP_DIGITS
+ 20, ndigits
);
164 /* GNU Modula-2 linking hooks. */
167 M2EXPORT(init
) (int, char **, char **)
172 M2EXPORT(fini
) (int, char **, char **)
181 extern "C" void __attribute__((__constructor__
))
182 M2EXPORT(ctor
) (void)
184 m2pim_M2RTS_RegisterModule ("ldtoa", M2LIBNAME
,
185 M2EXPORT(init
), M2EXPORT(fini
),