maint: update copyright
[gnulib.git] / m4 / roundf.m4
1 # roundf.m4 serial 17
2 dnl Copyright (C) 2007-2014 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
12   dnl Persuade glibc <math.h> to declare roundf().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14
15   gl_CHECK_MATH_LIB([ROUNDF_LIBM], [x = roundf (x);],
16     [extern
17      #ifdef __cplusplus
18      "C"
19      #endif
20      float roundf (float);
21     ])
22   if test "$ROUNDF_LIBM" != missing; then
23     HAVE_ROUNDF=1
24     dnl Also check whether it's declared.
25     dnl IRIX 6.5 has roundf() in libm but doesn't declare it in <math.h>.
26     AC_CHECK_DECLS([roundf], , [HAVE_DECL_ROUNDF=0], [[#include <math.h>]])
27
28     dnl Test whether roundf() produces correct results. On mingw, for
29     dnl x = 1/2 - 2^-25, the system's roundf() 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 roundf works], [gl_cv_func_roundf_works],
33       [
34         save_LIBS="$LIBS"
35         LIBS="$LIBS $ROUNDF_LIBM"
36         AC_RUN_IFELSE([AC_LANG_SOURCE([[
37 #include <float.h>
38 #include <math.h>
39 extern
40 #ifdef __cplusplus
41 "C"
42 #endif
43 float roundf (float);
44 #ifdef _MSC_VER
45 # pragma fenv_access (off)
46 #endif
47 int main()
48 {
49   /* 2^FLT_MANT_DIG.  */
50   static const float TWO_MANT_DIG =
51     /* Assume FLT_MANT_DIG <= 3 * 31.
52        Use the identity  n = floor(n/3) + floor((n+1)/3) + floor((n+2)/3).  */
53     (float) (1U << (FLT_MANT_DIG / 3))
54     * (float) (1U << ((FLT_MANT_DIG + 1) / 3))
55     * (float) (1U << ((FLT_MANT_DIG + 2) / 3));
56   volatile float x = 0.5f - 0.5f / TWO_MANT_DIG;
57   exit (x < 0.5f && roundf (x) != 0.0f);
58 }]])], [gl_cv_func_roundf_works=yes], [gl_cv_func_roundf_works=no],
59         [case "$host_os" in
60            mingw*) gl_cv_func_roundf_works="guessing no";;
61            *)      gl_cv_func_roundf_works="guessing yes";;
62          esac
63         ])
64         LIBS="$save_LIBS"
65       ])
66     case "$gl_cv_func_roundf_works" in
67       *no) REPLACE_ROUNDF=1 ;;
68     esac
69
70     m4_ifdef([gl_FUNC_ROUNDF_IEEE], [
71       if test $gl_roundf_required = ieee && test $REPLACE_ROUNDF = 0; then
72         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
73         AC_CACHE_CHECK([whether roundf works according to ISO C 99 with IEC 60559],
74           [gl_cv_func_roundf_ieee],
75           [
76             save_LIBS="$LIBS"
77             LIBS="$LIBS $ROUNDF_LIBM"
78             AC_RUN_IFELSE(
79               [AC_LANG_SOURCE([[
80 #ifndef __NO_MATH_INLINES
81 # define __NO_MATH_INLINES 1 /* for glibc */
82 #endif
83 #include <math.h>
84 extern
85 #ifdef __cplusplus
86 "C"
87 #endif
88 float roundf (float);
89 ]gl_FLOAT_MINUS_ZERO_CODE[
90 ]gl_FLOAT_SIGNBIT_CODE[
91 static float dummy (float f) { return 0; }
92 int main (int argc, char *argv[])
93 {
94   float (*my_roundf) (float) = argc ? roundf : dummy;
95   int result = 0;
96   /* Test whether roundf (-0.0f) is -0.0f.  */
97   if (signbitf (minus_zerof) && !signbitf (my_roundf (minus_zerof)))
98     result |= 1;
99   /* Test whether roundf (-0.3f) is -0.0f.  */
100   if (signbitf (-0.3f) && !signbitf (my_roundf (-0.3f)))
101     result |= 2;
102   return result;
103 }
104               ]])],
105               [gl_cv_func_roundf_ieee=yes],
106               [gl_cv_func_roundf_ieee=no],
107               [case "$host_os" in
108                          # Guess yes on glibc systems.
109                  *-gnu*) gl_cv_func_roundf_ieee="guessing yes" ;;
110                          # If we don't know, assume the worst.
111                  *)      gl_cv_func_roundf_ieee="guessing no" ;;
112                esac
113               ])
114             LIBS="$save_LIBS"
115           ])
116         case "$gl_cv_func_roundf_ieee" in
117           *yes) ;;
118           *) REPLACE_ROUNDF=1 ;;
119         esac
120       fi
121     ])
122   else
123     HAVE_ROUNDF=0
124     HAVE_DECL_ROUNDF=0
125   fi
126   if test $HAVE_ROUNDF = 0 || test $REPLACE_ROUNDF = 1; then
127     dnl Find libraries needed to link lib/roundf.c.
128     AC_CHECK_DECLS([ceilf, floorf], , , [[#include <math.h>]])
129     if test "$ac_cv_have_decl_floorf" = yes \
130        && test "$ac_cv_have_decl_ceilf" = yes; then
131       gl_FUNC_FLOORF_LIBS
132       gl_FUNC_CEILF_LIBS
133       if test "$FLOORF_LIBM" != '?' && test "$CEILF_LIBM" != '?'; then
134         AC_DEFINE([HAVE_FLOORF_AND_CEILF], [1],
135           [Define if the both the floorf() and ceilf() functions exist.])
136         ROUNDF_LIBM=
137         dnl Append $FLOORF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
138         case " $ROUNDF_LIBM " in
139           *" $FLOORF_LIBM "*) ;;
140           *) ROUNDF_LIBM="$ROUNDF_LIBM $FLOORF_LIBM" ;;
141         esac
142         dnl Append $CEILF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
143         case " $ROUNDF_LIBM " in
144           *" $CEILF_LIBM "*) ;;
145           *) ROUNDF_LIBM="$ROUNDF_LIBM $CEILF_LIBM" ;;
146         esac
147       else
148         ROUNDF_LIBM=
149       fi
150     else
151       ROUNDF_LIBM=
152     fi
153   fi
154   AC_SUBST([ROUNDF_LIBM])
155 ])