Also handle the case that isnan is a "type-generic" macro.
[gnulib.git] / m4 / isnanl.m4
1 # isnanl.m4 serial 1
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_NO_LIBM],
8 [
9   AC_REQUIRE([gt_TYPE_LONGDOUBLE])
10   if test $gt_cv_c_long_double = yes; then
11     AC_CACHE_CHECK([whether isnan(long double) can be used without linking with libm],
12       [gl_cv_func_isnanl_no_libm],
13       [
14         AC_TRY_LINK([#include <math.h>
15                      #ifdef isnan
16                      # undef isnanl
17                      # define isnanl(x) isnan ((long double)(x))
18                      #endif
19                      long double x;],
20                     [return isnanl (x);],
21           [gl_cv_func_isnanl_no_libm=yes],
22           [gl_cv_func_isnanl_no_libm=no])
23       ])
24     if test $gl_cv_func_isnanl_no_libm = yes; then
25       AC_DEFINE([HAVE_ISNANL_IN_LIBC], 1,
26         [Define if the isnan(long double) function is available in libc.])
27     else
28       AC_LIBOBJ([isnanl])
29       gl_LONG_DOUBLE_EXPONENT_LOCATION
30     fi
31   fi
32 ])
33
34 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
35 [
36   AC_CACHE_CHECK([where to find the exponent in a 'long double'],
37     [gl_cv_cc_long_double_expbit0],
38     [
39       AC_TRY_RUN([
40 #include <float.h>
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <string.h>
44 #define NWORDS \
45   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
46 typedef union { long double value; unsigned int word[NWORDS]; }
47         memory_long_double;
48 static unsigned int ored_words[NWORDS];
49 static unsigned int anded_words[NWORDS];
50 static void add_to_ored_words (long double x)
51 {
52   memory_long_double m;
53   size_t i;
54   /* Clear it first, in case
55      sizeof (long double) < sizeof (memory_long_double).  */
56   memset (&m, 0, sizeof (memory_long_double));
57   m.value = x;
58   for (i = 0; i < NWORDS; i++)
59     {
60       ored_words[i] |= m.word[i];
61       anded_words[i] &= m.word[i];
62     }
63 }
64 int main ()
65 {
66   size_t j;
67   FILE *fp = fopen ("conftest.out", "w");
68   if (fp == NULL)
69     return 1;
70   for (j = 0; j < NWORDS; j++)
71     anded_words[j] = ~ (unsigned int) 0;
72   add_to_ored_words (0.25L);
73   add_to_ored_words (0.5L);
74   add_to_ored_words (1.0L);
75   add_to_ored_words (2.0L);
76   add_to_ored_words (4.0L);
77   /* Remove bits that are common (e.g. if representation of the first mantissa
78      bit is explicit).  */
79   for (j = 0; j < NWORDS; j++)
80     ored_words[j] &= ~anded_words[j];
81   /* Now find the nonzero word.  */
82   for (j = 0; j < NWORDS; j++)
83     if (ored_words[j] != 0)
84       break;
85   if (j < NWORDS)
86     {
87       size_t i;
88       for (i = j + 1; i < NWORDS; i++)
89         if (ored_words[i] != 0)
90           {
91             fprintf (fp, "unknown");
92             return (fclose (fp) != 0);
93           }
94       for (i = 0; ; i++)
95         if ((ored_words[j] >> i) & 1)
96           {
97             fprintf (fp, "word %d bit %d", (int) j, (int) i);
98             return (fclose (fp) != 0);
99           }
100     }
101   fprintf (fp, "unknown");
102   return (fclose (fp) != 0);
103 }
104         ],
105         [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
106         [gl_cv_cc_long_double_expbit0="unknown"],
107         [
108           dnl When cross-compiling, we don't know. It depends on the
109           dnl ABI and compiler version. There are too many cases.
110           gl_cv_cc_long_double_expbit0="unknown"
111         ])
112       rm -f conftest.out
113     ])
114   case "$gl_cv_cc_long_double_expbit0" in
115     word*bit*)
116       word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
117       bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
118       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
119         [Define as the word index where to find the exponent of 'long double'.])
120       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
121         [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])
122       ;;
123   esac
124 ])