Override the system isnanf also on IRIX 6.5 with gcc.
[gnulib.git] / m4 / isnanf.m4
1 # isnanf.m4 serial 6
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() rejects Infinity (this fails on Solaris 2.5.1),
51 dnl recognizes a NaN (this fails on IRIX 6.5 with cc), and recognizes a NaN
52 dnl with in-memory representation 0x7fbfffff (this fails on IRIX 6.5).
53 AC_DEFUN([gl_ISNANF_WORKS],
54 [
55   AC_REQUIRE([AC_PROG_CC])
56   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
57   AC_REQUIRE([gl_FLOAT_EXPONENT_LOCATION])
58   AC_CACHE_CHECK([whether isnan(float) works], [gl_cv_func_isnanf_works],
59     [
60       AC_TRY_RUN([
61 #include <math.h>
62 #if __GNUC__ >= 4
63 # undef isnanf
64 # define isnanf(x) __builtin_isnanf ((float)(x))
65 #elif defined isnan
66 # undef isnanf
67 # define isnanf(x) isnan ((float)(x))
68 #endif
69 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
70 #ifdef __DECC
71 static float
72 NaN ()
73 {
74   static float zero = 0.0f;
75   return zero / zero;
76 }
77 #else
78 # define NaN() (0.0f / 0.0f)
79 #endif
80 #define NWORDS \
81   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
82 typedef union { unsigned int word[NWORDS]; float value; } memory_float;
83 int main()
84 {
85   memory_float m;
86
87   if (isnanf (1.0f / 0.0f))
88     return 1;
89
90   if (!isnanf (NaN ()))
91     return 1;
92
93 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
94   /* The isnanf function should be immune against changes in the sign bit and
95      in the mantissa bits.  The xor operation twiddles a bit that can only be
96      a sign bit or a mantissa bit.  */
97   if (FLT_EXPBIT0_WORD == 0 && FLT_EXPBIT0_BIT > 0)
98     {
99       m.value = NaN ();
100       /* Set the bits below the exponent to 01111...111.  */
101       m.word[0] &= -1U << FLT_EXPBIT0_BIT;
102       m.word[0] |= 1U << (FLT_EXPBIT0_BIT - 1) - 1;
103       if (!isnanf (m.value))
104         return 1;
105     }
106 #endif
107
108   return 0;
109 }], [gl_cv_func_isnanf_works=yes], [gl_cv_func_isnanf_works=no],
110         [case "$host_os" in
111            irix* | solaris*) gl_cv_func_isnanf_works="guessing no";;
112            *)                gl_cv_func_isnanf_works="guessing yes";;
113          esac
114         ])
115     ])
116 ])
117
118 AC_DEFUN([gl_FLOAT_EXPONENT_LOCATION],
119 [
120   AC_CACHE_CHECK([where to find the exponent in a 'float'],
121     [gl_cv_cc_float_expbit0],
122     [
123       AC_TRY_RUN([
124 #include <float.h>
125 #include <stddef.h>
126 #include <stdio.h>
127 #include <string.h>
128 #define NWORDS \
129   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
130 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
131 static unsigned int ored_words[NWORDS];
132 static unsigned int anded_words[NWORDS];
133 static void add_to_ored_words (float x)
134 {
135   memory_float m;
136   size_t i;
137   /* Clear it first, in case
138      sizeof (float) < sizeof (memory_float).  */
139   memset (&m, 0, sizeof (memory_float));
140   m.value = x;
141   for (i = 0; i < NWORDS; i++)
142     {
143       ored_words[i] |= m.word[i];
144       anded_words[i] &= m.word[i];
145     }
146 }
147 int main ()
148 {
149   size_t j;
150   FILE *fp = fopen ("conftest.out", "w");
151   if (fp == NULL)
152     return 1;
153   for (j = 0; j < NWORDS; j++)
154     anded_words[j] = ~ (unsigned int) 0;
155   add_to_ored_words (0.25f);
156   add_to_ored_words (0.5f);
157   add_to_ored_words (1.0f);
158   add_to_ored_words (2.0f);
159   add_to_ored_words (4.0f);
160   /* Remove bits that are common (e.g. if representation of the first mantissa
161      bit is explicit).  */
162   for (j = 0; j < NWORDS; j++)
163     ored_words[j] &= ~anded_words[j];
164   /* Now find the nonzero word.  */
165   for (j = 0; j < NWORDS; j++)
166     if (ored_words[j] != 0)
167       break;
168   if (j < NWORDS)
169     {
170       size_t i;
171       for (i = j + 1; i < NWORDS; i++)
172         if (ored_words[i] != 0)
173           {
174             fprintf (fp, "unknown");
175             return (fclose (fp) != 0);
176           }
177       for (i = 0; ; i++)
178         if ((ored_words[j] >> i) & 1)
179           {
180             fprintf (fp, "word %d bit %d", (int) j, (int) i);
181             return (fclose (fp) != 0);
182           }
183     }
184   fprintf (fp, "unknown");
185   return (fclose (fp) != 0);
186 }
187         ],
188         [gl_cv_cc_float_expbit0=`cat conftest.out`],
189         [gl_cv_cc_float_expbit0="unknown"],
190         [gl_cv_cc_float_expbit0="word 0 bit 23"])
191       rm -f conftest.out
192     ])
193   case "$gl_cv_cc_float_expbit0" in
194     word*bit*)
195       word=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
196       bit=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word.*bit //'`
197       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_WORD], [$word],
198         [Define as the word index where to find the exponent of 'float'.])
199       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_BIT], [$bit],
200         [Define as the bit index in the word where to find bit 0 of the exponent of 'float'.])
201       ;;
202   esac
203 ])