Move some declarations from acl.h to acl-internal.h.
[gnulib.git] / m4 / isnand.m4
1 # isnand.m4 serial 2
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.
6
7 dnl Check how to get or define isnand() without linking with libm.
8
9 AC_DEFUN([gl_FUNC_ISNAND_NO_LIBM],
10 [
11   AC_CACHE_CHECK([whether isnan(double) can be used without linking with libm],
12     [gl_cv_func_isnand_no_libm],
13     [
14       AC_TRY_LINK([#include <math.h>
15                    #if __GNUC__ >= 4
16                    # undef isnand
17                    # define isnand(x) __builtin_isnan ((double)(x))
18                    #else
19                    # undef isnand
20                    # define isnand(x) isnan ((double)(x))
21                    #endif
22                    double x;],
23                   [return isnand (x);],
24         [gl_cv_func_isnand_no_libm=yes],
25         [gl_cv_func_isnand_no_libm=no])
26     ])
27   if test $gl_cv_func_isnand_no_libm = yes; then
28     AC_DEFINE([HAVE_ISNAND_IN_LIBC], 1,
29       [Define if the isnan(double) function is available in libc.])
30   else
31     AC_LIBOBJ([isnand])
32     gl_DOUBLE_EXPONENT_LOCATION
33   fi
34 ])
35
36 AC_DEFUN([gl_DOUBLE_EXPONENT_LOCATION],
37 [
38   AC_CACHE_CHECK([where to find the exponent in a 'double'],
39     [gl_cv_cc_double_expbit0],
40     [
41       AC_TRY_RUN([
42 #include <float.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <string.h>
46 #define NWORDS \
47   ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
48 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
49 static unsigned int ored_words[NWORDS];
50 static unsigned int anded_words[NWORDS];
51 static void add_to_ored_words (double x)
52 {
53   memory_double m;
54   size_t i;
55   /* Clear it first, in case sizeof (double) < sizeof (memory_double).  */
56   memset (&m, 0, sizeof (memory_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.25);
73   add_to_ored_words (0.5);
74   add_to_ored_words (1.0);
75   add_to_ored_words (2.0);
76   add_to_ored_words (4.0);
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_double_expbit0=`cat conftest.out`],
106         [gl_cv_cc_double_expbit0="unknown"],
107         [
108           dnl On ARM, there are two 'double' floating-point formats, used by
109           dnl different sets of instructions: The older FPA instructions assume
110           dnl that they are stored in big-endian word order, while the words
111           dnl (like integer types) are stored in little-endian byte order.
112           dnl The newer VFP instructions assume little-endian order consistenly.
113           AC_EGREP_CPP([mixed_endianness], [
114 #if defined arm || defined __arm || defined __arm__
115   mixed_endianness
116 #endif
117             ],
118             [gl_cv_cc_double_expbit0="unknown"],
119             [
120               pushdef([AC_MSG_CHECKING],[:])dnl
121               pushdef([AC_MSG_RESULT],[:])dnl
122               pushdef([AC_MSG_RESULT_UNQUOTED],[:])dnl
123               AC_C_BIGENDIAN(
124                 [gl_cv_cc_double_expbit0="word 0 bit 20"],
125                 [gl_cv_cc_double_expbit0="word 1 bit 20"],
126                 [gl_cv_cc_double_expbit0="unknown"])
127               popdef([AC_MSG_RESULT_UNQUOTED])dnl
128               popdef([AC_MSG_RESULT])dnl
129               popdef([AC_MSG_CHECKING])dnl
130             ])
131         ])
132       rm -f conftest.out
133     ])
134   case "$gl_cv_cc_double_expbit0" in
135     word*bit*)
136       word=`echo "$gl_cv_cc_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
137       bit=`echo "$gl_cv_cc_double_expbit0" | sed -e 's/word.*bit //'`
138       AC_DEFINE_UNQUOTED([DBL_EXPBIT0_WORD], [$word],
139         [Define as the word index where to find the exponent of 'double'.])
140       AC_DEFINE_UNQUOTED([DBL_EXPBIT0_BIT], [$bit],
141         [Define as the bit index in the word where to find bit 0 of the exponent of 'double'.])
142       ;;
143   esac
144 ])