1*627f7eb2Smrg /* Round long double value to long 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 long int
llroundq(__float128 x)24*627f7eb2Smrg llroundq (__float128 x)
25*627f7eb2Smrg {
26*627f7eb2Smrg int64_t j0;
27*627f7eb2Smrg uint64_t i1, i0;
28*627f7eb2Smrg long 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 < 48)
38*627f7eb2Smrg {
39*627f7eb2Smrg if (j0 < 0)
40*627f7eb2Smrg return j0 < -1 ? 0 : sign;
41*627f7eb2Smrg else
42*627f7eb2Smrg {
43*627f7eb2Smrg i0 += 0x0000800000000000LL >> j0;
44*627f7eb2Smrg result = i0 >> (48 - j0);
45*627f7eb2Smrg }
46*627f7eb2Smrg }
47*627f7eb2Smrg else if (j0 < (int32_t) (8 * sizeof (long long int)) - 1)
48*627f7eb2Smrg {
49*627f7eb2Smrg if (j0 >= 112)
50*627f7eb2Smrg result = ((long long int) i0 << (j0 - 48)) | (i1 << (j0 - 112));
51*627f7eb2Smrg else
52*627f7eb2Smrg {
53*627f7eb2Smrg uint64_t j = i1 + (0x8000000000000000ULL >> (j0 - 48));
54*627f7eb2Smrg if (j < i1)
55*627f7eb2Smrg ++i0;
56*627f7eb2Smrg
57*627f7eb2Smrg if (j0 == 48)
58*627f7eb2Smrg result = (long long int) i0;
59*627f7eb2Smrg else
60*627f7eb2Smrg {
61*627f7eb2Smrg result = ((long long int) i0 << (j0 - 48)) | (j >> (112 - j0));
62*627f7eb2Smrg #ifdef FE_INVALID
63*627f7eb2Smrg if (sign == 1 && result == LLONG_MIN)
64*627f7eb2Smrg /* Rounding brought the value out of range. */
65*627f7eb2Smrg feraiseexcept (FE_INVALID);
66*627f7eb2Smrg #endif
67*627f7eb2Smrg }
68*627f7eb2Smrg }
69*627f7eb2Smrg }
70*627f7eb2Smrg else
71*627f7eb2Smrg {
72*627f7eb2Smrg /* The number is too large. Unless it rounds to LLONG_MIN,
73*627f7eb2Smrg FE_INVALID must be raised and the return value is
74*627f7eb2Smrg unspecified. */
75*627f7eb2Smrg #ifdef FE_INVALID
76*627f7eb2Smrg if (FIX_FLT128_LLONG_CONVERT_OVERFLOW
77*627f7eb2Smrg && !(sign == -1 && x > (__float128) LLONG_MIN - 0.5Q))
78*627f7eb2Smrg {
79*627f7eb2Smrg feraiseexcept (FE_INVALID);
80*627f7eb2Smrg return sign == 1 ? LLONG_MAX : LLONG_MIN;
81*627f7eb2Smrg }
82*627f7eb2Smrg else if (!FIX_FLT128_LLONG_CONVERT_OVERFLOW
83*627f7eb2Smrg && x <= (__float128) LLONG_MIN - 0.5Q)
84*627f7eb2Smrg {
85*627f7eb2Smrg /* If truncation produces LLONG_MIN, the cast will not raise
86*627f7eb2Smrg the exception, but may raise "inexact". */
87*627f7eb2Smrg feraiseexcept (FE_INVALID);
88*627f7eb2Smrg return LLONG_MIN;
89*627f7eb2Smrg }
90*627f7eb2Smrg #endif
91*627f7eb2Smrg return (long long int) x;
92*627f7eb2Smrg }
93*627f7eb2Smrg
94*627f7eb2Smrg return sign * result;
95*627f7eb2Smrg }
96