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