New upstream version 1.9
[debian/gzip] / m4 / exponentl.m4
1 # exponentl.m4 serial 4
2 dnl Copyright (C) 2007-2018 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 AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
7 [
8   AC_REQUIRE([gl_BIGENDIAN])
9   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
10   AC_CACHE_CHECK([where to find the exponent in a 'long double'],
11     [gl_cv_cc_long_double_expbit0],
12     [
13       AC_RUN_IFELSE(
14         [AC_LANG_SOURCE([[
15 #include <float.h>
16 #include <stddef.h>
17 #include <stdio.h>
18 #include <string.h>
19 #define NWORDS \
20   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
21 typedef union { long double value; unsigned int word[NWORDS]; }
22         memory_long_double;
23 static unsigned int ored_words[NWORDS];
24 static unsigned int anded_words[NWORDS];
25 static void add_to_ored_words (long double x)
26 {
27   memory_long_double m;
28   size_t i;
29   /* Clear it first, in case
30      sizeof (long double) < sizeof (memory_long_double).  */
31   memset (&m, 0, sizeof (memory_long_double));
32   m.value = x;
33   for (i = 0; i < NWORDS; i++)
34     {
35       ored_words[i] |= m.word[i];
36       anded_words[i] &= m.word[i];
37     }
38 }
39 int main ()
40 {
41   size_t j;
42   FILE *fp = fopen ("conftest.out", "w");
43   if (fp == NULL)
44     return 1;
45   for (j = 0; j < NWORDS; j++)
46     anded_words[j] = ~ (unsigned int) 0;
47   add_to_ored_words (0.25L);
48   add_to_ored_words (0.5L);
49   add_to_ored_words (1.0L);
50   add_to_ored_words (2.0L);
51   add_to_ored_words (4.0L);
52   /* Remove bits that are common (e.g. if representation of the first mantissa
53      bit is explicit).  */
54   for (j = 0; j < NWORDS; j++)
55     ored_words[j] &= ~anded_words[j];
56   /* Now find the nonzero word.  */
57   for (j = 0; j < NWORDS; j++)
58     if (ored_words[j] != 0)
59       break;
60   if (j < NWORDS)
61     {
62       size_t i;
63       for (i = j + 1; i < NWORDS; i++)
64         if (ored_words[i] != 0)
65           {
66             fprintf (fp, "unknown");
67             return (fclose (fp) != 0);
68           }
69       for (i = 0; ; i++)
70         if ((ored_words[j] >> i) & 1)
71           {
72             fprintf (fp, "word %d bit %d", (int) j, (int) i);
73             return (fclose (fp) != 0);
74           }
75     }
76   fprintf (fp, "unknown");
77   return (fclose (fp) != 0);
78 }
79         ]])],
80         [gl_cv_cc_long_double_expbit0=`cat conftest.out`],
81         [gl_cv_cc_long_double_expbit0="unknown"],
82         [
83           dnl When cross-compiling, in general we don't know. It depends on the
84           dnl ABI and compiler version. There are too many cases.
85           gl_cv_cc_long_double_expbit0="unknown"
86           case "$host_os" in
87             mingw*) # 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
114 ])