strnlen: port to Android 5.0 (API 21)
[gnulib.git] / m4 / round.m4
blob7b419a42e77eca248d4f8b223c6fa406ad8b84db
1 # round.m4
2 # serial 28
3 dnl Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_ROUND],
10   m4_divert_text([DEFAULTS], [gl_round_required=plain])
11   AC_REQUIRE([gl_MATH_H_DEFAULTS])
13   dnl Persuade glibc <math.h> to declare round().
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   gl_CHECK_MATH_LIB([ROUND_LIBM], [double], [x = round (x);],
17     [extern
18      #ifdef __cplusplus
19      "C"
20      #endif
21      double round (double);
22     ])
23   if test "$ROUND_LIBM" != missing; then
24     HAVE_ROUND=1
25     dnl Also check whether it's declared.
26     dnl IRIX 6.5 has round() in libm but doesn't declare it in <math.h>.
27     AC_CHECK_DECLS([round], , [HAVE_DECL_ROUND=0], [[#include <math.h>]])
29     dnl Test whether round() produces correct results. On NetBSD 3.0, for
30     dnl x = 1/2 - 2^-54, the system's round() returns a wrong result.
31     AC_REQUIRE([AC_PROG_CC])
32     AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
33     AC_CACHE_CHECK([whether round works], [gl_cv_func_round_works],
34       [
35         saved_LIBS="$LIBS"
36         LIBS="$LIBS $ROUND_LIBM"
37         AC_RUN_IFELSE([AC_LANG_SOURCE([[
38 #include <float.h>
39 #include <math.h>
40 extern
41 #ifdef __cplusplus
42 "C"
43 #endif
44 double round (double);
45 #if defined _MSC_VER && !defined __clang__
46 # pragma fenv_access (off)
47 #endif
48 int main()
50   /* 2^DBL_MANT_DIG.  */
51   static const double TWO_MANT_DIG =
52     /* Assume DBL_MANT_DIG <= 5 * 31.
53        Use the identity
54        n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
55     (double) (1U << (DBL_MANT_DIG / 5))
56     * (double) (1U << ((DBL_MANT_DIG + 1) / 5))
57     * (double) (1U << ((DBL_MANT_DIG + 2) / 5))
58     * (double) (1U << ((DBL_MANT_DIG + 3) / 5))
59     * (double) (1U << ((DBL_MANT_DIG + 4) / 5));
60   volatile double x = 0.5 - 0.5 / TWO_MANT_DIG;
61   return (x < 0.5 && round (x) != 0.0);
62 }]])], [gl_cv_func_round_works=yes], [gl_cv_func_round_works=no],
63         [case "$host_os" in
64            netbsd* | aix*)    gl_cv_func_round_works="guessing no" ;;
65                               # Guess yes on MSVC, no on mingw.
66            windows*-msvc*)    gl_cv_func_round_works="guessing yes" ;;
67            mingw* | windows*) AC_EGREP_CPP([Known], [
68 #ifdef _MSC_VER
69  Known
70 #endif
71                                 ],
72                                 [gl_cv_func_round_works="guessing yes"],
73                                 [gl_cv_func_round_works="guessing no"])
74                               ;;
75            *)                 gl_cv_func_round_works="guessing yes" ;;
76          esac
77         ])
78         LIBS="$saved_LIBS"
79       ])
80     case "$gl_cv_func_round_works" in
81       *no) REPLACE_ROUND=1 ;;
82     esac
84     m4_ifdef([gl_FUNC_ROUND_IEEE], [
85       if test $gl_round_required = ieee && test $REPLACE_ROUND = 0; then
86         AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
87         AC_CACHE_CHECK([whether round works according to ISO C 99 with IEC 60559],
88           [gl_cv_func_round_ieee],
89           [
90             saved_LIBS="$LIBS"
91             LIBS="$LIBS $ROUND_LIBM"
92             AC_RUN_IFELSE(
93               [AC_LANG_SOURCE([[
94 #ifndef __NO_MATH_INLINES
95 # define __NO_MATH_INLINES 1 /* for glibc */
96 #endif
97 #include <math.h>
98 extern
99 #ifdef __cplusplus
101 #endif
102 double round (double);
103 ]gl_DOUBLE_MINUS_ZERO_CODE[
104 ]gl_DOUBLE_SIGNBIT_CODE[
105 static double dummy (double f) { return 0; }
106 int main (int argc, char *argv[])
108   double (* volatile my_round) (double) = argc ? round : dummy;
109   /* Test whether round (-0.0) is -0.0.  */
110   if (signbitd (minus_zerod) && !signbitd (my_round (minus_zerod)))
111     return 1;
112   return 0;
114               ]])],
115               [gl_cv_func_round_ieee=yes],
116               [gl_cv_func_round_ieee=no],
117               [case "$host_os" in
118                                      # Guess yes on glibc systems.
119                  *-gnu* | gnu*)      gl_cv_func_round_ieee="guessing yes" ;;
120                                      # Guess yes on musl systems.
121                  *-musl* | midipix*) gl_cv_func_round_ieee="guessing yes" ;;
122                                      # Guess yes on MSVC, no on mingw.
123                  windows*-msvc*)     gl_cv_func_round_ieee="guessing yes" ;;
124                  mingw* | windows*)  AC_EGREP_CPP([Known], [
125 #ifdef _MSC_VER
126  Known
127 #endif
128                                        ],
129                                        [gl_cv_func_round_ieee="guessing yes"],
130                                        [gl_cv_func_round_ieee="guessing no"])
131                                      ;;
132                                      # If we don't know, obey --enable-cross-guesses.
133                  *)                  gl_cv_func_round_ieee="$gl_cross_guess_normal" ;;
134                esac
135               ])
136             LIBS="$saved_LIBS"
137           ])
138         case "$gl_cv_func_round_ieee" in
139           *yes) ;;
140           *) REPLACE_ROUND=1 ;;
141         esac
142       fi
143     ])
144   else
145     HAVE_ROUND=0
146     HAVE_DECL_ROUND=0
147   fi
148   if test $HAVE_ROUND = 0 || test $REPLACE_ROUND = 1; then
149     dnl Find libraries needed to link lib/round.c.
150     gl_FUNC_FLOOR_LIBS
151     gl_FUNC_CEIL_LIBS
152     ROUND_LIBM=
153     dnl Append $FLOOR_LIBM to ROUND_LIBM, avoiding gratuitous duplicates.
154     case " $ROUND_LIBM " in
155       *" $FLOOR_LIBM "*) ;;
156       *) ROUND_LIBM="$ROUND_LIBM $FLOOR_LIBM" ;;
157     esac
158     dnl Append $CEIL_LIBM to ROUND_LIBM, avoiding gratuitous duplicates.
159     case " $ROUND_LIBM " in
160       *" $CEIL_LIBM "*) ;;
161       *) ROUND_LIBM="$ROUND_LIBM $CEIL_LIBM" ;;
162     esac
163   fi
164   AC_SUBST([ROUND_LIBM])