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
40 AC_SUBST([ISNANL_LIBM])
43 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
45 AC_REQUIRE([gt_TYPE_LONGDOUBLE])
46 if test $gt_cv_c_long_double = yes; then
47 gl_HAVE_ISNANL_NO_LIBM
48 gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
49 if test $gl_func_isnanl_no_libm = yes; then
51 case "$gl_cv_func_isnanl_works" in
53 *) gl_func_isnanl_no_libm=no ;;
56 if test $gl_func_isnanl_no_libm = yes; then
57 AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
58 [Define if the isnan(long double) function is available in libc.])
61 gl_LONG_DOUBLE_EXPONENT_LOCATION
66 dnl Test whether isnanl() can be used without libm.
67 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
69 AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
70 [gl_cv_func_isnanl_no_libm],
72 AC_TRY_LINK([#include <math.h>
75 # define isnanl(x) isnan ((long double)(x))
79 [gl_cv_func_isnanl_no_libm=yes],
80 [gl_cv_func_isnanl_no_libm=no])
84 dnl Test whether isnanl() can be used with libm.
85 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
87 AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
88 [gl_cv_func_isnanl_in_libm],
92 AC_TRY_LINK([#include <math.h>
95 # define isnanl(x) isnan ((long double)(x))
99 [gl_cv_func_isnanl_in_libm=yes],
100 [gl_cv_func_isnanl_in_libm=no])
105 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
106 dnl infinite. This test fails e.g. on NetBSD/i386.
107 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
109 AC_REQUIRE([AC_PROG_CC])
110 AC_REQUIRE([gt_TYPE_LONGDOUBLE])
111 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
112 AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
119 # define isnanl(x) isnan ((long double)(x))
122 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
123 typedef union { long double value; unsigned int word[NWORDS]; }
127 memory_long_double m;
130 /* The isnanl function should be immune against changes in the sign bit and
131 in the mantissa bits. The xor operation twiddles a bit that can only be
132 a sign bit or a mantissa bit (since the exponent never extends to
134 m.value = 0.0L / 0.0L;
135 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
136 for (i = 0; i < NWORDS; i++)
138 if (!isnanl (m.value))
142 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
144 netbsd*) gl_cv_func_isnanl_works="guessing no";;
145 *) gl_cv_func_isnanl_works="guessing yes";;
151 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
153 AC_CACHE_CHECK([where to find the exponent in a 'long double'],
154 [gl_cv_cc_long_double_expbit0],
162 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
163 typedef union { long double value; unsigned int word[NWORDS]; }
165 static unsigned int ored_words[NWORDS];
166 static unsigned int anded_words[NWORDS];
167 static void add_to_ored_words (long double x)
169 memory_long_double m;
171 /* Clear it first, in case
172 sizeof (long double) < sizeof (memory_long_double). */
173 memset (&m, 0, sizeof (memory_long_double));
175 for (i = 0; i < NWORDS; i++)
177 ored_words[i] |= m.word[i];
178 anded_words[i] &= m.word[i];
184 FILE *fp = fopen ("conftest.out", "w");
187 for (j = 0; j < NWORDS; j++)
188 anded_words[j] = ~ (unsigned int) 0;
189 add_to_ored_words (0.25L);
190 add_to_ored_words (0.5L);
191 add_to_ored_words (1.0L);
192 add_to_ored_words (2.0L);
193 add_to_ored_words (4.0L);
194 /* Remove bits that are common (e.g. if representation of the first mantissa
196 for (j = 0; j < NWORDS; j++)
197 ored_words[j] &= ~anded_words[j];
198 /* Now find the nonzero word. */
199 for (j = 0; j < NWORDS; j++)
200 if (ored_words[j] != 0)
205 for (i = j + 1; i < NWORDS; i++)
206 if (ored_words[i] != 0)
208 fprintf (fp, "unknown");
209 return (fclose (fp) != 0);
212 if ((ored_words[j] >> i) & 1)
214 fprintf (fp, "word %d bit %d", (int) j, (int) i);
215 return (fclose (fp) != 0);
218 fprintf (fp, "unknown");
219 return (fclose (fp) != 0);
222 [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
223 [gl_cv_cc_long_double_expbit0="unknown"],
225 dnl When cross-compiling, we don't know. It depends on the
226 dnl ABI and compiler version. There are too many cases.
227 gl_cv_cc_long_double_expbit0="unknown"
231 case "$gl_cv_cc_long_double_expbit0" in
233 word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
234 bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
235 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
236 [Define as the word index where to find the exponent of 'long double'.])
237 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
238 [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])