immutable: Add tests.
[gnulib.git] / m4 / hypotl.m4
blob465e1fbc4f450eedac4efc7aa4d415a2382945af
1 # hypotl.m4 serial 11
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_HYPOTL],
9   m4_divert_text([DEFAULTS], [gl_hypotl_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_HYPOT])
13   dnl Persuade glibc <math.h> to declare hypotl().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   dnl Test whether hypotl() exists. Assume that hypotl(), 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([hypotl])
21   LIBS="$save_LIBS"
22   if test $ac_cv_func_hypotl = yes; then
23     HYPOTL_LIBM="$HYPOT_LIBM"
25     save_LIBS="$LIBS"
26     LIBS="$LIBS $HYPOTL_LIBM"
27     gl_FUNC_HYPOTL_WORKS
28     LIBS="$save_LIBS"
29     case "$gl_cv_func_hypotl_works" in
30       *yes) ;;
31       *) REPLACE_HYPOTL=1 ;;
32     esac
34     m4_ifdef([gl_FUNC_HYPOTL_IEEE], [
35       if test $gl_hypotl_required = ieee && test $REPLACE_HYPOTL = 0; then
36         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
37         AC_CACHE_CHECK([whether hypotl works according to ISO C 99 with IEC 60559],
38           [gl_cv_func_hypotl_ieee],
39           [
40             save_LIBS="$LIBS"
41             LIBS="$LIBS $HYPOTL_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 (long double x, long double y)
54   return x == y;
56 static long double dummy (long double x, long double y) { return 0; }
57 long double zero;
58 long double one = 1.0L;
59 int main (int argc, char *argv[])
61   long double (* volatile my_hypotl) (long double, long double) = argc ? hypotl : dummy;
62   long double f;
63   /* Test hypotl(NaN,Infinity).
64      This test fails on OSF/1 5.1 and native Windows.  */
65   f = my_hypotl (zero / zero, one / zero);
66   if (!numeric_equal (f, f))
67     return 1;
68   return 0;
70               ]])],
71               [gl_cv_func_hypotl_ieee=yes],
72               [gl_cv_func_hypotl_ieee=no],
73               [case "$host_os" in
74                                 # Guess yes on glibc systems.
75                  *-gnu* | gnu*) gl_cv_func_hypotl_ieee="guessing yes" ;;
76                                 # Guess yes on musl systems.
77                  *-musl*)       gl_cv_func_hypotl_ieee="guessing yes" ;;
78                                 # Guess yes on native Windows.
79                  mingw*)        gl_cv_func_hypotl_ieee="guessing yes" ;;
80                                 # If we don't know, obey --enable-cross-guesses.
81                  *)             gl_cv_func_hypotl_ieee="$gl_cross_guess_normal" ;;
82                esac
83               ])
84             LIBS="$save_LIBS"
85           ])
86         case "$gl_cv_func_hypotl_ieee" in
87           *yes) ;;
88           *) REPLACE_HYPOTL=1 ;;
89         esac
90       fi
91     ])
92   else
93     HAVE_HYPOTL=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_hypotl, not hypotl.
97     AC_CHECK_DECLS([hypotl], [REPLACE_HYPOTL=1], , [[#include <math.h>]])
98   fi
99   if test $HAVE_HYPOTL = 0 || test $REPLACE_HYPOTL = 1; then
100     dnl Find libraries needed to link lib/hypotl.c.
101     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
102       HYPOTL_LIBM="$HYPOT_LIBM"
103     else
104       AC_REQUIRE([gl_FUNC_FABSL])
105       AC_REQUIRE([gl_FUNC_FREXPL])
106       AC_REQUIRE([gl_FUNC_LDEXPL])
107       AC_REQUIRE([gl_FUNC_SQRTL])
108       HYPOTL_LIBM=
109       dnl Append $FABSL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
110       case " $HYPOTL_LIBM " in
111         *" $FABSL_LIBM "*) ;;
112         *) HYPOTL_LIBM="$HYPOTL_LIBM $FABSL_LIBM" ;;
113       esac
114       dnl Append $FREXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
115       case " $HYPOTL_LIBM " in
116         *" $FREXPL_LIBM "*) ;;
117         *) HYPOTL_LIBM="$HYPOTL_LIBM $FREXPL_LIBM" ;;
118       esac
119       dnl Append $LDEXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
120       case " $HYPOTL_LIBM " in
121         *" $LDEXPL_LIBM "*) ;;
122         *) HYPOTL_LIBM="$HYPOTL_LIBM $LDEXPL_LIBM" ;;
123       esac
124       dnl Append $SQRTL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
125       case " $HYPOTL_LIBM " in
126         *" $SQRTL_LIBM "*) ;;
127         *) HYPOTL_LIBM="$HYPOTL_LIBM $SQRTL_LIBM" ;;
128       esac
129     fi
130   fi
131   AC_SUBST([HYPOTL_LIBM])
134 dnl Test whether hypotl() works.
135 dnl On OpenBSD 5.1/SPARC,
136 dnl hypotl (2.5541394760659556563446062497337725156L, 7.7893454113437840832487794525518765265L)
137 dnl has rounding errors that eat up the last 8 to 9 decimal digits.
138 AC_DEFUN([gl_FUNC_HYPOTL_WORKS],
140   AC_REQUIRE([AC_PROG_CC])
141   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
142   AC_CACHE_CHECK([whether hypotl works], [gl_cv_func_hypotl_works],
143     [
144       AC_RUN_IFELSE(
145         [AC_LANG_SOURCE([[
146 #include <float.h>
147 #include <math.h>
148 static long double
149 my_ldexpl (long double x, int d)
151   for (; d > 0; d--)
152     x *= 2.0L;
153   for (; d < 0; d++)
154     x *= 0.5L;
155   return x;
157 volatile long double x;
158 volatile long double y;
159 volatile long double z;
160 int main ()
162   long double err;
164   x = 2.5541394760659556563446062497337725156L;
165   y = 7.7893454113437840832487794525518765265L;
166   z = hypotl (x, y);
167   err = z * z - (x * x + y * y);
168   err = my_ldexpl (err, LDBL_MANT_DIG);
169   if (err < 0)
170     err = - err;
171   if (err > 1000.0L)
172     return 1;
173   return 0;
175 ]])],
176         [gl_cv_func_hypotl_works=yes],
177         [gl_cv_func_hypotl_works=no],
178         [case "$host_os" in
179            openbsd*) gl_cv_func_hypotl_works="guessing no" ;;
180                      # Guess yes on native Windows.
181            mingw*)   gl_cv_func_hypotl_works="guessing yes" ;;
182            *)        gl_cv_func_hypotl_works="guessing yes" ;;
183          esac
184         ])
185     ])