1# hypotf.m4 serial 10
2dnl Copyright (C) 2012-2021 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_HYPOTF],
8[
9  m4_divert_text([DEFAULTS], [gl_hypotf_required=plain])
10  AC_REQUIRE([gl_MATH_H_DEFAULTS])
11  AC_REQUIRE([gl_FUNC_HYPOT])
12
13  dnl Persuade glibc <math.h> to declare hypotf().
14  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15
16  dnl Test whether hypotf() exists. Assume that hypotf(), 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([hypotf])
21  LIBS="$save_LIBS"
22  if test $ac_cv_func_hypotf = yes; then
23    HYPOTF_LIBM="$HYPOT_LIBM"
24
25    save_LIBS="$LIBS"
26    LIBS="$LIBS $HYPOTF_LIBM"
27    gl_FUNC_HYPOTF_WORKS
28    LIBS="$save_LIBS"
29    case "$gl_cv_func_hypotf_works" in
30      *yes) ;;
31      *) REPLACE_HYPOTF=1 ;;
32    esac
33
34    m4_ifdef([gl_FUNC_HYPOTF_IEEE], [
35      if test $gl_hypotf_required = ieee && test $REPLACE_HYPOTF = 0; then
36        AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
37        AC_CACHE_CHECK([whether hypotf works according to ISO C 99 with IEC 60559],
38          [gl_cv_func_hypotf_ieee],
39          [
40            save_LIBS="$LIBS"
41            LIBS="$LIBS $HYPOTF_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 (float x, float y)
53{
54  return x == y;
55}
56static float dummy (float x, float y) { return 0; }
57float zero;
58float one = 1.0f;
59int main (int argc, char *argv[])
60{
61  float (* volatile my_hypotf) (float, float) = argc ? hypotf : dummy;
62  float f;
63  /* Test hypotf(NaN,Infinity).
64     This test fails on OSF/1 5.1 and native Windows.  */
65  f = my_hypotf (zero / zero, one / zero);
66  if (!numeric_equal (f, f))
67    return 1;
68  return 0;
69}
70              ]])],
71              [gl_cv_func_hypotf_ieee=yes],
72              [gl_cv_func_hypotf_ieee=no],
73              [case "$host_os" in
74                                # Guess yes on glibc systems.
75                 *-gnu* | gnu*) gl_cv_func_hypotf_ieee="guessing yes" ;;
76                                # Guess yes on musl systems.
77                 *-musl*)       gl_cv_func_hypotf_ieee="guessing yes" ;;
78                                # Guess yes on native Windows.
79                 mingw*)        gl_cv_func_hypotf_ieee="guessing yes" ;;
80                                # If we don't know, obey --enable-cross-guesses.
81                 *)             gl_cv_func_hypotf_ieee="$gl_cross_guess_normal" ;;
82               esac
83              ])
84            LIBS="$save_LIBS"
85          ])
86        case "$gl_cv_func_hypotf_ieee" in
87          *yes) ;;
88          *) REPLACE_HYPOTF=1 ;;
89        esac
90      fi
91    ])
92  else
93    HAVE_HYPOTF=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_hypotf, not hypotf.
97    AC_CHECK_DECLS([hypotf], [REPLACE_HYPOTF=1], , [[#include <math.h>]])
98  fi
99  if test $HAVE_HYPOTF = 0 || test $REPLACE_HYPOTF = 1; then
100    dnl Find libraries needed to link lib/hypotf.c.
101    HYPOTF_LIBM="$HYPOT_LIBM"
102  fi
103  AC_SUBST([HYPOTF_LIBM])
104])
105
106dnl Test whether hypotf() works.
107dnl It returns wrong values on NetBSD 5.1/x86_64 and OpenBSD 4.9/x86.
108AC_DEFUN([gl_FUNC_HYPOTF_WORKS],
109[
110  AC_REQUIRE([AC_PROG_CC])
111  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
112  AC_CACHE_CHECK([whether hypotf works], [gl_cv_func_hypotf_works],
113    [
114      AC_RUN_IFELSE(
115        [AC_LANG_SOURCE([[
116#include <float.h>
117#include <math.h>
118volatile float x;
119volatile float y;
120volatile float z;
121int main ()
122{
123  int result = 0;
124  /* This test fails on NetBSD 5.1.  */
125  {
126    x = FLT_MIN * 2.0f;
127    y = FLT_MIN * 3.0f;
128    z = hypotf (x, y);
129    if (!(z >= FLT_MIN * 2.0f && z <= FLT_MIN * 4.0f))
130      result |= 1;
131  }
132  /* This test fails on OpenBSD 4.9.  */
133  {
134    x = FLT_MAX;
135    y = FLT_MAX * 0.5f;
136    z = hypotf (x, y);
137    if (!(z > 0 && z == z + z))
138      result |= 2;
139  }
140  return result;
141}
142]])],
143        [gl_cv_func_hypotf_works=yes],
144        [gl_cv_func_hypotf_works=no],
145        [case "$host_os" in
146           netbsd* | openbsd*) gl_cv_func_hypotf_works="guessing no" ;;
147                               # Guess yes on native Windows.
148           mingw*)             gl_cv_func_hypotf_works="guessing yes" ;;
149           *)                  gl_cv_func_hypotf_works="guessing yes" ;;
150         esac
151        ])
152    ])
153])
154