ft(interpreter): impl mul and div

This commit is contained in:
2025-07-02 20:32:13 +09:00
parent 60b5b6bd51
commit 803c6267d2
2 changed files with 58 additions and 23 deletions

View File

@@ -107,6 +107,32 @@ impl ImmediateOperand {
Self::Word(word) => return (word >> 15) == 1,
}
}
/// Multiply values and return the extended u32, split into two words.
pub fn mul(&self, other: Self) -> (Word, Word) {
let result: u32 = match self {
Self::Byte(lhsb) => match other {
Self::Byte(rhsb) => *lhsb as u32 * rhsb as u32,
Self::Word(rhsw) => match other.sign_extend() {
Self::Word(lhsw) => lhsw as u32 * rhsw as u32,
_ => panic!("unreachable"),
},
},
Self::Word(lhsw) => match other {
Self::Word(rhsw) => *lhsw as u32 * rhsw as u32,
Self::Byte(_) => match other.sign_extend() {
Self::Word(rhsw) => *lhsw as u32 * rhsw as u32,
_ => panic!("unreachable"),
},
},
};
let bytes = result.to_le_bytes();
let lower = Word::from_le_bytes([bytes[0], bytes[1]]);
let upper = Word::from_le_bytes([bytes[2], bytes[3]]);
(lower, upper)
}
}
impl PartialOrd for ImmediateOperand {
@@ -262,29 +288,6 @@ impl Sub<Byte> for ImmediateOperand {
}
}
}
impl Mul for ImmediateOperand {
type Output = Self;
fn mul(self, other: Self) -> Self {
match self {
ImmediateOperand::Byte(lhsb) => match other {
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_mul(rhsb)),
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(match other.sign_extend() {
ImmediateOperand::Word(lhsw) => lhsw.wrapping_mul(rhsw),
_ => panic!("unreachable"),
}),
},
ImmediateOperand::Word(lhsw) => match other {
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_mul(rhsw)),
ImmediateOperand::Byte(_) => ImmediateOperand::Word(match other.sign_extend() {
ImmediateOperand::Word(rhsw) => lhsw.wrapping_mul(rhsw),
_ => panic!("unreachable"),
}),
},
}
}
}
impl Shl for ImmediateOperand {
type Output = Self;