Merge pull request #139 from gwilymk/import-asm-macro

Import the new asm macro required in newer versions of nightly
This commit is contained in:
Gwilym Kuiper 2022-01-01 11:41:01 +00:00 committed by GitHub
commit 285643a973
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View file

@ -59,6 +59,7 @@ pub trait FixedWidthUnsignedInteger:
} }
pub trait FixedWidthSignedInteger: FixedWidthUnsignedInteger + Neg<Output = Self> { pub trait FixedWidthSignedInteger: FixedWidthUnsignedInteger + Neg<Output = Self> {
#[must_use]
fn fixed_abs(self) -> Self; fn fixed_abs(self) -> Self;
} }
@ -276,6 +277,7 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Num<I, N> {
self.0 / (I::one() << N) self.0 / (I::one() << N)
} }
#[must_use]
pub fn rem_euclid(self, rhs: Self) -> Self { pub fn rem_euclid(self, rhs: Self) -> Self {
let r = self % rhs; let r = self % rhs;
if r < I::zero().into() { if r < I::zero().into() {
@ -307,6 +309,7 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Num<I, N> {
} }
impl<const N: usize> Num<i32, N> { impl<const N: usize> Num<i32, N> {
#[must_use]
pub fn sqrt(self) -> Self { pub fn sqrt(self) -> Self {
assert_eq!(N % 2, 0, "N must be even to be able to square root"); assert_eq!(N % 2, 0, "N must be even to be able to square root");
assert!(self.0 >= 0, "sqrt is only valid for positive numbers"); assert!(self.0 >= 0, "sqrt is only valid for positive numbers");
@ -381,12 +384,14 @@ fn test_macro_conversion(_gba: &mut super::Gba) {
} }
impl<I: FixedWidthSignedInteger, const N: usize> Num<I, N> { impl<I: FixedWidthSignedInteger, const N: usize> Num<I, N> {
#[must_use]
pub fn abs(self) -> Self { pub fn abs(self) -> Self {
Num(self.0.fixed_abs()) Num(self.0.fixed_abs())
} }
/// domain of [0, 1]. /// domain of [0, 1].
/// see https://github.com/tarcieri/micromath/blob/24584465b48ff4e87cffb709c7848664db896b4f/src/float/cos.rs#L226 /// see https://github.com/tarcieri/micromath/blob/24584465b48ff4e87cffb709c7848664db896b4f/src/float/cos.rs#L226
#[must_use]
pub fn cos(self) -> Self { pub fn cos(self) -> Self {
let one: Self = I::one().into(); let one: Self = I::one().into();
let mut x = self; let mut x = self;
@ -402,6 +407,7 @@ impl<I: FixedWidthSignedInteger, const N: usize> Num<I, N> {
x x
} }
#[must_use]
pub fn sin(self) -> Self { pub fn sin(self) -> Self {
let one: Self = I::one().into(); let one: Self = I::one().into();
let four: I = 4.into(); let four: I = 4.into();
@ -697,12 +703,15 @@ impl<T: Number> SubAssign<Self> for Vector2D<T> {
} }
impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>> { impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>> {
#[must_use]
pub fn trunc(self) -> Vector2D<I> { pub fn trunc(self) -> Vector2D<I> {
Vector2D { Vector2D {
x: self.x.trunc(), x: self.x.trunc(),
y: self.y.trunc(), y: self.y.trunc(),
} }
} }
#[must_use]
pub fn floor(self) -> Vector2D<I> { pub fn floor(self) -> Vector2D<I> {
Vector2D { Vector2D {
x: self.x.floor(), x: self.x.floor(),
@ -712,14 +721,17 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>> {
} }
impl<const N: usize> Vector2D<Num<i32, N>> { impl<const N: usize> Vector2D<Num<i32, N>> {
#[must_use]
pub fn magnitude_squared(self) -> Num<i32, N> { pub fn magnitude_squared(self) -> Num<i32, N> {
self.x * self.x + self.y * self.y self.x * self.x + self.y * self.y
} }
#[must_use]
pub fn manhattan_distance(self) -> Num<i32, N> { pub fn manhattan_distance(self) -> Num<i32, N> {
self.x.abs() + self.y.abs() self.x.abs() + self.y.abs()
} }
#[must_use]
pub fn magnitude(self) -> Num<i32, N> { pub fn magnitude(self) -> Num<i32, N> {
self.magnitude_squared().sqrt() self.magnitude_squared().sqrt()
} }
@ -728,6 +740,7 @@ impl<const N: usize> Vector2D<Num<i32, N>> {
// algorithm https://en.wikipedia.org/wiki/Alpha_max_plus_beta_min_algorithm // algorithm https://en.wikipedia.org/wiki/Alpha_max_plus_beta_min_algorithm
// this has a maximum error of less than 4% of the true magnitude, probably // this has a maximum error of less than 4% of the true magnitude, probably
// depending on the size of your fixed point approximation // depending on the size of your fixed point approximation
#[must_use]
pub fn fast_magnitude(self) -> Num<i32, N> { pub fn fast_magnitude(self) -> Num<i32, N> {
let max = core::cmp::max(self.x, self.y); let max = core::cmp::max(self.x, self.y);
let min = core::cmp::min(self.x, self.y); let min = core::cmp::min(self.x, self.y);
@ -735,10 +748,12 @@ impl<const N: usize> Vector2D<Num<i32, N>> {
max * num!(0.960433870103) + min * num!(0.397824734759) max * num!(0.960433870103) + min * num!(0.397824734759)
} }
#[must_use]
pub fn normalise(self) -> Self { pub fn normalise(self) -> Self {
self / self.magnitude() self / self.magnitude()
} }
#[must_use]
pub fn fast_normalise(self) -> Self { pub fn fast_normalise(self) -> Self {
self / self.fast_magnitude() self / self.fast_magnitude()
} }
@ -790,6 +805,7 @@ pub struct Rect<T: Number> {
} }
impl<T: Number> Rect<T> { impl<T: Number> Rect<T> {
#[must_use]
pub fn new(position: Vector2D<T>, size: Vector2D<T>) -> Self { pub fn new(position: Vector2D<T>, size: Vector2D<T>) -> Self {
Rect { position, size } Rect { position, size }
} }
@ -808,7 +824,8 @@ impl<T: Number> Rect<T> {
&& self.position.y + self.size.y > other.position.y && self.position.y + self.size.y > other.position.y
} }
pub fn overlapping_rect(&self, other: Rect<T>) -> Rect<T> { #[must_use]
pub fn overlapping_rect(&self, other: Rect<T>) -> Self {
fn max<E: Number>(x: E, y: E) -> E { fn max<E: Number>(x: E, y: E) -> E {
if x > y { if x > y {
x x
@ -849,12 +866,11 @@ impl<T: FixedWidthUnsignedInteger + core::iter::Step> Rect<T> {
pub fn iter(self) -> impl Iterator<Item = (T, T)> { pub fn iter(self) -> impl Iterator<Item = (T, T)> {
(self.position.x..=(self.position.x + self.size.x)) (self.position.x..=(self.position.x + self.size.x))
.into_iter() .into_iter()
.map(move |x| { .flat_map(move |x| {
(self.position.y..=(self.position.y + self.size.y)) (self.position.y..=(self.position.y + self.size.y))
.into_iter() .into_iter()
.map(move |y| (x, y)) .map(move |y| (x, y))
}) })
.flatten()
} }
} }
@ -862,15 +878,20 @@ impl<T: Number> Vector2D<T> {
pub fn new(x: T, y: T) -> Self { pub fn new(x: T, y: T) -> Self {
Vector2D { x, y } Vector2D { x, y }
} }
pub fn get(self) -> (T, T) { pub fn get(self) -> (T, T) {
(self.x, self.y) (self.x, self.y)
} }
#[must_use]
pub fn hadamard(self, other: Self) -> Self { pub fn hadamard(self, other: Self) -> Self {
Self { Self {
x: self.x * other.x, x: self.x * other.x,
y: self.y * other.y, y: self.y * other.y,
} }
} }
#[must_use]
pub fn swap(self) -> Self { pub fn swap(self) -> Self {
Self { Self {
x: self.y, x: self.y,

View file

@ -1,3 +1,5 @@
use core::arch::asm;
use crate::display::object::AffineMatrixAttributes; use crate::display::object::AffineMatrixAttributes;
use crate::number::Num; use crate::number::Num;