19d5a6349SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* IEEE754 floating point arithmetic
31da177e4SLinus Torvalds * double precision: common utilities
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds * MIPS floating point support
71da177e4SLinus Torvalds * Copyright (C) 1994-2000 Algorithmics Ltd.
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include "ieee754dp.h"
111da177e4SLinus Torvalds
ieee754dp_div(union ieee754dp x,union ieee754dp y)122209bcb1SRalf Baechle union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y)
131da177e4SLinus Torvalds {
143f7cac41SRalf Baechle u64 rm;
153f7cac41SRalf Baechle int re;
163f7cac41SRalf Baechle u64 bm;
173f7cac41SRalf Baechle
181da177e4SLinus Torvalds COMPXDP;
191da177e4SLinus Torvalds COMPYDP;
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds EXPLODEXDP;
221da177e4SLinus Torvalds EXPLODEYDP;
231da177e4SLinus Torvalds
249e8bad1fSRalf Baechle ieee754_clearcx();
251da177e4SLinus Torvalds
261da177e4SLinus Torvalds FLUSHXDP;
271da177e4SLinus Torvalds FLUSHYDP;
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds switch (CLPAIR(xc, yc)) {
301da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
311da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
321da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
331da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
341da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
35d5afa7e9SMaciej W. Rozycki return ieee754dp_nanxcpt(y);
36d5afa7e9SMaciej W. Rozycki
37d5afa7e9SMaciej W. Rozycki case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
38d5afa7e9SMaciej W. Rozycki case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
391da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
401da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
411da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
421da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
43d5afa7e9SMaciej W. Rozycki return ieee754dp_nanxcpt(x);
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
461da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
471da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
481da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
491da177e4SLinus Torvalds return y;
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
521da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
531da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
541da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
551da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
561da177e4SLinus Torvalds return x;
571da177e4SLinus Torvalds
581da177e4SLinus Torvalds
593f7cac41SRalf Baechle /*
603f7cac41SRalf Baechle * Infinity handling
611da177e4SLinus Torvalds */
621da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
639e8bad1fSRalf Baechle ieee754_setcx(IEEE754_INVALID_OPERATION);
6490efba36SRalf Baechle return ieee754dp_indef();
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
671da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
681da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
691da177e4SLinus Torvalds return ieee754dp_zero(xs ^ ys);
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
721da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
731da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
741da177e4SLinus Torvalds return ieee754dp_inf(xs ^ ys);
751da177e4SLinus Torvalds
763f7cac41SRalf Baechle /*
773f7cac41SRalf Baechle * Zero handling
781da177e4SLinus Torvalds */
791da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
809e8bad1fSRalf Baechle ieee754_setcx(IEEE754_INVALID_OPERATION);
8190efba36SRalf Baechle return ieee754dp_indef();
821da177e4SLinus Torvalds
831da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
841da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
859e8bad1fSRalf Baechle ieee754_setcx(IEEE754_ZERO_DIVIDE);
8690efba36SRalf Baechle return ieee754dp_inf(xs ^ ys);
871da177e4SLinus Torvalds
881da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
891da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
901da177e4SLinus Torvalds return ieee754dp_zero(xs == ys ? 0 : 1);
911da177e4SLinus Torvalds
921da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
931da177e4SLinus Torvalds DPDNORMX;
94*c9b02990SLiangliang Huang fallthrough;
951da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
961da177e4SLinus Torvalds DPDNORMY;
971da177e4SLinus Torvalds break;
981da177e4SLinus Torvalds
991da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
1001da177e4SLinus Torvalds DPDNORMX;
1011da177e4SLinus Torvalds break;
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
1041da177e4SLinus Torvalds break;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds assert(xm & DP_HIDDEN_BIT);
1071da177e4SLinus Torvalds assert(ym & DP_HIDDEN_BIT);
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds /* provide rounding space */
1101da177e4SLinus Torvalds xm <<= 3;
1111da177e4SLinus Torvalds ym <<= 3;
1121da177e4SLinus Torvalds
1131da177e4SLinus Torvalds /* now the dirty work */
1141da177e4SLinus Torvalds
1153f7cac41SRalf Baechle rm = 0;
1163f7cac41SRalf Baechle re = xe - ye;
1171da177e4SLinus Torvalds
118ad8fb553SRalf Baechle for (bm = DP_MBIT(DP_FBITS + 2); bm; bm >>= 1) {
1191da177e4SLinus Torvalds if (xm >= ym) {
1201da177e4SLinus Torvalds xm -= ym;
1211da177e4SLinus Torvalds rm |= bm;
1221da177e4SLinus Torvalds if (xm == 0)
1231da177e4SLinus Torvalds break;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds xm <<= 1;
1261da177e4SLinus Torvalds }
1273f7cac41SRalf Baechle
1281da177e4SLinus Torvalds rm <<= 1;
1291da177e4SLinus Torvalds if (xm)
1301da177e4SLinus Torvalds rm |= 1; /* have remainder, set sticky */
1311da177e4SLinus Torvalds
1321da177e4SLinus Torvalds assert(rm);
1331da177e4SLinus Torvalds
1343f7cac41SRalf Baechle /*
1353f7cac41SRalf Baechle * Normalise rm to rounding precision ?
1361da177e4SLinus Torvalds */
137ad8fb553SRalf Baechle while ((rm >> (DP_FBITS + 3)) == 0) {
1381da177e4SLinus Torvalds rm <<= 1;
1391da177e4SLinus Torvalds re--;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds
14290efba36SRalf Baechle return ieee754dp_format(xs == ys ? 0 : 1, re, rm);
1431da177e4SLinus Torvalds }
144