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