obstack-zprintf: Add more tests.
[gnulib.git] / m4 / exponentd.m4
blobdb597afccc193545e17dd59bdfa851923eec164d
1 # exponentd.m4
2 # serial 4
3 dnl Copyright (C) 2007-2008, 2010-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN_ONCE([gl_DOUBLE_EXPONENT_LOCATION],
9   AC_CACHE_CHECK([where to find the exponent in a 'double'],
10     [gl_cv_cc_double_expbit0],
11     [
12       AC_RUN_IFELSE(
13         [AC_LANG_SOURCE([[
14 #include <float.h>
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <string.h>
18 #define NWORDS \
19   ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
20 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
21 static unsigned int ored_words[NWORDS];
22 static unsigned int anded_words[NWORDS];
23 static void add_to_ored_words (double x)
25   memory_double m;
26   size_t i;
27   /* Clear it first, in case sizeof (double) < sizeof (memory_double).  */
28   memset (&m, 0, sizeof (memory_double));
29   m.value = x;
30   for (i = 0; i < NWORDS; i++)
31     {
32       ored_words[i] |= m.word[i];
33       anded_words[i] &= m.word[i];
34     }
36 int main ()
38   size_t j;
39   FILE *fp = fopen ("conftest.out", "w");
40   if (fp == NULL)
41     return 1;
42   for (j = 0; j < NWORDS; j++)
43     anded_words[j] = ~ (unsigned int) 0;
44   add_to_ored_words (0.25);
45   add_to_ored_words (0.5);
46   add_to_ored_words (1.0);
47   add_to_ored_words (2.0);
48   add_to_ored_words (4.0);
49   /* Remove bits that are common (e.g. if representation of the first mantissa
50      bit is explicit).  */
51   for (j = 0; j < NWORDS; j++)
52     ored_words[j] &= ~anded_words[j];
53   /* Now find the nonzero word.  */
54   for (j = 0; j < NWORDS; j++)
55     if (ored_words[j] != 0)
56       break;
57   if (j < NWORDS)
58     {
59       size_t i;
60       for (i = j + 1; i < NWORDS; i++)
61         if (ored_words[i] != 0)
62           {
63             fprintf (fp, "unknown");
64             return (fclose (fp) != 0);
65           }
66       for (i = 0; ; i++)
67         if ((ored_words[j] >> i) & 1)
68           {
69             fprintf (fp, "word %d bit %d", (int) j, (int) i);
70             return (fclose (fp) != 0);
71           }
72     }
73   fprintf (fp, "unknown");
74   return (fclose (fp) != 0);
76         ]])],
77         [gl_cv_cc_double_expbit0=`cat conftest.out`],
78         [gl_cv_cc_double_expbit0="unknown"],
79         [
80           dnl On ARM, there are two 'double' floating-point formats, used by
81           dnl different sets of instructions: The older FPA instructions assume
82           dnl that they are stored in big-endian word order, while the words
83           dnl (like integer types) are stored in little-endian byte order.
84           dnl The newer VFP instructions assume little-endian order
85           dnl consistently.
86           AC_EGREP_CPP([mixed_endianness], [
87 #if defined arm || defined __arm || defined __arm__
88   mixed_endianness
89 #endif
90             ],
91             [gl_cv_cc_double_expbit0="unknown"],
92             [
93               pushdef([AC_MSG_CHECKING],[:])dnl
94               pushdef([AC_MSG_RESULT],[:])dnl
95               pushdef([AC_MSG_RESULT_UNQUOTED],[:])dnl
96               AC_C_BIGENDIAN(
97                 [gl_cv_cc_double_expbit0="word 0 bit 20"],
98                 [gl_cv_cc_double_expbit0="word 1 bit 20"],
99                 [gl_cv_cc_double_expbit0="unknown"])
100               popdef([AC_MSG_RESULT_UNQUOTED])dnl
101               popdef([AC_MSG_RESULT])dnl
102               popdef([AC_MSG_CHECKING])dnl
103             ])
104         ])
105       rm -f conftest.out
106     ])
107   case "$gl_cv_cc_double_expbit0" in
108     word*bit*)
109       word=`echo "$gl_cv_cc_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
110       bit=`echo "$gl_cv_cc_double_expbit0" | sed -e 's/word.*bit //'`
111       AC_DEFINE_UNQUOTED([DBL_EXPBIT0_WORD], [$word],
112         [Define as the word index where to find the exponent of 'double'.])
113       AC_DEFINE_UNQUOTED([DBL_EXPBIT0_BIT], [$bit],
114         [Define as the bit index in the word where to find bit 0 of the exponent of 'double'.])
115       ;;
116   esac