Add isnanf module.
[gnulib.git] / m4 / isnanf.m4
1 # isnanf.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 dnl Check how to get or define isnanf().
8
9 AC_DEFUN([gl_FUNC_ISNANF],
10 [
11   ISNANF_LIBM=
12   gl_HAVE_ISNANF_NO_LIBM
13   if test $gl_cv_func_isnanf_no_libm = no; then
14     gl_HAVE_ISNANF_IN_LIBM
15     if test $gl_cv_func_isnanf_in_libm = yes; then
16       ISNANF_LIBM=-lm
17     fi
18   fi
19   if test $gl_cv_func_isnanf_no_libm = yes \
20      || test $gl_cv_func_isnanf_in_libm = yes; then
21     save_LIBS="$LIBS"
22     LIBS="$LIBS $ISNANF_LIBM"
23     gl_ISNANF_WORKS
24     LIBS="$save_LIBS"
25     case "$gl_cv_func_isnanf_works" in
26       *yes) gl_func_isnanf=yes ;;
27       *)    gl_func_isnanf=no; ISNANF_LIBM= ;;
28     esac
29   else
30     gl_func_isnanf=no
31   fi
32   if test $gl_func_isnanf = yes; then
33     AC_DEFINE([HAVE_ISNANF], 1,
34       [Define if the isnan(float) function is available.])
35   else
36     gl_BUILD_ISNANF
37   fi
38   AC_SUBST([ISNANF_LIBM])
39 ])
40
41 dnl Check how to get or define isnanf() without linking with libm.
42
43 AC_DEFUN([gl_FUNC_ISNANF_NO_LIBM],
44 [
45   gl_HAVE_ISNANF_NO_LIBM
46   if test $gl_cv_func_isnanf_no_libm = yes; then
47     gl_ISNANF_WORKS
48   fi
49   if test $gl_cv_func_isnanf_no_libm = yes \
50      && { case "$gl_cv_func_isnanf_works" in
51             *yes) true;;
52             *) false;;
53           esac
54         }; then
55     AC_DEFINE([HAVE_ISNANF_IN_LIBC], 1,
56       [Define if the isnan(float) function is available in libc.])
57   else
58     gl_BUILD_ISNANF
59   fi
60 ])
61
62 dnl Pull in replacement isnanf definition.
63 AC_DEFUN([gl_BUILD_ISNANF],
64 [
65   AC_LIBOBJ([isnanf])
66   gl_FLOAT_EXPONENT_LOCATION
67 ])
68
69 dnl Test whether isnanf() can be used without libm.
70 AC_DEFUN([gl_HAVE_ISNANF_NO_LIBM],
71 [
72   AC_CACHE_CHECK([whether isnan(float) can be used without linking with libm],
73     [gl_cv_func_isnanf_no_libm],
74     [
75       AC_TRY_LINK([#include <math.h>
76                    #if __GNUC__ >= 4
77                    # undef isnanf
78                    # define isnanf(x) __builtin_isnanf ((float)(x))
79                    #elif defined isnan
80                    # undef isnanf
81                    # define isnanf(x) isnan ((float)(x))
82                    #endif
83                    float x;],
84                   [return isnanf (x);],
85         [gl_cv_func_isnanf_no_libm=yes],
86         [gl_cv_func_isnanf_no_libm=no])
87     ])
88 ])
89
90 dnl Test whether isnanf() can be used with libm.
91 AC_DEFUN([gl_HAVE_ISNANF_IN_LIBM],
92 [
93   AC_CACHE_CHECK([whether isnan(float) can be used with libm],
94     [gl_cv_func_isnanf_in_libm],
95     [
96       save_LIBS="$LIBS"
97       LIBS="$LIBS -lm"
98       AC_TRY_LINK([#include <math.h>
99                    #if __GNUC__ >= 4
100                    # undef isnanf
101                    # define isnanf(x) __builtin_isnanf ((float)(x))
102                    #elif defined isnan
103                    # undef isnanf
104                    # define isnanf(x) isnan ((float)(x))
105                    #endif
106                    float x;],
107                   [return isnanf (x);],
108         [gl_cv_func_isnanf_in_libm=yes],
109         [gl_cv_func_isnanf_in_libm=no])
110       LIBS="$save_LIBS"
111     ])
112 ])
113
114 dnl Test whether isnanf() rejects Infinity (this fails on Solaris 2.5.1),
115 dnl recognizes a NaN (this fails on IRIX 6.5 with cc), and recognizes a NaN
116 dnl with in-memory representation 0x7fbfffff (this fails on IRIX 6.5).
117 AC_DEFUN([gl_ISNANF_WORKS],
118 [
119   AC_REQUIRE([AC_PROG_CC])
120   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
121   AC_REQUIRE([gl_FLOAT_EXPONENT_LOCATION])
122   AC_CACHE_CHECK([whether isnan(float) works], [gl_cv_func_isnanf_works],
123     [
124       AC_TRY_RUN([
125 #include <math.h>
126 #if __GNUC__ >= 4
127 # undef isnanf
128 # define isnanf(x) __builtin_isnanf ((float)(x))
129 #elif defined isnan
130 # undef isnanf
131 # define isnanf(x) isnan ((float)(x))
132 #endif
133 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
134 #ifdef __DECC
135 static float
136 NaN ()
137 {
138   static float zero = 0.0f;
139   return zero / zero;
140 }
141 #else
142 # define NaN() (0.0f / 0.0f)
143 #endif
144 #define NWORDS \
145   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
146 typedef union { unsigned int word[NWORDS]; float value; } memory_float;
147 int main()
148 {
149   memory_float m;
150
151   if (isnanf (1.0f / 0.0f))
152     return 1;
153
154   if (!isnanf (NaN ()))
155     return 1;
156
157 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
158   /* The isnanf function should be immune against changes in the sign bit and
159      in the mantissa bits.  The xor operation twiddles a bit that can only be
160      a sign bit or a mantissa bit.  */
161   if (FLT_EXPBIT0_WORD == 0 && FLT_EXPBIT0_BIT > 0)
162     {
163       m.value = NaN ();
164       /* Set the bits below the exponent to 01111...111.  */
165       m.word[0] &= -1U << FLT_EXPBIT0_BIT;
166       m.word[0] |= 1U << (FLT_EXPBIT0_BIT - 1) - 1;
167       if (!isnanf (m.value))
168         return 1;
169     }
170 #endif
171
172   return 0;
173 }], [gl_cv_func_isnanf_works=yes], [gl_cv_func_isnanf_works=no],
174         [case "$host_os" in
175            irix* | solaris*) gl_cv_func_isnanf_works="guessing no";;
176            *)                gl_cv_func_isnanf_works="guessing yes";;
177          esac
178         ])
179     ])
180 ])
181
182 AC_DEFUN([gl_FLOAT_EXPONENT_LOCATION],
183 [
184   AC_CACHE_CHECK([where to find the exponent in a 'float'],
185     [gl_cv_cc_float_expbit0],
186     [
187       AC_TRY_RUN([
188 #include <float.h>
189 #include <stddef.h>
190 #include <stdio.h>
191 #include <string.h>
192 #define NWORDS \
193   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
194 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
195 static unsigned int ored_words[NWORDS];
196 static unsigned int anded_words[NWORDS];
197 static void add_to_ored_words (float x)
198 {
199   memory_float m;
200   size_t i;
201   /* Clear it first, in case
202      sizeof (float) < sizeof (memory_float).  */
203   memset (&m, 0, sizeof (memory_float));
204   m.value = x;
205   for (i = 0; i < NWORDS; i++)
206     {
207       ored_words[i] |= m.word[i];
208       anded_words[i] &= m.word[i];
209     }
210 }
211 int main ()
212 {
213   size_t j;
214   FILE *fp = fopen ("conftest.out", "w");
215   if (fp == NULL)
216     return 1;
217   for (j = 0; j < NWORDS; j++)
218     anded_words[j] = ~ (unsigned int) 0;
219   add_to_ored_words (0.25f);
220   add_to_ored_words (0.5f);
221   add_to_ored_words (1.0f);
222   add_to_ored_words (2.0f);
223   add_to_ored_words (4.0f);
224   /* Remove bits that are common (e.g. if representation of the first mantissa
225      bit is explicit).  */
226   for (j = 0; j < NWORDS; j++)
227     ored_words[j] &= ~anded_words[j];
228   /* Now find the nonzero word.  */
229   for (j = 0; j < NWORDS; j++)
230     if (ored_words[j] != 0)
231       break;
232   if (j < NWORDS)
233     {
234       size_t i;
235       for (i = j + 1; i < NWORDS; i++)
236         if (ored_words[i] != 0)
237           {
238             fprintf (fp, "unknown");
239             return (fclose (fp) != 0);
240           }
241       for (i = 0; ; i++)
242         if ((ored_words[j] >> i) & 1)
243           {
244             fprintf (fp, "word %d bit %d", (int) j, (int) i);
245             return (fclose (fp) != 0);
246           }
247     }
248   fprintf (fp, "unknown");
249   return (fclose (fp) != 0);
250 }
251         ],
252         [gl_cv_cc_float_expbit0=`cat conftest.out`],
253         [gl_cv_cc_float_expbit0="unknown"],
254         [gl_cv_cc_float_expbit0="word 0 bit 23"])
255       rm -f conftest.out
256     ])
257   case "$gl_cv_cc_float_expbit0" in
258     word*bit*)
259       word=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
260       bit=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word.*bit //'`
261       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_WORD], [$word],
262         [Define as the word index where to find the exponent of 'float'.])
263       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_BIT], [$bit],
264         [Define as the bit index in the word where to find bit 0 of the exponent of 'float'.])
265       ;;
266   esac
267 ])