impl traits for scalar types
[vector-math.git] / src / traits.rs
index c1d8095f2ca78bcde9e13ee19132afd676b25448..fa466542470170c81004ac958f736ee3b7da1647 100644 (file)
@@ -3,7 +3,7 @@ use core::ops::{
     Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
 };
 
-use crate::f16;
+use crate::f16::F16;
 
 #[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823
 macro_rules! make_float_type {
@@ -286,7 +286,7 @@ macro_rules! make_types {
                     $U16;
                     #[int(prim = i16 $(, scalar = $ScalarI16)?)]
                     $I16;
-                    #[float(prim = f16 $(, scalar = $ScalarF16)?)]
+                    #[float(prim = F16 $(, scalar = $ScalarF16)?)]
                     $F16;
                 },
                 {
@@ -412,10 +412,32 @@ pub trait ConvertTo<T> {
     fn to(self) -> T;
 }
 
-impl<T, U: Into<T>> ConvertTo<T> for U {
-    fn to(self) -> T {
-        self.into()
-    }
+macro_rules! impl_convert_to_using_as {
+    ($($src:ident -> [$($dest:ident),*];)*) => {
+        $($(
+            impl ConvertTo<$dest> for $src {
+                fn to(self) -> $dest {
+                    self as $dest
+                }
+            }
+        )*)*
+    };
+    ([$($src:ident),*] -> $dest:tt;) => {
+        impl_convert_to_using_as! {
+            $(
+                $src -> $dest;
+            )*
+        }
+    };
+    ([$($src:ident),*];) => {
+        impl_convert_to_using_as! {
+            [$($src),*] -> [$($src),*];
+        }
+    };
+}
+
+impl_convert_to_using_as! {
+    [u8, i8, u16, i16, u32, i32, u64, i64, f32, f64];
 }
 
 pub trait Number:
@@ -433,6 +455,21 @@ pub trait Number:
 {
 }
 
+impl<T> Number for T where
+    T: Compare
+        + Add<Output = Self>
+        + Sub<Output = Self>
+        + Mul<Output = Self>
+        + Div<Output = Self>
+        + Rem<Output = Self>
+        + AddAssign
+        + SubAssign
+        + MulAssign
+        + DivAssign
+        + RemAssign
+{
+}
+
 pub trait BitOps:
     Copy
     + BitAnd<Output = Self>
@@ -445,6 +482,18 @@ pub trait BitOps:
 {
 }
 
+impl<T> BitOps for T where
+    T: Copy
+        + BitAnd<Output = Self>
+        + BitOr<Output = Self>
+        + BitXor<Output = Self>
+        + Not<Output = Self>
+        + BitAndAssign
+        + BitOrAssign
+        + BitXorAssign
+{
+}
+
 pub trait Int<ShiftRhs>:
     Number
     + BitOps
@@ -459,6 +508,28 @@ pub trait UInt<ShiftRhs>: Int<ShiftRhs> {}
 
 pub trait SInt<ShiftRhs>: Int<ShiftRhs> + Neg<Output = Self> {}
 
+macro_rules! impl_uint {
+    ($($ty:ident),*) => {
+        $(
+            impl Int<u32> for $ty {}
+            impl UInt<u32> for $ty {}
+        )*
+    };
+}
+
+impl_uint![u8, u16, u32, u64];
+
+macro_rules! impl_int {
+    ($($ty:ident),*) => {
+        $(
+            impl Int<u32> for $ty {}
+            impl SInt<u32> for $ty {}
+        )*
+    };
+}
+
+impl_int![i8, i16, i32, i64];
+
 pub trait Float<BitsShiftRhs>: Number + Neg<Output = Self> {
     type BitsType: UInt<BitsShiftRhs>;
     fn abs(self) -> Self;
@@ -466,20 +537,92 @@ pub trait Float<BitsShiftRhs>: Number + Neg<Output = Self> {
     fn ceil(self) -> Self;
     fn floor(self) -> Self;
     fn round(self) -> Self;
+    #[cfg(feature = "fma")]
     fn fma(self, a: Self, b: Self) -> Self;
     fn is_nan(self) -> Self::Bool;
-    fn is_infinity(self) -> Self::Bool;
+    fn is_infinite(self) -> Self::Bool;
     fn is_finite(self) -> Self::Bool;
     fn from_bits(v: Self::BitsType) -> Self;
     fn to_bits(self) -> Self::BitsType;
 }
 
+macro_rules! impl_float {
+    ($ty:ty, $bits:ty) => {
+        impl Float<u32> for $ty {
+            type BitsType = $bits;
+            fn abs(self) -> Self {
+                #[cfg(feature = "std")]
+                return self.abs();
+                #[cfg(not(feature = "std"))]
+                todo!();
+            }
+            fn trunc(self) -> Self {
+                #[cfg(feature = "std")]
+                return self.trunc();
+                #[cfg(not(feature = "std"))]
+                todo!();
+            }
+            fn ceil(self) -> Self {
+                #[cfg(feature = "std")]
+                return self.ceil();
+                #[cfg(not(feature = "std"))]
+                todo!();
+            }
+            fn floor(self) -> Self {
+                #[cfg(feature = "std")]
+                return self.floor();
+                #[cfg(not(feature = "std"))]
+                todo!();
+            }
+            fn round(self) -> Self {
+                #[cfg(feature = "std")]
+                return self.round();
+                #[cfg(not(feature = "std"))]
+                todo!();
+            }
+            #[cfg(feature = "fma")]
+            fn fma(self, a: Self, b: Self) -> Self {
+                self.mul_add(a, b)
+            }
+            fn is_nan(self) -> Self::Bool {
+                self.is_nan()
+            }
+            fn is_infinite(self) -> Self::Bool {
+                self.is_infinite()
+            }
+            fn is_finite(self) -> Self::Bool {
+                self.is_finite()
+            }
+            fn from_bits(v: Self::BitsType) -> Self {
+                <$ty>::from_bits(v)
+            }
+            fn to_bits(self) -> Self::BitsType {
+                self.to_bits()
+            }
+        }
+    };
+}
+
+impl_float!(f32, u32);
+impl_float!(f64, u64);
+
 pub trait Bool: BitOps {}
 
+impl Bool for bool {}
+
 pub trait Select<T>: Bool {
     fn select(self, true_v: T, false_v: T) -> T;
 }
 
+impl<T> Select<T> for bool {
+    fn select(self, true_v: T, false_v: T) -> T {
+        if self {
+            true_v
+        } else {
+            false_v
+        }
+    }
+}
 pub trait Compare: Copy {
     type Bool: Bool + Select<Self>;
     fn eq(self, rhs: Self) -> Self::Bool;
@@ -489,3 +632,33 @@ pub trait Compare: Copy {
     fn le(self, rhs: Self) -> Self::Bool;
     fn ge(self, rhs: Self) -> Self::Bool;
 }
+
+macro_rules! impl_compare_using_partial_cmp {
+    ($($ty:ty),*) => {
+        $(
+            impl Compare for $ty {
+                type Bool = bool;
+                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_compare_using_partial_cmp![u8, i8, u16, i16, F16, u32, i32, f32, u64, i64, f64];