Work around roundf() bug on mingw.
[gnulib.git] / m4 / roundf.m4
1 # roundf.m4 serial 5
2 dnl Copyright (C) 2007-2008 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.
6
7 AC_DEFUN([gl_FUNC_ROUNDF],
8 [
9   AC_REQUIRE([gl_MATH_H_DEFAULTS])
10   dnl Persuade glibc <math.h> to declare roundf().
11   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
12   AC_CHECK_DECLS([roundf], , , [#include <math.h>])
13   if test "$ac_cv_have_decl_roundf" = yes; then
14     gl_CHECK_MATH_LIB([ROUNDF_LIBM], [x = roundf (x);])
15   fi
16   if test "$ac_cv_have_decl_roundf" = yes && 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],
22       [
23         save_LIBS="$LIBS"
24         LIBS="$LIBS $ROUNDF_LIBM"
25         AC_TRY_RUN([
26 #include <float.h>
27 #include <math.h>
28 int main()
29 {
30   /* 2^FLT_MANT_DIG.  */
31   static const float TWO_MANT_DIG =
32     /* Assume FLT_MANT_DIG <= 3 * 31.
33        Use the identity  n = floor(n/3) + floor((n+1)/3) + floor((n+2)/3).  */
34     (float) (1U << (FLT_MANT_DIG / 3))
35     * (float) (1U << ((FLT_MANT_DIG + 1) / 3))
36     * (float) (1U << ((FLT_MANT_DIG + 2) / 3));
37   volatile float x = 0.5f - 0.5f / TWO_MANT_DIG;
38   exit (x < 0.5f && roundf (x) != 0.0f);
39 }], [gl_cv_func_roundf_works=yes], [gl_cv_func_roundf_works=no],
40         [case "$host_os" in
41            mingw*) gl_cv_func_roundf_works="guessing no";;
42            *)      gl_cv_func_roundf_works="guessing yes";;
43          esac
44         ])
45         LIBS="$save_LIBS"
46       ])
47     case "$gl_cv_func_roundf_works" in
48       *no) ROUNDF_LIBM=missing ;;
49     esac
50   fi
51   if test "$ac_cv_have_decl_roundf" != yes || test "$ROUNDF_LIBM" = missing; then
52     REPLACE_ROUNDF=1
53     AC_LIBOBJ([roundf])
54     AC_CHECK_DECLS([ceilf, floorf], , , [#include <math.h>])
55     if test "$ac_cv_have_decl_floorf" = yes \
56        && test "$ac_cv_have_decl_ceilf" = yes; then
57       gl_FUNC_FLOORF_LIBS
58       gl_FUNC_CEILF_LIBS
59       if test "$FLOORF_LIBM" != '?' && test "$CEILF_LIBM" != '?'; then
60         AC_DEFINE([HAVE_FLOORF_AND_CEILF], 1,
61           [Define if the both the floorf() and ceilf() functions exist.])
62         ROUNDF_LIBM="$FLOORF_LIBM $CEILF_LIBM"
63       else
64         ROUNDF_LIBM=
65       fi
66     else
67       ROUNDF_LIBM=
68     fi
69   fi
70   AC_SUBST([ROUNDF_LIBM])
71 ])