Revamp lchown so that it lives in unistd.h where it belongs.
[gnulib.git] / m4 / isnanl.m4
1 # isnanl.m4 serial 5
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.
6
7 AC_DEFUN([gl_FUNC_ISNANL],
8 [
9   ISNANL_LIBM=
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
14       ISNANL_LIBM=-lm
15     fi
16   fi
17   if test $gl_cv_func_isnanl_no_libm = yes \
18      || test $gl_cv_func_isnanl_in_libm = yes; then
19     save_LIBS="$LIBS"
20     LIBS="$LIBS $ISNANL_LIBM"
21     gl_FUNC_ISNANL_WORKS
22     LIBS="$save_LIBS"
23     case "$gl_cv_func_isnanl_works" in
24       *yes) gl_func_isnanl=yes ;;
25       *)    gl_func_isnanl=no; ISNANL_LIBM= ;;
26     esac
27   else
28     gl_func_isnanl=no
29   fi
30   if test $gl_func_isnanl = yes; then
31     AC_DEFINE([HAVE_ISNANL], 1,
32       [Define if the isnan(long double) function is available.])
33   else
34     AC_LIBOBJ([isnanl])
35     gl_LONG_DOUBLE_EXPONENT_LOCATION
36   fi
37   AC_SUBST([ISNANL_LIBM])
38 ])
39
40 AC_DEFUN([gl_FUNC_ISNANL_NO_LIBM],
41 [
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
45     gl_FUNC_ISNANL_WORKS
46     case "$gl_cv_func_isnanl_works" in
47       *yes) ;;
48       *)    gl_func_isnanl_no_libm=no ;;
49     esac
50   fi
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.])
54   else
55     AC_LIBOBJ([isnanl])
56     gl_LONG_DOUBLE_EXPONENT_LOCATION
57   fi
58 ])
59
60 dnl Test whether isnanl() can be used without libm.
61 AC_DEFUN([gl_HAVE_ISNANL_NO_LIBM],
62 [
63   AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
64     [gl_cv_func_isnanl_no_libm],
65     [
66       AC_TRY_LINK([#include <math.h>
67                    #ifdef isnan
68                    # undef isnanl
69                    # define isnanl(x) isnan ((long double)(x))
70                    #endif
71                    long double x;],
72                   [return isnanl (x);],
73         [gl_cv_func_isnanl_no_libm=yes],
74         [gl_cv_func_isnanl_no_libm=no])
75     ])
76 ])
77
78 dnl Test whether isnanl() can be used with libm.
79 AC_DEFUN([gl_HAVE_ISNANL_IN_LIBM],
80 [
81   AC_CACHE_CHECK([whether isnan(long double) can be used with libm],
82     [gl_cv_func_isnanl_in_libm],
83     [
84       save_LIBS="$LIBS"
85       LIBS="$LIBS -lm"
86       AC_TRY_LINK([#include <math.h>
87                    #ifdef isnan
88                    # undef isnanl
89                    # define isnanl(x) isnan ((long double)(x))
90                    #endif
91                    long double x;],
92                   [return isnanl (x);],
93         [gl_cv_func_isnanl_in_libm=yes],
94         [gl_cv_func_isnanl_in_libm=no])
95       LIBS="$save_LIBS"
96     ])
97 ])
98
99 dnl Test whether isnanl() recognizes all numbers which are neither finite nor
100 dnl infinite. This test fails e.g. on NetBSD/i386 and on glibc/ia64.
101 AC_DEFUN([gl_FUNC_ISNANL_WORKS],
102 [
103   AC_REQUIRE([AC_PROG_CC])
104   AC_REQUIRE([AC_C_BIGENDIAN])
105   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
106   AC_CACHE_CHECK([whether isnanl works], [gl_cv_func_isnanl_works],
107     [
108       AC_TRY_RUN([
109 #include <float.h>
110 #include <limits.h>
111 #include <math.h>
112 #ifdef isnan
113 # undef isnanl
114 # define isnanl(x) isnan ((long double)(x))
115 #endif
116 #define NWORDS \
117   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
118 typedef union { unsigned int word[NWORDS]; long double value; }
119         memory_long_double;
120 int main ()
121 {
122   memory_long_double m;
123   unsigned int i;
124
125   /* The isnanl function should be immune against changes in the sign bit and
126      in the mantissa bits.  The xor operation twiddles a bit that can only be
127      a sign bit or a mantissa bit (since the exponent never extends to
128      bit 31).  */
129   m.value = 0.0L / 0.0L;
130   m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
131   for (i = 0; i < NWORDS; i++)
132     m.word[i] |= 1;
133   if (!isnanl (m.value))
134     return 1;
135
136 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
137 /* Representation of an 80-bit 'long double' as an initializer for a sequence
138    of 'unsigned int' words.  */
139 # ifdef WORDS_BIGENDIAN
140 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
141      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
142        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
143        (unsigned int) (mantlo) << 16                                        \
144      }
145 # else
146 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
147      { mantlo, manthi, exponent }
148 # endif
149   { /* Quiet NaN.  */
150     static memory_long_double x =
151       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
152     if (!isnanl (x.value))
153       return 1;
154   }
155   {
156     /* Signalling NaN.  */
157     static memory_long_double x =
158       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
159     if (!isnanl (x.value))
160       return 1;
161   }
162   /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
163      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
164        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
165        Application Architecture.
166        Table 5-2 "Floating-Point Register Encodings"
167        Figure 5-6 "Memory to Floating-Point Register Data Translation"
168    */
169   { /* Pseudo-NaN.  */
170     static memory_long_double x =
171       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
172     if (!isnanl (x.value))
173       return 1;
174   }
175   { /* Pseudo-Infinity.  */
176     static memory_long_double x =
177       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
178     if (!isnanl (x.value))
179       return 1;
180   }
181   { /* Pseudo-Zero.  */
182     static memory_long_double x =
183       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
184     if (!isnanl (x.value))
185       return 1;
186   }
187   { /* Unnormalized number.  */
188     static memory_long_double x =
189       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
190     if (!isnanl (x.value))
191       return 1;
192   }
193   { /* Pseudo-Denormal.  */
194     static memory_long_double x =
195       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
196     if (!isnanl (x.value))
197       return 1;
198   }
199 #endif
200
201   return 0;
202 }], [gl_cv_func_isnanl_works=yes], [gl_cv_func_isnanl_works=no],
203       [case "$host_cpu" in
204                                # Guess no on ia64, x86_64, i386.
205          ia64 | x86_64 | i*86) gl_cv_func_isnanl_works="guessing no";;
206          *)
207            case "$host_os" in
208              netbsd*) gl_cv_func_isnanl_works="guessing no";;
209              *)       gl_cv_func_isnanl_works="guessing yes";;
210            esac
211            ;;
212        esac
213       ])
214     ])
215 ])
216
217 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
218 [
219   AC_REQUIRE([AC_C_BIGENDIAN])
220   AC_CACHE_CHECK([where to find the exponent in a 'long double'],
221     [gl_cv_cc_long_double_expbit0],
222     [
223       AC_TRY_RUN([
224 #include <float.h>
225 #include <stddef.h>
226 #include <stdio.h>
227 #include <string.h>
228 #define NWORDS \
229   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
230 typedef union { long double value; unsigned int word[NWORDS]; }
231         memory_long_double;
232 static unsigned int ored_words[NWORDS];
233 static unsigned int anded_words[NWORDS];
234 static void add_to_ored_words (long double x)
235 {
236   memory_long_double m;
237   size_t i;
238   /* Clear it first, in case
239      sizeof (long double) < sizeof (memory_long_double).  */
240   memset (&m, 0, sizeof (memory_long_double));
241   m.value = x;
242   for (i = 0; i < NWORDS; i++)
243     {
244       ored_words[i] |= m.word[i];
245       anded_words[i] &= m.word[i];
246     }
247 }
248 int main ()
249 {
250   size_t j;
251   FILE *fp = fopen ("conftest.out", "w");
252   if (fp == NULL)
253     return 1;
254   for (j = 0; j < NWORDS; j++)
255     anded_words[j] = ~ (unsigned int) 0;
256   add_to_ored_words (0.25L);
257   add_to_ored_words (0.5L);
258   add_to_ored_words (1.0L);
259   add_to_ored_words (2.0L);
260   add_to_ored_words (4.0L);
261   /* Remove bits that are common (e.g. if representation of the first mantissa
262      bit is explicit).  */
263   for (j = 0; j < NWORDS; j++)
264     ored_words[j] &= ~anded_words[j];
265   /* Now find the nonzero word.  */
266   for (j = 0; j < NWORDS; j++)
267     if (ored_words[j] != 0)
268       break;
269   if (j < NWORDS)
270     {
271       size_t i;
272       for (i = j + 1; i < NWORDS; i++)
273         if (ored_words[i] != 0)
274           {
275             fprintf (fp, "unknown");
276             return (fclose (fp) != 0);
277           }
278       for (i = 0; ; i++)
279         if ((ored_words[j] >> i) & 1)
280           {
281             fprintf (fp, "word %d bit %d", (int) j, (int) i);
282             return (fclose (fp) != 0);
283           }
284     }
285   fprintf (fp, "unknown");
286   return (fclose (fp) != 0);
287 }
288         ],
289         [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
290         [gl_cv_cc_long_double_expbit0="unknown"],
291         [
292           dnl When cross-compiling, we don't know. It depends on the
293           dnl ABI and compiler version. There are too many cases.
294           gl_cv_cc_long_double_expbit0="unknown"
295         ])
296       rm -f conftest.out
297     ])
298   case "$gl_cv_cc_long_double_expbit0" in
299     word*bit*)
300       word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
301       bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
302       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
303         [Define as the word index where to find the exponent of 'long double'.])
304       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
305         [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])
306       ;;
307   esac
308 ])