blob: bef0e55e59381e2b5c5d03b4970a0f8132a3b6b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* IEEE754 floating point arithmetic
2 * double precision: common utilities
3 */
4/*
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can distribute it and/or modify it
9 * under the terms of the GNU General Public License (Version 2) as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
Ralf Baechle3f7cac42014-04-26 01:49:14 +020019 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "ieee754dp.h"
23
Ralf Baechle2209bcb2014-04-16 01:31:11 +020024union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Ralf Baechle3f7cac42014-04-26 01:49:14 +020026 u64 rm;
27 int re;
28 u64 bm;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 COMPXDP;
31 COMPYDP;
32
33 EXPLODEXDP;
34 EXPLODEYDP;
35
Ralf Baechle9e8bad12014-04-19 00:36:32 +020036 ieee754_clearcx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 FLUSHXDP;
39 FLUSHYDP;
40
41 switch (CLPAIR(xc, yc)) {
42 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
43 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
44 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
45 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
46 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
47 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
48 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
49 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
50 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
51 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
52 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
Ralf Baechle9e8bad12014-04-19 00:36:32 +020053 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020054 return ieee754dp_nanxcpt(ieee754dp_indef());
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
57 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
58 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
59 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
60 return y;
61
62 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
63 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
64 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
65 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
66 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
67 return x;
68
69
Ralf Baechle3f7cac42014-04-26 01:49:14 +020070 /*
71 * Infinity handling
72 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
Ralf Baechle9e8bad12014-04-19 00:36:32 +020074 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020075 return ieee754dp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
78 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
79 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
80 return ieee754dp_zero(xs ^ ys);
81
82 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
83 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
84 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
85 return ieee754dp_inf(xs ^ ys);
86
Ralf Baechle3f7cac42014-04-26 01:49:14 +020087 /*
88 * Zero handling
89 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
Ralf Baechle9e8bad12014-04-19 00:36:32 +020091 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020092 return ieee754dp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
95 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
Ralf Baechle9e8bad12014-04-19 00:36:32 +020096 ieee754_setcx(IEEE754_ZERO_DIVIDE);
Ralf Baechle90efba32014-04-25 03:19:57 +020097 return ieee754dp_inf(xs ^ ys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
100 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
101 return ieee754dp_zero(xs == ys ? 0 : 1);
102
103 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
104 DPDNORMX;
105
106 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
107 DPDNORMY;
108 break;
109
110 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
111 DPDNORMX;
112 break;
113
114 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
115 break;
116 }
117 assert(xm & DP_HIDDEN_BIT);
118 assert(ym & DP_HIDDEN_BIT);
119
120 /* provide rounding space */
121 xm <<= 3;
122 ym <<= 3;
123
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200124 /* now the dirty work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200126 rm = 0;
127 re = xe - ye;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200129 for (bm = DP_MBIT(DP_FBITS + 2); bm; bm >>= 1) {
130 if (xm >= ym) {
131 xm -= ym;
132 rm |= bm;
133 if (xm == 0)
134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200136 xm <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200138
139 rm <<= 1;
140 if (xm)
141 rm |= 1; /* have remainder, set sticky */
142
143 assert(rm);
144
145 /*
146 * Normalise rm to rounding precision ?
147 */
148 while ((rm >> (DP_FBITS + 3)) == 0) {
149 rm <<= 1;
150 re--;
151 }
152
153 return ieee754dp_format(xs == ys ? 0 : 1, re, rm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}