Debian Codegraph @

m4/strtod.m4

raw
1 # strtod.m4
2 # serial 31
3 dnl Copyright (C) 2002-2003, 2006-2025 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_FUNC_STRTOD],
10 [
11 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
12 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
13 m4_ifdef([gl_FUNC_STRTOD_OBSOLETE], [
14 dnl Test whether strtod is declared.
15 dnl Don't call AC_FUNC_STRTOD, because it does not have the right guess
16 dnl when cross-compiling.
17 dnl Don't call AC_CHECK_FUNCS([strtod]) because it would collide with the
18 dnl ac_cv_func_strtod variable set by the AC_FUNC_STRTOD macro.
19 AC_CHECK_DECLS_ONCE([strtod])
20 if test $ac_cv_have_decl_strtod != yes; then
21 HAVE_STRTOD=0
22 fi
23 ])
24 if test $HAVE_STRTOD = 1; then
25 AC_CACHE_CHECK([whether strtod obeys C99], [gl_cv_func_strtod_works],
26 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
27 #include <stdlib.h>
28 #include <float.h>
29 #include <math.h>
30 #include <errno.h>
31 /* Compare two numbers with ==.
32 This is a separate function because IRIX 6.5 "cc -O" miscompiles an
33 'x == x' test. */
34 static int
35 numeric_equal (double x, double y)
36 {
37 return x == y;
38 }
39 ]], [[
40 int result = 0;
41 {
42 /* In some old versions of Linux (2000 or before), strtod mis-parses
43 strings with leading '+'. */
44 const char *string = " +69";
45 char *term;
46 double value = strtod (string, &term);
47 if (value != 69 || term != (string + 4))
48 result |= 1;
49 }
50 {
51 /* Under Solaris 2.4, strtod returns the wrong value for the
52 terminating character under some conditions. */
53 const char *string = "NaN";
54 char *term;
55 strtod (string, &term);
56 if (term != string && *(term - 1) == 0)
57 result |= 1;
58 }
59 {
60 /* Older glibc and Cygwin mis-parse "-0x". */
61 const char *string = "-0x";
62 char *term;
63 double value = strtod (string, &term);
64 double zero = 0.0;
65 if (1.0 / value != -1.0 / zero || term != (string + 2))
66 result |= 2;
67 }
68 {
69 /* Many platforms do not parse hex floats. */
70 const char *string = "0XaP+1";
71 char *term;
72 double value = strtod (string, &term);
73 if (value != 20.0 || term != (string + 6))
74 result |= 4;
75 }
76 {
77 /* Many platforms do not parse infinities. HP-UX 11.31 parses inf,
78 but mistakenly sets errno. */
79 const char *string = "inf";
80 char *term;
81 double value;
82 errno = 0;
83 value = strtod (string, &term);
84 if (value != HUGE_VAL || term != (string + 3) || errno)
85 result |= 8;
86 }
87 {
88 /* glibc 2.7 and cygwin 1.5.24 misparse "nan()". */
89 const char *string = "nan()";
90 char *term;
91 double value = strtod (string, &term);
92 if (numeric_equal (value, value) || term != (string + 5))
93 result |= 16;
94 }
95 {
96 /* Darwin 10.6.1 (macOS 10.6.6) misparses "nan(". */
97 const char *string = "nan(";
98 char *term;
99 double value = strtod (string, &term);
100 if (numeric_equal (value, value) || term != (string + 3))
101 result |= 16;
102 }
103 #ifndef _MSC_VER /* On MSVC, this is expected behaviour. */
104 {
105 /* In Cygwin 2.9, strtod does not set errno upon
106 gradual underflow. */
107 const char *string = "1e-320";
108 char *term;
109 double value;
110 errno = 0;
111 value = strtod (string, &term);
112 if (term != (string + 6)
113 || (value > 0.0 && value <= DBL_MIN && errno != ERANGE))
114 result |= 32;
115 }
116 #endif
117 {
118 /* strtod could not set errno upon
119 flush-to-zero underflow. */
120 const char *string = "1E-100000";
121 char *term;
122 double value;
123 errno = 0;
124 value = strtod (string, &term);
125 if (term != (string + 9) || (value == 0.0L && errno != ERANGE))
126 result |= 64;
127 }
128 return result;
129 ]])],
130 [gl_cv_func_strtod_works=yes],
131 [result=$?
132 if expr $result '>=' 64 >/dev/null; then
133 gl_cv_func_strtod_works="no (underflow problem)"
134 else
135 if expr $result '>=' 32 >/dev/null; then
136 gl_cv_func_strtod_works="no (gradual underflow problem)"
137 else
138 gl_cv_func_strtod_works=no
139 fi
140 fi
141 ],
142 [dnl The last known bugs in glibc strtod(), as of this writing,
143 dnl were fixed in version 2.8
144 AC_EGREP_CPP([Lucky user],
145 [
146 #include <features.h>
147 #ifdef __GNU_LIBRARY__
148 #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8) || (__GLIBC__ > 2)) \
149 && !defined __UCLIBC__
150 Lucky user
151 #endif
152 #endif
153 ],
154 [gl_cv_func_strtod_works="guessing yes"],
155 [case "$host_os" in
156 # Guess yes on musl systems.
157 *-musl* | midipix*) gl_cv_func_strtod_works="guessing yes" ;;
158 # Guess 'no (gradual underflow problem)' on Cygwin.
159 cygwin*) gl_cv_func_strtod_works="guessing no (gradual underflow problem)" ;;
160 # Guess yes on native Windows.
161 mingw* | windows*) gl_cv_func_strtod_works="guessing yes" ;;
162 *) gl_cv_func_strtod_works="$gl_cross_guess_normal" ;;
163 esac
164 ])
165 ])
166 ])
167 case "$gl_cv_func_strtod_works" in
168 *yes) ;;
169 *)
170 REPLACE_STRTOD=1
171 case "$gl_cv_func_strtod_works" in
172 *"no (underflow problem)")
173 AC_DEFINE([STRTOD_HAS_UNDERFLOW_BUG], [1],
174 [Define to 1 if strtod does not set errno upon flush-to-zero underflow.])
175 ;;
176 *"no (gradual underflow problem)")
177 AC_DEFINE([STRTOD_HAS_GRADUAL_UNDERFLOW_PROBLEM], [1],
178 [Define to 1 if strtod does not set errno upon gradual underflow.])
179 ;;
180 esac
181 ;;
182 esac
183 fi
184 ])
185
186 # Prerequisites of lib/strtod.c.
187 AC_DEFUN([gl_PREREQ_STRTOD], [
188 AC_REQUIRE([gl_CHECK_LDEXP_NO_LIBM])
189 if test $gl_cv_func_ldexp_no_libm = yes; then
190 AC_DEFINE([HAVE_LDEXP_IN_LIBC], [1],
191 [Define if the ldexp function is available in libc.])
192 fi
193 gl_CHECK_FUNCS_ANDROID([nl_langinfo], [[#include <langinfo.h>]])
194 ])