1# exponentl.m4 serial 3
2dnl Copyright (C) 2007-2012 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6AC_DEFUN([gl_LONG_DOUBLE_EXPONENT_LOCATION],
7[
8  AC_REQUIRE([gl_BIGENDIAN])
9  AC_CACHE_CHECK([where to find the exponent in a 'long double'],
10    [gl_cv_cc_long_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 (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
20typedef union { long double value; unsigned int word[NWORDS]; }
21        memory_long_double;
22static unsigned int ored_words[NWORDS];
23static unsigned int anded_words[NWORDS];
24static void add_to_ored_words (long double x)
25{
26  memory_long_double m;
27  size_t i;
28  /* Clear it first, in case
29     sizeof (long double) < sizeof (memory_long_double).  */
30  memset (&m, 0, sizeof (memory_long_double));
31  m.value = x;
32  for (i = 0; i < NWORDS; i++)
33    {
34      ored_words[i] |= m.word[i];
35      anded_words[i] &= m.word[i];
36    }
37}
38int main ()
39{
40  size_t j;
41  FILE *fp = fopen ("conftest.out", "w");
42  if (fp == NULL)
43    return 1;
44  for (j = 0; j < NWORDS; j++)
45    anded_words[j] = ~ (unsigned int) 0;
46  add_to_ored_words (0.25L);
47  add_to_ored_words (0.5L);
48  add_to_ored_words (1.0L);
49  add_to_ored_words (2.0L);
50  add_to_ored_words (4.0L);
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);
77}
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, 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        ])
86      rm -f conftest.out
87    ])
88  case "$gl_cv_cc_long_double_expbit0" in
89    word*bit*)
90      word=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
91      bit=`echo "$gl_cv_cc_long_double_expbit0" | sed -e 's/word.*bit //'`
92      AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_WORD], [$word],
93        [Define as the word index where to find the exponent of 'long double'.])
94      AC_DEFINE_UNQUOTED([LDBL_EXPBIT0_BIT], [$bit],
95        [Define as the bit index in the word where to find bit 0 of the exponent of 'long double'.])
96      ;;
97  esac
98])
99