2 dnl Copyright (C) 2007-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_ROUNDF],
9 m4_divert_text([DEFAULTS], [gl_roundf_required=plain])
10 AC_REQUIRE([gl_MATH_H_DEFAULTS])
11 dnl Persuade glibc <math.h> to declare roundf().
12 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13 AC_CHECK_DECLS([roundf], , , [[#include <math.h>]])
14 if test "$ac_cv_have_decl_roundf" = yes; then
15 gl_CHECK_MATH_LIB([ROUNDF_LIBM], [x = roundf (x);])
16 if test "$ROUNDF_LIBM" != missing; then
17 dnl Test whether roundf() produces correct results. On mingw, for
18 dnl x = 1/2 - 2^-25, the system's roundf() returns a wrong result.
19 AC_REQUIRE([AC_PROG_CC])
20 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
21 AC_CACHE_CHECK([whether roundf works], [gl_cv_func_roundf_works],
24 LIBS="$LIBS $ROUNDF_LIBM"
25 AC_RUN_IFELSE([AC_LANG_SOURCE([[
29 # pragma fenv_access (off)
34 static const float TWO_MANT_DIG =
35 /* Assume FLT_MANT_DIG <= 3 * 31.
36 Use the identity n = floor(n/3) + floor((n+1)/3) + floor((n+2)/3). */
37 (float) (1U << (FLT_MANT_DIG / 3))
38 * (float) (1U << ((FLT_MANT_DIG + 1) / 3))
39 * (float) (1U << ((FLT_MANT_DIG + 2) / 3));
40 volatile float x = 0.5f - 0.5f / TWO_MANT_DIG;
41 exit (x < 0.5f && roundf (x) != 0.0f);
42 }]])], [gl_cv_func_roundf_works=yes], [gl_cv_func_roundf_works=no],
44 mingw*) gl_cv_func_roundf_works="guessing no";;
45 *) gl_cv_func_roundf_works="guessing yes";;
50 case "$gl_cv_func_roundf_works" in
51 *no) ROUNDF_LIBM=missing ;;
54 if test "$ROUNDF_LIBM" = missing; then
57 m4_ifdef([gl_FUNC_ROUNDF_IEEE], [
58 if test $gl_roundf_required = ieee && test $REPLACE_ROUNDF = 0; then
59 AC_CACHE_CHECK([whether roundf works according to ISO C 99 with IEC 60559],
60 [gl_cv_func_roundf_ieee],
63 LIBS="$LIBS $ROUNDF_LIBM"
66 #ifndef __NO_MATH_INLINES
67 # define __NO_MATH_INLINES 1 /* for glibc */
70 ]gl_FLOAT_MINUS_ZERO_CODE[
71 ]gl_FLOAT_SIGNBIT_CODE[
72 static float dummy (float f) { return 0; }
73 int main (int argc, char *argv[])
75 float (*my_roundf) (float) = argc ? roundf : dummy;
77 /* Test whether roundf (-0.0f) is -0.0f. */
78 if (signbitf (minus_zerof) && !signbitf (my_roundf (minus_zerof)))
80 /* Test whether roundf (-0.3f) is -0.0f. */
81 if (signbitf (-0.3f) && !signbitf (my_roundf (-0.3f)))
86 [gl_cv_func_roundf_ieee=yes],
87 [gl_cv_func_roundf_ieee=no],
88 [gl_cv_func_roundf_ieee="guessing no"])
91 case "$gl_cv_func_roundf_ieee" in
93 *) REPLACE_ROUNDF=1 ;;
100 if test $HAVE_DECL_ROUNDF = 0 || test $REPLACE_ROUNDF = 1; then
101 dnl Find libraries needed to link lib/roundf.c.
102 AC_CHECK_DECLS([ceilf, floorf], , , [[#include <math.h>]])
103 if test "$ac_cv_have_decl_floorf" = yes \
104 && test "$ac_cv_have_decl_ceilf" = yes; then
107 if test "$FLOORF_LIBM" != '?' && test "$CEILF_LIBM" != '?'; then
108 AC_DEFINE([HAVE_FLOORF_AND_CEILF], [1],
109 [Define if the both the floorf() and ceilf() functions exist.])
111 dnl Append $FLOORF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
112 case " $ROUNDF_LIBM " in
113 *" $FLOORF_LIBM "*) ;;
114 *) ROUNDF_LIBM="$ROUNDF_LIBM $FLOORF_LIBM" ;;
116 dnl Append $CEILF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
117 case " $ROUNDF_LIBM " in
118 *" $CEILF_LIBM "*) ;;
119 *) ROUNDF_LIBM="$ROUNDF_LIBM $CEILF_LIBM" ;;
128 AC_SUBST([ROUNDF_LIBM])