xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/truncq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Truncate argument to nearest integral value not larger than the argument.
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
truncq(__float128 x)26*181254a7Smrg truncq (__float128 x)
27*181254a7Smrg {
28*181254a7Smrg   int32_t j0;
29*181254a7Smrg   uint64_t i0, i1, sx;
30*181254a7Smrg 
31*181254a7Smrg   GET_FLT128_WORDS64 (i0, i1, x);
32*181254a7Smrg   sx = i0 & 0x8000000000000000ULL;
33*181254a7Smrg   j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
34*181254a7Smrg   if (j0 < 48)
35*181254a7Smrg     {
36*181254a7Smrg       if (j0 < 0)
37*181254a7Smrg 	/* The magnitude of the number is < 1 so the result is +-0.  */
38*181254a7Smrg 	SET_FLT128_WORDS64 (x, sx, 0);
39*181254a7Smrg       else
40*181254a7Smrg 	SET_FLT128_WORDS64 (x, i0 & ~(0x0000ffffffffffffLL >> j0), 0);
41*181254a7Smrg     }
42*181254a7Smrg   else if (j0 > 111)
43*181254a7Smrg     {
44*181254a7Smrg       if (j0 == 0x4000)
45*181254a7Smrg 	/* x is inf or NaN.  */
46*181254a7Smrg 	return x + x;
47*181254a7Smrg     }
48*181254a7Smrg   else
49*181254a7Smrg     {
50*181254a7Smrg       SET_FLT128_WORDS64 (x, i0, i1 & ~(0xffffffffffffffffULL >> (j0 - 48)));
51*181254a7Smrg     }
52*181254a7Smrg 
53*181254a7Smrg   return x;
54*181254a7Smrg }
55