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