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