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