1 /* Round toward nearest, breaking ties away from zero.
2 Copyright (C) 2007, 2010-2020 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Ben Pfaff <blp@gnu.org>, 2007.
18 Based heavily on code by Bruno Haible. */
20 #if ! defined USE_LONG_DOUBLE
31 #ifdef USE_LONG_DOUBLE
35 # define DOUBLE long double
36 # define MANT_DIG LDBL_MANT_DIG
38 # define L_(literal) literal##L
39 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORL_AND_CEILL
40 #elif ! defined USE_FLOAT
44 # define DOUBLE double
45 # define MANT_DIG DBL_MANT_DIG
47 # define L_(literal) literal
48 # define HAVE_FLOOR_AND_CEIL 1
49 #else /* defined USE_FLOAT */
54 # define MANT_DIG FLT_MANT_DIG
56 # define L_(literal) literal##f
57 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORF_AND_CEILF
60 /* -0.0. See minus-zero.h. */
61 #if defined __hpux || defined __sgi || defined __ICC
62 # define MINUS_ZERO (-MIN * MIN)
64 # define MINUS_ZERO L_(-0.0)
67 /* MSVC with option -fp:strict refuses to compile constant initializers that
68 contain floating-point operations. Pacify this compiler. */
69 #if defined _MSC_VER && !defined __clang__
70 # pragma fenv_access (off)
73 /* If we're being included from test-round2[f].c, it already defined names for
74 our round implementations. Otherwise, pick the preferred implementation for
76 #if !defined FLOOR_BASED_ROUND && !defined FLOOR_FREE_ROUND
77 # if HAVE_FLOOR_AND_CEIL
78 # define FLOOR_BASED_ROUND ROUND
80 # define FLOOR_FREE_ROUND ROUND
84 #ifdef FLOOR_BASED_ROUND
85 /* An implementation of the C99 round function based on floor and ceil. We use
86 this when floor and ceil are available, on the assumption that they are
87 faster than the open-coded versions below. */
89 FLOOR_BASED_ROUND (DOUBLE x
)
101 if (y
- x
>= L_(0.5))
106 #endif /* FLOOR_BASED_ROUND */
108 #ifdef FLOOR_FREE_ROUND
109 /* An implementation of the C99 round function without floor or ceil.
110 We use this when floor or ceil is missing. */
112 FLOOR_FREE_ROUND (DOUBLE x
)
114 /* 2^(MANT_DIG-1). */
115 static const DOUBLE TWO_MANT_DIG
=
116 /* Assume MANT_DIG <= 5 * 31.
118 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
119 (DOUBLE
) (1U << ((MANT_DIG
- 1) / 5))
120 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 1) / 5))
121 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 2) / 5))
122 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 3) / 5))
123 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 4) / 5));
125 /* The use of 'volatile' guarantees that excess precision bits are dropped at
126 each addition step and before the following comparison at the caller's
127 site. It is necessary on x86 systems where double-floats are not IEEE
128 compliant by default, to avoid that the results become platform and
129 compiler option dependent. 'volatile' is a portable alternative to gcc's
130 -ffloat-store option. */
131 volatile DOUBLE y
= x
;
132 volatile DOUBLE z
= y
;
136 /* Avoid rounding error for x = 0.5 - 2^(-MANT_DIG-1). */
139 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
140 else if (z
< TWO_MANT_DIG
)
142 /* Add 0.5 to the absolute value. */
144 /* Round to the next integer (nearest or up or down, doesn't
148 /* Enforce rounding down. */
153 else if (z
< L_(0.0))
155 /* Avoid rounding error for x = -(0.5 - 2^(-MANT_DIG-1)). */
158 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
159 else if (z
> -TWO_MANT_DIG
)
161 /* Add 0.5 to the absolute value. */
163 /* Round to the next integer (nearest or up or down, doesn't
167 /* Enforce rounding up. */
174 #endif /* FLOOR_FREE_ROUND */