Refresh 01-gnulib-directory.patch
[gnulib.git] / m4 / hypotl.m4
1 # hypotl.m4 serial 4
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
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
33
34     m4_ifdef([gl_FUNC_HYPOTL_IEEE], [
35       if test $gl_hypotl_required = ieee && test $REPLACE_HYPOTL = 0; then
36         AC_CACHE_CHECK([whether hypotl works according to ISO C 99 with IEC 60559],
37           [gl_cv_func_hypotl_ieee],
38           [
39             save_LIBS="$LIBS"
40             LIBS="$LIBS $HYPOTL_LIBM"
41             AC_RUN_IFELSE(
42               [AC_LANG_SOURCE([[
43 #ifndef __NO_MATH_INLINES
44 # define __NO_MATH_INLINES 1 /* for glibc */
45 #endif
46 #include <math.h>
47 /* Compare two numbers with ==.
48    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
49    'x == x' test.  */
50 static int
51 numeric_equal (long double x, long double y)
52 {
53   return x == y;
54 }
55 static long double dummy (long double x, long double y) { return 0; }
56 long double zero;
57 long double one = 1.0L;
58 int main (int argc, char *argv[])
59 {
60   long double (*my_hypotl) (long double, long double) = argc ? hypotl : dummy;
61   long double f;
62   /* Test hypotl(NaN,Infinity).
63      This test fails on OSF/1 5.1 and native Windows.  */
64   f = my_hypotl (zero / zero, one / zero);
65   if (!numeric_equal (f, f))
66     return 1;
67   return 0;
68 }
69               ]])],
70               [gl_cv_func_hypotl_ieee=yes],
71               [gl_cv_func_hypotl_ieee=no],
72               [gl_cv_func_hypotl_ieee="guessing no"])
73             LIBS="$save_LIBS"
74           ])
75         case "$gl_cv_func_hypotl_ieee" in
76           *yes) ;;
77           *) REPLACE_HYPOTL=1 ;;
78         esac
79       fi
80     ])
81   else
82     HAVE_HYPOTL=0
83   fi
84   if test $HAVE_HYPOTL = 0 || test $REPLACE_HYPOTL = 1; then
85     dnl Find libraries needed to link lib/hypotl.c.
86     if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
87       HYPOTL_LIBM="$HYPOT_LIBM"
88     else
89       AC_REQUIRE([gl_FUNC_FABSL])
90       AC_REQUIRE([gl_FUNC_FREXPL])
91       AC_REQUIRE([gl_FUNC_LDEXPL])
92       AC_REQUIRE([gl_FUNC_SQRTL])
93       HYPOTL_LIBM=
94       dnl Append $FABSL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
95       case " $HYPOTL_LIBM " in
96         *" $FABSL_LIBM "*) ;;
97         *) HYPOTL_LIBM="$HYPOTL_LIBM $FABSL_LIBM" ;;
98       esac
99       dnl Append $FREXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
100       case " $HYPOTL_LIBM " in
101         *" $FREXPL_LIBM "*) ;;
102         *) HYPOTL_LIBM="$HYPOTL_LIBM $FREXPL_LIBM" ;;
103       esac
104       dnl Append $LDEXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
105       case " $HYPOTL_LIBM " in
106         *" $LDEXPL_LIBM "*) ;;
107         *) HYPOTL_LIBM="$HYPOTL_LIBM $LDEXPL_LIBM" ;;
108       esac
109       dnl Append $SQRTL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
110       case " $HYPOTL_LIBM " in
111         *" $SQRTL_LIBM "*) ;;
112         *) HYPOTL_LIBM="$HYPOTL_LIBM $SQRTL_LIBM" ;;
113       esac
114     fi
115   fi
116   AC_SUBST([HYPOTL_LIBM])
117 ])
118
119 dnl Test whether hypotl() works.
120 dnl On OpenBSD 5.1/SPARC,
121 dnl hypotl (2.5541394760659556563446062497337725156L, 7.7893454113437840832487794525518765265L)
122 dnl has rounding errors that eat up the last 8 to 9 decimal digits.
123 AC_DEFUN([gl_FUNC_HYPOTL_WORKS],
124 [
125   AC_REQUIRE([AC_PROG_CC])
126   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
127   AC_CACHE_CHECK([whether hypotl works], [gl_cv_func_hypotl_works],
128     [
129       AC_RUN_IFELSE(
130         [AC_LANG_SOURCE([[
131 #include <float.h>
132 #include <math.h>
133 static long double
134 my_ldexpl (long double x, int d)
135 {
136   for (; d > 0; d--)
137     x *= 2.0L;
138   for (; d < 0; d++)
139     x *= 0.5L;
140   return x;
141 }
142 volatile long double x;
143 volatile long double y;
144 volatile long double z;
145 int main ()
146 {
147   long double err;
148
149   x = 2.5541394760659556563446062497337725156L;
150   y = 7.7893454113437840832487794525518765265L;
151   z = hypotl (x, y);
152   err = z * z - (x * x + y * y);
153   err = my_ldexpl (err, LDBL_MANT_DIG);
154   if (err < 0)
155     err = - err;
156   if (err > 1000.0L)
157     return 1;
158   return 0;
159 }
160 ]])],
161         [gl_cv_func_hypotl_works=yes],
162         [gl_cv_func_hypotl_works=no],
163         [case "$host_os" in
164            openbsd*) gl_cv_func_hypotl_works="guessing no";;
165            *)        gl_cv_func_hypotl_works="guessing yes";;
166          esac
167         ])
168     ])
169 ])