2 dnl Copyright (C) 2007, 2009-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.
7 AC_DEFUN([gl_FUNC_ROUND],
9 m4_divert_text([DEFAULTS], [gl_round_required=plain])
10 AC_REQUIRE([gl_MATH_H_DEFAULTS])
12 dnl Persuade glibc <math.h> to declare round().
13 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15 gl_CHECK_MATH_LIB([ROUND_LIBM], [x = round (x);],
20 double round (double);
22 if test "$ROUND_LIBM" != missing; then
24 dnl Also check whether it's declared.
25 dnl IRIX 6.5 has round() in libm but doesn't declare it in <math.h>.
26 AC_CHECK_DECLS([round], , [HAVE_DECL_ROUND=0], [[#include <math.h>]])
28 dnl Test whether round() produces correct results. On NetBSD 3.0, for
29 dnl x = 1/2 - 2^-54, the system's round() returns a wrong result.
30 AC_REQUIRE([AC_PROG_CC])
31 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
32 AC_CACHE_CHECK([whether round works], [gl_cv_func_round_works],
35 LIBS="$LIBS $ROUND_LIBM"
36 AC_RUN_IFELSE([AC_LANG_SOURCE([[
43 double round (double);
45 # pragma fenv_access (off)
50 static const double TWO_MANT_DIG =
51 /* Assume DBL_MANT_DIG <= 5 * 31.
53 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
54 (double) (1U << (DBL_MANT_DIG / 5))
55 * (double) (1U << ((DBL_MANT_DIG + 1) / 5))
56 * (double) (1U << ((DBL_MANT_DIG + 2) / 5))
57 * (double) (1U << ((DBL_MANT_DIG + 3) / 5))
58 * (double) (1U << ((DBL_MANT_DIG + 4) / 5));
59 volatile double x = 0.5 - 0.5 / TWO_MANT_DIG;
60 exit (x < 0.5 && round (x) != 0.0);
61 }]])], [gl_cv_func_round_works=yes], [gl_cv_func_round_works=no],
63 netbsd* | aix*) gl_cv_func_round_works="guessing no";;
64 *) gl_cv_func_round_works="guessing yes";;
69 case "$gl_cv_func_round_works" in
70 *no) REPLACE_ROUND=1 ;;
73 m4_ifdef([gl_FUNC_ROUND_IEEE], [
74 if test $gl_round_required = ieee && test $REPLACE_ROUND = 0; then
75 AC_CACHE_CHECK([whether round works according to ISO C 99 with IEC 60559],
76 [gl_cv_func_round_ieee],
79 LIBS="$LIBS $ROUND_LIBM"
82 #ifndef __NO_MATH_INLINES
83 # define __NO_MATH_INLINES 1 /* for glibc */
90 double round (double);
91 ]gl_DOUBLE_MINUS_ZERO_CODE[
92 ]gl_DOUBLE_SIGNBIT_CODE[
93 static double dummy (double f) { return 0; }
94 int main (int argc, char *argv[])
96 double (*my_round) (double) = argc ? round : dummy;
97 /* Test whether round (-0.0) is -0.0. */
98 if (signbitd (minus_zerod) && !signbitd (my_round (minus_zerod)))
103 [gl_cv_func_round_ieee=yes],
104 [gl_cv_func_round_ieee=no],
105 [gl_cv_func_round_ieee="guessing no"])
108 case "$gl_cv_func_round_ieee" in
110 *) REPLACE_ROUND=1 ;;
118 if test $HAVE_ROUND = 0 || test $REPLACE_ROUND = 1; then
119 dnl Find libraries needed to link lib/round.c.
123 dnl Append $FLOOR_LIBM to ROUND_LIBM, avoiding gratuitous duplicates.
124 case " $ROUND_LIBM " in
125 *" $FLOOR_LIBM "*) ;;
126 *) ROUND_LIBM="$ROUND_LIBM $FLOOR_LIBM" ;;
128 dnl Append $CEIL_LIBM to ROUND_LIBM, avoiding gratuitous duplicates.
129 case " $ROUND_LIBM " in
131 *) ROUND_LIBM="$ROUND_LIBM $CEIL_LIBM" ;;
134 AC_SUBST([ROUND_LIBM])