Move the isnanf(), isnand(), isnanl() declarations to <math.h>.
[gnulib.git] / m4 / isnanl.m4
1 # isnanl.m4 serial 9
2 dnl Copyright (C) 2007-2008 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_FUNC_ISNANL],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   ISNANL_LIBM=
11   gl_HAVE_ISNANL_NO_LIBM
12   if test $gl_cv_func_isnanl_no_libm = no; then
13     gl_HAVE_ISNANL_IN_LIBM
14     if test $gl_cv_func_isnanl_in_libm = yes; then
15       ISNANL_LIBM=-lm
16     fi
17   fi
18   if test $gl_cv_func_isnanl_no_libm = yes \
19      || test $gl_cv_func_isnanl_in_libm = yes; then
20     save_LIBS="$LIBS"
21     LIBS="$LIBS $ISNANL_LIBM"
22     gl_FUNC_ISNANL_WORKS
23     LIBS="$save_LIBS"
24     case "$gl_cv_func_isnanl_works" in
25       *yes) gl_func_isnanl=yes ;;
26       *)    gl_func_isnanl=no; ISNANL_LIBM= ;;
27     esac
28   else
29     gl_func_isnanl=no
30   fi
31   if test $gl_func_isnanl != yes; then
32     HAVE_ISNANL=0
33     gl_BUILD_ISNANL
34   fi
35   AC_SUBST([ISNANL_LIBM])
36 ])
37
38 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
39 [
40   gl_HAVE_ISNANL_NO_LIBM
41   gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
42   if test $gl_func_isnanl_no_libm = yes; then
43     gl_FUNC_ISNANL_WORKS
44     case "$gl_cv_func_isnanl_works" in
45       *yes) ;;
46       *)    gl_func_isnanl_no_libm=no ;;
47     esac
48   fi
49   if test $gl_func_isnanl_no_libm = yes; then
50     AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
51       [Define if the isnan(long double) function is available in libc.])
52   else
53     gl_BUILD_ISNANL
54   fi
55 ])
56
57 dnl Pull in replacement isnanl definition. It does not need -lm.
58 AC_DEFUN([gl_BUILD_ISNANL],
59 [
60   AC_LIBOBJ([isnanl])
61   gl_LONG_DOUBLE_EXPONENT_LOCATION
62 ])
63
64 dnl Test whether isnanl() can be used without libm.
65 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
66 [
67   AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
68     [gl_cv_func_isnanl_no_libm],
69     [
70       AC_TRY_LINK([#include <math.h>
71                    #if __GNUC__ >= 4
72                    # undef isnanl
73                    # define isnanl(x) __builtin_isnanl ((long double)(x))
74                    #elif defined isnan
75                    # undef isnanl
76                    # define isnanl(x) isnan ((long double)(x))
77                    #endif
78                    long double x;],
79                   [return isnanl (x);],
80         [gl_cv_func_isnanl_no_libm=yes],
81         [gl_cv_func_isnanl_no_libm=no])
82     ])
83 ])
84
85 dnl Test whether isnanl() can be used with libm.
86 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
87 [
88   AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
89     [gl_cv_func_isnanl_in_libm],
90     [
91       save_LIBS="$LIBS"
92       LIBS="$LIBS -lm"
93       AC_TRY_LINK([#include <math.h>
94                    #if __GNUC__ >= 4
95                    # undef isnanl
96                    # define isnanl(x) __builtin_isnanl ((long double)(x))
97                    #elif defined isnan
98                    # undef isnanl
99                    # define isnanl(x) isnan ((long double)(x))
100                    #endif
101                    long double x;],
102                   [return isnanl (x);],
103         [gl_cv_func_isnanl_in_libm=yes],
104         [gl_cv_func_isnanl_in_libm=no])
105       LIBS="$save_LIBS"
106     ])
107 ])
108
109 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
110 dnl infinite. This test fails e.g. on NetBSD/i386 and on glibc/ia64.
111 dnl Also, the GCC >= 4.0 built-in __builtin_isnanl does not pass the tests
112 dnl - for pseudo-denormals on i686 and x86_64,
113 dnl - for pseudo-zeroes, unnormalized numbers, and pseudo-denormals on ia64.
114 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
115 [
116   AC_REQUIRE([AC_PROG_CC])
117   AC_REQUIRE([AC_C_BIGENDIAN])
118   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
119   AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
120     [
121       AC_TRY_RUN([
122 #include <float.h>
123 #include <limits.h>
124 #include <math.h>
125 #if __GNUC__ >= 4
126 # undef isnanl
127 # define isnanl(x) __builtin_isnanl ((long double)(x))
128 #elif defined isnan
129 # undef isnanl
130 # define isnanl(x) isnan ((long double)(x))
131 #endif
132 #define NWORDS \
133   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
134 typedef union { unsigned int word[NWORDS]; long double value; }
135         memory_long_double;
136 int main ()
137 {
138   memory_long_double m;
139   unsigned int i;
140
141   /* gcc-3.4.3 on IRIX 6.5 appears to have a problem with this.  */
142   if (!isnanl (0.0L / 0.0L))
143     return 1;
144
145   /* The isnanl function should be immune against changes in the sign bit and
146      in the mantissa bits.  The xor operation twiddles a bit that can only be
147      a sign bit or a mantissa bit (since the exponent never extends to
148      bit 31).  */
149   m.value = 0.0L / 0.0L;
150   m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
151   for (i = 0; i < NWORDS; i++)
152     m.word[i] |= 1;
153   if (!isnanl (m.value))
154     return 1;
155
156 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
157 /* Representation of an 80-bit 'long double' as an initializer for a sequence
158    of 'unsigned int' words.  */
159 # ifdef WORDS_BIGENDIAN
160 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
161      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
162        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
163        (unsigned int) (mantlo) << 16                                        \
164      }
165 # else
166 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
167      { mantlo, manthi, exponent }
168 # endif
169   { /* Quiet NaN.  */
170     static memory_long_double x =
171       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
172     if (!isnanl (x.value))
173       return 1;
174   }
175   {
176     /* Signalling NaN.  */
177     static memory_long_double x =
178       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
179     if (!isnanl (x.value))
180       return 1;
181   }
182   /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
183      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
184        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
185        Application Architecture.
186        Table 5-2 "Floating-Point Register Encodings"
187        Figure 5-6 "Memory to Floating-Point Register Data Translation"
188    */
189   { /* Pseudo-NaN.  */
190     static memory_long_double x =
191       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
192     if (!isnanl (x.value))
193       return 1;
194   }
195   { /* Pseudo-Infinity.  */
196     static memory_long_double x =
197       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
198     if (!isnanl (x.value))
199       return 1;
200   }
201   { /* Pseudo-Zero.  */
202     static memory_long_double x =
203       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
204     if (!isnanl (x.value))
205       return 1;
206   }
207   { /* Unnormalized number.  */
208     static memory_long_double x =
209       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
210     if (!isnanl (x.value))
211       return 1;
212   }
213   { /* Pseudo-Denormal.  */
214     static memory_long_double x =
215       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
216     if (!isnanl (x.value))
217       return 1;
218   }
219 #endif
220
221   return 0;
222 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
223       [case "$host_cpu" in
224                                # Guess no on ia64, x86_64, i386.
225          ia64 | x86_64 | i*86) gl_cv_func_isnanl_works="guessing no";;
226          *)
227            case "$host_os" in
228              netbsd*) gl_cv_func_isnanl_works="guessing no";;
229              *)       gl_cv_func_isnanl_works="guessing yes";;
230            esac
231            ;;
232        esac
233       ])
234     ])
235 ])