-:function:::unsigned64:UnsignedMultiply:unsigned32 l, unsigned32 r
-*vr4100:
-// start-sanitize-vr4xxx
-*vr4121:
-// end-sanitize-vr4xxx
-// start-sanitize-vr4320
-*vr4320:
-// end-sanitize-vr4320
-// start-sanitize-cygnus
-*vr5400:
-// end-sanitize-cygnus
-{
- unsigned64 result = (unsigned64) l * (unsigned64) r;
- return result;
-}
-
-// start-sanitize-vr4xxx
-:function:::signed64:SaturatedAdd:signed32 l, signed32 r
-*vr4121:
-{
- signed64 result = (signed64) l + (signed64) r;
- if (result < 0)
- result = 0xFFFFFFFF8000000LL;
- else if (result > 0x000000007FFFFFFFLL)
- result = 0x000000007FFFFFFFLL;
- return result;
-}
+// Simulate the various kinds of multiply and multiply-accumulate instructions.
+// Perform an operation of the form:
+//
+// LHS (+/-) GPR[RS] * GPR[RT]
+//
+// and store it in the 64-bit accumulator. Optionally copy either LO or
+// HI into a general purpose register.
+//
+// - RD is the destination register of the LO or HI move
+// - RS are RT are the multiplication source registers
+// - ACCUMULATE_P is true if LHS should be the value of the 64-bit accumulator,
+// false if it should be 0.
+// - STORE_HI_P is true if HI should be stored in RD, false if LO should be.
+// - UNSIGNED_P is true if the operation should be unsigned.
+// - SATURATE_P is true if the result should be saturated to a 32-bit value.
+// - SUBTRACT_P is true if the right hand side should be subtraced from LHS,
+// false if it should be added.
+// - SHORT_P is true if RS and RT must be 16-bit numbers.
+// - DOUBLE_P is true if the 64-bit accumulator is in LO, false it is a
+// concatenation of the low 32 bits of HI and LO.
+:function:::void:do_vr_mul_op:int rd, int rs, int rt, int accumulate_p, int store_hi_p, int unsigned_p, int saturate_p, int subtract_p, int short_p, int double_p
+{
+ unsigned64 lhs, x, y, xcut, ycut, product, result;
+
+ check_mult_hilo (SD_, HIHISTORY, LOHISTORY);
+
+ lhs = (!accumulate_p ? 0 : double_p ? LO : U8_4 (HI, LO));
+ x = GPR[rs];
+ y = GPR[rt];
+
+ /* Work out the canonical form of X and Y from their significant bits. */
+ if (!short_p)
+ {
+ /* Normal sign-extension rule for 32-bit operands. */
+ xcut = EXTEND32 (x);
+ ycut = EXTEND32 (y);
+ }
+ else if (unsigned_p)
+ {
+ /* Operands must be zero-extended 16-bit numbers. */
+ xcut = x & 0xffff;
+ ycut = y & 0xffff;
+ }
+ else
+ {
+ /* Likewise but sign-extended. */
+ xcut = EXTEND16 (x);
+ ycut = EXTEND16 (y);
+ }
+ if (x != xcut || y != ycut)
+ sim_engine_abort (SD, CPU, CIA,
+ "invalid multiplication operand at 0x%08lx\n",
+ (long) CIA);
+
+ TRACE_ALU_INPUT2 (x, y);
+ product = (unsigned_p ? x * y : EXTEND32 (x) * EXTEND32 (y));
+ result = (subtract_p ? lhs - product : lhs + product);
+ if (saturate_p)
+ {
+ /* Saturate the result to 32 bits. An unsigned, unsaturated
+ result is zero-extended to 64 bits, but unsigned overflow
+ causes all 64 bits to be set. */
+ if (!unsigned_p && (unsigned64) EXTEND32 (result) != result)
+ result = ((signed64) result < 0 ? -0x7fffffff - 1 : 0x7fffffff);
+ else if (unsigned_p && (result >> 32) != 0)
+ result = (unsigned64) 0 - 1;
+ }
+ TRACE_ALU_RESULT (result);