1 /* Round double value to long long int.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <math_private.h>
23 #include <libm-alias-double.h>
24 #include <math-barriers.h>
26 /* Round to the nearest integer, with values exactly on a 0.5 boundary
27 rounded away from zero, regardless of the current rounding mode.
28 If (long long)x, when x is out of range of a long long, clips at
29 LLONG_MAX or LLONG_MIN, then this implementation also clips. */
36 /* The barrier prevents compiler from optimizing it to llround when
37 compiled with -fno-math-errno */
44 /* IEEE 1003.1 lround function. IEEE specifies "round to the nearest
45 integer value, rounding halfway cases away from zero, regardless of
46 the current rounding mode." However PowerPC Architecture defines
47 "round to Nearest" as "Choose the best approximation. In case of a
48 tie, choose the one that is even (least significant bit o).".
49 So we can't use the PowerPC "round to Nearest" mode. Instead we set
50 "round toward Zero" mode and round by adding +-0.5 before rounding
53 It is necessary to detect when x is (+-)0x1.fffffffffffffp-2
54 because adding +-0.5 in this case will cause an erroneous shift,
55 carry and round. We simply return 0 if 0.5 > x > -0.5. Likewise
56 if x is and odd number between +-(2^52 and 2^53-1) a shift and
57 carry will erroneously round if biased with +-0.5. Therefore if x
58 is greater/less than +-2^52 we don't need to bias the number with
67 /* Test whether an integer to avoid spurious "inexact". */
68 double t
= ax
+ 0x1p
+52;
83 /* Avoid incorrect exceptions from libgcc conversions (as of GCC
84 5): <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59412>. */
85 if (fabs (x
) < 0x1p
31)
86 xr
= (long long int) (long int) x
;
90 EXTRACT_WORDS64 (i0
, x
);
91 int exponent
= ((i0
>> 52) & 0x7ff) - 0x3ff;
94 unsigned long long int mant
95 = (i0
& ((1ULL << 52) - 1)) | (1ULL << 52);
97 /* llround is not required to raise "inexact". */
98 mant
>>= 52 - exponent
;
100 mant
<<= exponent
- 52;
101 xr
= (long long int) ((i0
& (1ULL << 63)) != 0 ? -mant
: mant
);
103 else if (x
== (double) LLONG_MIN
)
106 xr
= (long long int) (long int) x
<< 32;
109 /* Avoid spurious "inexact" converting LLONG_MAX to double, and from
110 subtraction when the result is out of range, by returning early
111 for arguments large enough that no rounding is needed. */
112 if (!(fabs (x
) < 0x1p
52))
114 double xrf
= (double) xr
;
119 xr
+= (long long) ((unsigned long long) xr
+ 1) > 0;
124 xr
-= (long long) ((unsigned long long) xr
- 1) < 0;
130 libm_alias_double (__llround
, llround
)