1# isfinite.m4 serial 17
2dnl Copyright (C) 2007-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.
6
7AC_DEFUN([gl_ISFINITE],
8[
9  AC_REQUIRE([gl_MATH_H_DEFAULTS])
10  AC_REQUIRE([AC_CANONICAL_HOST])
11  dnl Persuade glibc <math.h> to declare isfinite.
12  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13  AC_CHECK_DECLS([isfinite], , , [[#include <math.h>]])
14  if test "$ac_cv_have_decl_isfinite" = yes; then
15    gl_CHECK_MATH_LIB([ISFINITE_LIBM],
16     [x = isfinite (x) + isfinite ((float) x);])
17    if test "$ISFINITE_LIBM" != missing; then
18      dnl Test whether isfinite() on 'long double' works.
19      gl_ISFINITEL_WORKS
20      case "$gl_cv_func_isfinitel_works" in
21        *yes) ;;
22        *)    ISFINITE_LIBM=missing;;
23      esac
24      dnl Also, isfinite() on 'double' does not work on Linux/ia64 (because of
25      dnl signalling NaNs). But this does not have to be tested, since
26      dnl isfinite(long double) also does not work in this situation.
27    fi
28  fi
29  dnl On Solaris 10, with CC in C++ mode, isfinite is not available although
30  dnl is with cc in C mode. This cannot be worked around by defining
31  dnl _XOPEN_SOURCE=600, because the latter does not work in C++ mode on
32  dnl Solaris 11.0. Therefore use the replacement functions on Solaris.
33  if test "$ac_cv_have_decl_isfinite" != yes \
34     || test "$ISFINITE_LIBM" = missing \
35     || { case "$host_os" in solaris*) true;; *) false;; esac; }; then
36    REPLACE_ISFINITE=1
37    dnl No libraries are needed to link lib/isfinite.c.
38    ISFINITE_LIBM=
39  fi
40  AC_SUBST([ISFINITE_LIBM])
41])
42
43dnl Test whether isfinite() on 'long double' recognizes all canonical values
44dnl which are neither finite nor infinite.
45AC_DEFUN([gl_ISFINITEL_WORKS],
46[
47  AC_REQUIRE([AC_PROG_CC])
48  AC_REQUIRE([gl_BIGENDIAN])
49  AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
50  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
51  AC_CACHE_CHECK([whether isfinite(long double) works], [gl_cv_func_isfinitel_works],
52    [
53      AC_RUN_IFELSE([AC_LANG_SOURCE([[
54#include <float.h>
55#include <limits.h>
56#include <math.h>
57#define NWORDS \
58  ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
59typedef union { unsigned int word[NWORDS]; long double value; }
60        memory_long_double;
61/* On Irix 6.5, gcc 3.4.3 can't compute compile-time NaN, and needs the
62   runtime type conversion.  */
63#ifdef __sgi
64static long double NaNl ()
65{
66  double zero = 0.0;
67  return zero / zero;
68}
69#else
70# define NaNl() (0.0L / 0.0L)
71#endif
72int main ()
73{
74  int result = 0;
75
76  {
77    memory_long_double m;
78    unsigned int i;
79
80    /* The isfinite macro should be immune against changes in the sign bit and
81       in the mantissa bits.  The xor operation twiddles a bit that can only be
82       a sign bit or a mantissa bit (since the exponent never extends to
83       bit 31).  */
84    m.value = NaNl ();
85    m.word[NWORDS / 2] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
86    for (i = 0; i < NWORDS; i++)
87      m.word[i] |= 1;
88    if (isfinite (m.value))
89      result |= 1;
90  }
91
92#if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
93/* Representation of an 80-bit 'long double' as an initializer for a sequence
94   of 'unsigned int' words.  */
95# ifdef WORDS_BIGENDIAN
96#  define LDBL80_WORDS(exponent,manthi,mantlo) \
97     { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
98       ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16),   \
99       (unsigned int) (mantlo) << 16                                        \
100     }
101# else
102#  define LDBL80_WORDS(exponent,manthi,mantlo) \
103     { mantlo, manthi, exponent }
104# endif
105  { /* Quiet NaN.  */
106    static memory_long_double x =
107      { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
108    if (isfinite (x.value))
109      result |= 2;
110  }
111  {
112    /* Signalling NaN.  */
113    static memory_long_double x =
114      { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
115    if (isfinite (x.value))
116      result |= 2;
117  }
118  /* isfinite should return something even for noncanonical values.  */
119  { /* Pseudo-NaN.  */
120    static memory_long_double x =
121      { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
122    if (isfinite (x.value) && !isfinite (x.value))
123      result |= 4;
124  }
125  { /* Pseudo-Infinity.  */
126    static memory_long_double x =
127      { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
128    if (isfinite (x.value) && !isfinite (x.value))
129      result |= 8;
130  }
131  { /* Pseudo-Zero.  */
132    static memory_long_double x =
133      { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
134    if (isfinite (x.value) && !isfinite (x.value))
135      result |= 16;
136  }
137  { /* Unnormalized number.  */
138    static memory_long_double x =
139      { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
140    if (isfinite (x.value) && !isfinite (x.value))
141      result |= 32;
142  }
143  { /* Pseudo-Denormal.  */
144    static memory_long_double x =
145      { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
146    if (isfinite (x.value) && !isfinite (x.value))
147      result |= 64;
148  }
149#endif
150
151  return result;
152}]])],
153      [gl_cv_func_isfinitel_works=yes],
154      [gl_cv_func_isfinitel_works=no],
155      [case "$host_os" in
156                 # Guess no on native Windows.
157         mingw*) gl_cv_func_isfinitel_works="guessing no" ;;
158         *)      gl_cv_func_isfinitel_works="guessing yes" ;;
159       esac
160      ])
161    ])
162])
163