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 int
lrintq(__float128 x)31*627f7eb2Smrg lrintq (__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 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 int)) - 1)
47*627f7eb2Smrg {
48*627f7eb2Smrg if (j0 < 48)
49*627f7eb2Smrg {
50*627f7eb2Smrg #if defined FE_INVALID || defined FE_INEXACT
51*627f7eb2Smrg /* X < LONG_MAX + 1 implied by J0 < 31. */
52*627f7eb2Smrg if (sizeof (long int) == 4
53*627f7eb2Smrg && x > (__float128) LONG_MAX)
54*627f7eb2Smrg {
55*627f7eb2Smrg /* In the event of overflow we must raise the "invalid"
56*627f7eb2Smrg exception, but not "inexact". */
57*627f7eb2Smrg t = nearbyintq (x);
58*627f7eb2Smrg feraiseexcept (t == LONG_MAX ? FE_INEXACT : FE_INVALID);
59*627f7eb2Smrg }
60*627f7eb2Smrg else
61*627f7eb2Smrg #endif
62*627f7eb2Smrg {
63*627f7eb2Smrg w = two112[sx] + x;
64*627f7eb2Smrg t = w - two112[sx];
65*627f7eb2Smrg }
66*627f7eb2Smrg GET_FLT128_WORDS64 (i0, i1, t);
67*627f7eb2Smrg j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
68*627f7eb2Smrg i0 &= 0x0000ffffffffffffLL;
69*627f7eb2Smrg i0 |= 0x0001000000000000LL;
70*627f7eb2Smrg
71*627f7eb2Smrg result = (j0 < 0 ? 0 : i0 >> (48 - j0));
72*627f7eb2Smrg }
73*627f7eb2Smrg else if (j0 >= 112)
74*627f7eb2Smrg result = ((long int) i0 << (j0 - 48)) | (i1 << (j0 - 112));
75*627f7eb2Smrg else
76*627f7eb2Smrg {
77*627f7eb2Smrg #if defined FE_INVALID || defined FE_INEXACT
78*627f7eb2Smrg /* X < LONG_MAX + 1 implied by J0 < 63. */
79*627f7eb2Smrg if (sizeof (long int) == 8
80*627f7eb2Smrg && x > (__float128) LONG_MAX)
81*627f7eb2Smrg {
82*627f7eb2Smrg /* In the event of overflow we must raise the "invalid"
83*627f7eb2Smrg exception, but not "inexact". */
84*627f7eb2Smrg t = nearbyintq (x);
85*627f7eb2Smrg feraiseexcept (t == LONG_MAX ? FE_INEXACT : FE_INVALID);
86*627f7eb2Smrg }
87*627f7eb2Smrg else
88*627f7eb2Smrg #endif
89*627f7eb2Smrg {
90*627f7eb2Smrg w = two112[sx] + x;
91*627f7eb2Smrg t = w - two112[sx];
92*627f7eb2Smrg }
93*627f7eb2Smrg GET_FLT128_WORDS64 (i0, i1, t);
94*627f7eb2Smrg j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
95*627f7eb2Smrg i0 &= 0x0000ffffffffffffLL;
96*627f7eb2Smrg i0 |= 0x0001000000000000LL;
97*627f7eb2Smrg
98*627f7eb2Smrg if (j0 == 48)
99*627f7eb2Smrg result = (long int) i0;
100*627f7eb2Smrg else
101*627f7eb2Smrg result = ((long int) i0 << (j0 - 48)) | (i1 >> (112 - j0));
102*627f7eb2Smrg }
103*627f7eb2Smrg }
104*627f7eb2Smrg else
105*627f7eb2Smrg {
106*627f7eb2Smrg /* The number is too large. Unless it rounds to LONG_MIN,
107*627f7eb2Smrg FE_INVALID must be raised and the return value is
108*627f7eb2Smrg unspecified. */
109*627f7eb2Smrg #if defined FE_INVALID || defined FE_INEXACT
110*627f7eb2Smrg if (x < (__float128) LONG_MIN
111*627f7eb2Smrg && x > (__float128) LONG_MIN - 1)
112*627f7eb2Smrg {
113*627f7eb2Smrg /* If truncation produces LONG_MIN, the cast will not raise
114*627f7eb2Smrg the exception, but may raise "inexact". */
115*627f7eb2Smrg t = nearbyintq (x);
116*627f7eb2Smrg feraiseexcept (t == LONG_MIN ? FE_INEXACT : FE_INVALID);
117*627f7eb2Smrg return LONG_MIN;
118*627f7eb2Smrg }
119*627f7eb2Smrg else if (FIX_FLT128_LONG_CONVERT_OVERFLOW && x != (__float128) LONG_MIN)
120*627f7eb2Smrg {
121*627f7eb2Smrg feraiseexcept (FE_INVALID);
122*627f7eb2Smrg return sx == 0 ? LONG_MAX : LONG_MIN;
123*627f7eb2Smrg }
124*627f7eb2Smrg
125*627f7eb2Smrg #endif
126*627f7eb2Smrg return (long int) x;
127*627f7eb2Smrg }
128*627f7eb2Smrg
129*627f7eb2Smrg return sx ? -result : result;
130*627f7eb2Smrg }
131