blob: 715cd0534301ec54958b821bc0690d361aafce89 [file] [log] [blame]
Thomas Gleixner9d5a6342019-05-31 01:09:55 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* IEEE754 floating point arithmetic
3 * single precision
4 */
5/*
6 * MIPS floating point support
7 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "ieee754sp.h"
11
Ralf Baechle2209bcb2014-04-16 01:31:11 +020012union ieee754sp ieee754sp_add(union ieee754sp x, union ieee754sp y)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
Ralf Baechle3f7cac42014-04-26 01:49:14 +020014 int s;
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 COMPXSP;
17 COMPYSP;
18
19 EXPLODEXSP;
20 EXPLODEYSP;
21
Ralf Baechle9e8bad12014-04-19 00:36:32 +020022 ieee754_clearcx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 FLUSHXSP;
25 FLUSHYSP;
26
27 switch (CLPAIR(xc, yc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN):
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN):
30 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN):
31 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN):
32 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN):
Maciej W. Rozyckid5afa7e2015-04-03 23:25:34 +010033 return ieee754sp_nanxcpt(y);
34
35 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN):
36 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN):
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO):
38 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM):
39 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM):
40 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF):
Maciej W. Rozyckid5afa7e2015-04-03 23:25:34 +010041 return ieee754sp_nanxcpt(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN):
44 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN):
45 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN):
46 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN):
47 return y;
48
49 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN):
50 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO):
51 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM):
52 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM):
53 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF):
54 return x;
55
56
Ralf Baechle3f7cac42014-04-26 01:49:14 +020057 /*
58 * Infinity handling
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
61 if (xs == ys)
62 return x;
Ralf Baechle9e8bad12014-04-19 00:36:32 +020063 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020064 return ieee754sp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
67 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
68 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
69 return y;
70
71 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
72 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
73 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
74 return x;
75
Ralf Baechle3f7cac42014-04-26 01:49:14 +020076 /*
77 * Zero handling
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
80 if (xs == ys)
81 return x;
82 else
Ralf Baechle56a64732014-04-30 11:21:55 +020083 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
86 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
87 return x;
88
89 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
90 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
91 return y;
92
93 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
94 SPDNORMX;
Liangliang Huangc9b02992020-05-04 16:51:29 +080095 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
97 SPDNORMY;
98 break;
99
100 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
101 SPDNORMX;
102 break;
103
104 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
105 break;
106 }
107 assert(xm & SP_HIDDEN_BIT);
108 assert(ym & SP_HIDDEN_BIT);
109
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200110 /*
111 * Provide guard, round and stick bit space.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 xm <<= 3;
114 ym <<= 3;
115
116 if (xe > ye) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200117 /*
118 * Have to shift y fraction right to align.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200120 s = xe - ye;
Paul Burtondb57f292016-04-21 14:04:54 +0100121 ym = XSPSRS(ym, s);
122 ye += s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else if (ye > xe) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200124 /*
125 * Have to shift x fraction right to align.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200127 s = ye - xe;
Paul Burtondb57f292016-04-21 14:04:54 +0100128 xm = XSPSRS(xm, s);
129 xe += s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 assert(xe == ye);
132 assert(xe <= SP_EMAX);
133
134 if (xs == ys) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200135 /*
136 * Generate 28 bit result of adding two 27 bit numbers
137 * leaving result in xm, xs and xe.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
139 xm = xm + ym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Ralf Baechlead8fb5532014-04-22 15:51:55 +0200141 if (xm >> (SP_FBITS + 1 + 3)) { /* carry out */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 SPXSRSX1();
143 }
144 } else {
145 if (xm >= ym) {
146 xm = xm - ym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } else {
148 xm = ym - xm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 xs = ys;
150 }
151 if (xm == 0)
Ralf Baechle56a64732014-04-30 11:21:55 +0200152 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200154 /*
155 * Normalize in extended single precision
156 */
Ralf Baechlead8fb5532014-04-22 15:51:55 +0200157 while ((xm >> (SP_FBITS + 3)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 xm <<= 1;
159 xe--;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Ralf Baechle90efba32014-04-25 03:19:57 +0200162
163 return ieee754sp_format(xs, xe, xm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}