switch to using separate VecBool8/16/32/64
[vector-math.git] / src / ir.rs
index f1de05aa7d6bad699471d3afcbd4a5bd86724ac2..a31bece004894b45b290738fd954b96009f070fc 100644 (file)
--- a/src/ir.rs
+++ b/src/ir.rs
@@ -809,6 +809,23 @@ macro_rules! ir_value {
             }
         }
 
+        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
@@ -1060,12 +1077,41 @@ macro_rules! impl_number_ops {
     };
 }
 
+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)
@@ -1076,10 +1122,10 @@ macro_rules! impl_shift_ops {
                 }
             }
         }
-        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)
@@ -1090,13 +1136,13 @@ macro_rules! impl_shift_ops {
                 }
             }
         }
-        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;
             }
         }
@@ -1123,8 +1169,8 @@ macro_rules! impl_neg {
 }
 
 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
@@ -1163,12 +1209,12 @@ macro_rules! impl_integer_ops {
     ($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);
     };
 }
 
@@ -1176,8 +1222,8 @@ macro_rules! impl_uint_ops {
     ($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> {}
     };
 }
 
@@ -1192,8 +1238,8 @@ macro_rules! impl_sint_ops {
         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> {}
     };
 }
 
@@ -1203,8 +1249,8 @@ impl_sint_ops!(IrI32, IrVecI32);
 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>;
@@ -1330,8 +1376,8 @@ macro_rules! impl_float_ops {
         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);
     };
 }
 
@@ -1444,47 +1490,40 @@ ir_value!(
 );
 
 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,
-                    }
+    ($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,
                 }
             }
-        )*)*
-    };
-    ([$($src:ident),*] -> $dest:tt;) => {
-        impl_convert_to! {
-            $(
-                $src -> $dest;
-            )*
         }
     };
-    ([$($src:ident),*];) => {
-        impl_convert_to! {
-            [$($src),*] -> [$($src),*];
-        }
+    ($first:ident $(, $ty:ident)*) => {
+        $(
+            impl_convert_to!($first -> $ty);
+            impl_convert_to!($ty -> $first);
+        )*
+        impl_convert_to![$($ty),*];
+    };
+    () => {
     };
 }
+impl_convert_to![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_to![
+    IrVecU8, IrVecI8, IrVecU16, IrVecI16, IrVecF16, IrVecU32, IrVecI32, IrVecU64, IrVecI64,
+    IrVecF32, IrVecF64
+];
 
 macro_rules! impl_from {
     ($src:ident => [$($dest:ident),*]) => {
@@ -1564,15 +1603,18 @@ impl<'ctx> Context for &'ctx IrContext<'ctx> {
     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>;