Avoid some possibly wrong configure test results.
[gnulib.git] / m4 / exponentl.m4
blobeb7fcb4424fe517337b0af5c92a93573ffdebc1a
1 # exponentl.m4
2 # serial 7
3 dnl Copyright (C) 2007-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_LONG_DOUBLE_EXPONENT_LOCATION],
9   AC_REQUIRE([gl_BIGENDIAN])
10   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
11   AC_CACHE_CHECK([where to find the exponent in a 'long double'],
12     [gl_cv_cc_long_double_expbit0],
13     [
14       AC_RUN_IFELSE(
15         [AC_LANG_SOURCE([[
16 #include <float.h>
17 #include <stddef.h>
18 #include <stdio.h>
19 #include <string.h>
20 #define NWORDS \
21   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
22 typedef union { long double value; unsigned int word[NWORDS]; }
23         memory_long_double;
24 static unsigned int ored_words[NWORDS];
25 static unsigned int anded_words[NWORDS];
26 static void add_to_ored_words (long double *x)
28   memory_long_double m;
29   size_t i;
30   /* Clear it first, in case
31      sizeof (long double) < sizeof (memory_long_double).  */
32   memset (&m, 0, sizeof (memory_long_double));
33   m.value = *x;
34   for (i = 0; i < NWORDS; i++)
35     {
36       ored_words[i] |= m.word[i];
37       anded_words[i] &= m.word[i];
38     }
40 int main ()
42   static long double samples[5] = { 0.25L, 0.5L, 1.0L, 2.0L, 4.0L };
43   size_t j;
44   FILE *fp = fopen ("conftest.out", "w");
45   if (fp == NULL)
46     return 1;
47   for (j = 0; j < NWORDS; j++)
48     anded_words[j] = ~ (unsigned int) 0;
49   for (j = 0; j < 5; j++)
50     add_to_ored_words (&samples[j]);
51   /* Remove bits that are common (e.g. if representation of the first mantissa
52      bit is explicit).  */
53   for (j = 0; j < NWORDS; j++)
54     ored_words[j] &= ~anded_words[j];
55   /* Now find the nonzero word.  */
56   for (j = 0; j < NWORDS; j++)
57     if (ored_words[j] != 0)
58       break;
59   if (j < NWORDS)
60     {
61       size_t i;
62       for (i = j + 1; i < NWORDS; i++)
63         if (ored_words[i] != 0)
64           {
65             fprintf (fp, "unknown");
66             return (fclose (fp) != 0);
67           }
68       for (i = 0; ; i++)
69         if ((ored_words[j] >> i) & 1)
70           {
71             fprintf (fp, "word %d bit %d", (int) j, (int) i);
72             return (fclose (fp) != 0);
73           }
74     }
75   fprintf (fp, "unknown");
76   return (fclose (fp) != 0);
78         ]])],
79         [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
80         [gl_cv_cc_long_double_expbit0="unknown"],
81         [
82           dnl When cross-compiling, in general we don't know. It depends on the
83           dnl ABI and compiler version. There are too many cases.
84           gl_cv_cc_long_double_expbit0="unknown"
85           case "$host_os" in
86             mingw* | windows*)
87               # On native Windows (little-endian), we know the result
88               # in two cases: mingw, MSVC.
89               AC_EGREP_CPP([Known], [
90 #ifdef __MINGW32__
91  Known
92 #endif
93                 ], [gl_cv_cc_long_double_expbit0="word 2 bit 0"])
94               AC_EGREP_CPP([Known], [
95 #ifdef _MSC_VER
96  Known
97 #endif
98                 ], [gl_cv_cc_long_double_expbit0="word 1 bit 20"])
99               ;;
100           esac
101         ])
102       rm -f conftest.out
103     ])
104   case "$gl_cv_cc_long_double_expbit0" in
105     word*bit*)
106       word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
107       bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
108       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
109         [Define as the word index where to find the exponent of 'long double'.])
110       AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
111         [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])
112       ;;
113   esac