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