support IntCmp with primitive integer types

This commit is contained in:
Jacob Lifshay 2024-07-22 00:50:53 -07:00
parent 422330d195
commit 5707ede2ae
Signed by: programmerjake
SSH key fingerprint: SHA256:B1iRVvUJkvd7upMIiMqn6OyxvD2SgJkAH3ZnUOj6z+c
2 changed files with 49 additions and 29 deletions

View file

@ -784,38 +784,17 @@ macro_rules! cmp_op {
CanonicalType = DynIntType<<RhsType as IntTypeTrait>::Signed>,
CanonicalValue = DynInt<<RhsType as IntTypeTrait>::Signed>,
>,
Rhs: Value<Type = RhsType>,
Lhs: Value<Type = LhsType>,
> IntCmp<Expr<Rhs>> for Expr<Lhs>
{
type Output = Expr<UInt<1>>;
$(fn $fn(self, rhs: Expr<Rhs>) -> Self::Output {
$CmpOp::<LhsType, RhsType, UIntType<1>>::new_unchecked(
self.canonical(),
rhs.canonical(),
).to_expr()
})*
}
impl<
LhsType: IntTypeTrait<
CanonicalType = DynIntType<<LhsType as IntTypeTrait>::Signed>,
CanonicalValue = DynInt<<LhsType as IntTypeTrait>::Signed>,
>,
RhsType: IntTypeTrait<
Signed = LhsType::Signed,
CanonicalType = DynIntType<<RhsType as IntTypeTrait>::Signed>,
CanonicalValue = DynInt<<RhsType as IntTypeTrait>::Signed>,
>,
Rhs: Value<Type = RhsType>,
Rhs: ToExpr<Type = RhsType>,
Lhs: Value<Type = LhsType>,
> IntCmp<Rhs> for Expr<Lhs>
{
type Output = Expr<UInt<1>>;
$(fn $fn(self, rhs: Rhs) -> Self::Output {
self.$fn(rhs.to_expr())
$CmpOp::<LhsType, RhsType, UIntType<1>>::new_unchecked(
self.canonical(),
rhs.to_expr().canonical(),
).to_expr()
})*
}
@ -829,11 +808,11 @@ macro_rules! cmp_op {
CanonicalType = DynIntType<<RhsType as IntTypeTrait>::Signed>,
CanonicalValue = DynInt<<RhsType as IntTypeTrait>::Signed>,
>,
Rhs: Value<Type = RhsType>,
> IntCmp<Expr<Rhs>> for IntValue<LhsType> {
Rhs: ToExpr<Type = RhsType>,
> IntCmp<Rhs> for IntValue<LhsType> {
type Output = Expr<UInt<1>>;
$(fn $fn(self, rhs: Expr<Rhs>) -> Self::Output {
$(fn $fn(self, rhs: Rhs) -> Self::Output {
self.to_expr().$fn(rhs)
})*
}

View file

@ -860,6 +860,47 @@ pub trait IntCmp<Rhs> {
fn cmp_ge(self, rhs: Rhs) -> Self::Output;
}
macro_rules! forward_prim_int_cmp {
($prim_ty:ident) => {
impl<Rhs> IntCmp<Rhs> for $prim_ty
where
IntValue<<$prim_ty as ToExpr>::Type>: IntCmp<Rhs>,
{
type Output = <IntValue<<$prim_ty as ToExpr>::Type> as IntCmp<Rhs>>::Output;
fn cmp_eq(self, rhs: Rhs) -> Self::Output {
IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_eq(rhs)
}
fn cmp_ne(self, rhs: Rhs) -> Self::Output {
IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_ne(rhs)
}
fn cmp_lt(self, rhs: Rhs) -> Self::Output {
IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_lt(rhs)
}
fn cmp_le(self, rhs: Rhs) -> Self::Output {
IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_le(rhs)
}
fn cmp_gt(self, rhs: Rhs) -> Self::Output {
IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_gt(rhs)
}
fn cmp_ge(self, rhs: Rhs) -> Self::Output {
IntValue::<<$prim_ty as ToExpr>::Type>::from(self).cmp_ge(rhs)
}
}
};
}
forward_prim_int_cmp!(bool);
forward_prim_int_cmp!(u8);
forward_prim_int_cmp!(u16);
forward_prim_int_cmp!(u32);
forward_prim_int_cmp!(u64);
forward_prim_int_cmp!(u128);
forward_prim_int_cmp!(i8);
forward_prim_int_cmp!(i16);
forward_prim_int_cmp!(i32);
forward_prim_int_cmp!(i64);
forward_prim_int_cmp!(i128);
mod sealed {
pub trait Sealed {}
}