Skip to content

Commit

Permalink
Closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Aug 27, 2024
1 parent c2c4d88 commit 97f4c07
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

* **0.12.0**
* Adds an optional dependency on the [fixed](https://docs.rs/fixed) crate.
* The `i16fx8`, `i16fx14`, and `i32fx8` type aliases will alias either the `gba`
crate's internal fixed point type (feature off) or they will alias the
appropriate type from the `fixed` (feature on). The two type are bit
compatible, though the version in the `fixed` crate is more complete in the
methods provided.
* **Breaking:** The `gba` crate's internal fixed type had some conversion
methods renamed so that they would match the appropriate methods for the
same operation in the `fixed` crate.

* **0.11.6:**
* `on_gba` feature (default: enabled) that signals if the crate is running on a GBA.
Limited portions of the crate *can* be used even when not on the GBA (such as in a build script).
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "Zlib OR Apache-2.0 OR MIT"
default = ["track_caller", "on_gba"]
track_caller = []
on_gba = []
fixed = ["dep:fixed"]

[dependencies]
voladdress = "1.3.0"
Expand All @@ -18,6 +19,7 @@ bracer = "0.1.2"
critical-section = { version = "1.1.2", features = [
"restore-state-bool",
], optional = true }
fixed = { version = "1.28.0", optional = true }

[profile.dev]
opt-level = 3
Expand Down
12 changes: 6 additions & 6 deletions src/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn VBlankIntrWait() {
#[inline]
#[instruction_set(arm::t32)]
pub fn ArcTan(theta: i16fx14) -> i16fx14 {
let mut i = theta.into_raw();
let mut i = theta.to_bits();
unsafe {
core::arch::asm! {
"swi #0x09",
Expand All @@ -100,7 +100,7 @@ pub fn ArcTan(theta: i16fx14) -> i16fx14 {
options(pure, nomem, preserves_flags),
}
};
i16fx14::from_raw(i)
i16fx14::from_bits(i)
}

/// `0x0A`: The "2-argument arctangent" ([atan2][wp-atan2]).
Expand All @@ -112,9 +112,9 @@ pub fn ArcTan(theta: i16fx14) -> i16fx14 {
#[inline]
#[instruction_set(arm::t32)]
pub fn ArcTan2(x: i16fx14, y: i16fx14) -> u16 {
let x = x.into_raw();
let y = y.into_raw();
let output: u16;
let x: i16 = x.to_bits();
let y: i16 = y.to_bits();
let output: i16;
unsafe {
core::arch::asm! {
"swi #0x0A",
Expand All @@ -124,7 +124,7 @@ pub fn ArcTan2(x: i16fx14, y: i16fx14) -> u16 {
options(pure, nomem, preserves_flags),
}
};
output
output as u16
}

/// Used to provide info to a call of the [`BitUnPack`] function.
Expand Down
55 changes: 49 additions & 6 deletions src/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,65 @@ use core::ops::*;
/// `i16` with 8 bits of fixed-point fraction.
///
/// This is used by the affine matrix entries.
///
/// * This build of the docs does not use the `fixed` feature and uses the
/// crate's internal fixed point type.
#[allow(non_camel_case_types)]
#[cfg(not(feature = "fixed"))]
pub type i16fx8 = Fixed<i16, 8>;

/// `i16` with 14 bits of fixed-point fraction.
///
/// This is used by the [`ArcTan`](crate::bios::ArcTan) and
/// [`ArcTan2`](crate::bios::ArcTan2) BIOS functions.
///
/// * This build of the docs does not use the `fixed` feature and uses the
/// crate's internal fixed point type.
#[allow(non_camel_case_types)]
#[cfg(not(feature = "fixed"))]
pub type i16fx14 = Fixed<i16, 14>;

/// `i32` with 8 bits of fixed-point fraction.
///
/// This is used by the background reference point entries.
///
/// * This build of the docs does not use the `fixed` feature and uses the
/// crate's internal fixed point type.
#[allow(non_camel_case_types)]
#[cfg(not(feature = "fixed"))]
pub type i32fx8 = Fixed<i32, 8>;

/// `i16` with 8 bits of fixed-point fraction.
///
/// This is used by the affine matrix entries.
///
/// * This build of the docs uses the `fixed` feature and uses the fixed point
/// type from the `fixed` crate.
#[allow(non_camel_case_types)]
#[cfg(feature = "fixed")]
pub type i16fx8 = ::fixed::FixedI32<::fixed::types::extra::U8>;

/// `i16` with 14 bits of fixed-point fraction.
///
/// This is used by the [`ArcTan`](crate::bios::ArcTan) and
/// [`ArcTan2`](crate::bios::ArcTan2) BIOS functions.
///
/// * This build of the docs uses the `fixed` feature and uses the fixed point
/// type from the `fixed` crate.
#[allow(non_camel_case_types)]
#[cfg(feature = "fixed")]
pub type i16fx14 = ::fixed::FixedI32<::fixed::types::extra::U14>;

/// `i32` with 8 bits of fixed-point fraction.
///
/// This is used by the background reference point entries.
///
/// * This build of the docs uses the `fixed` feature and uses the fixed point
/// type from the `fixed` crate.
#[allow(non_camel_case_types)]
#[cfg(feature = "fixed")]
pub type i32fx8 = ::fixed::FixedI32<::fixed::types::extra::U8>;

/// A [fixed-point][wp-fp] number. This transparently wraps an integer with a
/// const generic for how many bits are fractional.
///
Expand Down Expand Up @@ -114,15 +157,15 @@ macro_rules! impl_common_fixed_ops {
#[inline]
#[must_use]
#[cfg_attr(feature = "track_caller", track_caller)]
pub const fn from_raw(i: $t) -> Self {
pub const fn from_bits(i: $t) -> Self {
Self(i)
}

/// Unwraps the inner value directly into the base type (no shift).
#[inline]
#[must_use]
#[cfg_attr(feature = "track_caller", track_caller)]
pub const fn into_raw(self) -> $t {
pub const fn to_bits(self) -> $t {
self.0
}

Expand Down Expand Up @@ -287,8 +330,8 @@ macro_rules! impl_signed_fixed_ops {
impl<const B: u32> core::fmt::Debug for Fixed<$t, B> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let whole: $t = self.trunc().into_raw() >> B;
let fract: $t = self.fract().into_raw();
let whole: $t = self.trunc().to_bits() >> B;
let fract: $t = self.fract().to_bits();
let divisor: $t = 1 << B;
if self.is_negative() {
let whole = whole.unsigned_abs();
Expand Down Expand Up @@ -345,8 +388,8 @@ macro_rules! impl_unsigned_fixed_ops {
impl<const B: u32> core::fmt::Debug for Fixed<$t, B> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let whole: $t = self.trunc().into_raw() >> B;
let fract: $t = self.fract().into_raw();
let whole: $t = self.trunc().to_bits() >> B;
let fract: $t = self.fract().to_bits();
let divisor: $t = 1 << B;
write!(f, "{whole}+{fract}/{divisor}")
}
Expand Down

0 comments on commit 97f4c07

Please sign in to comment.