verify: new macro 'assume'
[gnulib.git] / m4 / isfinite.m4
1 # isfinite.m4 serial 13
2 dnl Copyright (C) 2007-2013 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.
6
7 AC_DEFUN([gl_ISFINITE],
8 [
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])
35 ])
36
37 dnl Test whether isfinite() on 'long double' recognizes all numbers which are
38 dnl neither finite nor infinite. This test fails e.g. on i686, x86_64, ia64,
39 dnl because of
40 dnl - pseudo-denormals on x86_64,
41 dnl - pseudo-zeroes, unnormalized numbers, and pseudo-denormals on i686,
42 dnl - pseudo-NaN, pseudo-Infinity, pseudo-zeroes, unnormalized numbers, and
43 dnl   pseudo-denormals on ia64.
44 AC_DEFUN([gl_ISFINITEL_WORKS],
45 [
46   AC_REQUIRE([AC_PROG_CC])
47   AC_REQUIRE([gl_BIGENDIAN])
48   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
49   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
50   AC_CACHE_CHECK([whether isfinite(long double) works], [gl_cv_func_isfinitel_works],
51     [
52       AC_RUN_IFELSE([AC_LANG_SOURCE([[
53 #include <float.h>
54 #include <limits.h>
55 #include <math.h>
56 #define NWORDS \
57   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
58 typedef union { unsigned int word[NWORDS]; long double value; }
59         memory_long_double;
60 /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
61    runtime type conversion.  */
62 #ifdef __sgi
63 static long double NaNl ()
64 {
65   double zero = 0.0;
66   return zero / zero;
67 }
68 #else
69 # define NaNl() (0.0L / 0.0L)
70 #endif
71 int main ()
72 {
73   int result = 0;
74
75   {
76     memory_long_double m;
77     unsigned int i;
78
79     /* The isfinite macro should be immune against changes in the sign bit and
80        in the mantissa bits.  The xor operation twiddles a bit that can only be
81        a sign bit or a mantissa bit (since the exponent never extends to
82        bit 31).  */
83     m.value = NaNl ();
84     m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
85     for (i = 0; i < NWORDS; i++)
86       m.word[i] |= 1;
87     if (isfinite (m.value))
88       result |= 1;
89   }
90
91 #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
92 /* Representation of an 80-bit 'long double' as an initializer for a sequence
93    of 'unsigned int' words.  */
94 # ifdef WORDS_BIGENDIAN
95 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
96      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
97        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
98        (unsigned int) (mantlo) << 16                                        \
99      }
100 # else
101 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
102      { mantlo, manthi, exponent }
103 # endif
104   { /* Quiet NaN.  */
105     static memory_long_double x =
106       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
107     if (isfinite (x.value))
108       result |= 2;
109   }
110   {
111     /* Signalling NaN.  */
112     static memory_long_double x =
113       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
114     if (isfinite (x.value))
115       result |= 2;
116   }
117   /* The isfinite macro should recognize Pseudo-NaNs, Pseudo-Infinities,
118      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
119        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
120        Application Architecture.
121        Table 5-2 "Floating-Point Register Encodings"
122        Figure 5-6 "Memory to Floating-Point Register Data Translation"
123    */
124   { /* Pseudo-NaN.  */
125     static memory_long_double x =
126       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
127     if (isfinite (x.value))
128       result |= 4;
129   }
130   { /* Pseudo-Infinity.  */
131     static memory_long_double x =
132       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
133     if (isfinite (x.value))
134       result |= 8;
135   }
136   { /* Pseudo-Zero.  */
137     static memory_long_double x =
138       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
139     if (isfinite (x.value))
140       result |= 16;
141   }
142   { /* Unnormalized number.  */
143     static memory_long_double x =
144       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
145     if (isfinite (x.value))
146       result |= 32;
147   }
148   { /* Pseudo-Denormal.  */
149     static memory_long_double x =
150       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
151     if (isfinite (x.value))
152       result |= 64;
153   }
154 #endif
155
156   return result;
157 }]])], [gl_cv_func_isfinitel_works=yes], [gl_cv_func_isfinitel_works=no],
158       [case "$host_cpu" in
159                                # Guess no on ia64, x86_64, i386.
160          ia64 | x86_64 | i*86) gl_cv_func_isfinitel_works="guessing no";;
161          *)                    gl_cv_func_isfinitel_works="guessing yes";;
162        esac
163       ])
164     ])
165 ])