math: Ensure declarations of math functions.
[gnulib.git] / m4 / hypotl.m4
1 # hypotl.m4 serial 3
2 dnl Copyright (C) 2012 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.
6
7 AC_DEFUN([gl_FUNC_HYPOTL],
8 [
9   m4_divert_text([DEFAULTS], [gl_hypotl_required=plain])
10   AC_REQUIRE([gl_MATH_H_DEFAULTS])
11   AC_REQUIRE([gl_FUNC_HYPOT])
12
13   dnl Persuade glibc <math.h> to declare hypotl().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15
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"
24     m4_ifdef([gl_FUNC_HYPOTL_IEEE], [
25       if test $gl_hypotl_required = ieee && test $REPLACE_HYPOTL = 0; then
26         AC_CACHE_CHECK([whether hypotl works according to ISO C 99 with IEC 60559],
27           [gl_cv_func_hypotl_ieee],
28           [
29             save_LIBS="$LIBS"
30             LIBS="$LIBS $HYPOTL_LIBM"
31             AC_RUN_IFELSE(
32               [AC_LANG_SOURCE([[
33 #ifndef __NO_MATH_INLINES
34 # define __NO_MATH_INLINES 1 /* for glibc */
35 #endif
36 #include <math.h>
37 /* Compare two numbers with ==.
38    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
39    'x == x' test.  */
40 static int
41 numeric_equal (long double x, long double y)
42 {
43   return x == y;
44 }
45 static long double dummy (long double x, long double y) { return 0; }
46 long double zero;
47 long double one = 1.0L;
48 int main (int argc, char *argv[])
49 {
50   long double (*my_hypotl) (long double, long double) = argc ? hypotl : dummy;
51   long double f;
52   /* Test hypotl(NaN,Infinity).
53      This test fails on OSF/1 5.1 and native Windows.  */
54   f = my_hypotl (zero / zero, one / zero);
55   if (!numeric_equal (f, f))
56     return 1;
57   return 0;
58 }
59               ]])],
60               [gl_cv_func_hypotl_ieee=yes],
61               [gl_cv_func_hypotl_ieee=no],
62               [gl_cv_func_hypotl_ieee="guessing no"])
63             LIBS="$save_LIBS"
64           ])
65         case "$gl_cv_func_hypotl_ieee" in
66           *yes) ;;
67           *) REPLACE_HYPOTL=1 ;;
68         esac
69       fi
70     ])
71   else
72     HAVE_HYPOTL=0
73   fi
74   if test $HAVE_HYPOTL = 0 || test $REPLACE_HYPOTL = 1; then
75     dnl Find libraries needed to link lib/hypotl.c.
76     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
77       HYPOTL_LIBM="$HYPOT_LIBM"
78     else
79       AC_REQUIRE([gl_FUNC_FABSL])
80       AC_REQUIRE([gl_FUNC_FREXPL])
81       AC_REQUIRE([gl_FUNC_LDEXPL])
82       AC_REQUIRE([gl_FUNC_SQRTL])
83       HYPOTL_LIBM=
84       dnl Append $FABSL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
85       case " $HYPOTL_LIBM " in
86         *" $FABSL_LIBM "*) ;;
87         *) HYPOTL_LIBM="$HYPOTL_LIBM $FABSL_LIBM" ;;
88       esac
89       dnl Append $FREXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
90       case " $HYPOTL_LIBM " in
91         *" $FREXPL_LIBM "*) ;;
92         *) HYPOTL_LIBM="$HYPOTL_LIBM $FREXPL_LIBM" ;;
93       esac
94       dnl Append $LDEXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
95       case " $HYPOTL_LIBM " in
96         *" $LDEXPL_LIBM "*) ;;
97         *) HYPOTL_LIBM="$HYPOTL_LIBM $LDEXPL_LIBM" ;;
98       esac
99       dnl Append $SQRTL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
100       case " $HYPOTL_LIBM " in
101         *" $SQRTL_LIBM "*) ;;
102         *) HYPOTL_LIBM="$HYPOTL_LIBM $SQRTL_LIBM" ;;
103       esac
104     fi
105   fi
106   AC_SUBST([HYPOTL_LIBM])
107 ])