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