Update Gnulib; add new modules.
[bpt/guile.git] / m4 / isnanl.m4
1 # isnanl.m4 serial 14
2 dnl Copyright (C) 2007-2011 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.
6
7 AC_DEFUN([gl_FUNC_ISNANL],
8 [
9 AC_REQUIRE([gl_MATH_H_DEFAULTS])
10 ISNANL_LIBM=
11 gl_HAVE_ISNANL_NO_LIBM
12 if test $gl_cv_func_isnanl_no_libm = no; then
13 gl_HAVE_ISNANL_IN_LIBM
14 if test $gl_cv_func_isnanl_in_libm = yes; then
15 ISNANL_LIBM=-lm
16 fi
17 fi
18 if test $gl_cv_func_isnanl_no_libm = yes \
19 || test $gl_cv_func_isnanl_in_libm = yes; then
20 save_LIBS="$LIBS"
21 LIBS="$LIBS $ISNANL_LIBM"
22 gl_FUNC_ISNANL_WORKS
23 LIBS="$save_LIBS"
24 case "$gl_cv_func_isnanl_works" in
25 *yes) gl_func_isnanl=yes ;;
26 *) gl_func_isnanl=no; ISNANL_LIBM= ;;
27 esac
28 else
29 gl_func_isnanl=no
30 fi
31 if test $gl_func_isnanl != yes; then
32 HAVE_ISNANL=0
33 gl_BUILD_ISNANL
34 fi
35 AC_SUBST([ISNANL_LIBM])
36 ])
37
38 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
39 [
40 gl_HAVE_ISNANL_NO_LIBM
41 gl_func_isnanl_no_libm=$gl_cv_func_isnanl_no_libm
42 if test $gl_func_isnanl_no_libm = yes; then
43 gl_FUNC_ISNANL_WORKS
44 case "$gl_cv_func_isnanl_works" in
45 *yes) ;;
46 *) gl_func_isnanl_no_libm=no ;;
47 esac
48 fi
49 if test $gl_func_isnanl_no_libm = yes; then
50 AC_DEFINE([HAVE_ISNANL_IN_LIBC], [1],
51 [Define if the isnan(long double) function is available in libc.])
52 else
53 gl_BUILD_ISNANL
54 fi
55 ])
56
57 dnl Pull in replacement isnanl definition. It does not need -lm.
58 AC_DEFUN([gl_BUILD_ISNANL],
59 [
60 AC_LIBOBJ([isnanl])
61 gl_LONG_DOUBLE_EXPONENT_LOCATION
62 ])
63
64 dnl Test whether isnanl() can be used without libm.
65 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
66 [
67 AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
68 [gl_cv_func_isnanl_no_libm],
69 [
70 AC_LINK_IFELSE(
71 [AC_LANG_PROGRAM(
72 [[#include <math.h>
73 #if __GNUC__ >= 4
74 # undef isnanl
75 # define isnanl(x) __builtin_isnanl ((long double)(x))
76 #elif defined isnan
77 # undef isnanl
78 # define isnanl(x) isnan ((long double)(x))
79 #endif
80 long double x;]],
81 [[return isnanl (x);]])],
82 [gl_cv_func_isnanl_no_libm=yes],
83 [gl_cv_func_isnanl_no_libm=no])
84 ])
85 ])
86
87 dnl Test whether isnanl() can be used with libm.
88 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
89 [
90 AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
91 [gl_cv_func_isnanl_in_libm],
92 [
93 save_LIBS="$LIBS"
94 LIBS="$LIBS -lm"
95 AC_LINK_IFELSE(
96 [AC_LANG_PROGRAM(
97 [[#include <math.h>
98 #if __GNUC__ >= 4
99 # undef isnanl
100 # define isnanl(x) __builtin_isnanl ((long double)(x))
101 #elif defined isnan
102 # undef isnanl
103 # define isnanl(x) isnan ((long double)(x))
104 #endif
105 long double x;]],
106 [[return isnanl (x);]])],
107 [gl_cv_func_isnanl_in_libm=yes],
108 [gl_cv_func_isnanl_in_libm=no])
109 LIBS="$save_LIBS"
110 ])
111 ])
112
113 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
114 dnl infinite. This test fails e.g. on NetBSD/i386 and on glibc/ia64.
115 dnl Also, the GCC >= 4.0 built-in __builtin_isnanl does not pass the tests
116 dnl - for pseudo-denormals on i686 and x86_64,
117 dnl - for pseudo-zeroes, unnormalized numbers, and pseudo-denormals on ia64.
118 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
119 [
120 AC_REQUIRE([AC_PROG_CC])
121 AC_REQUIRE([gl_BIGENDIAN])
122 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
123 AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
124 [
125 AC_RUN_IFELSE(
126 [AC_LANG_SOURCE([[
127 #include <float.h>
128 #include <limits.h>
129 #include <math.h>
130 #if __GNUC__ >= 4
131 # undef isnanl
132 # define isnanl(x) __builtin_isnanl ((long double)(x))
133 #elif defined isnan
134 # undef isnanl
135 # define isnanl(x) isnan ((long double)(x))
136 #endif
137 #define NWORDS \
138 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
139 typedef union { unsigned int word[NWORDS]; long double value; }
140 memory_long_double;
141 /* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
142 runtime type conversion. */
143 #ifdef __sgi
144 static long double NaNl ()
145 {
146 double zero = 0.0;
147 return zero / zero;
148 }
149 #else
150 # define NaNl() (0.0L / 0.0L)
151 #endif
152 int main ()
153 {
154 int result = 0;
155
156 if (!isnanl (NaNl ()))
157 result |= 1;
158
159 {
160 memory_long_double m;
161 unsigned int i;
162
163 /* The isnanl function should be immune against changes in the sign bit and
164 in the mantissa bits. The xor operation twiddles a bit that can only be
165 a sign bit or a mantissa bit (since the exponent never extends to
166 bit 31). */
167 m.value = NaNl ();
168 m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
169 for (i = 0; i < NWORDS; i++)
170 m.word[i] |= 1;
171 if (!isnanl (m.value))
172 result |= 1;
173 }
174
175 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
176 /* Representation of an 80-bit 'long double' as an initializer for a sequence
177 of 'unsigned int' words. */
178 # ifdef WORDS_BIGENDIAN
179 # define LDBL80_WORDS(exponent,manthi,mantlo) \
180 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
181 ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \
182 (unsigned int) (mantlo) << 16 \
183 }
184 # else
185 # define LDBL80_WORDS(exponent,manthi,mantlo) \
186 { mantlo, manthi, exponent }
187 # endif
188 { /* Quiet NaN. */
189 static memory_long_double x =
190 { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
191 if (!isnanl (x.value))
192 result |= 2;
193 }
194 {
195 /* Signalling NaN. */
196 static memory_long_double x =
197 { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
198 if (!isnanl (x.value))
199 result |= 2;
200 }
201 /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
202 Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
203 Intel IA-64 Architecture Software Developer's Manual, Volume 1:
204 Application Architecture.
205 Table 5-2 "Floating-Point Register Encodings"
206 Figure 5-6 "Memory to Floating-Point Register Data Translation"
207 */
208 { /* Pseudo-NaN. */
209 static memory_long_double x =
210 { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
211 if (!isnanl (x.value))
212 result |= 4;
213 }
214 { /* Pseudo-Infinity. */
215 static memory_long_double x =
216 { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
217 if (!isnanl (x.value))
218 result |= 8;
219 }
220 { /* Pseudo-Zero. */
221 static memory_long_double x =
222 { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
223 if (!isnanl (x.value))
224 result |= 16;
225 }
226 { /* Unnormalized number. */
227 static memory_long_double x =
228 { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
229 if (!isnanl (x.value))
230 result |= 32;
231 }
232 { /* Pseudo-Denormal. */
233 static memory_long_double x =
234 { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
235 if (!isnanl (x.value))
236 result |= 64;
237 }
238 #endif
239
240 return result;
241 }]])],
242 [gl_cv_func_isnanl_works=yes],
243 [gl_cv_func_isnanl_works=no],
244 [case "$host_cpu" in
245 # Guess no on ia64, x86_64, i386.
246 ia64 | x86_64 | i*86) gl_cv_func_isnanl_works="guessing no";;
247 *)
248 case "$host_os" in
249 netbsd*) gl_cv_func_isnanl_works="guessing no";;
250 *) gl_cv_func_isnanl_works="guessing yes";;
251 esac
252 ;;
253 esac
254 ])
255 ])
256 ])