immutable: Add tests.
[gnulib.git] / m4 / roundf.m4
blob483d4f52fd1863298bedb14d46b4277047fb49bc
1 # roundf.m4 serial 24
2 dnl Copyright (C) 2007-2021 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])
12   dnl Persuade glibc <math.h> to declare roundf().
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
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>]])
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 #if defined _MSC_VER && !defined __clang__
45 # pragma fenv_access (off)
46 #endif
47 int main()
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   return (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                    # Guess yes on MSVC, no on mingw.
61            mingw*) AC_EGREP_CPP([Known], [
62 #ifdef _MSC_VER
63  Known
64 #endif
65                      ],
66                      [gl_cv_func_roundf_works="guessing yes"],
67                      [gl_cv_func_roundf_works="guessing no"])
68                    ;;
69            *)      gl_cv_func_roundf_works="guessing yes" ;;
70          esac
71         ])
72         LIBS="$save_LIBS"
73       ])
74     case "$gl_cv_func_roundf_works" in
75       *no) REPLACE_ROUNDF=1 ;;
76     esac
78     m4_ifdef([gl_FUNC_ROUNDF_IEEE], [
79       if test $gl_roundf_required = ieee && test $REPLACE_ROUNDF = 0; then
80         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
81         AC_CACHE_CHECK([whether roundf works according to ISO C 99 with IEC 60559],
82           [gl_cv_func_roundf_ieee],
83           [
84             save_LIBS="$LIBS"
85             LIBS="$LIBS $ROUNDF_LIBM"
86             AC_RUN_IFELSE(
87               [AC_LANG_SOURCE([[
88 #ifndef __NO_MATH_INLINES
89 # define __NO_MATH_INLINES 1 /* for glibc */
90 #endif
91 #include <math.h>
92 extern
93 #ifdef __cplusplus
94 "C"
95 #endif
96 float roundf (float);
97 ]gl_FLOAT_MINUS_ZERO_CODE[
98 ]gl_FLOAT_SIGNBIT_CODE[
99 static float dummy (float f) { return 0; }
100 int main (int argc, char *argv[])
102   float (* volatile my_roundf) (float) = argc ? roundf : dummy;
103   int result = 0;
104   /* Test whether roundf (-0.0f) is -0.0f.  */
105   if (signbitf (minus_zerof) && !signbitf (my_roundf (minus_zerof)))
106     result |= 1;
107   /* Test whether roundf (-0.3f) is -0.0f.  */
108   if (signbitf (-0.3f) && !signbitf (my_roundf (-0.3f)))
109     result |= 2;
110   return result;
112               ]])],
113               [gl_cv_func_roundf_ieee=yes],
114               [gl_cv_func_roundf_ieee=no],
115               [case "$host_os" in
116                                 # Guess yes on glibc systems.
117                  *-gnu* | gnu*) gl_cv_func_roundf_ieee="guessing yes" ;;
118                                 # Guess yes on musl systems.
119                  *-musl*)       gl_cv_func_roundf_ieee="guessing yes" ;;
120                                 # Guess yes on MSVC, no on mingw.
121                  mingw*)        AC_EGREP_CPP([Known], [
122 #ifdef _MSC_VER
123  Known
124 #endif
125                                   ],
126                                   [gl_cv_func_roundf_ieee="guessing yes"],
127                                   [gl_cv_func_roundf_ieee="guessing no"])
128                                 ;;
129                                 # If we don't know, obey --enable-cross-guesses.
130                  *)             gl_cv_func_roundf_ieee="$gl_cross_guess_normal" ;;
131                esac
132               ])
133             LIBS="$save_LIBS"
134           ])
135         case "$gl_cv_func_roundf_ieee" in
136           *yes) ;;
137           *) REPLACE_ROUNDF=1 ;;
138         esac
139       fi
140     ])
141   else
142     HAVE_ROUNDF=0
143     HAVE_DECL_ROUNDF=0
144   fi
145   if test $HAVE_ROUNDF = 0 || test $REPLACE_ROUNDF = 1; then
146     dnl Find libraries needed to link lib/roundf.c.
147     AC_CHECK_DECLS([ceilf, floorf], , , [[#include <math.h>]])
148     if test "$ac_cv_have_decl_floorf" = yes \
149        && test "$ac_cv_have_decl_ceilf" = yes; then
150       gl_FUNC_FLOORF_LIBS
151       gl_FUNC_CEILF_LIBS
152       if test "$FLOORF_LIBM" != '?' && test "$CEILF_LIBM" != '?'; then
153         AC_DEFINE([HAVE_FLOORF_AND_CEILF], [1],
154           [Define if the both the floorf() and ceilf() functions exist.])
155         ROUNDF_LIBM=
156         dnl Append $FLOORF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
157         case " $ROUNDF_LIBM " in
158           *" $FLOORF_LIBM "*) ;;
159           *) ROUNDF_LIBM="$ROUNDF_LIBM $FLOORF_LIBM" ;;
160         esac
161         dnl Append $CEILF_LIBM to ROUNDF_LIBM, avoiding gratuitous duplicates.
162         case " $ROUNDF_LIBM " in
163           *" $CEILF_LIBM "*) ;;
164           *) ROUNDF_LIBM="$ROUNDF_LIBM $CEILF_LIBM" ;;
165         esac
166       else
167         ROUNDF_LIBM=
168       fi
169     else
170       ROUNDF_LIBM=
171     fi
172   fi
173   AC_SUBST([ROUNDF_LIBM])