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.
7 dnl Check how to get or define isnanf() without linking with libm.
9 AC_DEFUN([gl_FUNC_ISNANF_NO_LIBM],
11 gl_HAVE_ISNANF_NO_LIBM
12 if test $gl_cv_func_isnanf_no_libm = yes; then
15 if test $gl_cv_func_isnanf_no_libm = yes \
16 && { case "$gl_cv_func_isnanf_works" in
21 AC_DEFINE([HAVE_ISNANF_IN_LIBC], 1,
22 [Define if the isnan(float) function is available in libc.])
25 gl_FLOAT_EXPONENT_LOCATION
29 dnl Test whether isnanf() can be used without libm.
30 AC_DEFUN([gl_HAVE_ISNANF_NO_LIBM],
32 AC_CACHE_CHECK([whether isnan(float) can be used without linking with libm],
33 [gl_cv_func_isnanf_no_libm],
35 AC_TRY_LINK([#include <math.h>
38 # define isnanf(x) __builtin_isnanf ((float)(x))
41 # define isnanf(x) isnan ((float)(x))
45 [gl_cv_func_isnanf_no_libm=yes],
46 [gl_cv_func_isnanf_no_libm=no])
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],
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],
64 # define isnanf(x) __builtin_isnanf ((float)(x))
67 # define isnanf(x) isnan ((float)(x))
69 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
74 static float zero = 0.0f;
78 # define NaN() (0.0f / 0.0f)
81 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
82 typedef union { unsigned int word[NWORDS]; float value; } memory_float;
87 if (isnanf (1.0f / 0.0f))
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)
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))
109 }], [gl_cv_func_isnanf_works=yes], [gl_cv_func_isnanf_works=no],
111 irix* | solaris*) gl_cv_func_isnanf_works="guessing no";;
112 *) gl_cv_func_isnanf_works="guessing yes";;
118 AC_DEFUN([gl_FLOAT_EXPONENT_LOCATION],
120 AC_CACHE_CHECK([where to find the exponent in a 'float'],
121 [gl_cv_cc_float_expbit0],
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)
137 /* Clear it first, in case
138 sizeof (float) < sizeof (memory_float). */
139 memset (&m, 0, sizeof (memory_float));
141 for (i = 0; i < NWORDS; i++)
143 ored_words[i] |= m.word[i];
144 anded_words[i] &= m.word[i];
150 FILE *fp = fopen ("conftest.out", "w");
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
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)
171 for (i = j + 1; i < NWORDS; i++)
172 if (ored_words[i] != 0)
174 fprintf (fp, "unknown");
175 return (fclose (fp) != 0);
178 if ((ored_words[j] >> i) & 1)
180 fprintf (fp, "word %d bit %d", (int) j, (int) i);
181 return (fclose (fp) != 0);
184 fprintf (fp, "unknown");
185 return (fclose (fp) != 0);
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"])
193 case "$gl_cv_cc_float_expbit0" in
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'.])