4d8063fae739e03496c8040216e1e7f7f36cc4a7
[debian/gzip] / m4 / exponentf.m4
1 # exponentf.m4 serial 1
2 dnl Copyright (C) 2007-2010 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_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_TRY_RUN([
12 #include <float.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #define NWORDS \
17   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
18 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
19 static unsigned int ored_words[NWORDS];
20 static unsigned int anded_words[NWORDS];
21 static void add_to_ored_words (float x)
22 {
23   memory_float m;
24   size_t i;
25   /* Clear it first, in case
26      sizeof (float) < sizeof (memory_float).  */
27   memset (&m, 0, sizeof (memory_float));
28   m.value = x;
29   for (i = 0; i < NWORDS; i++)
30     {
31       ored_words[i] |= m.word[i];
32       anded_words[i] &= m.word[i];
33     }
34 }
35 int main ()
36 {
37   size_t j;
38   FILE *fp = fopen ("conftest.out", "w");
39   if (fp == NULL)
40     return 1;
41   for (j = 0; j < NWORDS; j++)
42     anded_words[j] = ~ (unsigned int) 0;
43   add_to_ored_words (0.25f);
44   add_to_ored_words (0.5f);
45   add_to_ored_words (1.0f);
46   add_to_ored_words (2.0f);
47   add_to_ored_words (4.0f);
48   /* Remove bits that are common (e.g. if representation of the first mantissa
49      bit is explicit).  */
50   for (j = 0; j < NWORDS; j++)
51     ored_words[j] &= ~anded_words[j];
52   /* Now find the nonzero word.  */
53   for (j = 0; j < NWORDS; j++)
54     if (ored_words[j] != 0)
55       break;
56   if (j < NWORDS)
57     {
58       size_t i;
59       for (i = j + 1; i < NWORDS; i++)
60         if (ored_words[i] != 0)
61           {
62             fprintf (fp, "unknown");
63             return (fclose (fp) != 0);
64           }
65       for (i = 0; ; i++)
66         if ((ored_words[j] >> i) & 1)
67           {
68             fprintf (fp, "word %d bit %d", (int) j, (int) i);
69             return (fclose (fp) != 0);
70           }
71     }
72   fprintf (fp, "unknown");
73   return (fclose (fp) != 0);
74 }
75         ],
76         [gl_cv_cc_float_expbit0=`cat conftest.out`],
77         [gl_cv_cc_float_expbit0="unknown"],
78         [gl_cv_cc_float_expbit0="word 0 bit 23"])
79       rm -f conftest.out
80     ])
81   case "$gl_cv_cc_float_expbit0" in
82     word*bit*)
83       word=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word //' -e 's/ bit.*//'`
84       bit=`echo "$gl_cv_cc_float_expbit0" | sed -e 's/word.*bit //'`
85       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_WORD], [$word],
86         [Define as the word index where to find the exponent of 'float'.])
87       AC_DEFINE_UNQUOTED([FLT_EXPBIT0_BIT], [$bit],
88         [Define as the bit index in the word where to find bit 0 of the exponent of 'float'.])
89       ;;
90   esac
91 ])