| 1 |
/* Round towards positive infinity. |
| 2 |
Copyright (C) 2007, 2010-2025 Free Software Foundation, Inc. |
| 3 |
|
| 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. |
| 8 |
|
| 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. |
| 13 |
|
| 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/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2007. */ |
| 18 |
|
| 19 |
#if ! defined USE_LONG_DOUBLE |
| 20 |
# include <config.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
/* Specification. */ |
| 24 |
#include <math.h> |
| 25 |
|
| 26 |
#include <float.h> |
| 27 |
|
| 28 |
#undef MIN |
| 29 |
|
| 30 |
#ifdef USE_LONG_DOUBLE |
| 31 |
# define FUNC ceill |
| 32 |
# define DOUBLE long double |
| 33 |
# define MANT_DIG LDBL_MANT_DIG |
| 34 |
# define MIN LDBL_MIN |
| 35 |
# define L_(literal) literal##L |
| 36 |
#elif ! defined USE_FLOAT |
| 37 |
# define FUNC ceil |
| 38 |
# define DOUBLE double |
| 39 |
# define MANT_DIG DBL_MANT_DIG |
| 40 |
# define MIN DBL_MIN |
| 41 |
# define L_(literal) literal |
| 42 |
#else /* defined USE_FLOAT */ |
| 43 |
# define FUNC ceilf |
| 44 |
# define DOUBLE float |
| 45 |
# define MANT_DIG FLT_MANT_DIG |
| 46 |
# define MIN FLT_MIN |
| 47 |
# define L_(literal) literal##f |
| 48 |
#endif |
| 49 |
|
| 50 |
/* -0.0. See minus-zero.h. */ |
| 51 |
#if defined __hpux || defined __sgi || defined __ICC |
| 52 |
# define MINUS_ZERO (-MIN * MIN) |
| 53 |
#else |
| 54 |
# define MINUS_ZERO L_(-0.0) |
| 55 |
#endif |
| 56 |
|
| 57 |
/* MSVC with option -fp:strict refuses to compile constant initializers that |
| 58 |
contain floating-point operations. Pacify this compiler. */ |
| 59 |
#if defined _MSC_VER && !defined __clang__ |
| 60 |
# pragma fenv_access (off) |
| 61 |
#endif |
| 62 |
|
| 63 |
/* 2^(MANT_DIG-1). */ |
| 64 |
static const DOUBLE TWO_MANT_DIG = |
| 65 |
/* Assume MANT_DIG <= 5 * 31. |
| 66 |
Use the identity |
| 67 |
n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */ |
| 68 |
(DOUBLE) (1U << ((MANT_DIG - 1) / 5)) |
| 69 |
* (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5)) |
| 70 |
* (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5)) |
| 71 |
* (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5)) |
| 72 |
* (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5)); |
| 73 |
|
| 74 |
DOUBLE |
| 75 |
FUNC (DOUBLE x) |
| 76 |
{ |
| 77 |
/* The use of 'volatile' guarantees that excess precision bits are dropped |
| 78 |
at each addition step and before the following comparison at the caller's |
| 79 |
site. It is necessary on x86 systems where double-floats are not IEEE |
| 80 |
compliant by default, to avoid that the results become platform and compiler |
| 81 |
option dependent. 'volatile' is a portable alternative to gcc's |
| 82 |
-ffloat-store option. */ |
| 83 |
volatile DOUBLE y = x; |
| 84 |
volatile DOUBLE z = y; |
| 85 |
|
| 86 |
if (z > L_(0.0)) |
| 87 |
{ |
| 88 |
/* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */ |
| 89 |
if (z < TWO_MANT_DIG) |
| 90 |
{ |
| 91 |
/* Round to the next integer (nearest or up or down, doesn't matter). */ |
| 92 |
z += TWO_MANT_DIG; |
| 93 |
z -= TWO_MANT_DIG; |
| 94 |
/* Enforce rounding up. */ |
| 95 |
if (z < y) |
| 96 |
z += L_(1.0); |
| 97 |
} |
| 98 |
} |
| 99 |
else if (z < L_(0.0)) |
| 100 |
{ |
| 101 |
/* For -1 < x < 0, return -0.0 regardless of the current rounding |
| 102 |
mode. */ |
| 103 |
if (z > L_(-1.0)) |
| 104 |
z = MINUS_ZERO; |
| 105 |
/* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */ |
| 106 |
else if (z > - TWO_MANT_DIG) |
| 107 |
{ |
| 108 |
/* Round to the next integer (nearest or up or down, doesn't matter). */ |
| 109 |
z -= TWO_MANT_DIG; |
| 110 |
z += TWO_MANT_DIG; |
| 111 |
/* Enforce rounding up. */ |
| 112 |
if (z < y) |
| 113 |
z += L_(1.0); |
| 114 |
} |
| 115 |
} |
| 116 |
return z; |
| 117 |
} |