maint: update copyright
[gnulib.git] / m4 / isinf.m4
1 # isinf.m4 serial 9
2 dnl Copyright (C) 2007-2014 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_ISINF],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   dnl Persuade glibc <math.h> to declare isinf.
11   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
12   AC_CHECK_DECLS([isinf], , ,
13     [[#include <math.h>
14       #ifndef isinf
15       #error "isinf must be a macro, not a function"
16       #endif
17     ]])
18   if test "$ac_cv_have_decl_isinf" = yes; then
19     gl_CHECK_MATH_LIB([ISINF_LIBM], [x = isinf (x) + isinf ((float) x);])
20     if test "$ISINF_LIBM" != missing; then
21       dnl Test whether isinf() on 'long double' works.
22       gl_ISINFL_WORKS
23       case "$gl_cv_func_isinfl_works" in
24         *yes) ;;
25         *)    ISINF_LIBM=missing;;
26       esac
27     fi
28   fi
29   if test "$ac_cv_have_decl_isinf" != yes ||
30      test "$ISINF_LIBM" = missing; then
31     REPLACE_ISINF=1
32     dnl No libraries are needed to link lib/isinf.c.
33     ISINF_LIBM=
34   fi
35   AC_SUBST([ISINF_LIBM])
36 ])
37
38 dnl Test whether isinf() works:
39 dnl 1) Whether it correctly returns false for LDBL_MAX.
40 dnl 2) Whether on 'long double' recognizes all numbers which are neither
41 dnl    finite nor infinite. This test fails on OpenBSD/x86, but could also
42 dnl    fail e.g. on i686, x86_64, ia64, because of
43 dnl    - pseudo-denormals on x86_64,
44 dnl    - pseudo-zeroes, unnormalized numbers, and pseudo-denormals on i686,
45 dnl    - pseudo-NaN, pseudo-Infinity, pseudo-zeroes, unnormalized numbers, and
46 dnl      pseudo-denormals on ia64.
47 AC_DEFUN([gl_ISINFL_WORKS],
48 [
49   AC_REQUIRE([AC_PROG_CC])
50   AC_REQUIRE([gl_BIGENDIAN])
51   AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
52   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
53   AC_CACHE_CHECK([whether isinf(long double) works], [gl_cv_func_isinfl_works],
54     [
55       AC_RUN_IFELSE(
56         [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   if (isinf (LDBL_MAX))
80     result |= 1;
81
82   {
83     memory_long_double m;
84     unsigned int i;
85
86     /* The isinf macro should be immune against changes in the sign bit and
87        in the mantissa bits.  The xor operation twiddles a bit that can only be
88        a sign bit or a mantissa bit (since the exponent never extends to
89        bit 31).  */
90     m.value = NaNl ();
91     m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
92     for (i = 0; i < NWORDS; i++)
93       m.word[i] |= 1;
94     if (isinf (m.value))
95       result |= 2;
96   }
97
98 #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
99 /* Representation of an 80-bit 'long double' as an initializer for a sequence
100    of 'unsigned int' words.  */
101 # ifdef WORDS_BIGENDIAN
102 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
103      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
104        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
105        (unsigned int) (mantlo) << 16                                        \
106      }
107 # else
108 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
109      { mantlo, manthi, exponent }
110 # endif
111   { /* Quiet NaN.  */
112     static memory_long_double x =
113       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
114     if (isinf (x.value))
115       result |= 2;
116   }
117   {
118     /* Signalling NaN.  */
119     static memory_long_double x =
120       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
121     if (isinf (x.value))
122       result |= 2;
123   }
124   /* The isinf macro should recognize Pseudo-NaNs, Pseudo-Infinities,
125      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
126        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
127        Application Architecture.
128        Table 5-2 "Floating-Point Register Encodings"
129        Figure 5-6 "Memory to Floating-Point Register Data Translation"
130    */
131   { /* Pseudo-NaN.  */
132     static memory_long_double x =
133       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
134     if (isinf (x.value))
135       result |= 4;
136   }
137   { /* Pseudo-Infinity.  */
138     static memory_long_double x =
139       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
140     if (isinf (x.value))
141       result |= 8;
142   }
143   { /* Pseudo-Zero.  */
144     static memory_long_double x =
145       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
146     if (isinf (x.value))
147       result |= 16;
148   }
149   { /* Unnormalized number.  */
150     static memory_long_double x =
151       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
152     if (isinf (x.value))
153       result |= 32;
154   }
155   { /* Pseudo-Denormal.  */
156     static memory_long_double x =
157       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
158     if (isinf (x.value))
159       result |= 64;
160   }
161 #endif
162
163   return result;
164 }]])], [gl_cv_func_isinfl_works=yes], [gl_cv_func_isinfl_works=no],
165       [
166        case "$host" in
167            # Guess no on OpenBSD ia64, x86_64, i386.
168          ia64-*-openbsd* | x86_64-*-openbsd* | i*86-*-openbsd*)
169             gl_cv_func_isinfl_works="guessing no";;
170          *)
171             gl_cv_func_isinfl_works="guessing yes";;
172        esac
173       ])
174     ])
175 ])