mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 00:31:34 +11:00
commit
a3874e7140
|
@ -12,71 +12,79 @@ impl<const N: usize> From<i32> for Num<N> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Add for Num<N> {
|
impl<T, const N: usize> Add<T> for Num<N>
|
||||||
|
where
|
||||||
|
T: Into<Num<N>>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn add(self, rhs: Num<N>) -> Self::Output {
|
fn add(self, rhs: T) -> Self::Output {
|
||||||
Num(self.0 + rhs.0)
|
Num(self.0 + rhs.into().0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> AddAssign for Num<N> {
|
impl<T, const N: usize> AddAssign<T> for Num<N>
|
||||||
fn add_assign(&mut self, rhs: Self) {
|
where
|
||||||
self.0 += rhs.0
|
T: Into<Num<N>>,
|
||||||
|
{
|
||||||
|
fn add_assign(&mut self, rhs: T) {
|
||||||
|
self.0 = (*self + rhs.into()).0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Sub for Num<N> {
|
impl<T, const N: usize> Sub<T> for Num<N>
|
||||||
|
where
|
||||||
|
T: Into<Num<N>>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn sub(self, rhs: Num<N>) -> Self::Output {
|
fn sub(self, rhs: T) -> Self::Output {
|
||||||
Num(self.0 - rhs.0)
|
Num(self.0 - rhs.into().0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> SubAssign for Num<N> {
|
impl<T, const N: usize> SubAssign<T> for Num<N>
|
||||||
fn sub_assign(&mut self, rhs: Self) {
|
where
|
||||||
self.0 -= rhs.0
|
T: Into<Num<N>>,
|
||||||
|
{
|
||||||
|
fn sub_assign(&mut self, rhs: T) {
|
||||||
|
self.0 = (*self - rhs.into()).0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Mul for Num<N> {
|
impl<T, const N: usize> Mul<T> for Num<N>
|
||||||
|
where
|
||||||
|
T: Into<Num<N>>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn mul(self, rhs: Num<N>) -> Self::Output {
|
fn mul(self, rhs: T) -> Self::Output {
|
||||||
if N % 2 == 0 {
|
Num((self.0 * rhs.into().0) >> N)
|
||||||
Num((self.0 >> (N / 2)) * (rhs.0 >> (N / 2)))
|
|
||||||
} else {
|
|
||||||
Num((self.0 >> (1 + N / 2)) * (rhs.0 >> (N / 2)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> MulAssign for Num<N> {
|
impl<T, const N: usize> MulAssign<T> for Num<N>
|
||||||
fn mul_assign(&mut self, rhs: Self) {
|
where
|
||||||
if N % 2 == 0 {
|
T: Into<Num<N>>,
|
||||||
self.0 = (self.0 >> (N / 2)) * (rhs.0 >> (N / 2))
|
{
|
||||||
} else {
|
fn mul_assign(&mut self, rhs: T) {
|
||||||
self.0 = (self.0 >> (1 + N / 2)) * (rhs.0 >> (N / 2))
|
self.0 = (*self * rhs.into()).0
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Div for Num<N> {
|
impl<T, const N: usize> Div<T> for Num<N>
|
||||||
|
where
|
||||||
|
T: Into<Num<N>>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn div(self, rhs: Num<N>) -> Self::Output {
|
fn div(self, rhs: T) -> Self::Output {
|
||||||
if N % 2 == 0 {
|
Num((self.0 << N) / rhs.into().0)
|
||||||
Num((self.0 << (N / 2)) / (rhs.0 >> (N / 2)))
|
|
||||||
} else {
|
|
||||||
Num((self.0 << (1 + N / 2)) / (rhs.0 >> (N / 2)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> DivAssign for Num<N> {
|
impl<T, const N: usize> DivAssign<T> for Num<N>
|
||||||
fn div_assign(&mut self, rhs: Self) {
|
where
|
||||||
if N % 2 == 0 {
|
T: Into<Num<N>>,
|
||||||
self.0 = (self.0 << (N / 2)) / (rhs.0 >> (N / 2))
|
{
|
||||||
} else {
|
fn div_assign(&mut self, rhs: T) {
|
||||||
self.0 = (self.0 << (1 + N / 2)) / (rhs.0 >> (N / 2))
|
self.0 = (*self / rhs.into()).0
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,20 +116,56 @@ impl<const N: usize> Num<N> {
|
||||||
fn test_numbers(_gba: &mut super::Gba) {
|
fn test_numbers(_gba: &mut super::Gba) {
|
||||||
// test addition
|
// test addition
|
||||||
let n: Num<8> = 1.into();
|
let n: Num<8> = 1.into();
|
||||||
assert_eq!(n + 2.into(), 3.into(), "testing that 1 + 2 == 3");
|
assert_eq!(n + 2, 3.into(), "testing that 1 + 2 == 3");
|
||||||
|
|
||||||
// test multiplication
|
// test multiplication
|
||||||
let n: Num<8> = 5.into();
|
let n: Num<8> = 5.into();
|
||||||
assert_eq!(n * 3.into(), 15.into(), "testing that 5 * 3 == 15");
|
assert_eq!(n * 3, 15.into(), "testing that 5 * 3 == 15");
|
||||||
|
|
||||||
// test division
|
// test division
|
||||||
let n: Num<8> = 30.into();
|
let n: Num<8> = 30.into();
|
||||||
let p: Num<8> = 3.into();
|
let p: Num<8> = 3.into();
|
||||||
assert_eq!(n / 20.into(), p / 2.into(), "testing that 30 / 20 == 3 / 2");
|
assert_eq!(n / 20, p / 2, "testing that 30 / 20 == 3 / 2");
|
||||||
|
|
||||||
assert_ne!(n, p, "testing that 30 != 3");
|
assert_ne!(n, p, "testing that 30 != 3");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_division_by_one(_gba: &mut super::Gba) {
|
||||||
|
let one: Num<8> = 1.into();
|
||||||
|
|
||||||
|
for i in -40..40 {
|
||||||
|
let n: Num<8> = i.into();
|
||||||
|
assert_eq!(n / one, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_division_and_multiplication_by_16(_gba: &mut super::Gba) {
|
||||||
|
let sixteen: Num<8> = 16.into();
|
||||||
|
|
||||||
|
for i in -40..40 {
|
||||||
|
let n: Num<8> = i.into();
|
||||||
|
let m = n / sixteen;
|
||||||
|
|
||||||
|
assert_eq!(m * sixteen, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_division_by_2_and_15(_gba: &mut super::Gba) {
|
||||||
|
let two: Num<8> = 2.into();
|
||||||
|
let fifteen: Num<8> = 15.into();
|
||||||
|
let thirty: Num<8> = 30.into();
|
||||||
|
|
||||||
|
for i in -128..128 {
|
||||||
|
let n: Num<8> = i.into();
|
||||||
|
|
||||||
|
assert_eq!(n / two / fifteen, n / thirty);
|
||||||
|
assert_eq!(n / fifteen / two, n / thirty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<const N: usize> Display for Num<N> {
|
impl<const N: usize> Display for Num<N> {
|
||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
let integral = self.0 >> N;
|
let integral = self.0 >> N;
|
||||||
|
|
Loading…
Reference in a new issue