Correct handling of ARM when cross-compiling.
[gnulib.git] / m4 / isnan.m4
1 # isnan.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 dnl Check how to get or define isnan() without linking with libm.
8
9 AC_DEFUN([gl_FUNC_ISNAN_NO_LIBM],
10 [
11   AC_CHECK_FUNC([isnan],
12     [gl_cv_func_isnan_no_libm=yes],
13     [gl_cv_func_isnan_no_libm=no])
14   if test $gl_cv_func_isnan_no_libm = yes; then
15     AC_DEFINE([HAVE_ISNAN_IN_LIBC], 1,
16       [Define if the isnan() function is available in libc.])
17   else
18     AC_LIBOBJ([isnan])
19     gl_DOUBLE_EXPONENT_LOCATION
20   fi
21 ])
22
23 AC_DEFUN([gl_DOUBLE_EXPONENT_LOCATION],
24 [
25   AC_CACHE_CHECK([where to find the exponent in a 'double'],
26     [gl_cv_cc_double_expbit0],
27     [
28       AC_TRY_RUN([
29 #include <float.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <string.h>
33 #define NWORDS \
34   ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
35 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
36 static unsigned int ored_words[NWORDS];
37 static unsigned int anded_words[NWORDS];
38 static void add_to_ored_words (double x)
39 {
40   memory_double m;
41   size_t i;
42   /* Clear it first, in case sizeof (double) < sizeof (memory_double).  */
43   memset (&m, 0, sizeof (memory_double));
44   m.value = x;
45   for (i = 0; i < NWORDS; i++)
46     {
47       ored_words[i] |= m.word[i];
48       anded_words[i] &= m.word[i];
49     }
50 }
51 int main ()
52 {
53   size_t j;
54   FILE *fp = fopen ("conftest.out", "w");
55   if (fp == NULL)
56     return 1;
57   for (j = 0; j < NWORDS; j++)
58     anded_words[j] = ~ (unsigned int) 0;
59   add_to_ored_words (0.25);
60   add_to_ored_words (0.5);
61   add_to_ored_words (1.0);
62   add_to_ored_words (2.0);
63   add_to_ored_words (4.0);
64   /* Remove bits that are common (e.g. if representation of the first mantissa
65      bit is explicit).  */
66   for (j = 0; j < NWORDS; j++)
67     ored_words[j] &= ~anded_words[j];
68   /* Now find the nonzero word.  */
69   for (j = 0; j < NWORDS; j++)
70     if (ored_words[j] != 0)
71       break;
72   if (j < NWORDS)
73     {
74       size_t i;
75       for (i = j + 1; i < NWORDS; i++)
76         if (ored_words[i] != 0)
77           {
78             fprintf (fp, "unknown");
79             return (fclose (fp) != 0);
80           }
81       for (i = 0; ; i++)
82         if ((ored_words[j] >> i) & 1)
83           {
84             fprintf (fp, "word %d bit %d", (int) j, (int) i);
85             return (fclose (fp) != 0);
86           }
87     }
88   fprintf (fp, "unknown");
89   return (fclose (fp) != 0);
90 }
91         ],
92         [gl_cv_cc_double_expbit0=`cat conftest.out`],
93         [gl_cv_cc_double_expbit0="unknown"],
94         [
95           dnl On ARM, there are two 'double' floating-point formats, used by
96           dnl different sets of instructions: The older FPA instructions assume
97           dnl that they are stored in big-endian word order, while the words
98           dnl (like integer types) are stored in little-endian byte order.
99           dnl The newer VFP instructions assume little-endian order consistenly.
100           AC_EGREP_CPP([mixed_endianness], [
101 #if defined arm || defined __arm || defined __arm__
102   mixed_endianness
103 #endif
104             ],
105             [gl_cv_cc_double_expbit0="unknown"],
106             [
107               AC_C_BIGENDIAN(
108                 [gl_cv_cc_double_expbit0="word 0 bit 20"],
109                 [gl_cv_cc_double_expbit0="word 1 bit 20"],
110                 [gl_cv_cc_double_expbit0="unknown"])
111             ])
112         ])
113       rm -f conftest.out
114     ])
115   case "$gl_cv_cc_double_expbit0" in
116     word*bit*)
117       word=`echo "$gl_cv_cc_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
118       bit=`echo "$gl_cv_cc_double_expbit0" | sed -e 's/word.*bit //'`
119       AC_DEFINE_UNQUOTED([DBL_EXPBIT0_WORD], [$word],
120         [Define as the word index where to find the exponent of 'double'.])
121       AC_DEFINE_UNQUOTED([DBL_EXPBIT0_BIT], [$bit],
122         [Define as the bit index in the word where to find bit 0 of the exponent of 'double'.])
123       ;;
124   esac
125 ])