3 dnl Copyright (C) 2007, 2009-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_ROUND],
10 m4_divert_text([DEFAULTS], [gl_round_required=plain])
11 AC_REQUIRE([gl_MATH_H_DEFAULTS])
13 dnl Persuade glibc <math.h> to declare round().
14 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16 gl_CHECK_MATH_LIB([ROUND_LIBM], [double], [x = round (x);],
21 double round (double);
23 if test "$ROUND_LIBM" != missing; then
25 dnl Also check whether it's declared.
26 dnl IRIX 6.5 has round() in libm but doesn't declare it in <math.h>.
27 AC_CHECK_DECLS([round], , [HAVE_DECL_ROUND=0], [[#include <math.h>]])
29 dnl Test whether round() produces correct results. On NetBSD 3.0, for
30 dnl x = 1/2 - 2^-54, the system's round() returns a wrong result.
31 AC_REQUIRE([AC_PROG_CC])
32 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
33 AC_CACHE_CHECK([whether round works], [gl_cv_func_round_works],
36 LIBS="$LIBS $ROUND_LIBM"
37 AC_RUN_IFELSE([AC_LANG_SOURCE([[
44 double round (double);
45 #if defined _MSC_VER && !defined __clang__
46 # pragma fenv_access (off)
51 static const double TWO_MANT_DIG =
52 /* Assume DBL_MANT_DIG <= 5 * 31.
54 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
55 (double) (1U << (DBL_MANT_DIG / 5))
56 * (double) (1U << ((DBL_MANT_DIG + 1) / 5))
57 * (double) (1U << ((DBL_MANT_DIG + 2) / 5))
58 * (double) (1U << ((DBL_MANT_DIG + 3) / 5))
59 * (double) (1U << ((DBL_MANT_DIG + 4) / 5));
60 volatile double x = 0.5 - 0.5 / TWO_MANT_DIG;
61 return (x < 0.5 && round (x) != 0.0);
62 }]])], [gl_cv_func_round_works=yes], [gl_cv_func_round_works=no],
64 netbsd* | aix*) gl_cv_func_round_works="guessing no" ;;
65 # Guess yes on MSVC, no on mingw.
66 windows*-msvc*) gl_cv_func_round_works="guessing yes" ;;
67 mingw* | windows*) AC_EGREP_CPP([Known], [
72 [gl_cv_func_round_works="guessing yes"],
73 [gl_cv_func_round_works="guessing no"])
75 *) gl_cv_func_round_works="guessing yes" ;;
80 case "$gl_cv_func_round_works" in
81 *no) REPLACE_ROUND=1 ;;
84 m4_ifdef([gl_FUNC_ROUND_IEEE], [
85 if test $gl_round_required = ieee && test $REPLACE_ROUND = 0; then
86 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
87 AC_CACHE_CHECK([whether round works according to ISO C 99 with IEC 60559],
88 [gl_cv_func_round_ieee],
91 LIBS="$LIBS $ROUND_LIBM"
94 #ifndef __NO_MATH_INLINES
95 # define __NO_MATH_INLINES 1 /* for glibc */
102 double round (double);
103 ]gl_DOUBLE_MINUS_ZERO_CODE[
104 ]gl_DOUBLE_SIGNBIT_CODE[
105 static double dummy (double f) { return 0; }
106 int main (int argc, char *argv[])
108 double (* volatile my_round) (double) = argc ? round : dummy;
109 /* Test whether round (-0.0) is -0.0. */
110 if (signbitd (minus_zerod) && !signbitd (my_round (minus_zerod)))
115 [gl_cv_func_round_ieee=yes],
116 [gl_cv_func_round_ieee=no],
118 # Guess yes on glibc systems.
119 *-gnu* | gnu*) gl_cv_func_round_ieee="guessing yes" ;;
120 # Guess yes on musl systems.
121 *-musl* | midipix*) gl_cv_func_round_ieee="guessing yes" ;;
122 # Guess yes on MSVC, no on mingw.
123 windows*-msvc*) gl_cv_func_round_ieee="guessing yes" ;;
124 mingw* | windows*) AC_EGREP_CPP([Known], [
129 [gl_cv_func_round_ieee="guessing yes"],
130 [gl_cv_func_round_ieee="guessing no"])
132 # If we don't know, obey --enable-cross-guesses.
133 *) gl_cv_func_round_ieee="$gl_cross_guess_normal" ;;
138 case "$gl_cv_func_round_ieee" in
140 *) REPLACE_ROUND=1 ;;
148 if test $HAVE_ROUND = 0 || test $REPLACE_ROUND = 1; then
149 dnl Find libraries needed to link lib/round.c.
153 dnl Append $FLOOR_LIBM to ROUND_LIBM, avoiding gratuitous duplicates.
154 case " $ROUND_LIBM " in
155 *" $FLOOR_LIBM "*) ;;
156 *) ROUND_LIBM="$ROUND_LIBM $FLOOR_LIBM" ;;
158 dnl Append $CEIL_LIBM to ROUND_LIBM, avoiding gratuitous duplicates.
159 case " $ROUND_LIBM " in
161 *) ROUND_LIBM="$ROUND_LIBM $CEIL_LIBM" ;;
164 AC_SUBST([ROUND_LIBM])