From fe0e9f81963f8fc9e0cc75063fa28b9ba9cecee0 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 5 Jun 2021 16:34:31 +0100 Subject: [PATCH] Add the ability to change the base of a fixnum --- agb/src/number.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/agb/src/number.rs b/agb/src/number.rs index b5fb06a7..812ef05b 100644 --- a/agb/src/number.rs +++ b/agb/src/number.rs @@ -6,6 +6,14 @@ use core::{ #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct Num(i32); +fn change_base(num: Num) -> Num { + if N < M { + Num(num.0 << (M - N)) + } else { + Num(num.0 >> (N - M)) + } +} + impl From for Num { fn from(value: i32) -> Self { Num(value << N) @@ -166,6 +174,15 @@ fn test_division_by_2_and_15(_gba: &mut super::Gba) { } } +#[test_case] +fn test_change_base(_gba: &mut super::Gba) { + let two: Num<9> = 2.into(); + let three: Num<4> = 3.into(); + + assert_eq!(two + change_base(three), 5.into()); + assert_eq!(three + change_base(two), 5.into()); +} + impl Display for Num { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let integral = self.0 >> N;