Add isnan module.
[gnulib.git] / m4 / isnanl.m4
1 # isnanl.m4 serial 7
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   ISNANL_LIBM=
10   gl_HAVE_ISNANL_NO_LIBM
11   if test $gl_cv_func_isnanl_no_libm = no; then
12     gl_HAVE_ISNANL_IN_LIBM
13     if test $gl_cv_func_isnanl_in_libm = yes; then
14       ISNANL_LIBM=-lm
15     fi
16   fi
17   if test $gl_cv_func_isnanl_no_libm = yes \
18      || test $gl_cv_func_isnanl_in_libm = yes; then
19     save_LIBS="$LIBS"
20     LIBS="$LIBS $ISNANL_LIBM"
21     gl_FUNC_ISNANL_WORKS
22     LIBS="$save_LIBS"
23     case "$gl_cv_func_isnanl_works" in
24       *yes) gl_func_isnanl=yes ;;
25       *)    gl_func_isnanl=no; ISNANL_LIBM= ;;
26     esac
27   else
28     gl_func_isnanl=no
29   fi
30   if test $gl_func_isnanl = yes; then
31     AC_DEFINE([HAVE_ISNANL], 1,
32       [Define if the isnan(long double) function is available.])
33   else
34     gl_BUILD_ISNANL
35   fi
36   AC_SUBST([ISNANL_LIBM])
37 ])
38
39 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
40 [
41   gl_HAVE_ISNANL_NO_LIBM
42   gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
43   if test $gl_func_isnanl_no_libm = yes; then
44     gl_FUNC_ISNANL_WORKS
45     case "$gl_cv_func_isnanl_works" in
46       *yes) ;;
47       *)    gl_func_isnanl_no_libm=no ;;
48     esac
49   fi
50   if test $gl_func_isnanl_no_libm = yes; then
51     AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
52       [Define if the isnan(long double) function is available in libc.])
53   else
54     gl_BUILD_ISNANL
55   fi
56 ])
57
58 dnl Pull in replacement isnanl definition.
59 AC_DEFUN([gl_BUILD_ISNANL],
60 [
61   AC_LIBOBJ([isnanl])
62   gl_LONG_DOUBLE_EXPONENT_LOCATION
63 ])
64
65 dnl Test whether isnanl() can be used without libm.
66 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
67 [
68   AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
69     [gl_cv_func_isnanl_no_libm],
70     [
71       AC_TRY_LINK([#include <math.h>
72                    #if __GNUC__ >= 4
73                    # undef isnanl
74                    # define isnanl(x) __builtin_isnanl ((long double)(x))
75                    #elif defined isnan
76                    # undef isnanl
77                    # define isnanl(x) isnan ((long double)(x))
78                    #endif
79                    long double x;],
80                   [return isnanl (x);],
81         [gl_cv_func_isnanl_no_libm=yes],
82         [gl_cv_func_isnanl_no_libm=no])
83     ])
84 ])
85
86 dnl Test whether isnanl() can be used with libm.
87 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
88 [
89   AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
90     [gl_cv_func_isnanl_in_libm],
91     [
92       save_LIBS="$LIBS"
93       LIBS="$LIBS -lm"
94       AC_TRY_LINK([#include <math.h>
95                    #if __GNUC__ >= 4
96                    # undef isnanl
97                    # define isnanl(x) __builtin_isnanl ((long double)(x))
98                    #elif defined isnan
99                    # undef isnanl
100                    # define isnanl(x) isnan ((long double)(x))
101                    #endif
102                    long double x;],
103                   [return isnanl (x);],
104         [gl_cv_func_isnanl_in_libm=yes],
105         [gl_cv_func_isnanl_in_libm=no])
106       LIBS="$save_LIBS"
107     ])
108 ])
109
110 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
111 dnl infinite. This test fails e.g. on NetBSD/i386 and on glibc/ia64.
112 dnl Also, the GCC >= 4.0 built-in __builtin_isnanl does not pass the tests
113 dnl - for pseudo-denormals on i686 and x86_64,
114 dnl - for pseudo-zeroes, unnormalized numbers, and pseudo-denormals on ia64.
115 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
116 [
117   AC_REQUIRE([AC_PROG_CC])
118   AC_REQUIRE([AC_C_BIGENDIAN])
119   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
120   AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
121     [
122       AC_TRY_RUN([
123 #include <float.h>
124 #include <limits.h>
125 #include <math.h>
126 #if __GNUC__ >= 4
127 # undef isnanl
128 # define isnanl(x) __builtin_isnanl ((long double)(x))
129 #elif defined isnan
130 # undef isnanl
131 # define isnanl(x) isnan ((long double)(x))
132 #endif
133 #define NWORDS \
134   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
135 typedef union { unsigned int word[NWORDS]; long double value; }
136         memory_long_double;
137 int main ()
138 {
139   memory_long_double m;
140   unsigned int i;
141
142   /* gcc-3.4.3 on IRIX 6.5 appears to have a problem with this.  */
143   if (!isnanl (0.0L / 0.0L))
144     return 1;
145
146   /* The isnanl function should be immune against changes in the sign bit and
147      in the mantissa bits.  The xor operation twiddles a bit that can only be
148      a sign bit or a mantissa bit (since the exponent never extends to
149      bit 31).  */
150   m.value = 0.0L / 0.0L;
151   m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
152   for (i = 0; i < NWORDS; i++)
153     m.word[i] |= 1;
154   if (!isnanl (m.value))
155     return 1;
156
157 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
158 /* Representation of an 80-bit 'long double' as an initializer for a sequence
159    of 'unsigned int' words.  */
160 # ifdef WORDS_BIGENDIAN
161 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
162      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
163        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
164        (unsigned int) (mantlo) << 16                                        \
165      }
166 # else
167 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
168      { mantlo, manthi, exponent }
169 # endif
170   { /* Quiet NaN.  */
171     static memory_long_double x =
172       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
173     if (!isnanl (x.value))
174       return 1;
175   }
176   {
177     /* Signalling NaN.  */
178     static memory_long_double x =
179       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
180     if (!isnanl (x.value))
181       return 1;
182   }
183   /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
184      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
185        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
186        Application Architecture.
187        Table 5-2 "Floating-Point Register Encodings"
188        Figure 5-6 "Memory to Floating-Point Register Data Translation"
189    */
190   { /* Pseudo-NaN.  */
191     static memory_long_double x =
192       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
193     if (!isnanl (x.value))
194       return 1;
195   }
196   { /* Pseudo-Infinity.  */
197     static memory_long_double x =
198       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
199     if (!isnanl (x.value))
200       return 1;
201   }
202   { /* Pseudo-Zero.  */
203     static memory_long_double x =
204       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
205     if (!isnanl (x.value))
206       return 1;
207   }
208   { /* Unnormalized number.  */
209     static memory_long_double x =
210       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
211     if (!isnanl (x.value))
212       return 1;
213   }
214   { /* Pseudo-Denormal.  */
215     static memory_long_double x =
216       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
217     if (!isnanl (x.value))
218       return 1;
219   }
220 #endif
221
222   return 0;
223 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
224       [case "$host_cpu" in
225                                # Guess no on ia64, x86_64, i386.
226          ia64 | x86_64 | i*86) gl_cv_func_isnanl_works="guessing no";;
227          *)
228            case "$host_os" in
229              netbsd*) gl_cv_func_isnanl_works="guessing no";;
230              *)       gl_cv_func_isnanl_works="guessing yes";;
231            esac
232            ;;
233        esac
234       ])
235     ])
236 ])
237
238 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
239 [
240   AC_REQUIRE([AC_C_BIGENDIAN])
241   AC_CACHE_CHECK([where to find the exponent in a 'long double'],
242     [gl_cv_cc_long_double_expbit0],
243     [
244       AC_TRY_RUN([
245 #include <float.h>
246 #include <stddef.h>
247 #include <stdio.h>
248 #include <string.h>
249 #define NWORDS \
250   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
251 typedef union { long double value; unsigned int word[NWORDS]; }
252         memory_long_double;
253 static unsigned int ored_words[NWORDS];
254 static unsigned int anded_words[NWORDS];
255 static void add_to_ored_words (long double x)
256 {
257   memory_long_double m;
258   size_t i;
259   /* Clear it first, in case
260      sizeof (long double) < sizeof (memory_long_double).  */
261   memset (&m, 0, sizeof (memory_long_double));
262   m.value = x;
263   for (i = 0; i < NWORDS; i++)
264     {
265       ored_words[i] |= m.word[i];
266       anded_words[i] &= m.word[i];
267     }
268 }
269 int main ()
270 {
271   size_t j;
272   FILE *fp = fopen ("conftest.out", "w");
273   if (fp == NULL)
274     return 1;
275   for (j = 0; j < NWORDS; j++)
276     anded_words[j] = ~ (unsigned int) 0;
277   add_to_ored_words (0.25L);
278   add_to_ored_words (0.5L);
279   add_to_ored_words (1.0L);
280   add_to_ored_words (2.0L);
281   add_to_ored_words (4.0L);
282   /* Remove bits that are common (e.g. if representation of the first mantissa
283      bit is explicit).  */
284   for (j = 0; j < NWORDS; j++)
285     ored_words[j] &= ~anded_words[j];
286   /* Now find the nonzero word.  */
287   for (j = 0; j < NWORDS; j++)
288     if (ored_words[j] != 0)
289       break;
290   if (j < NWORDS)
291     {
292       size_t i;
293       for (i = j + 1; i < NWORDS; i++)
294         if (ored_words[i] != 0)
295           {
296             fprintf (fp, "unknown");
297             return (fclose (fp) != 0);
298           }
299       for (i = 0; ; i++)
300         if ((ored_words[j] >> i) & 1)
301           {
302             fprintf (fp, "word %d bit %d", (int) j, (int) i);
303             return (fclose (fp) != 0);
304           }
305     }
306   fprintf (fp, "unknown");
307   return (fclose (fp) != 0);
308 }
309         ],
310         [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
311         [gl_cv_cc_long_double_expbit0="unknown"],
312         [
313           dnl When cross-compiling, we don't know. It depends on the
314           dnl ABI and compiler version. There are too many cases.
315           gl_cv_cc_long_double_expbit0="unknown"
316         ])
317       rm -f conftest.out
318     ])
319   case "$gl_cv_cc_long_double_expbit0" in
320     word*bit*)
321       word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
322       bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
323       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
324         [Define as the word index where to find the exponent of 'long double'.])
325       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
326         [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])
327       ;;
328   esac
329 ])