xref: /aosp_15_r20/external/compiler-rt/lib/builtins/ppc/gcc_qdiv.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* This file is distributed under the University of Illinois Open Source
2*7c3d14c8STreehugger Robot  * License. See LICENSE.TXT for details.
3*7c3d14c8STreehugger Robot  */
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot /* long double __gcc_qdiv(long double x, long double y);
6*7c3d14c8STreehugger Robot  * This file implements the PowerPC 128-bit double-double division operation.
7*7c3d14c8STreehugger Robot  * This implementation is shamelessly cribbed from Apple's DDRT, circa 1993(!)
8*7c3d14c8STreehugger Robot  */
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot #include "DD.h"
11*7c3d14c8STreehugger Robot 
__gcc_qdiv(long double a,long double b)12*7c3d14c8STreehugger Robot long double __gcc_qdiv(long double a, long double b)
13*7c3d14c8STreehugger Robot {
14*7c3d14c8STreehugger Robot 	static const uint32_t infinityHi = UINT32_C(0x7ff00000);
15*7c3d14c8STreehugger Robot 	DD dst = { .ld = a }, src = { .ld = b };
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot 	register double x = dst.s.hi, x1 = dst.s.lo,
18*7c3d14c8STreehugger Robot 					y = src.s.hi, y1 = src.s.lo;
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot     double yHi, yLo, qHi, qLo;
21*7c3d14c8STreehugger Robot     double yq, tmp, q;
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot     q = x / y;
24*7c3d14c8STreehugger Robot 
25*7c3d14c8STreehugger Robot 	/* Detect special cases */
26*7c3d14c8STreehugger Robot 	if (q == 0.0) {
27*7c3d14c8STreehugger Robot 		dst.s.hi = q;
28*7c3d14c8STreehugger Robot 		dst.s.lo = 0.0;
29*7c3d14c8STreehugger Robot 		return dst.ld;
30*7c3d14c8STreehugger Robot 	}
31*7c3d14c8STreehugger Robot 
32*7c3d14c8STreehugger Robot 	const doublebits qBits = { .d = q };
33*7c3d14c8STreehugger Robot 	if (((uint32_t)(qBits.x >> 32) & infinityHi) == infinityHi) {
34*7c3d14c8STreehugger Robot 		dst.s.hi = q;
35*7c3d14c8STreehugger Robot 		dst.s.lo = 0.0;
36*7c3d14c8STreehugger Robot 		return dst.ld;
37*7c3d14c8STreehugger Robot 	}
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot     yHi = high26bits(y);
40*7c3d14c8STreehugger Robot     qHi = high26bits(q);
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot     yq = y * q;
43*7c3d14c8STreehugger Robot     yLo = y - yHi;
44*7c3d14c8STreehugger Robot     qLo = q - qHi;
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot     tmp = LOWORDER(yq, yHi, yLo, qHi, qLo);
47*7c3d14c8STreehugger Robot     tmp = (x - yq) - tmp;
48*7c3d14c8STreehugger Robot     tmp = ((tmp + x1) - y1 * q) / y;
49*7c3d14c8STreehugger Robot     x = q + tmp;
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot     dst.s.lo = (q - x) + tmp;
52*7c3d14c8STreehugger Robot     dst.s.hi = x;
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot     return dst.ld;
55*7c3d14c8STreehugger Robot }
56