This source file includes following definitions.
- _sp_maddf
- ieee754sp_maddf
- ieee754sp_msubf
1
2
3
4
5
6
7
8
9
10
11
12 #include "ieee754sp.h"
13
14
15 static union ieee754sp _sp_maddf(union ieee754sp z, union ieee754sp x,
16 union ieee754sp y, enum maddf_flags flags)
17 {
18 int re;
19 int rs;
20 unsigned int rm;
21 u64 rm64;
22 u64 zm64;
23 int s;
24
25 COMPXSP;
26 COMPYSP;
27 COMPZSP;
28
29 EXPLODEXSP;
30 EXPLODEYSP;
31 EXPLODEZSP;
32
33 FLUSHXSP;
34 FLUSHYSP;
35 FLUSHZSP;
36
37 ieee754_clearcx();
38
39
40
41
42
43 if (zc == IEEE754_CLASS_SNAN)
44 return ieee754sp_nanxcpt(z);
45 if (xc == IEEE754_CLASS_SNAN)
46 return ieee754sp_nanxcpt(x);
47 if (yc == IEEE754_CLASS_SNAN)
48 return ieee754sp_nanxcpt(y);
49 if (zc == IEEE754_CLASS_QNAN)
50 return z;
51 if (xc == IEEE754_CLASS_QNAN)
52 return x;
53 if (yc == IEEE754_CLASS_QNAN)
54 return y;
55
56 if (zc == IEEE754_CLASS_DNORM)
57 SPDNORMZ;
58
59
60 switch (CLPAIR(xc, yc)) {
61
62
63
64
65
66 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO):
67 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF):
68 ieee754_setcx(IEEE754_INVALID_OPERATION);
69 return ieee754sp_indef();
70
71 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF):
72 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF):
73 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM):
74 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM):
75 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF):
76 if ((zc == IEEE754_CLASS_INF) &&
77 ((!(flags & MADDF_NEGATE_PRODUCT) && (zs != (xs ^ ys))) ||
78 ((flags & MADDF_NEGATE_PRODUCT) && (zs == (xs ^ ys))))) {
79
80
81
82
83 ieee754_setcx(IEEE754_INVALID_OPERATION);
84 return ieee754sp_indef();
85 }
86
87
88
89
90
91
92
93 if (flags & MADDF_NEGATE_PRODUCT)
94 return ieee754sp_inf(1 ^ (xs ^ ys));
95 else
96 return ieee754sp_inf(xs ^ ys);
97
98 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO):
99 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM):
100 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM):
101 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO):
102 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO):
103 if (zc == IEEE754_CLASS_INF)
104 return ieee754sp_inf(zs);
105 if (zc == IEEE754_CLASS_ZERO) {
106
107 if ((!(flags & MADDF_NEGATE_PRODUCT)
108 && (zs == (xs ^ ys))) ||
109 ((flags & MADDF_NEGATE_PRODUCT)
110 && (zs != (xs ^ ys))))
111
112
113
114
115
116
117 return z;
118
119 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
120 }
121
122 return z;
123
124 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM):
125 SPDNORMX;
126
127
128 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM):
129 if (zc == IEEE754_CLASS_INF)
130 return ieee754sp_inf(zs);
131 SPDNORMY;
132 break;
133
134 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM):
135 if (zc == IEEE754_CLASS_INF)
136 return ieee754sp_inf(zs);
137 SPDNORMX;
138 break;
139
140 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM):
141 if (zc == IEEE754_CLASS_INF)
142 return ieee754sp_inf(zs);
143
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157 assert(xm & SP_HIDDEN_BIT);
158 assert(ym & SP_HIDDEN_BIT);
159
160 re = xe + ye;
161 rs = xs ^ ys;
162 if (flags & MADDF_NEGATE_PRODUCT)
163 rs ^= 1;
164
165
166 rm64 = (uint64_t)xm * ym;
167
168
169 rm64 = rm64 << 16;
170
171
172 if ((int64_t) rm64 < 0) {
173 rm64 = rm64 >> 1;
174 re++;
175 }
176
177 assert(rm64 & (1 << 62));
178
179 if (zc == IEEE754_CLASS_ZERO) {
180
181
182
183
184
185 rm = XSPSRS64(rm64, (62 - 26));
186 return ieee754sp_format(rs, re, rm);
187 }
188
189
190 zm64 = (uint64_t)zm << (62 - 23);
191 assert(zm64 & (1 << 62));
192
193
194 if (ze > re) {
195
196
197
198 s = ze - re;
199 rm64 = XSPSRS64(rm64, s);
200 re += s;
201 } else if (re > ze) {
202
203
204
205 s = re - ze;
206 zm64 = XSPSRS64(zm64, s);
207 ze += s;
208 }
209 assert(ze == re);
210 assert(ze <= SP_EMAX);
211
212
213 if (zs == rs) {
214
215
216
217
218 zm64 = zm64 + rm64;
219 if ((int64_t)zm64 < 0) {
220 zm64 = XSPSRS1(zm64);
221 ze++;
222 }
223 } else {
224 if (zm64 >= rm64) {
225 zm64 = zm64 - rm64;
226 } else {
227 zm64 = rm64 - zm64;
228 zs = rs;
229 }
230 if (zm64 == 0)
231 return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD);
232
233
234
235
236 while ((zm64 >> 62) == 0) {
237 zm64 <<= 1;
238 ze--;
239 }
240 }
241
242
243
244
245
246
247 zm = XSPSRS64(zm64, (62 - 26));
248
249 return ieee754sp_format(zs, ze, zm);
250 }
251
252 union ieee754sp ieee754sp_maddf(union ieee754sp z, union ieee754sp x,
253 union ieee754sp y)
254 {
255 return _sp_maddf(z, x, y, 0);
256 }
257
258 union ieee754sp ieee754sp_msubf(union ieee754sp z, union ieee754sp x,
259 union ieee754sp y)
260 {
261 return _sp_maddf(z, x, y, MADDF_NEGATE_PRODUCT);
262 }