immutable: Add tests.
[gnulib.git] / m4 / hypotf.m4
blob122b18f74044b4c7abd1e9765f58bc64ad349b46
1 # hypotf.m4 serial 10
2 dnl Copyright (C) 2012-2021 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_FUNC_HYPOTF],
9   m4_divert_text([DEFAULTS], [gl_hypotf_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_HYPOT])
13   dnl Persuade glibc <math.h> to declare hypotf().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   dnl Test whether hypotf() exists. Assume that hypotf(), if it exists, is
17   dnl defined in the same library as hypot().
18   save_LIBS="$LIBS"
19   LIBS="$LIBS $HYPOT_LIBM"
20   AC_CHECK_FUNCS([hypotf])
21   LIBS="$save_LIBS"
22   if test $ac_cv_func_hypotf = yes; then
23     HYPOTF_LIBM="$HYPOT_LIBM"
25     save_LIBS="$LIBS"
26     LIBS="$LIBS $HYPOTF_LIBM"
27     gl_FUNC_HYPOTF_WORKS
28     LIBS="$save_LIBS"
29     case "$gl_cv_func_hypotf_works" in
30       *yes) ;;
31       *) REPLACE_HYPOTF=1 ;;
32     esac
34     m4_ifdef([gl_FUNC_HYPOTF_IEEE], [
35       if test $gl_hypotf_required = ieee && test $REPLACE_HYPOTF = 0; then
36         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
37         AC_CACHE_CHECK([whether hypotf works according to ISO C 99 with IEC 60559],
38           [gl_cv_func_hypotf_ieee],
39           [
40             save_LIBS="$LIBS"
41             LIBS="$LIBS $HYPOTF_LIBM"
42             AC_RUN_IFELSE(
43               [AC_LANG_SOURCE([[
44 #ifndef __NO_MATH_INLINES
45 # define __NO_MATH_INLINES 1 /* for glibc */
46 #endif
47 #include <math.h>
48 /* Compare two numbers with ==.
49    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
50    'x == x' test.  */
51 static int
52 numeric_equal (float x, float y)
54   return x == y;
56 static float dummy (float x, float y) { return 0; }
57 float zero;
58 float one = 1.0f;
59 int main (int argc, char *argv[])
61   float (* volatile my_hypotf) (float, float) = argc ? hypotf : dummy;
62   float f;
63   /* Test hypotf(NaN,Infinity).
64      This test fails on OSF/1 5.1 and native Windows.  */
65   f = my_hypotf (zero / zero, one / zero);
66   if (!numeric_equal (f, f))
67     return 1;
68   return 0;
70               ]])],
71               [gl_cv_func_hypotf_ieee=yes],
72               [gl_cv_func_hypotf_ieee=no],
73               [case "$host_os" in
74                                 # Guess yes on glibc systems.
75                  *-gnu* | gnu*) gl_cv_func_hypotf_ieee="guessing yes" ;;
76                                 # Guess yes on musl systems.
77                  *-musl*)       gl_cv_func_hypotf_ieee="guessing yes" ;;
78                                 # Guess yes on native Windows.
79                  mingw*)        gl_cv_func_hypotf_ieee="guessing yes" ;;
80                                 # If we don't know, obey --enable-cross-guesses.
81                  *)             gl_cv_func_hypotf_ieee="$gl_cross_guess_normal" ;;
82                esac
83               ])
84             LIBS="$save_LIBS"
85           ])
86         case "$gl_cv_func_hypotf_ieee" in
87           *yes) ;;
88           *) REPLACE_HYPOTF=1 ;;
89         esac
90       fi
91     ])
92   else
93     HAVE_HYPOTF=0
94     dnl If the function is declared but does not appear to exist, it may be
95     dnl defined as an inline function. In order to avoid a conflict, we have
96     dnl to define rpl_hypotf, not hypotf.
97     AC_CHECK_DECLS([hypotf], [REPLACE_HYPOTF=1], , [[#include <math.h>]])
98   fi
99   if test $HAVE_HYPOTF = 0 || test $REPLACE_HYPOTF = 1; then
100     dnl Find libraries needed to link lib/hypotf.c.
101     HYPOTF_LIBM="$HYPOT_LIBM"
102   fi
103   AC_SUBST([HYPOTF_LIBM])
106 dnl Test whether hypotf() works.
107 dnl It returns wrong values on NetBSD 5.1/x86_64 and OpenBSD 4.9/x86.
108 AC_DEFUN([gl_FUNC_HYPOTF_WORKS],
110   AC_REQUIRE([AC_PROG_CC])
111   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
112   AC_CACHE_CHECK([whether hypotf works], [gl_cv_func_hypotf_works],
113     [
114       AC_RUN_IFELSE(
115         [AC_LANG_SOURCE([[
116 #include <float.h>
117 #include <math.h>
118 volatile float x;
119 volatile float y;
120 volatile float z;
121 int main ()
123   int result = 0;
124   /* This test fails on NetBSD 5.1.  */
125   {
126     x = FLT_MIN * 2.0f;
127     y = FLT_MIN * 3.0f;
128     z = hypotf (x, y);
129     if (!(z >= FLT_MIN * 2.0f && z <= FLT_MIN * 4.0f))
130       result |= 1;
131   }
132   /* This test fails on OpenBSD 4.9.  */
133   {
134     x = FLT_MAX;
135     y = FLT_MAX * 0.5f;
136     z = hypotf (x, y);
137     if (!(z > 0 && z == z + z))
138       result |= 2;
139   }
140   return result;
142 ]])],
143         [gl_cv_func_hypotf_works=yes],
144         [gl_cv_func_hypotf_works=no],
145         [case "$host_os" in
146            netbsd* | openbsd*) gl_cv_func_hypotf_works="guessing no" ;;
147                                # Guess yes on native Windows.
148            mingw*)             gl_cv_func_hypotf_works="guessing yes" ;;
149            *)                  gl_cv_func_hypotf_works="guessing yes" ;;
150          esac
151         ])
152     ])