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.
7 AC_DEFUN([gl_FUNC_ISNANL],
9 AC_REQUIRE([gt_TYPE_LONGDOUBLE])
11 if test $gt_cv_c_long_double = yes; then
12 gl_HAVE_ISNANL_NO_LIBM
13 if test $gl_cv_func_isnanl_no_libm = no; then
14 gl_HAVE_ISNANL_IN_LIBM
15 if test $gl_cv_func_isnanl_in_libm = yes; then
19 if test $gl_cv_func_isnanl_no_libm = yes \
20 || test $gl_cv_func_isnanl_in_libm = yes; then
22 LIBS="$LIBS $ISNANL_LIBM"
25 case "$gl_cv_func_isnanl_works" in
26 *yes) gl_func_isnanl=yes ;;
27 *) gl_func_isnanl=no; ISNANL_LIBM= ;;
32 if test $gl_func_isnanl = yes; then
33 AC_DEFINE([HAVE_ISNANL], 1,
34 [Define if the isnan(long double) function is available.])
37 gl_LONG_DOUBLE_EXPONENT_LOCATION
42 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
44 AC_REQUIRE([gt_TYPE_LONGDOUBLE])
45 if test $gt_cv_c_long_double = yes; then
46 gl_HAVE_ISNANL_NO_LIBM
47 gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
48 if test $gl_func_isnanl_no_libm = yes; then
50 case "$gl_cv_func_isnanl_works" in
52 *) gl_func_isnanl_no_libm=no ;;
55 if test $gl_func_isnanl_no_libm = yes; then
56 AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
57 [Define if the isnan(long double) function is available in libc.])
60 gl_LONG_DOUBLE_EXPONENT_LOCATION
65 dnl Test whether isnanl() can be used without libm.
66 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
68 AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
69 [gl_cv_func_isnanl_no_libm],
71 AC_TRY_LINK([#include <math.h>
74 # define isnanl(x) isnan ((long double)(x))
78 [gl_cv_func_isnanl_no_libm=yes],
79 [gl_cv_func_isnanl_no_libm=no])
83 dnl Test whether isnanl() can be used with libm.
84 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
86 AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
87 [gl_cv_func_isnanl_in_libm],
91 AC_TRY_LINK([#include <math.h>
94 # define isnanl(x) isnan ((long double)(x))
98 [gl_cv_func_isnanl_in_libm=yes],
99 [gl_cv_func_isnanl_in_libm=no])
104 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
105 dnl infinite. This test fails e.g. on NetBSD/i386.
106 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
108 AC_REQUIRE([AC_PROG_CC])
109 AC_REQUIRE([gt_TYPE_LONGDOUBLE])
110 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
111 AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
118 # define isnanl(x) isnan ((long double)(x))
121 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
122 typedef union { long double value; unsigned int word[NWORDS]; }
126 memory_long_double m;
129 /* The isnanl function should be immune against changes in the sign bit and
130 in the mantissa bits. The xor operation twiddles a bit that can only be
131 a sign bit or a mantissa bit (since the exponent never extends to
133 m.value = 0.0L / 0.0L;
134 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
135 for (i = 0; i < NWORDS; i++)
137 if (!isnanl (m.value))
141 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
143 netbsd*) gl_cv_func_isnanl_works="guessing no";;
144 *) gl_cv_func_isnanl_works="guessing yes";;
150 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
152 AC_CACHE_CHECK([where to find the exponent in a 'long double'],
153 [gl_cv_cc_long_double_expbit0],
161 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
162 typedef union { long double value; unsigned int word[NWORDS]; }
164 static unsigned int ored_words[NWORDS];
165 static unsigned int anded_words[NWORDS];
166 static void add_to_ored_words (long double x)
168 memory_long_double m;
170 /* Clear it first, in case
171 sizeof (long double) < sizeof (memory_long_double). */
172 memset (&m, 0, sizeof (memory_long_double));
174 for (i = 0; i < NWORDS; i++)
176 ored_words[i] |= m.word[i];
177 anded_words[i] &= m.word[i];
183 FILE *fp = fopen ("conftest.out", "w");
186 for (j = 0; j < NWORDS; j++)
187 anded_words[j] = ~ (unsigned int) 0;
188 add_to_ored_words (0.25L);
189 add_to_ored_words (0.5L);
190 add_to_ored_words (1.0L);
191 add_to_ored_words (2.0L);
192 add_to_ored_words (4.0L);
193 /* Remove bits that are common (e.g. if representation of the first mantissa
195 for (j = 0; j < NWORDS; j++)
196 ored_words[j] &= ~anded_words[j];
197 /* Now find the nonzero word. */
198 for (j = 0; j < NWORDS; j++)
199 if (ored_words[j] != 0)
204 for (i = j + 1; i < NWORDS; i++)
205 if (ored_words[i] != 0)
207 fprintf (fp, "unknown");
208 return (fclose (fp) != 0);
211 if ((ored_words[j] >> i) & 1)
213 fprintf (fp, "word %d bit %d", (int) j, (int) i);
214 return (fclose (fp) != 0);
217 fprintf (fp, "unknown");
218 return (fclose (fp) != 0);
221 [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
222 [gl_cv_cc_long_double_expbit0="unknown"],
224 dnl When cross-compiling, we don't know. It depends on the
225 dnl ABI and compiler version. There are too many cases.
226 gl_cv_cc_long_double_expbit0="unknown"
230 case "$gl_cv_cc_long_double_expbit0" in
232 word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
233 bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
234 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
235 [Define as the word index where to find the exponent of 'long double'.])
236 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
237 [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])