eviscerate PhantomData

This commit is contained in:
Quaternions 2024-08-29 20:07:18 -07:00
parent 46d89619bd
commit 0924518922
2 changed files with 14 additions and 44 deletions

View File

@ -1,11 +1,10 @@
use bnum::{BInt,cast::As};
use typenum::Unsigned;
use std::marker::PhantomData;
#[derive(Clone,Copy,Debug,Hash)]
pub struct Fixed<const CHUNKS:usize,Frac>{
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>{
@ -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_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));
}
impl<const CHUNKS:usize,Frac> Fixed<CHUNKS,Frac>{
pub const fn from_bits(bits:BInt::<CHUNKS>)->Self{
Self{
bits,
frac:PhantomData,
frac:std::marker::PhantomData,
}
}
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>
{
fn from(value:T)->Self{
Self{
bits:BInt::<{CHUNKS}>::from(value)<<Frac::U32,
frac:PhantomData,
}
Self::from_bits(BInt::<{CHUNKS}>::from(value)<<Frac::U32)
}
}
@ -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>{
type Output=Self;
fn neg(self)->Self{
Self{
bits:self.bits.neg(),
frac:PhantomData,
}
Self::from_bits(self.bits.neg())
}
}
@ -80,10 +75,7 @@ macro_rules! impl_additive_operator {
type Output = $output;
fn $method(self, other: Self) -> Self::Output {
Self {
bits:self.bits.$method(other.bits),
frac:PhantomData,
}
Self::from_bits(self.bits.$method(other.bits))
}
}
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;
fn $method(self, other: U) -> Self::Output {
Self {
bits:self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32),
frac:PhantomData,
}
Self::from_bits(self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32))
}
}
};
@ -144,10 +133,7 @@ macro_rules! impl_multiply_operator_const {
//this can be done better but that is a job for later
let lhs=self.bits.as_::<BInt::<{$width*2}>>();
let rhs=other.bits.as_::<BInt::<{$width*2}>>();
Self {
bits:lhs.mul(rhs).shr(Frac::U32).as_(),
frac:PhantomData,
}
Self::from_bits(lhs.mul(rhs).shr(Frac::U32).as_())
}
}
};
@ -172,10 +158,7 @@ macro_rules! impl_divide_operator_const {
//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 rhs=other.bits.as_::<BInt::<{$width*2}>>();
Self {
bits:lhs.div(rhs).as_(),
frac:PhantomData,
}
Self::from_bits(lhs.div(rhs).as_())
}
}
};
@ -199,10 +182,7 @@ macro_rules! impl_multiplicatave_operator {
type Output = $output;
fn $method(self, other: U) -> Self::Output {
Self {
bits:self.bits.$method(BInt::<CHUNKS>::from(other)),
frac:PhantomData,
}
Self::from_bits(self.bits.$method(BInt::<CHUNKS>::from(other)))
}
}
};
@ -276,10 +256,7 @@ macro_rules! impl_shift_operator {
type Output = $output;
fn $method(self, other: u32) -> Self::Output {
Self {
bits:self.bits.$method(other),
frac:PhantomData,
}
Self::from_bits(self.bits.$method(other))
}
}
};

View File

@ -3,7 +3,6 @@ use bnum::cast::As;
use typenum::{Sum,Unsigned};
use crate::fixed::Fixed;
use fixed_wide_traits::wide::WideMul;
use std::marker::PhantomData;
macro_rules! impl_wide_mul {
($lhs: expr,$rhs: expr) => {
@ -14,10 +13,7 @@ macro_rules! impl_wide_mul {
{
type Output=Fixed<{$lhs+$rhs},Sum<A,B>>;
fn wide_mul(self,rhs:Fixed<$rhs,B>)->Self::Output{
Fixed{
bits:self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>(),
frac:PhantomData,
}
Fixed::from_bits(self.bits.as_::<BInt<{$lhs+$rhs}>>()*rhs.bits.as_::<BInt<{$lhs+$rhs}>>())
}
}
};
@ -44,10 +40,7 @@ impl_wide_mul_all!(
);
impl<const SRC:usize,Frac> Fixed<SRC,Frac>{
pub fn widen<const DST:usize>(self)->Fixed<DST,Frac>{
Fixed{
bits:self.bits.as_::<BInt<DST>>(),
frac:PhantomData,
}
Fixed::from_bits(self.bits.as_::<BInt<DST>>())
}
}