eviscerate PhantomData
This commit is contained in:
parent
46d89619bd
commit
0924518922
@ -1,11 +1,10 @@
|
|||||||
use bnum::{BInt,cast::As};
|
use bnum::{BInt,cast::As};
|
||||||
use typenum::Unsigned;
|
use typenum::Unsigned;
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
#[derive(Clone,Copy,Debug,Hash)]
|
#[derive(Clone,Copy,Debug,Hash)]
|
||||||
pub struct Fixed<const CHUNKS:usize,Frac>{
|
pub struct Fixed<const CHUNKS:usize,Frac>{
|
||||||
pub(crate)bits:BInt<{CHUNKS}>,
|
pub(crate)bits:BInt<{CHUNKS}>,
|
||||||
pub(crate)frac:PhantomData<Frac>,
|
pub(crate)frac:std::marker::PhantomData<Frac>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
|
impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
|
||||||
@ -20,10 +19,12 @@ impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
|
|||||||
pub const NEG_ONE:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32));
|
pub const NEG_ONE:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32));
|
||||||
pub const NEG_TWO:Self=Self::from_bits(BInt::<CHUNKS>::NEG_TWO.shl(Frac::U32));
|
pub const NEG_TWO:Self=Self::from_bits(BInt::<CHUNKS>::NEG_TWO.shl(Frac::U32));
|
||||||
pub const NEG_HALF:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32-1));
|
pub const NEG_HALF:Self=Self::from_bits(BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32-1));
|
||||||
|
}
|
||||||
|
impl<const CHUNKS:usize,Frac> Fixed<CHUNKS,Frac>{
|
||||||
pub const fn from_bits(bits:BInt::<CHUNKS>)->Self{
|
pub const fn from_bits(bits:BInt::<CHUNKS>)->Self{
|
||||||
Self{
|
Self{
|
||||||
bits,
|
bits,
|
||||||
frac:PhantomData,
|
frac:std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub const fn to_bits(self)->BInt<CHUNKS>{
|
pub const fn to_bits(self)->BInt<CHUNKS>{
|
||||||
@ -39,10 +40,7 @@ impl<const CHUNKS:usize,Frac:Unsigned,T> From<T> for Fixed<CHUNKS,Frac>
|
|||||||
BInt<CHUNKS>:From<T>
|
BInt<CHUNKS>:From<T>
|
||||||
{
|
{
|
||||||
fn from(value:T)->Self{
|
fn from(value:T)->Self{
|
||||||
Self{
|
Self::from_bits(BInt::<{CHUNKS}>::from(value)<<Frac::U32)
|
||||||
bits:BInt::<{CHUNKS}>::from(value)<<Frac::U32,
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,10 +65,7 @@ impl<const CHUNKS:usize,Frac> Ord for Fixed<CHUNKS,Frac>{
|
|||||||
impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
|
impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
|
||||||
type Output=Self;
|
type Output=Self;
|
||||||
fn neg(self)->Self{
|
fn neg(self)->Self{
|
||||||
Self{
|
Self::from_bits(self.bits.neg())
|
||||||
bits:self.bits.neg(),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,10 +75,7 @@ macro_rules! impl_additive_operator {
|
|||||||
type Output = $output;
|
type Output = $output;
|
||||||
|
|
||||||
fn $method(self, other: Self) -> Self::Output {
|
fn $method(self, other: Self) -> Self::Output {
|
||||||
Self {
|
Self::from_bits(self.bits.$method(other.bits))
|
||||||
bits:self.bits.$method(other.bits),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
|
||||||
@ -93,10 +85,7 @@ macro_rules! impl_additive_operator {
|
|||||||
type Output = $output;
|
type Output = $output;
|
||||||
|
|
||||||
fn $method(self, other: U) -> Self::Output {
|
fn $method(self, other: U) -> Self::Output {
|
||||||
Self {
|
Self::from_bits(self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32))
|
||||||
bits:self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -144,10 +133,7 @@ macro_rules! impl_multiply_operator_const {
|
|||||||
//this can be done better but that is a job for later
|
//this can be done better but that is a job for later
|
||||||
let lhs=self.bits.as_::<BInt::<{$width*2}>>();
|
let lhs=self.bits.as_::<BInt::<{$width*2}>>();
|
||||||
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
||||||
Self {
|
Self::from_bits(lhs.mul(rhs).shr(Frac::U32).as_())
|
||||||
bits:lhs.mul(rhs).shr(Frac::U32).as_(),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -172,10 +158,7 @@ macro_rules! impl_divide_operator_const {
|
|||||||
//this only needs to be $width+Frac::U32/64+1 but MUH CONST GENERICS!!!!!
|
//this only needs to be $width+Frac::U32/64+1 but MUH CONST GENERICS!!!!!
|
||||||
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(Frac::U32);
|
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(Frac::U32);
|
||||||
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
|
||||||
Self {
|
Self::from_bits(lhs.div(rhs).as_())
|
||||||
bits:lhs.div(rhs).as_(),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -199,10 +182,7 @@ macro_rules! impl_multiplicatave_operator {
|
|||||||
type Output = $output;
|
type Output = $output;
|
||||||
|
|
||||||
fn $method(self, other: U) -> Self::Output {
|
fn $method(self, other: U) -> Self::Output {
|
||||||
Self {
|
Self::from_bits(self.bits.$method(BInt::<CHUNKS>::from(other)))
|
||||||
bits:self.bits.$method(BInt::<CHUNKS>::from(other)),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -276,10 +256,7 @@ macro_rules! impl_shift_operator {
|
|||||||
type Output = $output;
|
type Output = $output;
|
||||||
|
|
||||||
fn $method(self, other: u32) -> Self::Output {
|
fn $method(self, other: u32) -> Self::Output {
|
||||||
Self {
|
Self::from_bits(self.bits.$method(other))
|
||||||
bits:self.bits.$method(other),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -3,7 +3,6 @@ use bnum::cast::As;
|
|||||||
use typenum::{Sum,Unsigned};
|
use typenum::{Sum,Unsigned};
|
||||||
use crate::fixed::Fixed;
|
use crate::fixed::Fixed;
|
||||||
use fixed_wide_traits::wide::WideMul;
|
use fixed_wide_traits::wide::WideMul;
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
macro_rules! impl_wide_mul {
|
macro_rules! impl_wide_mul {
|
||||||
($lhs: expr,$rhs: expr) => {
|
($lhs: expr,$rhs: expr) => {
|
||||||
@ -14,10 +13,7 @@ macro_rules! impl_wide_mul {
|
|||||||
{
|
{
|
||||||
type Output=Fixed<{$lhs+$rhs},Sum<A,B>>;
|
type Output=Fixed<{$lhs+$rhs},Sum<A,B>>;
|
||||||
fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{
|
fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{
|
||||||
Fixed{
|
Fixed::from_bits(self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>())
|
||||||
bits:self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>(),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -44,10 +40,7 @@ impl_wide_mul_all!(
|
|||||||
);
|
);
|
||||||
impl<const SRC:usize,Frac> Fixed<SRC,Frac>{
|
impl<const SRC:usize,Frac> Fixed<SRC,Frac>{
|
||||||
pub fn widen<const DST:usize>(self)->Fixed<DST,Frac>{
|
pub fn widen<const DST:usize>(self)->Fixed<DST,Frac>{
|
||||||
Fixed{
|
Fixed::from_bits(self.bits.as_::<BInt<DST>>())
|
||||||
bits:self.bits.as_::<BInt<DST>>(),
|
|
||||||
frac:PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user