Debian Codegraph @

m4/isnanl.m4

raw
1 # isnanl.m4
2 # serial 26
3 dnl Copyright (C) 2007-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_ONCE([gl_FUNC_ISNANL],
10 [
11 AC_REQUIRE([gl_MATH_H_DEFAULTS])
12 ISNANL_LIBM=
13 gl_HAVE_ISNANL_NO_LIBM
14 if test $gl_cv_func_isnanl_no_libm = no; then
15 gl_HAVE_ISNANL_IN_LIBM
16 if test $gl_cv_func_isnanl_in_libm = yes; then
17 ISNANL_LIBM=-lm
18 fi
19 fi
20 dnl The variable gl_func_isnanl set here is used by isnan.m4.
21 if test $gl_cv_func_isnanl_no_libm = yes || test -n "$ISNANL_LIBM"; then
22 saved_LIBS="$LIBS"
23 LIBS="$LIBS $ISNANL_LIBM"
24 gl_FUNC_ISNANL_WORKS
25 LIBS="$saved_LIBS"
26 case "$gl_cv_func_isnanl_works" in
27 *yes) gl_func_isnanl=yes ;;
28 *) gl_func_isnanl=no; ISNANL_LIBM= ;;
29 esac
30 else
31 gl_func_isnanl=no
32 fi
33 if test $gl_func_isnanl != yes; then
34 HAVE_ISNANL=0
35 fi
36 AC_SUBST([ISNANL_LIBM])
37 ])
38
39 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
40 [
41 gl_HAVE_ISNANL_NO_LIBM
42 gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
43 if test $gl_func_isnanl_no_libm = yes; then
44 gl_FUNC_ISNANL_WORKS
45 case "$gl_cv_func_isnanl_works" in
46 *yes) ;;
47 *) gl_func_isnanl_no_libm=no ;;
48 esac
49 fi
50 if test $gl_func_isnanl_no_libm = yes; then
51 AC_DEFINE([HAVE_ISNANL_IN_LIBC], [1],
52 [Define if the isnan(long double) function is available in libc.])
53 fi
54 ])
55
56 dnl Prerequisites of replacement isnanl definition. It does not need -lm.
57 AC_DEFUN([gl_PREREQ_ISNANL],
58 [
59 gl_LONG_DOUBLE_EXPONENT_LOCATION
60 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
61 ])
62
63 dnl Test whether isnanl() can be used without libm.
64 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
65 [
66 AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
67 [gl_cv_func_isnanl_no_libm],
68 [
69 AC_LINK_IFELSE(
70 [AC_LANG_PROGRAM(
71 [[#include <math.h>
72 #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
73 # undef isnanl
74 # define isnanl(x) __builtin_isnan ((long double)(x))
75 #elif defined isnan
76 # undef isnanl
77 # define isnanl(x) isnan ((long double)(x))
78 #endif
79 long double x;]],
80 [[return isnanl (x);]])],
81 [gl_cv_func_isnanl_no_libm=yes],
82 [gl_cv_func_isnanl_no_libm=no])
83 ])
84 ])
85
86 dnl Test whether isnanl() can be used with libm.
87 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
88 [
89 AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
90 [gl_cv_func_isnanl_in_libm],
91 [
92 saved_LIBS="$LIBS"
93 LIBS="$LIBS -lm"
94 AC_LINK_IFELSE(
95 [AC_LANG_PROGRAM(
96 [[#include <math.h>
97 #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
98 # undef isnanl
99 # define isnanl(x) __builtin_isnan ((long double)(x))
100 #elif defined isnan
101 # undef isnanl
102 # define isnanl(x) isnan ((long double)(x))
103 #endif
104 long double x;]],
105 [[return isnanl (x);]])],
106 [gl_cv_func_isnanl_in_libm=yes],
107 [gl_cv_func_isnanl_in_libm=no])
108 LIBS="$saved_LIBS"
109 ])
110 ])
111
112 dnl Test whether isnanl() recognizes all canonical numbers which are neither
113 dnl finite nor infinite.
114 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
115 [
116 AC_REQUIRE([AC_PROG_CC])
117 AC_REQUIRE([gl_BIGENDIAN])
118 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
119 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
120 AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
121 [
122 AC_RUN_IFELSE(
123 [AC_LANG_SOURCE([[
124 #include <float.h>
125 #include <limits.h>
126 #include <math.h>
127 #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
128 # undef isnanl
129 # define isnanl(x) __builtin_isnan ((long double)(x))
130 #elif defined isnan
131 # undef isnanl
132 # define isnanl(x) isnan ((long double)(x))
133 #endif
134 #define NWORDS \
135 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
136 typedef union { unsigned int word[NWORDS]; long double value; }
137 memory_long_double;
138 /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
139 runtime type conversion. */
140 #ifdef __sgi
141 static long double NaNl ()
142 {
143 double zero = 0.0;
144 return zero / zero;
145 }
146 #else
147 # define NaNl() (0.0L / 0.0L)
148 #endif
149 int main ()
150 {
151 int result = 0;
152
153 if (!isnanl (NaNl ()))
154 result |= 1;
155
156 {
157 memory_long_double m;
158 unsigned int i;
159
160 /* The isnanl function should be immune against changes in the sign bit and
161 in the mantissa bits. The xor operation twiddles a bit that can only be
162 a sign bit or a mantissa bit (since the exponent never extends to
163 bit 31). */
164 m.value = NaNl ();
165 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
166 for (i = 0; i < NWORDS; i++)
167 m.word[i] |= 1;
168 if (!isnanl (m.value))
169 result |= 1;
170 }
171
172 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
173 /* Representation of an 80-bit 'long double' as an initializer for a sequence
174 of 'unsigned int' words. */
175 # ifdef WORDS_BIGENDIAN
176 # define LDBL80_WORDS(exponent,manthi,mantlo) \
177 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
178 ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
179 (unsigned int) (mantlo) << 16 \
180 }
181 # else
182 # define LDBL80_WORDS(exponent,manthi,mantlo) \
183 { mantlo, manthi, exponent }
184 # endif
185 { /* Quiet NaN. */
186 static memory_long_double x =
187 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
188 if (!isnanl (x.value))
189 result |= 2;
190 }
191 {
192 /* Signalling NaN. */
193 static memory_long_double x =
194 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
195 if (!isnanl (x.value))
196 result |= 2;
197 }
198 /* isnanl should return something even for noncanonical values. */
199 { /* Pseudo-NaN. */
200 static memory_long_double x =
201 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
202 if (isnanl (x.value) && !isnanl (x.value))
203 result |= 4;
204 }
205 { /* Pseudo-Infinity. */
206 static memory_long_double x =
207 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
208 if (isnanl (x.value) && !isnanl (x.value))
209 result |= 8;
210 }
211 { /* Pseudo-Zero. */
212 static memory_long_double x =
213 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
214 if (isnanl (x.value) && !isnanl (x.value))
215 result |= 16;
216 }
217 { /* Unnormalized number. */
218 static memory_long_double x =
219 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
220 if (isnanl (x.value) && !isnanl (x.value))
221 result |= 32;
222 }
223 { /* Pseudo-Denormal. */
224 static memory_long_double x =
225 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
226 if (isnanl (x.value) && !isnanl (x.value))
227 result |= 64;
228 }
229 #endif
230
231 return result;
232 }]])],
233 [gl_cv_func_isnanl_works=yes],
234 [gl_cv_func_isnanl_works=no],
235 [case "$host_os" in
236 mingw* | windows*) # Guess yes on mingw, no on MSVC.
237 AC_EGREP_CPP([Known], [
238 #ifdef __MINGW32__
239 Known
240 #endif
241 ],
242 [gl_cv_func_isnanl_works="guessing yes"],
243 [gl_cv_func_isnanl_works="guessing no"])
244 ;;
245 *) gl_cv_func_isnanl_works="guessing yes" ;;
246 esac
247 ])
248 ])
249 ])