xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/lroundq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Round long double value to long int.
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 #include "quadmath-imp.h"
22*181254a7Smrg 
23*181254a7Smrg long int
lroundq(__float128 x)24*181254a7Smrg lroundq (__float128 x)
25*181254a7Smrg {
26*181254a7Smrg   int64_t j0;
27*181254a7Smrg   uint64_t i1, i0;
28*181254a7Smrg   long int result;
29*181254a7Smrg   int sign;
30*181254a7Smrg 
31*181254a7Smrg   GET_FLT128_WORDS64 (i0, i1, x);
32*181254a7Smrg   j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
33*181254a7Smrg   sign = (i0 & 0x8000000000000000ULL) != 0 ? -1 : 1;
34*181254a7Smrg   i0 &= 0x0000ffffffffffffLL;
35*181254a7Smrg   i0 |= 0x0001000000000000LL;
36*181254a7Smrg 
37*181254a7Smrg   if (j0 < (int32_t) (8 * sizeof (long int)) - 1)
38*181254a7Smrg     {
39*181254a7Smrg       if (j0 < 48)
40*181254a7Smrg 	{
41*181254a7Smrg 	  if (j0 < 0)
42*181254a7Smrg 	    return j0 < -1 ? 0 : sign;
43*181254a7Smrg 	  else
44*181254a7Smrg 	    {
45*181254a7Smrg 	      i0 += 0x0000800000000000LL >> j0;
46*181254a7Smrg 	      result = i0 >> (48 - j0);
47*181254a7Smrg #ifdef FE_INVALID
48*181254a7Smrg 	      if (sizeof (long int) == 4
49*181254a7Smrg 		  && sign == 1
50*181254a7Smrg 		  && result == LONG_MIN)
51*181254a7Smrg 		/* Rounding brought the value out of range.  */
52*181254a7Smrg 		feraiseexcept (FE_INVALID);
53*181254a7Smrg #endif
54*181254a7Smrg 	    }
55*181254a7Smrg 	}
56*181254a7Smrg       else if (j0 >= 112)
57*181254a7Smrg 	result = ((long int) i0 << (j0 - 48)) | (i1 << (j0 - 112));
58*181254a7Smrg       else
59*181254a7Smrg 	{
60*181254a7Smrg 	  uint64_t j = i1 + (0x8000000000000000ULL >> (j0 - 48));
61*181254a7Smrg 	  if (j < i1)
62*181254a7Smrg 	    ++i0;
63*181254a7Smrg 
64*181254a7Smrg 	  if (j0 == 48)
65*181254a7Smrg 	    result = (long int) i0;
66*181254a7Smrg 	  else
67*181254a7Smrg 	    {
68*181254a7Smrg 	      result = ((long int) i0 << (j0 - 48)) | (j >> (112 - j0));
69*181254a7Smrg #ifdef FE_INVALID
70*181254a7Smrg 	      if (sizeof (long int) == 8
71*181254a7Smrg 		  && sign == 1
72*181254a7Smrg 		  && result == LONG_MIN)
73*181254a7Smrg 		/* Rounding brought the value out of range.  */
74*181254a7Smrg 		feraiseexcept (FE_INVALID);
75*181254a7Smrg #endif
76*181254a7Smrg 	    }
77*181254a7Smrg 	}
78*181254a7Smrg     }
79*181254a7Smrg   else
80*181254a7Smrg     {
81*181254a7Smrg       /* The number is too large.  Unless it rounds to LONG_MIN,
82*181254a7Smrg 	 FE_INVALID must be raised and the return value is
83*181254a7Smrg 	 unspecified.  */
84*181254a7Smrg #ifdef FE_INVALID
85*181254a7Smrg       if (FIX_FLT128_LONG_CONVERT_OVERFLOW
86*181254a7Smrg 	  && !(sign == -1 && x > (__float128) LONG_MIN - 0.5Q))
87*181254a7Smrg 	{
88*181254a7Smrg 	  feraiseexcept (FE_INVALID);
89*181254a7Smrg 	  return sign == 1 ? LONG_MAX : LONG_MIN;
90*181254a7Smrg 	}
91*181254a7Smrg       else if (!FIX_FLT128_LONG_CONVERT_OVERFLOW
92*181254a7Smrg 	       && x <= (__float128) LONG_MIN - 0.5Q)
93*181254a7Smrg 	{
94*181254a7Smrg 	  /* If truncation produces LONG_MIN, the cast will not raise
95*181254a7Smrg 	     the exception, but may raise "inexact".  */
96*181254a7Smrg 	  feraiseexcept (FE_INVALID);
97*181254a7Smrg 	  return LONG_MIN;
98*181254a7Smrg 	}
99*181254a7Smrg #endif
100*181254a7Smrg       /* The number is too large.  It is left implementation defined
101*181254a7Smrg 	 what happens.  */
102*181254a7Smrg       return (long int) x;
103*181254a7Smrg     }
104*181254a7Smrg 
105*181254a7Smrg   return sign * result;
106*181254a7Smrg }
107