1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
5 * Floating-point emulation code
12 * @(#) pa/spmath/dfmpy.c $Revision: 1.1 $
15 * Double Precision Floating-point Multiply
17 * External Interfaces:
18 * dbl_fmpy(srcptr1,srcptr2,dstptr,status)
20 * Internal Interfaces:
23 * <<please update with a overview of the operation of this file>>
30 #include "dbl_float.h"
33 * Double Precision Floating-point Multiply
38 dbl_floating_point *srcptr1,
39 dbl_floating_point *srcptr2,
40 dbl_floating_point *dstptr,
43 register unsigned int opnd1p1, opnd1p2, opnd2p1, opnd2p2;
44 register unsigned int opnd3p1, opnd3p2, resultp1, resultp2;
45 register int dest_exponent, count;
46 register boolean inexact = FALSE, guardbit = FALSE, stickybit = FALSE;
49 Dbl_copyfromptr(srcptr1,opnd1p1,opnd1p2);
50 Dbl_copyfromptr(srcptr2,opnd2p1,opnd2p2);
53 * set sign bit of result
55 if (Dbl_sign(opnd1p1) ^ Dbl_sign(opnd2p1))
56 Dbl_setnegativezerop1(resultp1);
57 else Dbl_setzerop1(resultp1);
59 * check first operand for NaN's or infinity
61 if (Dbl_isinfinity_exponent(opnd1p1)) {
62 if (Dbl_iszero_mantissa(opnd1p1,opnd1p2)) {
63 if (Dbl_isnotnan(opnd2p1,opnd2p2)) {
64 if (Dbl_iszero_exponentmantissa(opnd2p1,opnd2p2)) {
66 * invalid since operands are infinity
69 if (Is_invalidtrap_enabled())
70 return(INVALIDEXCEPTION);
72 Dbl_makequietnan(resultp1,resultp2);
73 Dbl_copytoptr(resultp1,resultp2,dstptr);
79 Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
80 Dbl_copytoptr(resultp1,resultp2,dstptr);
86 * is NaN; signaling or quiet?
88 if (Dbl_isone_signaling(opnd1p1)) {
89 /* trap if INVALIDTRAP enabled */
90 if (Is_invalidtrap_enabled())
91 return(INVALIDEXCEPTION);
94 Dbl_set_quiet(opnd1p1);
97 * is second operand a signaling NaN?
99 else if (Dbl_is_signalingnan(opnd2p1)) {
100 /* trap if INVALIDTRAP enabled */
101 if (Is_invalidtrap_enabled())
102 return(INVALIDEXCEPTION);
105 Dbl_set_quiet(opnd2p1);
106 Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
112 Dbl_copytoptr(opnd1p1,opnd1p2,dstptr);
117 * check second operand for NaN's or infinity
119 if (Dbl_isinfinity_exponent(opnd2p1)) {
120 if (Dbl_iszero_mantissa(opnd2p1,opnd2p2)) {
121 if (Dbl_iszero_exponentmantissa(opnd1p1,opnd1p2)) {
122 /* invalid since operands are zero & infinity */
123 if (Is_invalidtrap_enabled())
124 return(INVALIDEXCEPTION);
126 Dbl_makequietnan(opnd2p1,opnd2p2);
127 Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
133 Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
134 Dbl_copytoptr(resultp1,resultp2,dstptr);
138 * is NaN; signaling or quiet?
140 if (Dbl_isone_signaling(opnd2p1)) {
141 /* trap if INVALIDTRAP enabled */
142 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
145 Dbl_set_quiet(opnd2p1);
150 Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
156 dest_exponent = Dbl_exponent(opnd1p1) + Dbl_exponent(opnd2p1) -DBL_BIAS;
161 if (Dbl_isnotzero_exponent(opnd1p1)) {
163 Dbl_clear_signexponent_set_hidden(opnd1p1);
167 if (Dbl_iszero_mantissa(opnd1p1,opnd1p2)) {
168 Dbl_setzero_exponentmantissa(resultp1,resultp2);
169 Dbl_copytoptr(resultp1,resultp2,dstptr);
172 /* is denormalized, adjust exponent */
173 Dbl_clear_signexponent(opnd1p1);
174 Dbl_leftshiftby1(opnd1p1,opnd1p2);
175 Dbl_normalize(opnd1p1,opnd1p2,dest_exponent);
177 /* opnd2 needs to have hidden bit set with msb in hidden bit */
178 if (Dbl_isnotzero_exponent(opnd2p1)) {
179 Dbl_clear_signexponent_set_hidden(opnd2p1);
183 if (Dbl_iszero_mantissa(opnd2p1,opnd2p2)) {
184 Dbl_setzero_exponentmantissa(resultp1,resultp2);
185 Dbl_copytoptr(resultp1,resultp2,dstptr);
188 /* is denormalized; want to normalize */
189 Dbl_clear_signexponent(opnd2p1);
190 Dbl_leftshiftby1(opnd2p1,opnd2p2);
191 Dbl_normalize(opnd2p1,opnd2p2,dest_exponent);
194 /* Multiply two source mantissas together */
196 /* make room for guard bits */
197 Dbl_leftshiftby7(opnd2p1,opnd2p2);
198 Dbl_setzero(opnd3p1,opnd3p2);
200 * Four bits at a time are inspected in each loop, and a
201 * simple shift and add multiply algorithm is used.
203 for (count=1;count<=DBL_P;count+=4) {
204 stickybit |= Dlow4p2(opnd3p2);
205 Dbl_rightshiftby4(opnd3p1,opnd3p2);
206 if (Dbit28p2(opnd1p2)) {
207 /* Twoword_add should be an ADDC followed by an ADD. */
208 Twoword_add(opnd3p1, opnd3p2, opnd2p1<<3 | opnd2p2>>29,
211 if (Dbit29p2(opnd1p2)) {
212 Twoword_add(opnd3p1, opnd3p2, opnd2p1<<2 | opnd2p2>>30,
215 if (Dbit30p2(opnd1p2)) {
216 Twoword_add(opnd3p1, opnd3p2, opnd2p1<<1 | opnd2p2>>31,
219 if (Dbit31p2(opnd1p2)) {
220 Twoword_add(opnd3p1, opnd3p2, opnd2p1, opnd2p2);
222 Dbl_rightshiftby4(opnd1p1,opnd1p2);
224 if (Dbit3p1(opnd3p1)==0) {
225 Dbl_leftshiftby1(opnd3p1,opnd3p2);
228 /* result mantissa >= 2. */
231 /* check for denormalized result */
232 while (Dbit3p1(opnd3p1)==0) {
233 Dbl_leftshiftby1(opnd3p1,opnd3p2);
237 * check for guard, sticky and inexact bits
239 stickybit |= Dallp2(opnd3p2) << 25;
240 guardbit = (Dallp2(opnd3p2) << 24) >> 31;
241 inexact = guardbit | stickybit;
243 /* align result mantissa */
244 Dbl_rightshiftby8(opnd3p1,opnd3p2);
249 if (inexact && (dest_exponent>0 || Is_underflowtrap_enabled())) {
250 Dbl_clear_signexponent(opnd3p1);
251 switch (Rounding_mode()) {
253 if (Dbl_iszero_sign(resultp1))
254 Dbl_increment(opnd3p1,opnd3p2);
257 if (Dbl_isone_sign(resultp1))
258 Dbl_increment(opnd3p1,opnd3p2);
262 if (stickybit || Dbl_isone_lowmantissap2(opnd3p2))
263 Dbl_increment(opnd3p1,opnd3p2);
266 if (Dbl_isone_hidden(opnd3p1)) dest_exponent++;
268 Dbl_set_mantissa(resultp1,resultp2,opnd3p1,opnd3p2);
273 if (dest_exponent >= DBL_INFINITY_EXPONENT) {
274 /* trap if OVERFLOWTRAP enabled */
275 if (Is_overflowtrap_enabled()) {
277 * Adjust bias of result
279 Dbl_setwrapped_exponent(resultp1,dest_exponent,ovfl);
280 Dbl_copytoptr(resultp1,resultp2,dstptr);
282 if (Is_inexacttrap_enabled())
283 return (OVERFLOWEXCEPTION | INEXACTEXCEPTION);
284 else Set_inexactflag();
285 return (OVERFLOWEXCEPTION);
289 /* set result to infinity or largest number */
290 Dbl_setoverflow(resultp1,resultp2);
295 else if (dest_exponent <= 0) {
296 /* trap if UNDERFLOWTRAP enabled */
297 if (Is_underflowtrap_enabled()) {
299 * Adjust bias of result
301 Dbl_setwrapped_exponent(resultp1,dest_exponent,unfl);
302 Dbl_copytoptr(resultp1,resultp2,dstptr);
304 if (Is_inexacttrap_enabled())
305 return (UNDERFLOWEXCEPTION | INEXACTEXCEPTION);
306 else Set_inexactflag();
307 return (UNDERFLOWEXCEPTION);
310 /* Determine if should set underflow flag */
312 if (dest_exponent == 0 && inexact) {
313 switch (Rounding_mode()) {
315 if (Dbl_iszero_sign(resultp1)) {
316 Dbl_increment(opnd3p1,opnd3p2);
317 if (Dbl_isone_hiddenoverflow(opnd3p1))
319 Dbl_decrement(opnd3p1,opnd3p2);
323 if (Dbl_isone_sign(resultp1)) {
324 Dbl_increment(opnd3p1,opnd3p2);
325 if (Dbl_isone_hiddenoverflow(opnd3p1))
327 Dbl_decrement(opnd3p1,opnd3p2);
331 if (guardbit && (stickybit ||
332 Dbl_isone_lowmantissap2(opnd3p2))) {
333 Dbl_increment(opnd3p1,opnd3p2);
334 if (Dbl_isone_hiddenoverflow(opnd3p1))
336 Dbl_decrement(opnd3p1,opnd3p2);
343 * denormalize result or set to signed zero
346 Dbl_denormalize(opnd3p1,opnd3p2,dest_exponent,guardbit,
349 /* return zero or smallest number */
351 switch (Rounding_mode()) {
353 if (Dbl_iszero_sign(resultp1)) {
354 Dbl_increment(opnd3p1,opnd3p2);
358 if (Dbl_isone_sign(resultp1)) {
359 Dbl_increment(opnd3p1,opnd3p2);
363 if (guardbit && (stickybit ||
364 Dbl_isone_lowmantissap2(opnd3p2))) {
365 Dbl_increment(opnd3p1,opnd3p2);
369 if (is_tiny) Set_underflowflag();
371 Dbl_set_exponentmantissa(resultp1,resultp2,opnd3p1,opnd3p2);
373 else Dbl_set_exponent(resultp1,dest_exponent);
374 /* check for inexact */
375 Dbl_copytoptr(resultp1,resultp2,dstptr);
377 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
378 else Set_inexactflag();