blob: 70aa00b644005bfdc906a346764adf14e75f8244 [file] [log] [blame]
Edward O'Callaghanccf48132009-08-09 18:41:02 +00001/* This file is distributed under the University of Illinois Open Source
2 * License. See LICENSE.TXT for details.
3 */
Daniel Dunbarfd089992009-06-26 16:47:03 +00004
Edward O'Callaghanccf48132009-08-09 18:41:02 +00005/* long double __gcc_qdiv(long double x, long double y);
6 * This file implements the PowerPC 128-bit double-double division operation.
7 * This implementation is shamelessly cribbed from Apple's DDRT, circa 1993(!)
8 */
Daniel Dunbarfd089992009-06-26 16:47:03 +00009
10#include "DD.h"
11
12long double __gcc_qdiv(long double a, long double b)
13{
14 static const uint32_t infinityHi = UINT32_C(0x7ff00000);
15 DD dst = { .ld = a }, src = { .ld = b };
16
Edward O'Callaghanccf48132009-08-09 18:41:02 +000017 register double x = dst.s.hi, x1 = dst.s.lo,
18 y = src.s.hi, y1 = src.s.lo;
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
20 double yHi, yLo, qHi, qLo;
21 double yq, tmp, q;
22
23 q = x / y;
24
Edward O'Callaghanccf48132009-08-09 18:41:02 +000025 /* Detect special cases */
Daniel Dunbarfd089992009-06-26 16:47:03 +000026 if (q == 0.0) {
Edward O'Callaghanccf48132009-08-09 18:41:02 +000027 dst.s.hi = q;
28 dst.s.lo = 0.0;
Daniel Dunbarfd089992009-06-26 16:47:03 +000029 return dst.ld;
30 }
31
32 const doublebits qBits = { .d = q };
33 if (((uint32_t)(qBits.x >> 32) & infinityHi) == infinityHi) {
Edward O'Callaghanccf48132009-08-09 18:41:02 +000034 dst.s.hi = q;
35 dst.s.lo = 0.0;
Daniel Dunbarfd089992009-06-26 16:47:03 +000036 return dst.ld;
37 }
38
39 yHi = high26bits(y);
40 qHi = high26bits(q);
41
42 yq = y * q;
43 yLo = y - yHi;
44 qLo = q - qHi;
45
46 tmp = LOWORDER(yq, yHi, yLo, qHi, qLo);
47 tmp = (x - yq) - tmp;
48 tmp = ((tmp + x1) - y1 * q) / y;
49 x = q + tmp;
50
Edward O'Callaghanccf48132009-08-09 18:41:02 +000051 dst.s.lo = (q - x) + tmp;
52 dst.s.hi = x;
Daniel Dunbarfd089992009-06-26 16:47:03 +000053
54 return dst.ld;
55}