libc-config: Fix __GNUC_PREREQ macro.
[gnulib.git] / m4 / isfinite.m4
blob09bb8cbaec3e6752ee467a68b8bfee1e0c0b64b6
1 # isfinite.m4 serial 16
2 dnl Copyright (C) 2007-2017 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN([gl_ISFINITE],
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   dnl Persuade glibc <math.h> to declare isfinite.
11   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
12   AC_CHECK_DECLS([isfinite], , , [[#include <math.h>]])
13   if test "$ac_cv_have_decl_isfinite" = yes; then
14     gl_CHECK_MATH_LIB([ISFINITE_LIBM],
15      [x = isfinite (x) + isfinite ((float) x);])
16     if test "$ISFINITE_LIBM" != missing; then
17       dnl Test whether isfinite() on 'long double' works.
18       gl_ISFINITEL_WORKS
19       case "$gl_cv_func_isfinitel_works" in
20         *yes) ;;
21         *)    ISFINITE_LIBM=missing;;
22       esac
23       dnl Also, isfinite() on 'double' does not work on Linux/ia64 (because of
24       dnl signalling NaNs). But this does not have to be tested, since
25       dnl isfinite(long double) also does not work in this situation.
26     fi
27   fi
28   if test "$ac_cv_have_decl_isfinite" != yes ||
29      test "$ISFINITE_LIBM" = missing; then
30     REPLACE_ISFINITE=1
31     dnl No libraries are needed to link lib/isfinite.c.
32     ISFINITE_LIBM=
33   fi
34   AC_SUBST([ISFINITE_LIBM])
37 dnl Test whether isfinite() on 'long double' recognizes all canonical values
38 dnl which are neither finite nor infinite.
39 AC_DEFUN([gl_ISFINITEL_WORKS],
41   AC_REQUIRE([AC_PROG_CC])
42   AC_REQUIRE([gl_BIGENDIAN])
43   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
44   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
45   AC_CACHE_CHECK([whether isfinite(long double) works], [gl_cv_func_isfinitel_works],
46     [
47       AC_RUN_IFELSE([AC_LANG_SOURCE([[
48 #include <float.h>
49 #include <limits.h>
50 #include <math.h>
51 #define NWORDS \
52   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
53 typedef union { unsigned int word[NWORDS]; long double value; }
54         memory_long_double;
55 /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
56    runtime type conversion.  */
57 #ifdef __sgi
58 static long double NaNl ()
60   double zero = 0.0;
61   return zero / zero;
63 #else
64 # define NaNl() (0.0L / 0.0L)
65 #endif
66 int main ()
68   int result = 0;
70   {
71     memory_long_double m;
72     unsigned int i;
74     /* The isfinite macro should be immune against changes in the sign bit and
75        in the mantissa bits.  The xor operation twiddles a bit that can only be
76        a sign bit or a mantissa bit (since the exponent never extends to
77        bit 31).  */
78     m.value = NaNl ();
79     m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
80     for (i = 0; i < NWORDS; i++)
81       m.word[i] |= 1;
82     if (isfinite (m.value))
83       result |= 1;
84   }
86 #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
87 /* Representation of an 80-bit 'long double' as an initializer for a sequence
88    of 'unsigned int' words.  */
89 # ifdef WORDS_BIGENDIAN
90 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
91      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
92        ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16),   \
93        (unsigned int) (mantlo) << 16                                        \
94      }
95 # else
96 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
97      { mantlo, manthi, exponent }
98 # endif
99   { /* Quiet NaN.  */
100     static memory_long_double x =
101       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
102     if (isfinite (x.value))
103       result |= 2;
104   }
105   {
106     /* Signalling NaN.  */
107     static memory_long_double x =
108       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
109     if (isfinite (x.value))
110       result |= 2;
111   }
112   /* isfinite should return something even for noncanonical values.  */
113   { /* Pseudo-NaN.  */
114     static memory_long_double x =
115       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
116     if (isfinite (x.value) && !isfinite (x.value))
117       result |= 4;
118   }
119   { /* Pseudo-Infinity.  */
120     static memory_long_double x =
121       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
122     if (isfinite (x.value) && !isfinite (x.value))
123       result |= 8;
124   }
125   { /* Pseudo-Zero.  */
126     static memory_long_double x =
127       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
128     if (isfinite (x.value) && !isfinite (x.value))
129       result |= 16;
130   }
131   { /* Unnormalized number.  */
132     static memory_long_double x =
133       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
134     if (isfinite (x.value) && !isfinite (x.value))
135       result |= 32;
136   }
137   { /* Pseudo-Denormal.  */
138     static memory_long_double x =
139       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
140     if (isfinite (x.value) && !isfinite (x.value))
141       result |= 64;
142   }
143 #endif
145   return result;
146 }]])],
147       [gl_cv_func_isfinitel_works=yes],
148       [gl_cv_func_isfinitel_works=no],
149       [case "$host_os" in
150                  # Guess no on native Windows.
151          mingw*) gl_cv_func_isfinitel_works="guessing no" ;;
152          *)      gl_cv_func_isfinitel_works="guessing yes" ;;
153        esac
154       ])
155     ])