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