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