Oops.
[gnulib.git] / m4 / isnanl.m4
1 # isnanl.m4 serial 2
2 dnl Copyright (C) 2007 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([gt_TYPE_LONGDOUBLE])
10   ISNANL_LIBM=
11   if test $gt_cv_c_long_double = yes; then
12     gl_HAVE_ISNANL_NO_LIBM
13     if test $gl_cv_func_isnanl_no_libm = no; then
14       gl_HAVE_ISNANL_IN_LIBM
15       if test $gl_cv_func_isnanl_in_libm = yes; then
16         ISNANL_LIBM=-lm
17       fi
18     fi
19     if test $gl_cv_func_isnanl_no_libm = yes \
20        || test $gl_cv_func_isnanl_in_libm = yes; then
21       save_LIBS="$LIBS"
22       LIBS="$LIBS $ISNANL_LIBM"
23       gl_FUNC_ISNANL_WORKS
24       LIBS="$save_LIBS"
25       case "$gl_cv_func_isnanl_works" in
26         *yes) gl_func_isnanl=yes ;;
27         *)    gl_func_isnanl=no; ISNANL_LIBM= ;;
28       esac
29     else
30       gl_func_isnanl=no
31     fi
32     if test $gl_func_isnanl = yes; then
33       AC_DEFINE([HAVE_ISNANL], 1,
34         [Define if the isnan(long double) function is available.])
35     else
36       AC_LIBOBJ([isnanl])
37       gl_LONG_DOUBLE_EXPONENT_LOCATION
38     fi
39   fi
40 ])
41
42 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
43 [
44   AC_REQUIRE([gt_TYPE_LONGDOUBLE])
45   if test $gt_cv_c_long_double = yes; then
46     gl_HAVE_ISNANL_NO_LIBM
47     gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
48     if test $gl_func_isnanl_no_libm = yes; then
49       gl_FUNC_ISNANL_WORKS
50       case "$gl_cv_func_isnanl_works" in
51         *yes) ;;
52         *)    gl_func_isnanl_no_libm=no ;;
53       esac
54     fi
55     if test $gl_func_isnanl_no_libm = yes; then
56       AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
57         [Define if the isnan(long double) function is available in libc.])
58     else
59       AC_LIBOBJ([isnanl])
60       gl_LONG_DOUBLE_EXPONENT_LOCATION
61     fi
62   fi
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                    #ifdef isnan
73                    # undef isnanl
74                    # define isnanl(x) isnan ((long double)(x))
75                    #endif
76                    long double x;],
77                   [return isnanl (x);],
78         [gl_cv_func_isnanl_no_libm=yes],
79         [gl_cv_func_isnanl_no_libm=no])
80     ])
81 ])
82
83 dnl Test whether isnanl() can be used with libm.
84 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
85 [
86   AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
87     [gl_cv_func_isnanl_in_libm],
88     [
89       save_LIBS="$LIBS"
90       LIBS="$LIBS -lm"
91       AC_TRY_LINK([#include <math.h>
92                    #ifdef isnan
93                    # undef isnanl
94                    # define isnanl(x) isnan ((long double)(x))
95                    #endif
96                    long double x;],
97                   [return isnanl (x);],
98         [gl_cv_func_isnanl_in_libm=yes],
99         [gl_cv_func_isnanl_in_libm=no])
100       LIBS="$save_LIBS"
101     ])
102 ])
103
104 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
105 dnl infinite. This test fails e.g. on NetBSD/i386.
106 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
107 [
108   AC_REQUIRE([AC_PROG_CC])
109   AC_REQUIRE([gt_TYPE_LONGDOUBLE])
110   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
111   AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
112     [
113       AC_TRY_RUN([
114 #include <limits.h>
115 #include <math.h>
116 #ifdef isnan
117 # undef isnanl
118 # define isnanl(x) isnan ((long double)(x))
119 #endif
120 #define NWORDS \
121   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
122 typedef union { long double value; unsigned int word[NWORDS]; }
123         memory_long_double;
124 int main ()
125 {
126   memory_long_double m;
127   unsigned int i;
128
129   /* The isnanl function should be immune against changes in the sign bit and
130      in the mantissa bits.  The xor operation twiddles a bit that can only be
131      a sign bit or a mantissa bit (since the exponent never extends to
132      bit 31).  */
133   m.value = 0.0L / 0.0L;
134   m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
135   for (i = 0; i < NWORDS; i++)
136     m.word[i] |= 1;
137   if (!isnanl (m.value))
138     return 1;
139
140   return 0;
141 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
142       [case "$host_os" in
143          netbsd*) gl_cv_func_isnanl_works="guessing no";;
144          *)       gl_cv_func_isnanl_works="guessing yes";;
145        esac
146       ])
147     ])
148 ])
149
150 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
151 [
152   AC_CACHE_CHECK([where to find the exponent in a 'long double'],
153     [gl_cv_cc_long_double_expbit0],
154     [
155       AC_TRY_RUN([
156 #include <float.h>
157 #include <stddef.h>
158 #include <stdio.h>
159 #include <string.h>
160 #define NWORDS \
161   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
162 typedef union { long double value; unsigned int word[NWORDS]; }
163         memory_long_double;
164 static unsigned int ored_words[NWORDS];
165 static unsigned int anded_words[NWORDS];
166 static void add_to_ored_words (long double x)
167 {
168   memory_long_double m;
169   size_t i;
170   /* Clear it first, in case
171      sizeof (long double) < sizeof (memory_long_double).  */
172   memset (&m, 0, sizeof (memory_long_double));
173   m.value = x;
174   for (i = 0; i < NWORDS; i++)
175     {
176       ored_words[i] |= m.word[i];
177       anded_words[i] &= m.word[i];
178     }
179 }
180 int main ()
181 {
182   size_t j;
183   FILE *fp = fopen ("conftest.out", "w");
184   if (fp == NULL)
185     return 1;
186   for (j = 0; j < NWORDS; j++)
187     anded_words[j] = ~ (unsigned int) 0;
188   add_to_ored_words (0.25L);
189   add_to_ored_words (0.5L);
190   add_to_ored_words (1.0L);
191   add_to_ored_words (2.0L);
192   add_to_ored_words (4.0L);
193   /* Remove bits that are common (e.g. if representation of the first mantissa
194      bit is explicit).  */
195   for (j = 0; j < NWORDS; j++)
196     ored_words[j] &= ~anded_words[j];
197   /* Now find the nonzero word.  */
198   for (j = 0; j < NWORDS; j++)
199     if (ored_words[j] != 0)
200       break;
201   if (j < NWORDS)
202     {
203       size_t i;
204       for (i = j + 1; i < NWORDS; i++)
205         if (ored_words[i] != 0)
206           {
207             fprintf (fp, "unknown");
208             return (fclose (fp) != 0);
209           }
210       for (i = 0; ; i++)
211         if ((ored_words[j] >> i) & 1)
212           {
213             fprintf (fp, "word %d bit %d", (int) j, (int) i);
214             return (fclose (fp) != 0);
215           }
216     }
217   fprintf (fp, "unknown");
218   return (fclose (fp) != 0);
219 }
220         ],
221         [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
222         [gl_cv_cc_long_double_expbit0="unknown"],
223         [
224           dnl When cross-compiling, we don't know. It depends on the
225           dnl ABI and compiler version. There are too many cases.
226           gl_cv_cc_long_double_expbit0="unknown"
227         ])
228       rm -f conftest.out
229     ])
230   case "$gl_cv_cc_long_double_expbit0" in
231     word*bit*)
232       word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
233       bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
234       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
235         [Define as the word index where to find the exponent of 'long double'.])
236       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
237         [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])
238       ;;
239   esac
240 ])