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