Debian Codegraph @

m4/isnanf.m4

raw
1 # isnanf.m4
2 # serial 21
3 dnl Copyright (C) 2007-2025 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 dnl This file is offered as-is, without any warranty.
8
9 dnl Check how to get or define isnanf().
10
11 AC_DEFUN([gl_FUNC_ISNANF],
12 [
13 AC_REQUIRE([gl_MATH_H_DEFAULTS])
14 ISNANF_LIBM=
15 gl_HAVE_ISNANF_NO_LIBM
16 if test $gl_cv_func_isnanf_no_libm = no; then
17 gl_HAVE_ISNANF_IN_LIBM
18 if test $gl_cv_func_isnanf_in_libm = yes; then
19 ISNANF_LIBM=-lm
20 fi
21 fi
22 dnl The variable gl_func_isnanf set here is used by isnan.m4.
23 if test $gl_cv_func_isnanf_no_libm = yes || test -n "$ISNANF_LIBM"; then
24 saved_LIBS="$LIBS"
25 LIBS="$LIBS $ISNANF_LIBM"
26 gl_ISNANF_WORKS
27 LIBS="$saved_LIBS"
28 case "$gl_cv_func_isnanf_works" in
29 *yes) gl_func_isnanf=yes ;;
30 *) gl_func_isnanf=no; ISNANF_LIBM= ;;
31 esac
32 else
33 gl_func_isnanf=no
34 fi
35 if test $gl_func_isnanf != yes; then
36 HAVE_ISNANF=0
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 gl_func_isnanf_no_libm=yes
56 AC_DEFINE([HAVE_ISNANF_IN_LIBC], [1],
57 [Define if the isnan(float) function is available in libc.])
58 else
59 gl_func_isnanf_no_libm=no
60 fi
61 ])
62
63 dnl Prerequisites of replacement isnanf definition. It does not need -lm.
64 AC_DEFUN([gl_PREREQ_ISNANF],
65 [
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) || (__clang_major__ >= 4)
79 # undef isnanf
80 # define isnanf(x) __builtin_isnan ((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 saved_LIBS="$LIBS"
99 LIBS="$LIBS -lm"
100 AC_LINK_IFELSE(
101 [AC_LANG_PROGRAM(
102 [[#include <math.h>
103 #if (__GNUC__ >= 4) || (__clang_major__ >= 4)
104 # undef isnanf
105 # define isnanf(x) __builtin_isnan ((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="$saved_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) || (__clang_major__ >= 4)
132 # undef isnanf
133 # define isnanf(x) __builtin_isnan ((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 mingw* | windows*) # Guess yes on mingw, no on MSVC.
186 AC_EGREP_CPP([Known], [
187 #ifdef __MINGW32__
188 Known
189 #endif
190 ],
191 [gl_cv_func_isnanf_works="guessing yes"],
192 [gl_cv_func_isnanf_works="guessing no"])
193 ;;
194 *) gl_cv_func_isnanf_works="guessing yes" ;;
195 esac
196 ])
197 ])
198 ])