1 /* Round double value to long long int.
2 Copyright (C) 1997-2017 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 <http://www.gnu.org/licenses/>. */
21 #include <math_ldbl_opt.h>
22 #include <math_private.h>
24 #include <libm-alias-double.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. */
39 /* Avoid incorrect exceptions from libgcc conversions (as of GCC
40 5): <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59412>. */
41 if (fabs (x
) < 0x1p
31)
42 xr
= (long long int) (long int) x
;
46 EXTRACT_WORDS64 (i0
, x
);
47 int exponent
= ((i0
>> 52) & 0x7ff) - 0x3ff;
50 unsigned long long int mant
51 = (i0
& ((1ULL << 52) - 1)) | (1ULL << 52);
53 /* llround is not required to raise "inexact". */
54 mant
>>= 52 - exponent
;
56 mant
<<= exponent
- 52;
57 xr
= (long long int) ((i0
& (1ULL << 63)) != 0 ? -mant
: mant
);
59 else if (x
== (double) LLONG_MIN
)
62 xr
= (long long int) (long int) x
<< 32;
65 /* Avoid spurious "inexact" converting LLONG_MAX to double, and from
66 subtraction when the result is out of range, by returning early
67 for arguments large enough that no rounding is needed. */
68 if (!(fabs (x
) < 0x1p
52))
70 double xrf
= (double) xr
;
75 xr
+= (long long) ((unsigned long long) xr
+ 1) > 0;
80 xr
-= (long long) ((unsigned long long) xr
- 1) < 0;
84 libm_alias_double (__llround
, llround
)