add FixedLong type

png-branch
an 2019-03-13 12:56:04 -04:00
parent 93e6119604
commit 54d253c89b
1 changed files with 15 additions and 6 deletions

View File

@ -67,7 +67,7 @@ macro_rules! define_fixed_types {
$( $(
$(#[$outer:meta])* $(#[$outer:meta])*
struct $t:ident ( $ti:ident, $bytes:expr ) : struct $t:ident ( $ti:ident, $bytes:expr ) :
$tu:ident, $frac_bits:expr; $test:ident $tu:ident, $tb:ident, $frac_bits:expr; $test:ident
)* )*
) => {$( ) => {$(
$(#[$outer])* $(#[$outer])*
@ -109,6 +109,10 @@ macro_rules! define_fixed_types {
#[inline] #[inline]
pub const fn fract(self) -> $t {$t(self.0 & $t::frac_mask_i())} pub const fn fract(self) -> $t {$t(self.0 & $t::frac_mask_i())}
/// Returns the integer part of a number as an integer.
#[inline]
pub const fn integ(self) -> $ti {self.0 >> $t::frac_bits()}
/// Returns the number of ones in the bit representation of self. /// Returns the number of ones in the bit representation of self.
#[inline] #[inline]
pub const fn count_ones(self) -> u32 {self.0.count_ones()} pub const fn count_ones(self) -> u32 {self.0.count_ones()}
@ -218,13 +222,13 @@ macro_rules! define_fixed_types {
#[inline] #[inline]
fn div_k(x: $ti, y: $ti) -> $ti fn div_k(x: $ti, y: $ti) -> $ti
{ {
(i64::from(x) * i64::from($t::one()) / i64::from(y)) as $ti ($tb::from(x) * $tb::from($t::one()) / $tb::from(y)) as $ti
} }
#[inline] #[inline]
fn mul_k(x: $ti, y: $ti) -> $ti fn mul_k(x: $ti, y: $ti) -> $ti
{ {
(i64::from(x) * i64::from(y) / i64::from($t::one())) as $ti ($tb::from(x) * $tb::from(y) / $tb::from($t::one())) as $ti
} }
} }
@ -423,17 +427,22 @@ define_fixed_types! {
/// ///
/// The format of this type is `0.9s`, but because of the implementation, /// The format of this type is `0.9s`, but because of the implementation,
/// the real format is `7.9s`. /// the real format is `7.9s`.
struct Angle(i16, 2) : u16, 9; angle_tests struct Angle(i16, 2) : u16, i32, 9; angle_tests
/// A fixed point type representing a world unit. /// A fixed point type representing a world unit.
/// ///
/// The format of this type is `5.10s`. This has caused eternal suffering. /// The format of this type is `5.10s`. This has caused eternal suffering.
struct Unit(i16, 2) : u16, 10; unit_tests struct Unit(i16, 2) : u16, i32, 10; unit_tests
/// A generic fixed point type. /// A generic fixed point type.
/// ///
/// The format of this type is `15.16s`. /// The format of this type is `15.16s`.
struct Fixed(i32, 4) : u32, 16; fixed_tests struct Fixed(i32, 4) : u32, i64, 16; fixed_tests
/// A generic, long fixed point type.
///
/// The format of this type is `31.32s`.
struct FixedLong(i64, 8) : u64, i128, 32; fixed_long_tests
} }
#[test] #[test]