NEWS.stable: log cherry-pick [7b2b693]->[a43f6fd] autoupdate
[gnulib.git] / m4 / isnanf.m4
1 # isnanf.m4 serial 12
2 dnl Copyright (C) 2007-2011 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 dnl Check how to get or define isnanf().
8
9 AC_DEFUN([gl_FUNC_ISNANF],
10 [
11   AC_REQUIRE([gl_MATH_H_DEFAULTS])
12   ISNANF_LIBM=
13   gl_HAVE_ISNANF_NO_LIBM
14   if test $gl_cv_func_isnanf_no_libm = no; then
15     gl_HAVE_ISNANF_IN_LIBM
16     if test $gl_cv_func_isnanf_in_libm = yes; then
17       ISNANF_LIBM=-lm
18     fi
19   fi
20   dnl The variable gl_func_isnanf set here is used by isnan.m4.
21   if test $gl_cv_func_isnanf_no_libm = yes \
22      || test $gl_cv_func_isnanf_in_libm = yes; then
23     save_LIBS="$LIBS"
24     LIBS="$LIBS $ISNANF_LIBM"
25     gl_ISNANF_WORKS
26     LIBS="$save_LIBS"
27     case "$gl_cv_func_isnanf_works" in
28       *yes) gl_func_isnanf=yes ;;
29       *)    gl_func_isnanf=no; ISNANF_LIBM= ;;
30     esac
31   else
32     gl_func_isnanf=no
33   fi
34   if test $gl_func_isnanf != yes; then
35     HAVE_ISNANF=0
36     gl_BUILD_ISNANF
37   fi
38   AC_SUBST([ISNANF_LIBM])
39 ])
40
41 dnl Check how to get or define isnanf() without linking with libm.
42
43 AC_DEFUN([gl_FUNC_ISNANF_NO_LIBM],
44 [
45   gl_HAVE_ISNANF_NO_LIBM
46   if test $gl_cv_func_isnanf_no_libm = yes; then
47     gl_ISNANF_WORKS
48   fi
49   if test $gl_cv_func_isnanf_no_libm = yes \
50      && { case "$gl_cv_func_isnanf_works" in
51             *yes) true;;
52             *) false;;
53           esac
54         }; then
55     AC_DEFINE([HAVE_ISNANF_IN_LIBC], [1],
56       [Define if the isnan(float) function is available in libc.])
57   else
58     gl_BUILD_ISNANF
59   fi
60 ])
61
62 dnl Pull in replacement isnanf definition. It does not need -lm.
63 AC_DEFUN([gl_BUILD_ISNANF],
64 [
65   AC_LIBOBJ([isnanf])
66   gl_FLOAT_EXPONENT_LOCATION
67 ])
68
69 dnl Test whether isnanf() can be used without libm.
70 AC_DEFUN([gl_HAVE_ISNANF_NO_LIBM],
71 [
72   AC_CACHE_CHECK([whether isnan(float) can be used without linking with libm],
73     [gl_cv_func_isnanf_no_libm],
74     [
75       AC_LINK_IFELSE(
76         [AC_LANG_PROGRAM(
77            [[#include <math.h>
78              #if __GNUC__ >= 4
79              # undef isnanf
80              # define isnanf(x) __builtin_isnanf ((float)(x))
81              #elif defined isnan
82              # undef isnanf
83              # define isnanf(x) isnan ((float)(x))
84              #endif
85              float x;]],
86            [[return isnanf (x);]])],
87         [gl_cv_func_isnanf_no_libm=yes],
88         [gl_cv_func_isnanf_no_libm=no])
89     ])
90 ])
91
92 dnl Test whether isnanf() can be used with libm.
93 AC_DEFUN([gl_HAVE_ISNANF_IN_LIBM],
94 [
95   AC_CACHE_CHECK([whether isnan(float) can be used with libm],
96     [gl_cv_func_isnanf_in_libm],
97     [
98       save_LIBS="$LIBS"
99       LIBS="$LIBS -lm"
100       AC_LINK_IFELSE(
101         [AC_LANG_PROGRAM(
102            [[#include <math.h>
103              #if __GNUC__ >= 4
104              # undef isnanf
105              # define isnanf(x) __builtin_isnanf ((float)(x))
106              #elif defined isnan
107              # undef isnanf
108              # define isnanf(x) isnan ((float)(x))
109              #endif
110              float x;]],
111            [[return isnanf (x);]])],
112         [gl_cv_func_isnanf_in_libm=yes],
113         [gl_cv_func_isnanf_in_libm=no])
114       LIBS="$save_LIBS"
115     ])
116 ])
117
118 dnl Test whether isnanf() rejects Infinity (this fails on Solaris 2.5.1),
119 dnl recognizes a NaN (this fails on IRIX 6.5 with cc), and recognizes a NaN
120 dnl with in-memory representation 0x7fbfffff (this fails on IRIX 6.5).
121 AC_DEFUN([gl_ISNANF_WORKS],
122 [
123   AC_REQUIRE([AC_PROG_CC])
124   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
125   AC_REQUIRE([gl_FLOAT_EXPONENT_LOCATION])
126   AC_CACHE_CHECK([whether isnan(float) works], [gl_cv_func_isnanf_works],
127     [
128       AC_RUN_IFELSE(
129         [AC_LANG_SOURCE([[
130 #include <math.h>
131 #if __GNUC__ >= 4
132 # undef isnanf
133 # define isnanf(x) __builtin_isnanf ((float)(x))
134 #elif defined isnan
135 # undef isnanf
136 # define isnanf(x) isnan ((float)(x))
137 #endif
138 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
139 #ifdef __DECC
140 static float
141 NaN ()
142 {
143   static float zero = 0.0f;
144   return zero / zero;
145 }
146 #else
147 # define NaN() (0.0f / 0.0f)
148 #endif
149 #define NWORDS \
150   ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
151 typedef union { unsigned int word[NWORDS]; float value; } memory_float;
152 int main()
153 {
154   int result = 0;
155
156   if (isnanf (1.0f / 0.0f))
157     result |= 1;
158
159   if (!isnanf (NaN ()))
160     result |= 2;
161
162 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
163   /* The isnanf function should be immune against changes in the sign bit and
164      in the mantissa bits.  The xor operation twiddles a bit that can only be
165      a sign bit or a mantissa bit.  */
166   if (FLT_EXPBIT0_WORD == 0 && FLT_EXPBIT0_BIT > 0)
167     {
168       memory_float m;
169
170       m.value = NaN ();
171       /* Set the bits below the exponent to 01111...111.  */
172       m.word[0] &= -1U << FLT_EXPBIT0_BIT;
173       m.word[0] |= 1U << (FLT_EXPBIT0_BIT - 1) - 1;
174       if (!isnanf (m.value))
175         result |= 4;
176     }
177 #endif
178
179   return result;
180 }]])],
181         [gl_cv_func_isnanf_works=yes],
182         [gl_cv_func_isnanf_works=no],
183         [case "$host_os" in
184            irix* | solaris*) gl_cv_func_isnanf_works="guessing no";;
185            *)                gl_cv_func_isnanf_works="guessing yes";;
186          esac
187         ])
188     ])
189 ])