use crate::{
f16::F16,
- traits::{Bool, Compare, Context, ConvertTo, Float, Int, Make, SInt, Select, UInt},
+ traits::{
+ Bool, Compare, Context, ConvertFrom, ConvertTo, Float, Int, Make, SInt, Select, UInt,
+ },
};
use std::{
borrow::Borrow,
}
}
+ impl<'ctx> Select<$vec_name<'ctx>> for IrBool<'ctx> {
+ fn select(self, true_v: $vec_name<'ctx>, false_v: $vec_name<'ctx>) -> $vec_name<'ctx> {
+ let value = self
+ .ctx
+ .make_operation(
+ Opcode::Select,
+ [self.value, true_v.value, false_v.value],
+ $vec_name::TYPE,
+ )
+ .into();
+ $vec_name {
+ value,
+ ctx: self.ctx,
+ }
+ }
+ }
+
impl<'ctx> From<$name<'ctx>> for $vec_name<'ctx> {
fn from(v: $name<'ctx>) -> Self {
let value = v
};
}
+macro_rules! impl_bool_compare {
+ ($ty:ident) => {
+ impl<'ctx> Compare for $ty<'ctx> {
+ type Bool = Self;
+ fn eq(self, rhs: Self) -> Self::Bool {
+ !(self ^ rhs)
+ }
+ fn ne(self, rhs: Self) -> Self::Bool {
+ self ^ rhs
+ }
+ fn lt(self, rhs: Self) -> Self::Bool {
+ !self & rhs
+ }
+ fn gt(self, rhs: Self) -> Self::Bool {
+ self & !rhs
+ }
+ fn le(self, rhs: Self) -> Self::Bool {
+ !self | rhs
+ }
+ fn ge(self, rhs: Self) -> Self::Bool {
+ self | !rhs
+ }
+ }
+ };
+}
+
+impl_bool_compare!(IrBool);
+impl_bool_compare!(IrVecBool);
+
macro_rules! impl_shift_ops {
- ($ty:ident, $rhs:ident) => {
- impl<'ctx> Shl<$rhs<'ctx>> for $ty<'ctx> {
+ ($ty:ident) => {
+ impl<'ctx> Shl for $ty<'ctx> {
type Output = Self;
- fn shl(self, rhs: $rhs<'ctx>) -> Self::Output {
+ fn shl(self, rhs: Self) -> Self::Output {
let value = self
.ctx
.make_operation(Opcode::Shl, [self.value, rhs.value], Self::TYPE)
}
}
}
- impl<'ctx> Shr<$rhs<'ctx>> for $ty<'ctx> {
+ impl<'ctx> Shr for $ty<'ctx> {
type Output = Self;
- fn shr(self, rhs: $rhs<'ctx>) -> Self::Output {
+ fn shr(self, rhs: Self) -> Self::Output {
let value = self
.ctx
.make_operation(Opcode::Shr, [self.value, rhs.value], Self::TYPE)
}
}
}
- impl<'ctx> ShlAssign<$rhs<'ctx>> for $ty<'ctx> {
- fn shl_assign(&mut self, rhs: $rhs<'ctx>) {
+ impl<'ctx> ShlAssign for $ty<'ctx> {
+ fn shl_assign(&mut self, rhs: Self) {
*self = *self << rhs;
}
}
- impl<'ctx> ShrAssign<$rhs<'ctx>> for $ty<'ctx> {
- fn shr_assign(&mut self, rhs: $rhs<'ctx>) {
+ impl<'ctx> ShrAssign for $ty<'ctx> {
+ fn shr_assign(&mut self, rhs: Self) {
*self = *self >> rhs;
}
}
}
macro_rules! impl_int_trait {
- ($ty:ident, $u32:ident) => {
- impl<'ctx> Int<$u32<'ctx>> for $ty<'ctx> {
+ ($ty:ident) => {
+ impl<'ctx> Int for $ty<'ctx> {
fn leading_zeros(self) -> Self {
let value = self
.ctx
($scalar:ident, $vec:ident) => {
impl_bit_ops!($scalar);
impl_number_ops!($scalar, IrBool);
- impl_shift_ops!($scalar, IrU32);
+ impl_shift_ops!($scalar);
impl_bit_ops!($vec);
impl_number_ops!($vec, IrVecBool);
- impl_shift_ops!($vec, IrVecU32);
- impl_int_trait!($scalar, IrU32);
- impl_int_trait!($vec, IrVecU32);
+ impl_shift_ops!($vec);
+ impl_int_trait!($scalar);
+ impl_int_trait!($vec);
};
}
($scalar:ident, $vec:ident) => {
impl_integer_ops!($scalar, $vec);
- impl<'ctx> UInt<IrU32<'ctx>> for $scalar<'ctx> {}
- impl<'ctx> UInt<IrVecU32<'ctx>> for $vec<'ctx> {}
+ impl<'ctx> UInt for $scalar<'ctx> {}
+ impl<'ctx> UInt for $vec<'ctx> {}
};
}
impl_neg!($scalar);
impl_neg!($vec);
- impl<'ctx> SInt<IrU32<'ctx>> for $scalar<'ctx> {}
- impl<'ctx> SInt<IrVecU32<'ctx>> for $vec<'ctx> {}
+ impl<'ctx> SInt for $scalar<'ctx> {}
+ impl<'ctx> SInt for $vec<'ctx> {}
};
}
impl_sint_ops!(IrI64, IrVecI64);
macro_rules! impl_float {
- ($float:ident, $bits:ident, $signed_bits:ident, $u32:ident) => {
- impl<'ctx> Float<$u32<'ctx>> for $float<'ctx> {
+ ($float:ident, $bits:ident, $signed_bits:ident) => {
+ impl<'ctx> Float for $float<'ctx> {
type FloatEncoding = <$float<'ctx> as Make>::Prim;
type BitsType = $bits<'ctx>;
type SignedBitsType = $signed_bits<'ctx>;
impl_number_ops!($vec, IrVecBool);
impl_neg!($scalar);
impl_neg!($vec);
- impl_float!($scalar, $scalar_bits, $scalar_signed_bits, IrU32);
- impl_float!($vec, $vec_bits, $vec_signed_bits, IrVecU32);
+ impl_float!($scalar, $scalar_bits, $scalar_signed_bits);
+ impl_float!($vec, $vec_bits, $vec_signed_bits);
};
}
}
);
-macro_rules! impl_convert_to {
- ($($src:ident -> [$($dest:ident),*];)*) => {
- $($(
- impl<'ctx> ConvertTo<$dest<'ctx>> for $src<'ctx> {
- fn to(self) -> $dest<'ctx> {
- let value = if $src::TYPE == $dest::TYPE {
- self.value
- } else {
- self
- .ctx
- .make_operation(Opcode::Cast, [self.value], $dest::TYPE)
- .into()
- };
- $dest {
- value,
- ctx: self.ctx,
- }
+macro_rules! impl_convert_from {
+ ($src:ident -> $dest:ident) => {
+ impl<'ctx> ConvertFrom<$src<'ctx>> for $dest<'ctx> {
+ fn cvt_from(v: $src<'ctx>) -> Self {
+ let value = if $src::TYPE == $dest::TYPE {
+ v.value
+ } else {
+ v
+ .ctx
+ .make_operation(Opcode::Cast, [v.value], $dest::TYPE)
+ .into()
+ };
+ $dest {
+ value,
+ ctx: v.ctx,
}
}
- )*)*
- };
- ([$($src:ident),*] -> $dest:tt;) => {
- impl_convert_to! {
- $(
- $src -> $dest;
- )*
}
};
- ([$($src:ident),*];) => {
- impl_convert_to! {
- [$($src),*] -> [$($src),*];
- }
+ ($first:ident $(, $ty:ident)*) => {
+ $(
+ impl_convert_from!($first -> $ty);
+ impl_convert_from!($ty -> $first);
+ )*
+ impl_convert_from![$($ty),*];
+ };
+ () => {
};
}
+impl_convert_from![IrU8, IrI8, IrU16, IrI16, IrF16, IrU32, IrI32, IrU64, IrI64, IrF32, IrF64];
-impl_convert_to! {
- [IrU8, IrI8, IrU16, IrI16, IrF16, IrU32, IrI32, IrU64, IrI64, IrF32, IrF64];
-}
-
-impl_convert_to! {
- [IrVecU8, IrVecI8, IrVecU16, IrVecI16, IrVecF16, IrVecU32, IrVecI32, IrVecU64, IrVecI64, IrVecF32, IrVecF64];
-}
+impl_convert_from![
+ IrVecU8, IrVecI8, IrVecU16, IrVecI16, IrVecF16, IrVecU32, IrVecI32, IrVecU64, IrVecI64,
+ IrVecF32, IrVecF64
+];
macro_rules! impl_from {
($src:ident => [$($dest:ident),*]) => {
type U64 = IrU64<'ctx>;
type I64 = IrI64<'ctx>;
type F64 = IrF64<'ctx>;
- type VecBool = IrVecBool<'ctx>;
+ type VecBool8 = IrVecBool<'ctx>;
type VecU8 = IrVecU8<'ctx>;
type VecI8 = IrVecI8<'ctx>;
+ type VecBool16 = IrVecBool<'ctx>;
type VecU16 = IrVecU16<'ctx>;
type VecI16 = IrVecI16<'ctx>;
type VecF16 = IrVecF16<'ctx>;
+ type VecBool32 = IrVecBool<'ctx>;
type VecU32 = IrVecU32<'ctx>;
type VecI32 = IrVecI32<'ctx>;
type VecF32 = IrVecF32<'ctx>;
+ type VecBool64 = IrVecBool<'ctx>;
type VecU64 = IrVecU64<'ctx>;
type VecI64 = IrVecI64<'ctx>;
type VecF64 = IrVecF64<'ctx>;