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 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) __builtin_isnanl ((long double)(x))
72 # define isnanl(x) isnan ((long double)(x))
76 [gl_cv_func_isnanl_no_libm=yes],
77 [gl_cv_func_isnanl_no_libm=no])
81 dnl Test whether isnanl() can be used with libm.
82 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
84 AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
85 [gl_cv_func_isnanl_in_libm],
89 AC_TRY_LINK([#include <math.h>
92 # define isnanl(x) __builtin_isnanl ((long double)(x))
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 and on glibc/ia64.
107 dnl Also, the GCC >= 4.0 built-in __builtin_isnanl does not pass the tests
108 dnl - for pseudo-denormals on i686 and x86_64,
109 dnl - for pseudo-zeroes, unnormalized numbers, and pseudo-denormals on ia64.
110 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
112 AC_REQUIRE([AC_PROG_CC])
113 AC_REQUIRE([AC_C_BIGENDIAN])
114 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
115 AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
123 # define isnanl(x) __builtin_isnanl ((long double)(x))
126 # define isnanl(x) isnan ((long double)(x))
129 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
130 typedef union { unsigned int word[NWORDS]; long double value; }
134 memory_long_double m;
137 /* The isnanl function should be immune against changes in the sign bit and
138 in the mantissa bits. The xor operation twiddles a bit that can only be
139 a sign bit or a mantissa bit (since the exponent never extends to
141 m.value = 0.0L / 0.0L;
142 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
143 for (i = 0; i < NWORDS; i++)
145 if (!isnanl (m.value))
148 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
149 /* Representation of an 80-bit 'long double' as an initializer for a sequence
150 of 'unsigned int' words. */
151 # ifdef WORDS_BIGENDIAN
152 # define LDBL80_WORDS(exponent,manthi,mantlo) \
153 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
154 ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
155 (unsigned int) (mantlo) << 16 \
158 # define LDBL80_WORDS(exponent,manthi,mantlo) \
159 { mantlo, manthi, exponent }
162 static memory_long_double x =
163 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
164 if (!isnanl (x.value))
168 /* Signalling NaN. */
169 static memory_long_double x =
170 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
171 if (!isnanl (x.value))
174 /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
175 Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
176 Intel IA-64 Architecture Software Developer's Manual, Volume 1:
177 Application Architecture.
178 Table 5-2 "Floating-Point Register Encodings"
179 Figure 5-6 "Memory to Floating-Point Register Data Translation"
182 static memory_long_double x =
183 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
184 if (!isnanl (x.value))
187 { /* Pseudo-Infinity. */
188 static memory_long_double x =
189 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
190 if (!isnanl (x.value))
194 static memory_long_double x =
195 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
196 if (!isnanl (x.value))
199 { /* Unnormalized number. */
200 static memory_long_double x =
201 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
202 if (!isnanl (x.value))
205 { /* Pseudo-Denormal. */
206 static memory_long_double x =
207 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
208 if (!isnanl (x.value))
214 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
216 # Guess no on ia64, x86_64, i386.
217 ia64 | x86_64 | i*86) gl_cv_func_isnanl_works="guessing no";;
220 netbsd*) gl_cv_func_isnanl_works="guessing no";;
221 *) gl_cv_func_isnanl_works="guessing yes";;
229 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
231 AC_REQUIRE([AC_C_BIGENDIAN])
232 AC_CACHE_CHECK([where to find the exponent in a 'long double'],
233 [gl_cv_cc_long_double_expbit0],
241 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
242 typedef union { long double value; unsigned int word[NWORDS]; }
244 static unsigned int ored_words[NWORDS];
245 static unsigned int anded_words[NWORDS];
246 static void add_to_ored_words (long double x)
248 memory_long_double m;
250 /* Clear it first, in case
251 sizeof (long double) < sizeof (memory_long_double). */
252 memset (&m, 0, sizeof (memory_long_double));
254 for (i = 0; i < NWORDS; i++)
256 ored_words[i] |= m.word[i];
257 anded_words[i] &= m.word[i];
263 FILE *fp = fopen ("conftest.out", "w");
266 for (j = 0; j < NWORDS; j++)
267 anded_words[j] = ~ (unsigned int) 0;
268 add_to_ored_words (0.25L);
269 add_to_ored_words (0.5L);
270 add_to_ored_words (1.0L);
271 add_to_ored_words (2.0L);
272 add_to_ored_words (4.0L);
273 /* Remove bits that are common (e.g. if representation of the first mantissa
275 for (j = 0; j < NWORDS; j++)
276 ored_words[j] &= ~anded_words[j];
277 /* Now find the nonzero word. */
278 for (j = 0; j < NWORDS; j++)
279 if (ored_words[j] != 0)
284 for (i = j + 1; i < NWORDS; i++)
285 if (ored_words[i] != 0)
287 fprintf (fp, "unknown");
288 return (fclose (fp) != 0);
291 if ((ored_words[j] >> i) & 1)
293 fprintf (fp, "word %d bit %d", (int) j, (int) i);
294 return (fclose (fp) != 0);
297 fprintf (fp, "unknown");
298 return (fclose (fp) != 0);
301 [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
302 [gl_cv_cc_long_double_expbit0="unknown"],
304 dnl When cross-compiling, we don't know. It depends on the
305 dnl ABI and compiler version. There are too many cases.
306 gl_cv_cc_long_double_expbit0="unknown"
310 case "$gl_cv_cc_long_double_expbit0" in
312 word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
313 bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
314 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
315 [Define as the word index where to find the exponent of 'long double'.])
316 AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
317 [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])