1*627f7eb2Smrg /* Round argument to nearest integral value according to current rounding
2*627f7eb2Smrg direction.
3*627f7eb2Smrg Copyright (C) 1997-2018 Free Software Foundation, Inc.
4*627f7eb2Smrg This file is part of the GNU C Library.
5*627f7eb2Smrg Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
6*627f7eb2Smrg Jakub Jelinek <jj@ultra.linux.cz>, 1999.
7*627f7eb2Smrg
8*627f7eb2Smrg The GNU C Library is free software; you can redistribute it and/or
9*627f7eb2Smrg modify it under the terms of the GNU Lesser General Public
10*627f7eb2Smrg License as published by the Free Software Foundation; either
11*627f7eb2Smrg version 2.1 of the License, or (at your option) any later version.
12*627f7eb2Smrg
13*627f7eb2Smrg The GNU C Library is distributed in the hope that it will be useful,
14*627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
15*627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16*627f7eb2Smrg Lesser General Public License for more details.
17*627f7eb2Smrg
18*627f7eb2Smrg You should have received a copy of the GNU Lesser General Public
19*627f7eb2Smrg License along with the GNU C Library; if not, see
20*627f7eb2Smrg <http://www.gnu.org/licenses/>. */
21*627f7eb2Smrg
22*627f7eb2Smrg #include "quadmath-imp.h"
23*627f7eb2Smrg
24*627f7eb2Smrg static const __float128 two112[2] =
25*627f7eb2Smrg {
26*627f7eb2Smrg 5.19229685853482762853049632922009600E+33Q, /* 0x406F000000000000, 0 */
27*627f7eb2Smrg -5.19229685853482762853049632922009600E+33Q /* 0xC06F000000000000, 0 */
28*627f7eb2Smrg };
29*627f7eb2Smrg
30*627f7eb2Smrg long long int
llrintq(__float128 x)31*627f7eb2Smrg llrintq (__float128 x)
32*627f7eb2Smrg {
33*627f7eb2Smrg int32_t j0;
34*627f7eb2Smrg uint64_t i0,i1;
35*627f7eb2Smrg __float128 w;
36*627f7eb2Smrg __float128 t;
37*627f7eb2Smrg long long int result;
38*627f7eb2Smrg int sx;
39*627f7eb2Smrg
40*627f7eb2Smrg GET_FLT128_WORDS64 (i0, i1, x);
41*627f7eb2Smrg j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
42*627f7eb2Smrg sx = i0 >> 63;
43*627f7eb2Smrg i0 &= 0x0000ffffffffffffLL;
44*627f7eb2Smrg i0 |= 0x0001000000000000LL;
45*627f7eb2Smrg
46*627f7eb2Smrg if (j0 < (int32_t) (8 * sizeof (long long int)) - 1)
47*627f7eb2Smrg {
48*627f7eb2Smrg #if defined FE_INVALID || defined FE_INEXACT
49*627f7eb2Smrg /* X < LLONG_MAX + 1 implied by J0 < 63. */
50*627f7eb2Smrg if (x > (__float128) LLONG_MAX)
51*627f7eb2Smrg {
52*627f7eb2Smrg /* In the event of overflow we must raise the "invalid"
53*627f7eb2Smrg exception, but not "inexact". */
54*627f7eb2Smrg t = nearbyintq (x);
55*627f7eb2Smrg feraiseexcept (t == LLONG_MAX ? FE_INEXACT : FE_INVALID);
56*627f7eb2Smrg }
57*627f7eb2Smrg else
58*627f7eb2Smrg #endif
59*627f7eb2Smrg {
60*627f7eb2Smrg w = two112[sx] + x;
61*627f7eb2Smrg t = w - two112[sx];
62*627f7eb2Smrg }
63*627f7eb2Smrg GET_FLT128_WORDS64 (i0, i1, t);
64*627f7eb2Smrg j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
65*627f7eb2Smrg i0 &= 0x0000ffffffffffffLL;
66*627f7eb2Smrg i0 |= 0x0001000000000000LL;
67*627f7eb2Smrg
68*627f7eb2Smrg if (j0 < 0)
69*627f7eb2Smrg result = 0;
70*627f7eb2Smrg else if (j0 <= 48)
71*627f7eb2Smrg result = i0 >> (48 - j0);
72*627f7eb2Smrg else
73*627f7eb2Smrg result = ((long long int) i0 << (j0 - 48)) | (i1 >> (112 - j0));
74*627f7eb2Smrg }
75*627f7eb2Smrg else
76*627f7eb2Smrg {
77*627f7eb2Smrg /* The number is too large. Unless it rounds to LLONG_MIN,
78*627f7eb2Smrg FE_INVALID must be raised and the return value is
79*627f7eb2Smrg unspecified. */
80*627f7eb2Smrg #if defined FE_INVALID || defined FE_INEXACT
81*627f7eb2Smrg if (x < (__float128) LLONG_MIN
82*627f7eb2Smrg && x > (__float128) LLONG_MIN - 1)
83*627f7eb2Smrg {
84*627f7eb2Smrg /* If truncation produces LLONG_MIN, the cast will not raise
85*627f7eb2Smrg the exception, but may raise "inexact". */
86*627f7eb2Smrg t = nearbyintq (x);
87*627f7eb2Smrg feraiseexcept (t == LLONG_MIN ? FE_INEXACT : FE_INVALID);
88*627f7eb2Smrg return LLONG_MIN;
89*627f7eb2Smrg }
90*627f7eb2Smrg else if (FIX_FLT128_LLONG_CONVERT_OVERFLOW && x != (__float128) LLONG_MIN)
91*627f7eb2Smrg {
92*627f7eb2Smrg feraiseexcept (FE_INVALID);
93*627f7eb2Smrg return sx == 0 ? LLONG_MAX : LLONG_MIN;
94*627f7eb2Smrg }
95*627f7eb2Smrg
96*627f7eb2Smrg #endif
97*627f7eb2Smrg return (long long int) x;
98*627f7eb2Smrg }
99*627f7eb2Smrg
100*627f7eb2Smrg return sx ? -result : result;
101*627f7eb2Smrg }
102