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],
10 gl_HAVE_ISNANL_NO_LIBM
11 if test $gl_cv_func_isnanl_no_libm = no; then
12 gl_HAVE_ISNANL_IN_LIBM
13 if test $gl_cv_func_isnanl_in_libm = yes; then
17 if test $gl_cv_func_isnanl_no_libm = yes \
18 || test $gl_cv_func_isnanl_in_libm = yes; then
20 LIBS="$LIBS $ISNANL_LIBM"
23 case "$gl_cv_func_isnanl_works" in
24 *yes) gl_func_isnanl=yes ;;
25 *) gl_func_isnanl=no; ISNANL_LIBM= ;;
30 if test $gl_func_isnanl = yes; then
31 AC_DEFINE([HAVE_ISNANL], 1,
32 [Define if the isnan(long double) function is available.])
35 gl_LONG_DOUBLE_EXPONENT_LOCATION
37 AC_SUBST([ISNANL_LIBM])
40 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
42 gl_HAVE_ISNANL_NO_LIBM
43 gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
44 if test $gl_func_isnanl_no_libm = yes; then
46 case "$gl_cv_func_isnanl_works" in
48 *) gl_func_isnanl_no_libm=no ;;
51 if test $gl_func_isnanl_no_libm = yes; then
52 AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
53 [Define if the isnan(long double) function is available in libc.])
56 gl_LONG_DOUBLE_EXPONENT_LOCATION
60 dnl Test whether isnanl() can be used without libm.
61 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
63 AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
64 [gl_cv_func_isnanl_no_libm],
66 AC_TRY_LINK([#include <math.h>
69 # define isnanl(x) isnan ((long double)(x))
73 [gl_cv_func_isnanl_no_libm=yes],
74 [gl_cv_func_isnanl_no_libm=no])
78 dnl Test whether isnanl() can be used with libm.
79 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
81 AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
82 [gl_cv_func_isnanl_in_libm],
86 AC_TRY_LINK([#include <math.h>
89 # define isnanl(x) isnan ((long double)(x))
93 [gl_cv_func_isnanl_in_libm=yes],
94 [gl_cv_func_isnanl_in_libm=no])
99 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
100 dnl infinite. This test fails e.g. on NetBSD/i386.
101 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
103 AC_REQUIRE([AC_PROG_CC])
104 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
105 AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
112 # define isnanl(x) isnan ((long double)(x))
115 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
116 typedef union { long double value; unsigned int word[NWORDS]; }
120 memory_long_double m;
123 /* The isnanl function should be immune against changes in the sign bit and
124 in the mantissa bits. The xor operation twiddles a bit that can only be
125 a sign bit or a mantissa bit (since the exponent never extends to
127 m.value = 0.0L / 0.0L;
128 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
129 for (i = 0; i < NWORDS; i++)
131 if (!isnanl (m.value))
135 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
137 netbsd*) gl_cv_func_isnanl_works="guessing no";;
138 *) gl_cv_func_isnanl_works="guessing yes";;
144 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
146 AC_CACHE_CHECK([where to find the exponent in a 'long double'],
147 [gl_cv_cc_long_double_expbit0],
155 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
156 typedef union { long double value; unsigned int word[NWORDS]; }
158 static unsigned int ored_words[NWORDS];
159 static unsigned int anded_words[NWORDS];
160 static void add_to_ored_words (long double x)
162 memory_long_double m;
164 /* Clear it first, in case
165 sizeof (long double) < sizeof (memory_long_double). */
166 memset (&m, 0, sizeof (memory_long_double));
168 for (i = 0; i < NWORDS; i++)
170 ored_words[i] |= m.word[i];
171 anded_words[i] &= m.word[i];
177 FILE *fp = fopen ("conftest.out", "w");
180 for (j = 0; j < NWORDS; j++)
181 anded_words[j] = ~ (unsigned int) 0;
182 add_to_ored_words (0.25L);
183 add_to_ored_words (0.5L);
184 add_to_ored_words (1.0L);
185 add_to_ored_words (2.0L);
186 add_to_ored_words (4.0L);
187 /* Remove bits that are common (e.g. if representation of the first mantissa
189 for (j = 0; j < NWORDS; j++)
190 ored_words[j] &= ~anded_words[j];
191 /* Now find the nonzero word. */
192 for (j = 0; j < NWORDS; j++)
193 if (ored_words[j] != 0)
198 for (i = j + 1; i < NWORDS; i++)
199 if (ored_words[i] != 0)
201 fprintf (fp, "unknown");
202 return (fclose (fp) != 0);
205 if ((ored_words[j] >> i) & 1)
207 fprintf (fp, "word %d bit %d", (int) j, (int) i);
208 return (fclose (fp) != 0);
211 fprintf (fp, "unknown");
212 return (fclose (fp) != 0);
215 [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
216 [gl_cv_cc_long_double_expbit0="unknown"],
218 dnl When cross-compiling, we don't know. It depends on the
219 dnl ABI and compiler version. There are too many cases.
220 gl_cv_cc_long_double_expbit0="unknown"
224 case "$gl_cv_cc_long_double_expbit0" in
226 word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
227 bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
228 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
229 [Define as the word index where to find the exponent of 'long double'.])
230 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
231 [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])