1*627f7eb2Smrg /* Round long double to integer away from zero.
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 #define NO_MATH_REDIRECT
22*627f7eb2Smrg
23*627f7eb2Smrg #include "quadmath-imp.h"
24*627f7eb2Smrg
25*627f7eb2Smrg __float128
roundq(__float128 x)26*627f7eb2Smrg roundq (__float128 x)
27*627f7eb2Smrg {
28*627f7eb2Smrg int32_t j0;
29*627f7eb2Smrg uint64_t i1, i0;
30*627f7eb2Smrg
31*627f7eb2Smrg GET_FLT128_WORDS64 (i0, i1, x);
32*627f7eb2Smrg j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
33*627f7eb2Smrg if (j0 < 48)
34*627f7eb2Smrg {
35*627f7eb2Smrg if (j0 < 0)
36*627f7eb2Smrg {
37*627f7eb2Smrg i0 &= 0x8000000000000000ULL;
38*627f7eb2Smrg if (j0 == -1)
39*627f7eb2Smrg i0 |= 0x3fff000000000000LL;
40*627f7eb2Smrg i1 = 0;
41*627f7eb2Smrg }
42*627f7eb2Smrg else
43*627f7eb2Smrg {
44*627f7eb2Smrg uint64_t i = 0x0000ffffffffffffLL >> j0;
45*627f7eb2Smrg if (((i0 & i) | i1) == 0)
46*627f7eb2Smrg /* X is integral. */
47*627f7eb2Smrg return x;
48*627f7eb2Smrg
49*627f7eb2Smrg i0 += 0x0000800000000000LL >> j0;
50*627f7eb2Smrg i0 &= ~i;
51*627f7eb2Smrg i1 = 0;
52*627f7eb2Smrg }
53*627f7eb2Smrg }
54*627f7eb2Smrg else if (j0 > 111)
55*627f7eb2Smrg {
56*627f7eb2Smrg if (j0 == 0x4000)
57*627f7eb2Smrg /* Inf or NaN. */
58*627f7eb2Smrg return x + x;
59*627f7eb2Smrg else
60*627f7eb2Smrg return x;
61*627f7eb2Smrg }
62*627f7eb2Smrg else
63*627f7eb2Smrg {
64*627f7eb2Smrg uint64_t i = -1ULL >> (j0 - 48);
65*627f7eb2Smrg if ((i1 & i) == 0)
66*627f7eb2Smrg /* X is integral. */
67*627f7eb2Smrg return x;
68*627f7eb2Smrg
69*627f7eb2Smrg uint64_t j = i1 + (1LL << (111 - j0));
70*627f7eb2Smrg if (j < i1)
71*627f7eb2Smrg i0 += 1;
72*627f7eb2Smrg i1 = j;
73*627f7eb2Smrg i1 &= ~i;
74*627f7eb2Smrg }
75*627f7eb2Smrg
76*627f7eb2Smrg SET_FLT128_WORDS64 (x, i0, i1);
77*627f7eb2Smrg return x;
78*627f7eb2Smrg }
79