1# hypotl.m4 serial 5
2dnl Copyright (C) 2012-2014 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_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_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.  */
51static int
52numeric_equal (long double x, long double y)
53{
54  return x == y;
55}
56static long double dummy (long double x, long double y) { return 0; }
57long double zero;
58long double one = 1.0L;
59int main (int argc, char *argv[])
60{
61  long double (*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;
69}
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*) gl_cv_func_hypotl_ieee="guessing yes" ;;
76                         # If we don't know, assume the worst.
77                 *)      gl_cv_func_hypotl_ieee="guessing no" ;;
78               esac
79              ])
80            LIBS="$save_LIBS"
81          ])
82        case "$gl_cv_func_hypotl_ieee" in
83          *yes) ;;
84          *) REPLACE_HYPOTL=1 ;;
85        esac
86      fi
87    ])
88  else
89    HAVE_HYPOTL=0
90  fi
91  if test $HAVE_HYPOTL = 0 || test $REPLACE_HYPOTL = 1; then
92    dnl Find libraries needed to link lib/hypotl.c.
93    if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
94      HYPOTL_LIBM="$HYPOT_LIBM"
95    else
96      AC_REQUIRE([gl_FUNC_FABSL])
97      AC_REQUIRE([gl_FUNC_FREXPL])
98      AC_REQUIRE([gl_FUNC_LDEXPL])
99      AC_REQUIRE([gl_FUNC_SQRTL])
100      HYPOTL_LIBM=
101      dnl Append $FABSL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
102      case " $HYPOTL_LIBM " in
103        *" $FABSL_LIBM "*) ;;
104        *) HYPOTL_LIBM="$HYPOTL_LIBM $FABSL_LIBM" ;;
105      esac
106      dnl Append $FREXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
107      case " $HYPOTL_LIBM " in
108        *" $FREXPL_LIBM "*) ;;
109        *) HYPOTL_LIBM="$HYPOTL_LIBM $FREXPL_LIBM" ;;
110      esac
111      dnl Append $LDEXPL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
112      case " $HYPOTL_LIBM " in
113        *" $LDEXPL_LIBM "*) ;;
114        *) HYPOTL_LIBM="$HYPOTL_LIBM $LDEXPL_LIBM" ;;
115      esac
116      dnl Append $SQRTL_LIBM to HYPOTL_LIBM, avoiding gratuitous duplicates.
117      case " $HYPOTL_LIBM " in
118        *" $SQRTL_LIBM "*) ;;
119        *) HYPOTL_LIBM="$HYPOTL_LIBM $SQRTL_LIBM" ;;
120      esac
121    fi
122  fi
123  AC_SUBST([HYPOTL_LIBM])
124])
125
126dnl Test whether hypotl() works.
127dnl On OpenBSD 5.1/SPARC,
128dnl hypotl (2.5541394760659556563446062497337725156L, 7.7893454113437840832487794525518765265L)
129dnl has rounding errors that eat up the last 8 to 9 decimal digits.
130AC_DEFUN([gl_FUNC_HYPOTL_WORKS],
131[
132  AC_REQUIRE([AC_PROG_CC])
133  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
134  AC_CACHE_CHECK([whether hypotl works], [gl_cv_func_hypotl_works],
135    [
136      AC_RUN_IFELSE(
137        [AC_LANG_SOURCE([[
138#include <float.h>
139#include <math.h>
140static long double
141my_ldexpl (long double x, int d)
142{
143  for (; d > 0; d--)
144    x *= 2.0L;
145  for (; d < 0; d++)
146    x *= 0.5L;
147  return x;
148}
149volatile long double x;
150volatile long double y;
151volatile long double z;
152int main ()
153{
154  long double err;
155
156  x = 2.5541394760659556563446062497337725156L;
157  y = 7.7893454113437840832487794525518765265L;
158  z = hypotl (x, y);
159  err = z * z - (x * x + y * y);
160  err = my_ldexpl (err, LDBL_MANT_DIG);
161  if (err < 0)
162    err = - err;
163  if (err > 1000.0L)
164    return 1;
165  return 0;
166}
167]])],
168        [gl_cv_func_hypotl_works=yes],
169        [gl_cv_func_hypotl_works=no],
170        [case "$host_os" in
171           openbsd*) gl_cv_func_hypotl_works="guessing no";;
172           *)        gl_cv_func_hypotl_works="guessing yes";;
173         esac
174        ])
175    ])
176])
177