1# exponentf.m4 serial 2
2dnl Copyright (C) 2007-2008, 2010-2020 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_FLOAT_EXPONENT_LOCATION],
7[
8  AC_CACHE_CHECK([where to find the exponent in a 'float'],
9    [gl_cv_cc_float_expbit0],
10    [
11      AC_RUN_IFELSE(
12        [AC_LANG_SOURCE([[
13#include <float.h>
14#include <stddef.h>
15#include <stdio.h>
16#include <string.h>
17#define NWORDS \
18  ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
19typedef union { float value; unsigned int word[NWORDS]; } memory_float;
20static unsigned int ored_words[NWORDS];
21static unsigned int anded_words[NWORDS];
22static void add_to_ored_words (float x)
23{
24  memory_float m;
25  size_t i;
26  /* Clear it first, in case
27     sizeof (float) < sizeof (memory_float).  */
28  memset (&m, 0, sizeof (memory_float));
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    }
35}
36int main ()
37{
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.25f);
45  add_to_ored_words (0.5f);
46  add_to_ored_words (1.0f);
47  add_to_ored_words (2.0f);
48  add_to_ored_words (4.0f);
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);
75}
76        ]])],
77        [gl_cv_cc_float_expbit0=`cat conftest.out`],
78        [gl_cv_cc_float_expbit0="unknown"],
79        [gl_cv_cc_float_expbit0="word 0 bit 23"])
80      rm -f conftest.out
81    ])
82  case "$gl_cv_cc_float_expbit0" in
83    word*bit*)
84      word=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
85      bit=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word.*bit //'`
86      AC_DEFINE_UNQUOTED([FLT_EXPBIT0_WORD], [$word],
87        [Define as the word index where to find the exponent of 'float'.])
88      AC_DEFINE_UNQUOTED([FLT_EXPBIT0_BIT], [$bit],
89        [Define as the bit index in the word where to find bit 0 of the exponent of 'float'.])
90      ;;
91  esac
92])
93