Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*---------------------------------------------------------------------------+
0003  |  reg_mul.c                                                                |
0004  |                                                                           |
0005  | Multiply one FPU_REG by another, put the result in a destination FPU_REG. |
0006  |                                                                           |
0007  | Copyright (C) 1992,1993,1997                                              |
0008  |                  W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
0009  |                  E-mail   billm@suburbia.net                              |
0010  |                                                                           |
0011  | Returns the tag of the result if no exceptions or errors occurred.        |
0012  |                                                                           |
0013  +---------------------------------------------------------------------------*/
0014 
0015 /*---------------------------------------------------------------------------+
0016  | The destination may be any FPU_REG, including one of the source FPU_REGs. |
0017  +---------------------------------------------------------------------------*/
0018 
0019 #include "fpu_emu.h"
0020 #include "exception.h"
0021 #include "reg_constant.h"
0022 #include "fpu_system.h"
0023 
0024 /*
0025   Multiply two registers to give a register result.
0026   The sources are st(deststnr) and (b,tagb,signb).
0027   The destination is st(deststnr).
0028   */
0029 /* This routine must be called with non-empty source registers */
0030 int FPU_mul(FPU_REG const *b, u_char tagb, int deststnr, int control_w)
0031 {
0032     FPU_REG *a = &st(deststnr);
0033     FPU_REG *dest = a;
0034     u_char taga = FPU_gettagi(deststnr);
0035     u_char saved_sign = getsign(dest);
0036     u_char sign = (getsign(a) ^ getsign(b));
0037     int tag;
0038 
0039     if (!(taga | tagb)) {
0040         /* Both regs Valid, this should be the most common case. */
0041 
0042         tag =
0043             FPU_u_mul(a, b, dest, control_w, sign,
0044                   exponent(a) + exponent(b));
0045         if (tag < 0) {
0046             setsign(dest, saved_sign);
0047             return tag;
0048         }
0049         FPU_settagi(deststnr, tag);
0050         return tag;
0051     }
0052 
0053     if (taga == TAG_Special)
0054         taga = FPU_Special(a);
0055     if (tagb == TAG_Special)
0056         tagb = FPU_Special(b);
0057 
0058     if (((taga == TAG_Valid) && (tagb == TW_Denormal))
0059         || ((taga == TW_Denormal) && (tagb == TAG_Valid))
0060         || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
0061         FPU_REG x, y;
0062         if (denormal_operand() < 0)
0063             return FPU_Exception;
0064 
0065         FPU_to_exp16(a, &x);
0066         FPU_to_exp16(b, &y);
0067         tag = FPU_u_mul(&x, &y, dest, control_w, sign,
0068                 exponent16(&x) + exponent16(&y));
0069         if (tag < 0) {
0070             setsign(dest, saved_sign);
0071             return tag;
0072         }
0073         FPU_settagi(deststnr, tag);
0074         return tag;
0075     } else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) {
0076         if (((tagb == TW_Denormal) || (taga == TW_Denormal))
0077             && (denormal_operand() < 0))
0078             return FPU_Exception;
0079 
0080         /* Must have either both arguments == zero, or
0081            one valid and the other zero.
0082            The result is therefore zero. */
0083         FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
0084         /* The 80486 book says that the answer is +0, but a real
0085            80486 behaves this way.
0086            IEEE-754 apparently says it should be this way. */
0087         setsign(dest, sign);
0088         return TAG_Zero;
0089     }
0090     /* Must have infinities, NaNs, etc */
0091     else if ((taga == TW_NaN) || (tagb == TW_NaN)) {
0092         return real_2op_NaN(b, tagb, deststnr, &st(0));
0093     } else if (((taga == TW_Infinity) && (tagb == TAG_Zero))
0094            || ((tagb == TW_Infinity) && (taga == TAG_Zero))) {
0095         return arith_invalid(deststnr); /* Zero*Infinity is invalid */
0096     } else if (((taga == TW_Denormal) || (tagb == TW_Denormal))
0097            && (denormal_operand() < 0)) {
0098         return FPU_Exception;
0099     } else if (taga == TW_Infinity) {
0100         FPU_copy_to_regi(a, TAG_Special, deststnr);
0101         setsign(dest, sign);
0102         return TAG_Special;
0103     } else if (tagb == TW_Infinity) {
0104         FPU_copy_to_regi(b, TAG_Special, deststnr);
0105         setsign(dest, sign);
0106         return TAG_Special;
0107     }
0108 #ifdef PARANOID
0109     else {
0110         EXCEPTION(EX_INTERNAL | 0x102);
0111         return FPU_Exception;
0112     }
0113 #endif /* PARANOID */
0114 
0115     return 0;
0116 }