aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorriperiperi <rhy3756547@hotmail.com>2018-06-13 14:55:45 +0100
committergdkchan <gab.dark.100@gmail.com>2018-06-13 10:55:45 -0300
commitafa5bf81e3c842a494ac008b1333ce0f7e2582f9 (patch)
treee2fffa296e1da7fe082cae622cbb1ca0ec1a0996
parent36827c2355fd148fa6eef918452e25d1b43b2182 (diff)
Faster soft implementation of smulh and umulh (#134)
* Faster soft implementation of smulh and umulh * smulh: Fixed mul with 0 acting like it had a negative result. * Use compliment for negative smulh result.
-rw-r--r--ChocolArm64/Instruction/ASoftFallback.cs22
1 files changed, 20 insertions, 2 deletions
diff --git a/ChocolArm64/Instruction/ASoftFallback.cs b/ChocolArm64/Instruction/ASoftFallback.cs
index 8ed55e20..3f8ab5e3 100644
--- a/ChocolArm64/Instruction/ASoftFallback.cs
+++ b/ChocolArm64/Instruction/ASoftFallback.cs
@@ -153,12 +153,30 @@ namespace ChocolArm64.Instruction
public static long SMulHi128(long LHS, long RHS)
{
- return (long)(BigInteger.Multiply(LHS, RHS) >> 64);
+ bool LSign = (LHS < 0);
+ bool RSign = (RHS < 0);
+
+ long Result = (long)UMulHi128((ulong)(LSign ? -LHS : LHS), (ulong)(RSign ? -RHS : RHS));
+
+ if (LSign != RSign && LHS != 0 && RHS != 0)
+ return ~Result; //for negative results, hi 64-bits start at 0xFFF... and count back
+ return Result;
}
public static ulong UMulHi128(ulong LHS, ulong RHS)
{
- return (ulong)(BigInteger.Multiply(LHS, RHS) >> 64);
+ //long multiplication
+ //multiply 32 bits at a time in 64 bit, the result is what's carried over 64 bits.
+ ulong LHigh = LHS >> 32;
+ ulong LLow = LHS & 0xFFFFFFFF;
+ ulong RHigh = RHS >> 32;
+ ulong RLow = RHS & 0xFFFFFFFF;
+ ulong Z2 = LLow * RLow;
+ ulong T = LHigh * RLow + (Z2 >> 32);
+ ulong Z1 = T & 0xFFFFFFFF;
+ ulong Z0 = T >> 32;
+ Z1 += LLow * RHigh;
+ return LHigh * RHigh + Z0 + (Z1 >> 32);
}
}
}