xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/lrintq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Round argument to nearest integral value according to current rounding
2*181254a7Smrg    direction.
3*181254a7Smrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
4*181254a7Smrg    This file is part of the GNU C Library.
5*181254a7Smrg    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
6*181254a7Smrg 		  Jakub Jelinek <jj@ultra.linux.cz>, 1999.
7*181254a7Smrg 
8*181254a7Smrg    The GNU C Library is free software; you can redistribute it and/or
9*181254a7Smrg    modify it under the terms of the GNU Lesser General Public
10*181254a7Smrg    License as published by the Free Software Foundation; either
11*181254a7Smrg    version 2.1 of the License, or (at your option) any later version.
12*181254a7Smrg 
13*181254a7Smrg    The GNU C Library is distributed in the hope that it will be useful,
14*181254a7Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*181254a7Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*181254a7Smrg    Lesser General Public License for more details.
17*181254a7Smrg 
18*181254a7Smrg    You should have received a copy of the GNU Lesser General Public
19*181254a7Smrg    License along with the GNU C Library; if not, see
20*181254a7Smrg    <http://www.gnu.org/licenses/>.  */
21*181254a7Smrg 
22*181254a7Smrg #include "quadmath-imp.h"
23*181254a7Smrg 
24*181254a7Smrg static const __float128 two112[2] =
25*181254a7Smrg {
26*181254a7Smrg   5.19229685853482762853049632922009600E+33Q, /* 0x406F000000000000, 0 */
27*181254a7Smrg  -5.19229685853482762853049632922009600E+33Q  /* 0xC06F000000000000, 0 */
28*181254a7Smrg };
29*181254a7Smrg 
30*181254a7Smrg long int
lrintq(__float128 x)31*181254a7Smrg lrintq (__float128 x)
32*181254a7Smrg {
33*181254a7Smrg   int32_t j0;
34*181254a7Smrg   uint64_t i0,i1;
35*181254a7Smrg   __float128 w;
36*181254a7Smrg   __float128 t;
37*181254a7Smrg   long int result;
38*181254a7Smrg   int sx;
39*181254a7Smrg 
40*181254a7Smrg   GET_FLT128_WORDS64 (i0, i1, x);
41*181254a7Smrg   j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
42*181254a7Smrg   sx = i0 >> 63;
43*181254a7Smrg   i0 &= 0x0000ffffffffffffLL;
44*181254a7Smrg   i0 |= 0x0001000000000000LL;
45*181254a7Smrg 
46*181254a7Smrg   if (j0 < (int32_t) (8 * sizeof (long int)) - 1)
47*181254a7Smrg     {
48*181254a7Smrg       if (j0 < 48)
49*181254a7Smrg 	{
50*181254a7Smrg #if defined FE_INVALID || defined FE_INEXACT
51*181254a7Smrg 	  /* X < LONG_MAX + 1 implied by J0 < 31.  */
52*181254a7Smrg 	  if (sizeof (long int) == 4
53*181254a7Smrg 	      && x > (__float128) LONG_MAX)
54*181254a7Smrg 	    {
55*181254a7Smrg 	      /* In the event of overflow we must raise the "invalid"
56*181254a7Smrg 		 exception, but not "inexact".  */
57*181254a7Smrg 	      t = nearbyintq (x);
58*181254a7Smrg 	      feraiseexcept (t == LONG_MAX ? FE_INEXACT : FE_INVALID);
59*181254a7Smrg 	    }
60*181254a7Smrg 	  else
61*181254a7Smrg #endif
62*181254a7Smrg 	    {
63*181254a7Smrg 	      w = two112[sx] + x;
64*181254a7Smrg 	      t = w - two112[sx];
65*181254a7Smrg 	    }
66*181254a7Smrg 	  GET_FLT128_WORDS64 (i0, i1, t);
67*181254a7Smrg 	  j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
68*181254a7Smrg 	  i0 &= 0x0000ffffffffffffLL;
69*181254a7Smrg 	  i0 |= 0x0001000000000000LL;
70*181254a7Smrg 
71*181254a7Smrg 	  result = (j0 < 0 ? 0 : i0 >> (48 - j0));
72*181254a7Smrg 	}
73*181254a7Smrg       else if (j0 >= 112)
74*181254a7Smrg 	result = ((long int) i0 << (j0 - 48)) | (i1 << (j0 - 112));
75*181254a7Smrg       else
76*181254a7Smrg 	{
77*181254a7Smrg #if defined FE_INVALID || defined FE_INEXACT
78*181254a7Smrg 	  /* X < LONG_MAX + 1 implied by J0 < 63.  */
79*181254a7Smrg 	  if (sizeof (long int) == 8
80*181254a7Smrg 	      && x > (__float128) LONG_MAX)
81*181254a7Smrg 	    {
82*181254a7Smrg 	      /* In the event of overflow we must raise the "invalid"
83*181254a7Smrg 		 exception, but not "inexact".  */
84*181254a7Smrg 	      t = nearbyintq (x);
85*181254a7Smrg 	      feraiseexcept (t == LONG_MAX ? FE_INEXACT : FE_INVALID);
86*181254a7Smrg 	    }
87*181254a7Smrg 	  else
88*181254a7Smrg #endif
89*181254a7Smrg 	    {
90*181254a7Smrg 	      w = two112[sx] + x;
91*181254a7Smrg 	      t = w - two112[sx];
92*181254a7Smrg 	    }
93*181254a7Smrg 	  GET_FLT128_WORDS64 (i0, i1, t);
94*181254a7Smrg 	  j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
95*181254a7Smrg 	  i0 &= 0x0000ffffffffffffLL;
96*181254a7Smrg 	  i0 |= 0x0001000000000000LL;
97*181254a7Smrg 
98*181254a7Smrg 	  if (j0 == 48)
99*181254a7Smrg 	    result = (long int) i0;
100*181254a7Smrg 	  else
101*181254a7Smrg 	    result = ((long int) i0 << (j0 - 48)) | (i1 >> (112 - j0));
102*181254a7Smrg 	}
103*181254a7Smrg     }
104*181254a7Smrg   else
105*181254a7Smrg     {
106*181254a7Smrg       /* The number is too large.  Unless it rounds to LONG_MIN,
107*181254a7Smrg 	 FE_INVALID must be raised and the return value is
108*181254a7Smrg 	 unspecified.  */
109*181254a7Smrg #if defined FE_INVALID || defined FE_INEXACT
110*181254a7Smrg       if (x < (__float128) LONG_MIN
111*181254a7Smrg 	  && x > (__float128) LONG_MIN - 1)
112*181254a7Smrg 	{
113*181254a7Smrg 	  /* If truncation produces LONG_MIN, the cast will not raise
114*181254a7Smrg 	     the exception, but may raise "inexact".  */
115*181254a7Smrg 	  t = nearbyintq (x);
116*181254a7Smrg 	  feraiseexcept (t == LONG_MIN ? FE_INEXACT : FE_INVALID);
117*181254a7Smrg 	  return LONG_MIN;
118*181254a7Smrg 	}
119*181254a7Smrg       else if (FIX_FLT128_LONG_CONVERT_OVERFLOW && x != (__float128) LONG_MIN)
120*181254a7Smrg 	{
121*181254a7Smrg 	  feraiseexcept (FE_INVALID);
122*181254a7Smrg 	  return sx == 0 ? LONG_MAX : LONG_MIN;
123*181254a7Smrg 	}
124*181254a7Smrg 
125*181254a7Smrg #endif
126*181254a7Smrg       return (long int) x;
127*181254a7Smrg     }
128*181254a7Smrg 
129*181254a7Smrg   return sx ? -result : result;
130*181254a7Smrg }
131