From 4f0e0339b112b459da784f80a8f4ea387ec29286 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Wed, 20 Dec 2023 11:44:01 +0000 Subject: [PATCH] Kill pow(). --- ykrt/src/compile/jitc_yk/jit_ir.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ykrt/src/compile/jitc_yk/jit_ir.rs b/ykrt/src/compile/jitc_yk/jit_ir.rs index 65e402302..82215cf23 100644 --- a/ykrt/src/compile/jitc_yk/jit_ir.rs +++ b/ykrt/src/compile/jitc_yk/jit_ir.rs @@ -89,7 +89,8 @@ pub enum Operand { impl Operand { pub(crate) fn new(kind: OpKind, val: u64) -> Self { - if val < 2u64.pow(u32::try_from(SHORT_OPERAND_VALUE_SIZE).unwrap()) { + // check if the operand's value can fit in a short operand. + if val & (u64::MAX << SHORT_OPERAND_VALUE_SIZE) == 0 { Self::Short(ShortOperand::new(kind, val)) } else { todo!() @@ -418,4 +419,17 @@ mod tests { debug_assert_eq!(inst.operand(1), Operand::Short(ShortOperand(0))); debug_assert_eq!(inst.operand(2), Operand::Short(ShortOperand(0x3ffff))); } + + #[test] + fn does_fit_short_operand() { + for i in 0..SHORT_OPERAND_VALUE_SIZE { + Operand::new(OpKind::Local, 1 << i); + } + } + + #[test] + #[should_panic] + fn doesnt_fit_short_operand() { + Operand::new(OpKind::Local, 1 << SHORT_OPERAND_VALUE_SIZE); + } }