1 /* Round towards negative infinity.
2 Copyright (C) 2007, 2010-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 #if ! defined USE_LONG_DOUBLE
28 #ifdef USE_LONG_DOUBLE
30 # define DOUBLE long double
31 # define MANT_DIG LDBL_MANT_DIG
32 # define L_(literal) literal##L
33 #elif ! defined USE_FLOAT
35 # define DOUBLE double
36 # define MANT_DIG DBL_MANT_DIG
37 # define L_(literal) literal
38 #else /* defined USE_FLOAT */
41 # define MANT_DIG FLT_MANT_DIG
42 # define L_(literal) literal##f
45 /* MSVC with option -fp:strict refuses to compile constant initializers that
46 contain floating-point operations. Pacify this compiler. */
47 #if defined _MSC_VER && !defined __clang__
48 # pragma fenv_access (off)
52 static const DOUBLE TWO_MANT_DIG
=
53 /* Assume MANT_DIG <= 5 * 31.
55 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
56 (DOUBLE
) (1U << ((MANT_DIG
- 1) / 5))
57 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 1) / 5))
58 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 2) / 5))
59 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 3) / 5))
60 * (DOUBLE
) (1U << ((MANT_DIG
- 1 + 4) / 5));
65 /* The use of 'volatile' guarantees that excess precision bits are dropped
66 at each addition step and before the following comparison at the caller's
67 site. It is necessary on x86 systems where double-floats are not IEEE
68 compliant by default, to avoid that the results become platform and compiler
69 option dependent. 'volatile' is a portable alternative to gcc's
70 -ffloat-store option. */
71 volatile DOUBLE y
= x
;
72 volatile DOUBLE z
= y
;
76 /* For 0 < x < 1, return +0.0 even if the current rounding mode is
80 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
81 else if (z
< TWO_MANT_DIG
)
83 /* Round to the next integer (nearest or up or down, doesn't matter). */
86 /* Enforce rounding down. */
93 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
94 if (z
> - TWO_MANT_DIG
)
96 /* Round to the next integer (nearest or up or down, doesn't matter). */
99 /* Enforce rounding down. */