Debian Codegraph @

m4/round.m4

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