xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/remquoq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Compute remainder and a congruent to the quotient.
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 static const __float128 zero = 0.0;
24*181254a7Smrg 
25*181254a7Smrg 
26*181254a7Smrg __float128
remquoq(__float128 x,__float128 y,int * quo)27*181254a7Smrg remquoq (__float128 x, __float128 y, int *quo)
28*181254a7Smrg {
29*181254a7Smrg   int64_t hx,hy;
30*181254a7Smrg   uint64_t sx,lx,ly,qs;
31*181254a7Smrg   int cquo;
32*181254a7Smrg 
33*181254a7Smrg   GET_FLT128_WORDS64 (hx, lx, x);
34*181254a7Smrg   GET_FLT128_WORDS64 (hy, ly, y);
35*181254a7Smrg   sx = hx & 0x8000000000000000ULL;
36*181254a7Smrg   qs = sx ^ (hy & 0x8000000000000000ULL);
37*181254a7Smrg   hy &= 0x7fffffffffffffffLL;
38*181254a7Smrg   hx &= 0x7fffffffffffffffLL;
39*181254a7Smrg 
40*181254a7Smrg   /* Purge off exception values.  */
41*181254a7Smrg   if ((hy | ly) == 0)
42*181254a7Smrg     return (x * y) / (x * y); 			/* y = 0 */
43*181254a7Smrg   if ((hx >= 0x7fff000000000000LL)		/* x not finite */
44*181254a7Smrg       || ((hy >= 0x7fff000000000000LL)		/* y is NaN */
45*181254a7Smrg 	  && (((hy - 0x7fff000000000000LL) | ly) != 0)))
46*181254a7Smrg     return (x * y) / (x * y);
47*181254a7Smrg 
48*181254a7Smrg   if (hy <= 0x7ffbffffffffffffLL)
49*181254a7Smrg     x = fmodq (x, 8 * y);              /* now x < 8y */
50*181254a7Smrg 
51*181254a7Smrg   if (((hx - hy) | (lx - ly)) == 0)
52*181254a7Smrg     {
53*181254a7Smrg       *quo = qs ? -1 : 1;
54*181254a7Smrg       return zero * x;
55*181254a7Smrg     }
56*181254a7Smrg 
57*181254a7Smrg   x  = fabsq (x);
58*181254a7Smrg   y  = fabsq (y);
59*181254a7Smrg   cquo = 0;
60*181254a7Smrg 
61*181254a7Smrg   if (hy <= 0x7ffcffffffffffffLL && x >= 4 * y)
62*181254a7Smrg     {
63*181254a7Smrg       x -= 4 * y;
64*181254a7Smrg       cquo += 4;
65*181254a7Smrg     }
66*181254a7Smrg   if (hy <= 0x7ffdffffffffffffLL && x >= 2 * y)
67*181254a7Smrg     {
68*181254a7Smrg       x -= 2 * y;
69*181254a7Smrg       cquo += 2;
70*181254a7Smrg     }
71*181254a7Smrg 
72*181254a7Smrg   if (hy < 0x0002000000000000LL)
73*181254a7Smrg     {
74*181254a7Smrg       if (x + x > y)
75*181254a7Smrg 	{
76*181254a7Smrg 	  x -= y;
77*181254a7Smrg 	  ++cquo;
78*181254a7Smrg 	  if (x + x >= y)
79*181254a7Smrg 	    {
80*181254a7Smrg 	      x -= y;
81*181254a7Smrg 	      ++cquo;
82*181254a7Smrg 	    }
83*181254a7Smrg 	}
84*181254a7Smrg     }
85*181254a7Smrg   else
86*181254a7Smrg     {
87*181254a7Smrg       __float128 y_half = 0.5Q * y;
88*181254a7Smrg       if (x > y_half)
89*181254a7Smrg 	{
90*181254a7Smrg 	  x -= y;
91*181254a7Smrg 	  ++cquo;
92*181254a7Smrg 	  if (x >= y_half)
93*181254a7Smrg 	    {
94*181254a7Smrg 	      x -= y;
95*181254a7Smrg 	      ++cquo;
96*181254a7Smrg 	    }
97*181254a7Smrg 	}
98*181254a7Smrg     }
99*181254a7Smrg 
100*181254a7Smrg   *quo = qs ? -cquo : cquo;
101*181254a7Smrg 
102*181254a7Smrg   /* Ensure correct sign of zero result in round-downward mode.  */
103*181254a7Smrg   if (x == 0)
104*181254a7Smrg     x = 0;
105*181254a7Smrg   if (sx)
106*181254a7Smrg     x = -x;
107*181254a7Smrg   return x;
108*181254a7Smrg }
109