xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/roundq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Round long double to integer away from zero.
2*181254a7Smrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
3*181254a7Smrg    This file is part of the GNU C Library.
4*181254a7Smrg    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
5*181254a7Smrg 		  Jakub Jelinek <jj@ultra.linux.cz>, 1999.
6*181254a7Smrg 
7*181254a7Smrg    The GNU C Library is free software; you can redistribute it and/or
8*181254a7Smrg    modify it under the terms of the GNU Lesser General Public
9*181254a7Smrg    License as published by the Free Software Foundation; either
10*181254a7Smrg    version 2.1 of the License, or (at your option) any later version.
11*181254a7Smrg 
12*181254a7Smrg    The GNU C Library is distributed in the hope that it will be useful,
13*181254a7Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*181254a7Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*181254a7Smrg    Lesser General Public License for more details.
16*181254a7Smrg 
17*181254a7Smrg    You should have received a copy of the GNU Lesser General Public
18*181254a7Smrg    License along with the GNU C Library; if not, see
19*181254a7Smrg    <http://www.gnu.org/licenses/>.  */
20*181254a7Smrg 
21*181254a7Smrg #define NO_MATH_REDIRECT
22*181254a7Smrg 
23*181254a7Smrg #include "quadmath-imp.h"
24*181254a7Smrg 
25*181254a7Smrg __float128
roundq(__float128 x)26*181254a7Smrg roundq (__float128 x)
27*181254a7Smrg {
28*181254a7Smrg   int32_t j0;
29*181254a7Smrg   uint64_t i1, i0;
30*181254a7Smrg 
31*181254a7Smrg   GET_FLT128_WORDS64 (i0, i1, x);
32*181254a7Smrg   j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
33*181254a7Smrg   if (j0 < 48)
34*181254a7Smrg     {
35*181254a7Smrg       if (j0 < 0)
36*181254a7Smrg 	{
37*181254a7Smrg 	  i0 &= 0x8000000000000000ULL;
38*181254a7Smrg 	  if (j0 == -1)
39*181254a7Smrg 	    i0 |= 0x3fff000000000000LL;
40*181254a7Smrg 	  i1 = 0;
41*181254a7Smrg 	}
42*181254a7Smrg       else
43*181254a7Smrg 	{
44*181254a7Smrg 	  uint64_t i = 0x0000ffffffffffffLL >> j0;
45*181254a7Smrg 	  if (((i0 & i) | i1) == 0)
46*181254a7Smrg 	    /* X is integral.  */
47*181254a7Smrg 	    return x;
48*181254a7Smrg 
49*181254a7Smrg 	  i0 += 0x0000800000000000LL >> j0;
50*181254a7Smrg 	  i0 &= ~i;
51*181254a7Smrg 	  i1 = 0;
52*181254a7Smrg 	}
53*181254a7Smrg     }
54*181254a7Smrg   else if (j0 > 111)
55*181254a7Smrg     {
56*181254a7Smrg       if (j0 == 0x4000)
57*181254a7Smrg 	/* Inf or NaN.  */
58*181254a7Smrg 	return x + x;
59*181254a7Smrg       else
60*181254a7Smrg 	return x;
61*181254a7Smrg     }
62*181254a7Smrg   else
63*181254a7Smrg     {
64*181254a7Smrg       uint64_t i = -1ULL >> (j0 - 48);
65*181254a7Smrg       if ((i1 & i) == 0)
66*181254a7Smrg 	/* X is integral.  */
67*181254a7Smrg 	return x;
68*181254a7Smrg 
69*181254a7Smrg       uint64_t j = i1 + (1LL << (111 - j0));
70*181254a7Smrg       if (j < i1)
71*181254a7Smrg 	i0 += 1;
72*181254a7Smrg       i1 = j;
73*181254a7Smrg       i1 &= ~i;
74*181254a7Smrg     }
75*181254a7Smrg 
76*181254a7Smrg   SET_FLT128_WORDS64 (x, i0, i1);
77*181254a7Smrg   return x;
78*181254a7Smrg }
79