split <int>.cast() into cast_as_type and cast to be more useful when casting to deduced fixed types

This commit is contained in:
Jacob Lifshay 2024-07-19 17:12:29 -07:00
parent af95bb2f58
commit dba883e300
Signed by: programmerjake
SSH key fingerprint: SHA256:B1iRVvUJkvd7upMIiMqn6OyxvD2SgJkAH3ZnUOj6z+c
2 changed files with 15 additions and 9 deletions

View file

@ -11,7 +11,7 @@ use crate::{
}, },
int::{ int::{
DynInt, DynIntType, DynSInt, DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, Int, DynInt, DynIntType, DynSInt, DynSIntType, DynUInt, DynUIntType, FixedOrDynIntType, Int,
IntCmp, IntTypeTrait, IntValue, UInt, UIntType, IntCmp, IntType, IntTypeTrait, IntValue, UInt, UIntType,
}, },
intern::{Intern, Interned}, intern::{Intern, Interned},
reset::{ reset::{
@ -888,7 +888,7 @@ fixed_ary_op! {
}, },
fn simulate(&self, sim_state: &mut SimState) -> _ { fn simulate(&self, sim_state: &mut SimState) -> _ {
self.value.simulate(sim_state).cast(self.ty.canonical()) self.value.simulate(sim_state).cast_as_type(self.ty.canonical())
} }
fn expr_enum(&self) -> _ { fn expr_enum(&self) -> _ {
@ -935,7 +935,7 @@ impl<
>, >,
> Expr<IntValue<FromType>> > Expr<IntValue<FromType>>
{ {
pub fn cast< pub fn cast_as_type<
ToType: IntTypeTrait< ToType: IntTypeTrait<
CanonicalType = DynIntType<<ToType as IntTypeTrait>::Signed>, CanonicalType = DynIntType<<ToType as IntTypeTrait>::Signed>,
CanonicalValue = DynInt<<ToType as IntTypeTrait>::Signed>, CanonicalValue = DynInt<<ToType as IntTypeTrait>::Signed>,
@ -946,17 +946,20 @@ impl<
) -> Expr<ToType::Value> { ) -> Expr<ToType::Value> {
CastInt::<FromType, ToType>::new_unchecked(self.canonical(), ty).to_expr() CastInt::<FromType, ToType>::new_unchecked(self.canonical(), ty).to_expr()
} }
pub fn cast<Signed: GenericConstBool, const WIDTH: usize>(self) -> Expr<Int<Signed, WIDTH>> {
self.cast_as_type(IntType::new())
}
pub fn as_same_width_uint(self) -> Expr<IntValue<FromType::SameWidthUInt>> { pub fn as_same_width_uint(self) -> Expr<IntValue<FromType::SameWidthUInt>> {
self.cast(self.ty().as_same_width_uint()) self.cast_as_type(self.ty().as_same_width_uint())
} }
pub fn as_same_width_sint(self) -> Expr<IntValue<FromType::SameWidthSInt>> { pub fn as_same_width_sint(self) -> Expr<IntValue<FromType::SameWidthSInt>> {
self.cast(self.ty().as_same_width_sint()) self.cast_as_type(self.ty().as_same_width_sint())
} }
pub fn as_same_value_uint(self) -> Expr<DynUInt> { pub fn as_same_value_uint(self) -> Expr<DynUInt> {
self.cast(self.ty().as_same_value_uint()) self.cast_as_type(self.ty().as_same_value_uint())
} }
pub fn as_same_value_sint(self) -> Expr<DynSInt> { pub fn as_same_value_sint(self) -> Expr<DynSInt> {
self.cast(self.ty().as_same_value_sint()) self.cast_as_type(self.ty().as_same_value_sint())
} }
} }
@ -1020,7 +1023,7 @@ impl<
return false.to_expr(); return false.to_expr();
} }
let index = index.min(width - 1); let index = index.min(width - 1);
self.slice(index..=index).cast(UIntType::new()) self.slice(index..=index).cast()
} }
pub fn is_negative(self) -> Expr<UInt<1>> { pub fn is_negative(self) -> Expr<UInt<1>> {
if T::Signed::VALUE { if T::Signed::VALUE {

View file

@ -211,7 +211,7 @@ impl<
IntValue::with_type(new_type, self.into_uint_value()) IntValue::with_type(new_type, self.into_uint_value())
} }
} }
pub fn cast< pub fn cast_as_type<
NewType: IntTypeTrait< NewType: IntTypeTrait<
CanonicalType = DynIntType<<NewType as IntTypeTrait>::Signed>, CanonicalType = DynIntType<<NewType as IntTypeTrait>::Signed>,
CanonicalValue = DynInt<<NewType as IntTypeTrait>::Signed>, CanonicalValue = DynInt<<NewType as IntTypeTrait>::Signed>,
@ -226,6 +226,9 @@ impl<
IntValue::with_type(new_type, self.uint_value()) IntValue::with_type(new_type, self.uint_value())
} }
} }
pub fn cast<Signed: GenericConstBool, const WIDTH: usize>(self) -> Int<Signed, WIDTH> {
self.cast_as_type(IntType::new())
}
pub fn as_same_width_uint(self) -> IntValue<T::SameWidthUInt> { pub fn as_same_width_uint(self) -> IntValue<T::SameWidthUInt> {
IntValue { IntValue {
ty: self.ty.as_same_width_uint(), ty: self.ty.as_same_width_uint(),