cff22369e73f33b3966e92dea153f934dd13d469
[gnulib.git] / m4 / isnanf.m4
1 # isnanf.m4 serial 5
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() without linking with libm.
8
9 AC_DEFUN([gl_FUNC_ISNANF_NO_LIBM],
10 [
11   gl_HAVE_ISNANF_NO_LIBM
12   if test $gl_cv_func_isnanf_no_libm = yes; then
13     gl_ISNANF_WORKS
14   fi
15   if test $gl_cv_func_isnanf_no_libm = yes \
16      && { case "$gl_cv_func_isnanf_works" in
17             *yes) true;;
18             *) false;;
19           esac
20         }; then
21     AC_DEFINE([HAVE_ISNANF_IN_LIBC], 1,
22       [Define if the isnan(float) function is available in libc.])
23   else
24     AC_LIBOBJ([isnanf])
25     gl_FLOAT_EXPONENT_LOCATION
26   fi
27 ])
28
29 dnl Test whether isnanf() can be used without libm.
30 AC_DEFUN([gl_HAVE_ISNANF_NO_LIBM],
31 [
32   AC_CACHE_CHECK([whether isnan(float) can be used without linking with libm],
33     [gl_cv_func_isnanf_no_libm],
34     [
35       AC_TRY_LINK([#include <math.h>
36                    #if __GNUC__ >= 4
37                    # undef isnanf
38                    # define isnanf(x) __builtin_isnanf ((float)(x))
39                    #elif defined isnan
40                    # undef isnanf
41                    # define isnanf(x) isnan ((float)(x))
42                    #endif
43                    float x;],
44                   [return isnanf (x);],
45         [gl_cv_func_isnanf_no_libm=yes],
46         [gl_cv_func_isnanf_no_libm=no])
47     ])
48 ])
49
50 dnl Test whether isnanf() recognizes a NaN (this fails on IRIX 6.5) and rejects
51 dnl Infinity (this fails on Solaris 2.5.1).
52 AC_DEFUN([gl_ISNANF_WORKS],
53 [
54   AC_REQUIRE([AC_PROG_CC])
55   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
56   AC_CACHE_CHECK([whether isnan(float) works], [gl_cv_func_isnanf_works],
57     [
58       AC_TRY_RUN([
59 #include <math.h>
60 #if __GNUC__ >= 4
61 # undef isnanf
62 # define isnanf(x) __builtin_isnanf ((float)(x))
63 #elif defined isnan
64 # undef isnanf
65 # define isnanf(x) isnan ((float)(x))
66 #endif
67 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
68 #ifdef __DECC
69 static float
70 NaN ()
71 {
72   static float zero = 0.0f;
73   return zero / zero;
74 }
75 #else
76 # define NaN() (0.0f / 0.0f)
77 #endif
78 int main()
79 {
80   if (!isnanf (NaN ()))
81     return 1;
82   if (isnanf (1.0f / 0.0f))
83     return 1;
84   return 0;
85 }], [gl_cv_func_isnanf_works=yes], [gl_cv_func_isnanf_works=no],
86         [case "$host_os" in
87            irix* | solaris*) gl_cv_func_isnanf_works="guessing no";;
88            *)                gl_cv_func_isnanf_works="guessing yes";;
89          esac
90         ])
91     ])
92 ])
93
94 AC_DEFUN([gl_FLOAT_EXPONENT_LOCATION],
95 [
96   AC_CACHE_CHECK([where to find the exponent in a 'float'],
97     [gl_cv_cc_float_expbit0],
98     [
99       AC_TRY_RUN([
100 #include <float.h>
101 #include <stddef.h>
102 #include <stdio.h>
103 #include <string.h>
104 #define NWORDS \
105   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
106 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
107 static unsigned int ored_words[NWORDS];
108 static unsigned int anded_words[NWORDS];
109 static void add_to_ored_words (float x)
110 {
111   memory_float m;
112   size_t i;
113   /* Clear it first, in case
114      sizeof (float) < sizeof (memory_float).  */
115   memset (&m, 0, sizeof (memory_float));
116   m.value = x;
117   for (i = 0; i < NWORDS; i++)
118     {
119       ored_words[i] |= m.word[i];
120       anded_words[i] &= m.word[i];
121     }
122 }
123 int main ()
124 {
125   size_t j;
126   FILE *fp = fopen ("conftest.out", "w");
127   if (fp == NULL)
128     return 1;
129   for (j = 0; j < NWORDS; j++)
130     anded_words[j] = ~ (unsigned int) 0;
131   add_to_ored_words (0.25f);
132   add_to_ored_words (0.5f);
133   add_to_ored_words (1.0f);
134   add_to_ored_words (2.0f);
135   add_to_ored_words (4.0f);
136   /* Remove bits that are common (e.g. if representation of the first mantissa
137      bit is explicit).  */
138   for (j = 0; j < NWORDS; j++)
139     ored_words[j] &= ~anded_words[j];
140   /* Now find the nonzero word.  */
141   for (j = 0; j < NWORDS; j++)
142     if (ored_words[j] != 0)
143       break;
144   if (j < NWORDS)
145     {
146       size_t i;
147       for (i = j + 1; i < NWORDS; i++)
148         if (ored_words[i] != 0)
149           {
150             fprintf (fp, "unknown");
151             return (fclose (fp) != 0);
152           }
153       for (i = 0; ; i++)
154         if ((ored_words[j] >> i) & 1)
155           {
156             fprintf (fp, "word %d bit %d", (int) j, (int) i);
157             return (fclose (fp) != 0);
158           }
159     }
160   fprintf (fp, "unknown");
161   return (fclose (fp) != 0);
162 }
163         ],
164         [gl_cv_cc_float_expbit0=`cat conftest.out`],
165         [gl_cv_cc_float_expbit0="unknown"],
166         [gl_cv_cc_float_expbit0="word 0 bit 23"])
167       rm -f conftest.out
168     ])
169   case "$gl_cv_cc_float_expbit0" in
170     word*bit*)
171       word=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
172       bit=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word.*bit //'`
173       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_WORD], [$word],
174         [Define as the word index where to find the exponent of 'float'.])
175       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_BIT], [$bit],
176         [Define as the bit index in the word where to find bit 0 of the exponent of 'float'.])
177       ;;
178   esac
179 ])