fixed: inline functions Q_Q
This commit is contained in:
parent
91b96e4b5d
commit
1fd7a6eafd
@ -42,12 +42,14 @@ impl<const N:usize,const F:usize,T> From<T> for Fixed<N,F>
|
||||
where
|
||||
BInt<N>:From<T>
|
||||
{
|
||||
#[inline]
|
||||
fn from(value:T)->Self{
|
||||
Self::from_bits(BInt::<{N}>::from(value)<<F as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N:usize,const F:usize> PartialEq for Fixed<N,F>{
|
||||
#[inline]
|
||||
fn eq(&self,other:&Self)->bool{
|
||||
self.bits.eq(&other.bits)
|
||||
}
|
||||
@ -57,6 +59,7 @@ where
|
||||
T:Copy,
|
||||
BInt::<N>:From<T>,
|
||||
{
|
||||
#[inline]
|
||||
fn eq(&self,&other:&T)->bool{
|
||||
self.bits.eq(&other.into())
|
||||
}
|
||||
@ -64,6 +67,7 @@ where
|
||||
impl<const N:usize,const F:usize> Eq for Fixed<N,F>{}
|
||||
|
||||
impl<const N:usize,const F:usize> PartialOrd for Fixed<N,F>{
|
||||
#[inline]
|
||||
fn partial_cmp(&self,other:&Self)->Option<std::cmp::Ordering>{
|
||||
self.bits.partial_cmp(&other.bits)
|
||||
}
|
||||
@ -73,11 +77,13 @@ impl<const N:usize,const F:usize,T> PartialOrd<T> for Fixed<N,F>
|
||||
T:Copy,
|
||||
BInt::<N>:From<T>,
|
||||
{
|
||||
#[inline]
|
||||
fn partial_cmp(&self,&other:&T)->Option<std::cmp::Ordering>{
|
||||
self.bits.partial_cmp(&other.into())
|
||||
}
|
||||
}
|
||||
impl<const N:usize,const F:usize> Ord for Fixed<N,F>{
|
||||
#[inline]
|
||||
fn cmp(&self,other:&Self)->std::cmp::Ordering{
|
||||
self.bits.cmp(&other.bits)
|
||||
}
|
||||
@ -85,11 +91,13 @@ impl<const N:usize,const F:usize> Ord for Fixed<N,F>{
|
||||
|
||||
impl<const N:usize,const F:usize> std::ops::Neg for Fixed<N,F>{
|
||||
type Output=Self;
|
||||
#[inline]
|
||||
fn neg(self)->Self{
|
||||
Self::from_bits(self.bits.neg())
|
||||
}
|
||||
}
|
||||
impl<const N:usize,const F:usize> std::iter::Sum for Fixed<N,F>{
|
||||
#[inline]
|
||||
fn sum<I:Iterator<Item=Self>>(iter:I)->Self{
|
||||
let mut sum=Self::ZERO;
|
||||
for elem in iter{
|
||||
@ -109,6 +117,7 @@ macro_rules! impl_additive_operator {
|
||||
}
|
||||
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
|
||||
type Output = $output;
|
||||
#[inline]
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
self.$method(other)
|
||||
}
|
||||
@ -118,6 +127,7 @@ macro_rules! impl_additive_operator {
|
||||
BInt::<N>:From<U>,
|
||||
{
|
||||
type Output = $output;
|
||||
#[inline]
|
||||
fn $method(self, other: U) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(BInt::<N>::from(other).shl(F as u32)))
|
||||
}
|
||||
@ -127,6 +137,7 @@ macro_rules! impl_additive_operator {
|
||||
macro_rules! impl_additive_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait for $struct<N,F>{
|
||||
#[inline]
|
||||
fn $method(&mut self, other: Self) {
|
||||
self.bits.$method(other.bits);
|
||||
}
|
||||
@ -135,6 +146,7 @@ macro_rules! impl_additive_assign_operator {
|
||||
where
|
||||
BInt::<N>:From<U>,
|
||||
{
|
||||
#[inline]
|
||||
fn $method(&mut self, other: U) {
|
||||
self.bits.$method(BInt::<N>::from(other).shl(F as u32));
|
||||
}
|
||||
@ -166,6 +178,7 @@ macro_rules! impl_multiplicative_operator_not_const_generic {
|
||||
( ($struct: ident, $trait: ident, $method: ident, $output: ty ), $width:expr ) => {
|
||||
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
||||
type Output = $output;
|
||||
#[inline]
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
paste::item!{
|
||||
self.[<fixed_ $method>](other)
|
||||
@ -177,6 +190,7 @@ macro_rules! impl_multiplicative_operator_not_const_generic {
|
||||
macro_rules! impl_multiplicative_assign_operator_not_const_generic {
|
||||
( ($struct: ident, $trait: ident, $method: ident, $non_assign_method: ident ), $width:expr ) => {
|
||||
impl<const F:usize> core::ops::$trait for $struct<$width,F>{
|
||||
#[inline]
|
||||
fn $method(&mut self, other: Self) {
|
||||
paste::item!{
|
||||
*self=self.[<fixed_ $non_assign_method>](other);
|
||||
@ -227,7 +241,7 @@ macro_rules! impl_multiplicative_operator {
|
||||
BInt::<N>:From<U>+core::ops::$trait,
|
||||
{
|
||||
type Output = $output;
|
||||
|
||||
#[inline]
|
||||
fn $method(self, other: U) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(BInt::<N>::from(other)))
|
||||
}
|
||||
@ -240,6 +254,7 @@ macro_rules! impl_multiplicative_assign_operator {
|
||||
where
|
||||
BInt::<N>:From<U>+core::ops::$trait,
|
||||
{
|
||||
#[inline]
|
||||
fn $method(&mut self, other: U) {
|
||||
self.bits.$method(BInt::<N>::from(other));
|
||||
}
|
||||
@ -276,6 +291,7 @@ impl_multiplicative_operator!( Fixed, Div, div, Self );
|
||||
#[cfg(feature="deferred-division")]
|
||||
impl<const LHS_N:usize,const LHS_F:usize,const RHS_N:usize,const RHS_F:usize> core::ops::Div<Fixed<RHS_N,RHS_F>> for Fixed<LHS_N,LHS_F>{
|
||||
type Output=ratio_ops::ratio::Ratio<Fixed<LHS_N,LHS_F>,Fixed<RHS_N,RHS_F>>;
|
||||
#[inline]
|
||||
fn div(self, other: Fixed<RHS_N,RHS_F>)->Self::Output{
|
||||
ratio_ops::ratio::Ratio::new(self,other)
|
||||
}
|
||||
@ -285,7 +301,7 @@ macro_rules! impl_shift_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait<u32> for $struct<N,F>{
|
||||
type Output = $output;
|
||||
|
||||
#[inline]
|
||||
fn $method(self, other: u32) -> Self::Output {
|
||||
Self::from_bits(self.bits.$method(other))
|
||||
}
|
||||
@ -295,6 +311,7 @@ macro_rules! impl_shift_operator {
|
||||
macro_rules! impl_shift_assign_operator {
|
||||
( $struct: ident, $trait: ident, $method: ident ) => {
|
||||
impl<const N:usize,const F:usize> core::ops::$trait<u32> for $struct<N,F>{
|
||||
#[inline]
|
||||
fn $method(&mut self, other: u32) {
|
||||
self.bits.$method(other);
|
||||
}
|
||||
@ -312,6 +329,7 @@ macro_rules! impl_wide_operators{
|
||||
($lhs:expr,$rhs:expr)=>{
|
||||
impl core::ops::Mul<Fixed<$rhs,{$rhs*32}>> for Fixed<$lhs,{$lhs*32}>{
|
||||
type Output=Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>;
|
||||
#[inline]
|
||||
fn mul(self, other: Fixed<$rhs,{$rhs*32}>)->Self::Output{
|
||||
paste::item!{
|
||||
self.[<wide_mul_ $lhs _ $rhs>](other)
|
||||
@ -321,6 +339,7 @@ macro_rules! impl_wide_operators{
|
||||
#[cfg(not(feature="deferred-division"))]
|
||||
impl core::ops::Div<Fixed<$rhs,{$rhs*32}>> for Fixed<$lhs,{$lhs*32}>{
|
||||
type Output=Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>;
|
||||
#[inline]
|
||||
fn div(self, other: Fixed<$rhs,{$rhs*32}>)->Self::Output{
|
||||
paste::item!{
|
||||
self.[<wide_div_ $lhs _ $rhs>](other)
|
||||
@ -341,6 +360,7 @@ macro_rules! impl_wide_not_const_generic{
|
||||
impl Fixed<$lhs,{$lhs*32}>
|
||||
{
|
||||
paste::item!{
|
||||
#[inline]
|
||||
pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:Fixed<$rhs,{$rhs*32}>)->Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{
|
||||
let lhs=self.bits.as_::<BInt<{$lhs+$rhs}>>();
|
||||
let rhs=rhs.bits.as_::<BInt<{$lhs+$rhs}>>();
|
||||
@ -349,6 +369,7 @@ macro_rules! impl_wide_not_const_generic{
|
||||
/// This operation cannot represent the fraction exactly,
|
||||
/// but it shapes the output to have precision for the
|
||||
/// largest and smallest possible fractions.
|
||||
#[inline]
|
||||
pub fn [<wide_div_ $lhs _ $rhs>](self,rhs:Fixed<$rhs,{$rhs*32}>)->Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{
|
||||
// (lhs/2^LHS_FRAC)/(rhs/2^RHS_FRAC)
|
||||
let lhs=self.bits.as_::<BInt<{$lhs+$rhs}>>().shl($rhs*64);
|
||||
@ -382,6 +403,7 @@ macro_repeated!(
|
||||
(1,15)
|
||||
);
|
||||
impl<const SRC:usize,const F:usize> Fixed<SRC,F>{
|
||||
#[inline]
|
||||
pub fn resize_into<const DST:usize>(self)->Fixed<DST,F>{
|
||||
Fixed::from_bits(self.bits.as_::<BInt<DST>>())
|
||||
}
|
||||
@ -390,12 +412,14 @@ impl<const SRC:usize,const F:usize> Fixed<SRC,F>{
|
||||
macro_rules! impl_not_const_generic{
|
||||
($n:expr)=>{
|
||||
impl Fixed<{$n*2},{$n*2*32}>{
|
||||
#[inline]
|
||||
pub fn halve_precision(self)->Fixed<$n,{$n*32}>{
|
||||
Fixed::from_bits(bnum::cast::As::as_(self.bits.shr($n*32)))
|
||||
}
|
||||
}
|
||||
impl Fixed<$n,{$n*32}>{
|
||||
paste::item!{
|
||||
#[inline]
|
||||
pub fn sqrt_unchecked(self)->Self{
|
||||
//1<<max_shift must be the minimum power of two which when squared is greater than self
|
||||
//calculating max_shift:
|
||||
@ -420,6 +444,7 @@ macro_rules! impl_not_const_generic{
|
||||
result
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn sqrt(self)->Self{
|
||||
if self<Self::ZERO{
|
||||
panic!("Square root less than zero")
|
||||
@ -427,6 +452,7 @@ macro_rules! impl_not_const_generic{
|
||||
self.sqrt_unchecked()
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn sqrt_checked(self)->Option<Self>{
|
||||
if self<Self::ZERO{
|
||||
None
|
||||
|
Loading…
Reference in New Issue
Block a user