floor*-ieee, ceil*-ieee, trunc*-ieee, round*-ieee: More robust checks.
[gnulib.git] / m4 / roundf.m4
1 # roundf.m4 serial 12
2 dnl Copyright (C) 2007-2011 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   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],
22         [
23           save_LIBS="$LIBS"
24           LIBS="$LIBS $ROUNDF_LIBM"
25           AC_RUN_IFELSE([AC_LANG_SOURCE([[
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 "$ROUNDF_LIBM" = missing; then
52       REPLACE_ROUNDF=1
53     fi
54     m4_ifdef([gl_FUNC_ROUNDF_IEEE], [
55       if test $gl_roundf_required = ieee && test $REPLACE_ROUNDF = 0; then
56         AC_CACHE_CHECK([whether roundf works according to ISO C 99 with IEC 60559],
57           [gl_cv_func_roundf_ieee],
58           [
59             save_LIBS="$LIBS"
60             LIBS="$LIBS $ROUNDF_LIBM"
61             AC_RUN_IFELSE(
62               [AC_LANG_SOURCE([[
63 #ifndef __NO_MATH_INLINES
64 # define __NO_MATH_INLINES 1 /* for glibc */
65 #endif
66 #include <math.h>
67 ]gl_FLOAT_MINUS_ZERO_CODE[
68 ]gl_FLOAT_SIGNBIT_CODE[
69 static float dummy (float f) { return 0; }
70 int main (int argc, char *argv[])
71 {
72   float (*my_roundf) (float) = argc ? roundf : dummy;
73   /* Test whether roundf (-0.0f) is -0.0f.  */
74   if (signbitf (minus_zerof) && !signbitf (my_roundf (minus_zerof)))
75     return 1;
76   return 0;
77 }
78               ]])],
79               [gl_cv_func_roundf_ieee=yes],
80               [gl_cv_func_roundf_ieee=no],
81               [gl_cv_func_roundf_ieee="guessing no"])
82             LIBS="$save_LIBS"
83           ])
84         case "$gl_cv_func_roundf_ieee" in
85           *yes) ;;
86           *) REPLACE_ROUNDF=1 ;;
87         esac
88       fi
89     ])
90   else
91     HAVE_DECL_ROUNDF=0
92   fi
93   if test $HAVE_DECL_ROUNDF = 0 || test $REPLACE_ROUNDF = 1; then
94     dnl Find libraries needed to link lib/roundf.c.
95     AC_CHECK_DECLS([ceilf, floorf], , , [#include <math.h>])
96     if test "$ac_cv_have_decl_floorf" = yes \
97        && test "$ac_cv_have_decl_ceilf" = yes; then
98       gl_FUNC_FLOORF_LIBS
99       gl_FUNC_CEILF_LIBS
100       if test "$FLOORF_LIBM" != '?' && test "$CEILF_LIBM" != '?'; then
101         AC_DEFINE([HAVE_FLOORF_AND_CEILF], [1],
102           [Define if the both the floorf() and ceilf() functions exist.])
103         ROUNDF_LIBM=
104         dnl Append $FLOORF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
105         case " $ROUNDF_LIBM " in
106           *" $FLOORF_LIBM "*) ;;
107           *) ROUNDF_LIBM="$ROUNDF_LIBM $FLOORF_LIBM" ;;
108         esac
109         dnl Append $CEILF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
110         case " $ROUNDF_LIBM " in
111           *" $CEILF_LIBM "*) ;;
112           *) ROUNDF_LIBM="$ROUNDF_LIBM $CEILF_LIBM" ;;
113         esac
114       else
115         ROUNDF_LIBM=
116       fi
117     else
118       ROUNDF_LIBM=
119     fi
120   fi
121   AC_SUBST([ROUNDF_LIBM])
122 ])