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