From 4c7a1f20ba6068e1e9a020db9210c7f224453942 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Sun, 2 May 2021 16:01:10 -0700 Subject: [PATCH] finish traits macros --- src/traits.rs | 626 ++++++++++++++++++++++---------------------------- 1 file changed, 276 insertions(+), 350 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index d5c7b77..b8eb99d 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -5,6 +5,216 @@ use core::ops::{ use crate::F16; +#[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823 +macro_rules! make_float_type { + ( + #[u32 = $u32:ident] + #[bool = $bool:ident] + [ + $({ + #[uint] + $uint_smaller:ident; + #[int] + $int_smaller:ident; + $( + #[float] + $float_smaller:ident; + )? + },)* + ], + { + #[uint] + $uint:ident; + #[int] + $int:ident; + #[float(prim = $float_prim:ident $(, scalar = $float_scalar:ident)?)] + $float:ident; + }, + [ + $({ + #[uint] + $uint_larger:ident; + #[int] + $int_larger:ident; + $( + #[float] + $float_larger:ident; + )? + },)* + ] + ) => { + type $float: Float + $(+ From)? + + Compare + + Make + $(+ ConvertTo)* + $(+ ConvertTo)* + $($(+ ConvertTo)?)* + + ConvertTo + + ConvertTo + $(+ ConvertTo)* + $(+ ConvertTo)* + $($(+ Into)?)*; + }; + ( + #[u32 = $u32:ident] + #[bool = $bool:ident] + [$($smaller:tt,)*], + { + #[uint] + $uint:ident; + #[int] + $int:ident; + }, + [$($larger:tt,)*] + ) => {}; +} + +#[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823 +macro_rules! make_uint_int_float_type { + ( + #[u32 = $u32:ident] + #[bool = $bool:ident] + [ + $({ + #[uint($($uint_smaller_traits:tt)*)] + $uint_smaller:ident; + #[int($($int_smaller_traits:tt)*)] + $int_smaller:ident; + $( + #[float($($float_smaller_traits:tt)*)] + $float_smaller:ident; + )? + },)* + ], + { + #[uint(prim = $uint_prim:ident $(, scalar = $uint_scalar:ident)?)] + $uint:ident; + #[int(prim = $int_prim:ident $(, scalar = $int_scalar:ident)?)] + $int:ident; + $( + #[float(prim = $float_prim:ident $(, scalar = $float_scalar:ident)?)] + $float:ident; + )? + }, + [ + $({ + #[uint($($uint_larger_traits:tt)*)] + $uint_larger:ident; + #[int($($int_larger_traits:tt)*)] + $int_larger:ident; + $( + #[float($($float_larger_traits:tt)*)] + $float_larger:ident; + )? + },)* + ] + ) => { + type $uint: UInt + $(+ From)? + + Compare + + Make + $(+ ConvertTo)* + $(+ ConvertTo)* + $($(+ ConvertTo)?)* + + ConvertTo + $(+ ConvertTo)? + $(+ Into)* + $(+ Into)* + $($(+ Into)?)*; + type $int: SInt + $(+ From)? + + Compare + + Make + $(+ ConvertTo)* + $(+ ConvertTo)* + $($(+ ConvertTo)?)* + + ConvertTo + $(+ ConvertTo)? + $(+ ConvertTo)* + $(+ Into)* + $($(+ Into)?)*; + make_float_type! { + #[u32 = $u32] + #[bool = $bool] + [ + $({ + #[uint] + $uint_smaller; + #[int] + $int_smaller; + $( + #[float] + $float_smaller; + )? + },)* + ], + { + #[uint] + $uint; + #[int] + $int; + $( + #[float(prim = $float_prim $(, scalar = $float_scalar)?)] + $float; + )? + }, + [ + $({ + #[uint] + $uint_larger; + #[int] + $int_larger; + $( + #[float] + $float_larger; + )? + },)* + ] + } + }; +} + +macro_rules! make_uint_int_float_types { + ( + #[u32 = $u32:ident] + #[bool = $bool:ident] + [$($smaller:tt,)*], + $current:tt, + [$first_larger:tt, $($larger:tt,)*] + ) => { + make_uint_int_float_type! { + #[u32 = $u32] + #[bool = $bool] + [$($smaller,)*], + $current, + [$first_larger, $($larger,)*] + } + make_uint_int_float_types! { + #[u32 = $u32] + #[bool = $bool] + [$($smaller,)* $current,], + $first_larger, + [$($larger,)*] + } + }; + ( + #[u32 = $u32:ident] + #[bool = $bool:ident] + [$($smaller:tt,)*], + $current:tt, + [] + ) => { + make_uint_int_float_type! { + #[u32 = $u32] + #[bool = $bool] + [$($smaller,)*], + $current, + [] + } + }; +} + #[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823 macro_rules! make_types { ( @@ -56,151 +266,48 @@ macro_rules! make_types { $(#[scalar = $ScalarF64:ident])? type $F64:ident; ) => { - type Bool: Bool + Make + Select; - type U8: UInt - + Compare - + Make - + ConvertTo - + Into - + Into - + Into - + Into - + Into - + Into - + Into - + Into - + Into; - type U16: UInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into - + Into - + Into - + Into - + Into; - type U32: UInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into - + Into; - type U64: UInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo; - type I8: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + Into - + Into - + ConvertTo - + Into - + Into - + ConvertTo - + Into - + Into; - type I16: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into - + ConvertTo - + Into - + Into; - type I32: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into; - type I64: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo; - type F16: Float - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + ConvertTo - + ConvertTo - + Into; - type F32: Float - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into; - type F64: Float - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo; - }; + type $Bool: Bool + $(+ From)? + + Make + + Select; + make_uint_int_float_types! { + #[u32 = $U32] + #[bool = $Bool] + [], + { + #[uint(prim = u8 $(, scalar = $ScalarU8)?)] + $U8; + #[int(prim = i8 $(, scalar = $ScalarI8)?)] + $I8; + }, + [ + { + #[uint(prim = u16 $(, scalar = $ScalarU16)?)] + $U16; + #[int(prim = i16 $(, scalar = $ScalarI16)?)] + $I16; + #[float(prim = F16 $(, scalar = $ScalarF16)?)] + $F16; + }, + { + #[uint(prim = u32 $(, scalar = $ScalarU32)?)] + $U32; + #[int(prim = i32 $(, scalar = $ScalarI32)?)] + $I32; + #[float(prim = f32 $(, scalar = $ScalarF32)?)] + $F32; + }, + { + #[uint(prim = u64 $(, scalar = $ScalarU64)?)] + $U64; + #[int(prim = i64 $(, scalar = $ScalarI64)?)] + $I64; + #[float(prim = f64 $(, scalar = $ScalarF64)?)] + $F64; + }, + ] + } + }; } /// reference used to build IR for Kazan; an empty type for `core::simd` @@ -244,234 +351,53 @@ pub trait Context: Copy { } make_types! { #[bool] - #[scalar = ScalarBool] - type Bool; + #[scalar = Bool] + type VecBool; #[u8] - #[scalar = ScalarU8] - type U8; + #[scalar = U8] + type VecU8; #[u16] - #[scalar = ScalarU16] - type U16; + #[scalar = U16] + type VecU16; #[u32] - #[scalar = ScalarU32] - type U32; + #[scalar = U32] + type VecU32; #[u64] - #[scalar = ScalarU64] - type U64; + #[scalar = U64] + type VecU64; #[i8] - #[scalar = ScalarI8] - type I8; + #[scalar = I8] + type VecI8; #[i16] - #[scalar = ScalarI16] - type I16; + #[scalar = I16] + type VecI16; #[i32] - #[scalar = ScalarI32] - type I32; + #[scalar = I32] + type VecI32; #[i64] - #[scalar = ScalarI64] - type I64; + #[scalar = I64] + type VecI64; #[f16] - #[scalar = ScalarF16] - type F16; + #[scalar = F16] + type VecF16; #[f32] - #[scalar = ScalarF32] - type F32; + #[scalar = F32] + type VecF32; #[f64] - #[scalar = ScalarF64] - type F64; + #[scalar = F64] + type VecF64; } - type Bool: Bool + Make + Select; - type U8: UInt - + Compare - + Make - + ConvertTo - + Into - + Into - + Into - + Into - + Into - + Into - + Into - + Into - + Into; - type U16: UInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into - + Into - + Into - + Into - + Into; - type U32: UInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into - + Into; - type U64: UInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo; - type I8: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + Into - + Into - + ConvertTo - + Into - + Into - + ConvertTo - + Into - + Into; - type I16: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into - + ConvertTo - + Into - + Into; - type I32: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + Into; - type I64: SInt - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo; - type F16: Float - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into - + ConvertTo - + ConvertTo - + Into; - type F32: Float - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + Into; - type F64: Float - + Compare - + Make - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo - + ConvertTo; - // Vector types - type VecBool: From + Bool + Make + Select; - type VecU8: From - + UInt - + Compare - + Make; - type VecI8: From - + SInt - + Compare - + Make; - type VecU16: From - + UInt - + Compare - + Make; - type VecI16: From - + SInt - + Compare - + Make; - type VecF16: From + Float + Compare + Make; - type VecU32: From - + UInt - + Compare - + Make; - type VecI32: From - + SInt - + Compare - + Make; - type VecF32: From + Float + Compare + Make; - type VecU64: From - + UInt - + Compare - + Make; - type VecI64: From - + SInt - + Compare - + Make; - type VecF64: From + Float + Compare + Make; fn make>(self, v: T::Prim) -> T { T::make(self, v) } -- 2.30.2