Debian Codegraph @

m4/isfinite.m4

raw
1 # isfinite.m4
2 # serial 20
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([gl_ISFINITE],
10 [
11 AC_REQUIRE([gl_MATH_H_DEFAULTS])
12 AC_REQUIRE([AC_CANONICAL_HOST])
13 dnl Persuade glibc <math.h> to declare isfinite.
14 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15 AC_CHECK_DECLS([isfinite], , , [[#include <math.h>]])
16 if test "$ac_cv_have_decl_isfinite" = yes; then
17 gl_CHECK_MATH_LIB([ISFINITE_LIBM], [double],
18 [x = isfinite (x) + isfinite ((float) x);])
19 if test "$ISFINITE_LIBM" != missing; then
20 dnl Test whether isfinite() on 'long double' works.
21 gl_ISFINITEL_WORKS
22 case "$gl_cv_func_isfinitel_works" in
23 *yes) ;;
24 *) ISFINITE_LIBM=missing;;
25 esac
26 dnl Also, isfinite() on 'double' does not work on Linux/ia64 (because of
27 dnl signalling NaNs). But this does not have to be tested, since
28 dnl isfinite(long double) also does not work in this situation.
29 fi
30 fi
31 dnl On Solaris 10, with CC in C++ mode, isfinite is not available although
32 dnl is with cc in C mode. This cannot be worked around by defining
33 dnl _XOPEN_SOURCE=600, because the latter does not work in C++ mode on
34 dnl Solaris 11.0. Therefore use the replacement functions on Solaris.
35 if test "$ac_cv_have_decl_isfinite" != yes \
36 || test "$ISFINITE_LIBM" = missing \
37 || { case "$host_os" in solaris*) true;; *) false;; esac; }; then
38 REPLACE_ISFINITE=1
39 dnl No libraries are needed to link lib/isfinite.c.
40 ISFINITE_LIBM=
41 fi
42 AC_SUBST([ISFINITE_LIBM])
43 ])
44
45 dnl Test whether isfinite() on 'long double' recognizes all canonical values
46 dnl which are neither finite nor infinite.
47 AC_DEFUN([gl_ISFINITEL_WORKS],
48 [
49 AC_REQUIRE([AC_PROG_CC])
50 AC_REQUIRE([gl_BIGENDIAN])
51 AC_REQUIRE([gl_LONG_DOUBLE_EXPONENT_LOCATION])
52 AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
53 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
54 AC_CACHE_CHECK([whether isfinite(long double) works], [gl_cv_func_isfinitel_works],
55 [
56 AC_RUN_IFELSE([AC_LANG_SOURCE([[
57 #include <float.h>
58 #include <limits.h>
59 #include <math.h>
60 #define NWORDS \
61 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
62 typedef union { unsigned int word[NWORDS]; long double value; }
63 memory_long_double;
64 /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
65 runtime type conversion. */
66 #ifdef __sgi
67 static long double NaNl ()
68 {
69 double zero = 0.0;
70 return zero / zero;
71 }
72 #else
73 # define NaNl() (0.0L / 0.0L)
74 #endif
75 int main ()
76 {
77 int result = 0;
78
79 {
80 memory_long_double m;
81 unsigned int i;
82
83 /* The isfinite macro should be immune against changes in the sign bit and
84 in the mantissa bits. The xor operation twiddles a bit that can only be
85 a sign bit or a mantissa bit (since the exponent never extends to
86 bit 31). */
87 m.value = NaNl ();
88 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
89 for (i = 0; i < NWORDS; i++)
90 m.word[i] |= 1;
91 if (isfinite (m.value))
92 result |= 1;
93
94 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
95 /* Another NaN, more precisely crafted. */
96 m.value = NaNl ();
97 #if defined __powerpc__ && LDBL_MANT_DIG == 106
98 /* This is PowerPC "double double", a pair of two doubles. Inf and NaN are
99 represented as the corresponding 64-bit IEEE values in the first double;
100 the second is ignored. Manipulate only the first double. */
101 #define HNWORDS \
102 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
103 #else
104 #define HNWORDS NWORDS
105 #endif
106 #if LDBL_EXPBIT0_BIT > 0
107 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
108 #else
109 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < HNWORDS / 2 ? 1 : - 1)]
110 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
111 #endif
112 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < HNWORDS / 2 ? 1 : - 1)]
113 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
114 #undef HNWORDS
115 if (isfinite (m.value))
116 result |= 1;
117 #endif
118 }
119
120 #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
121 /* Representation of an 80-bit 'long double' as an initializer for a sequence
122 of 'unsigned int' words. */
123 # ifdef WORDS_BIGENDIAN
124 # define LDBL80_WORDS(exponent,manthi,mantlo) \
125 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
126 ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
127 (unsigned int) (mantlo) << 16 \
128 }
129 # else
130 # define LDBL80_WORDS(exponent,manthi,mantlo) \
131 { mantlo, manthi, exponent }
132 # endif
133 { /* Quiet NaN. */
134 static memory_long_double x =
135 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
136 if (isfinite (x.value))
137 result |= 2;
138 }
139 {
140 /* Signalling NaN. */
141 static memory_long_double x =
142 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
143 if (isfinite (x.value))
144 result |= 2;
145 }
146 /* isfinite should return something even for noncanonical values. */
147 { /* Pseudo-NaN. */
148 static memory_long_double x =
149 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
150 if (isfinite (x.value) && !isfinite (x.value))
151 result |= 4;
152 }
153 { /* Pseudo-Infinity. */
154 static memory_long_double x =
155 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
156 if (isfinite (x.value) && !isfinite (x.value))
157 result |= 8;
158 }
159 { /* Pseudo-Zero. */
160 static memory_long_double x =
161 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
162 if (isfinite (x.value) && !isfinite (x.value))
163 result |= 16;
164 }
165 { /* Unnormalized number. */
166 static memory_long_double x =
167 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
168 if (isfinite (x.value) && !isfinite (x.value))
169 result |= 32;
170 }
171 { /* Pseudo-Denormal. */
172 static memory_long_double x =
173 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
174 if (isfinite (x.value) && !isfinite (x.value))
175 result |= 64;
176 }
177 #endif
178
179 return result;
180 }]])],
181 [gl_cv_func_isfinitel_works=yes],
182 [gl_cv_func_isfinitel_works=no],
183 [case "$host_os" in
184 # Guess no on native Windows.
185 mingw* | windows*) gl_cv_func_isfinitel_works="guessing no" ;;
186 *) gl_cv_func_isfinitel_works="guessing yes" ;;
187 esac
188 ])
189 ])
190 ])