add trunc implementation
[vector-math.git] / src / traits.rs
index 2ec48157c3b691883936c6d2e485e8b042c3e1b7..c02b60919cddc332266ce23b36318f2a45ec9e76 100644 (file)
@@ -172,6 +172,9 @@ pub trait Float:
         + Compare<Bool = Self::Bool>
         + ConvertFrom<Self>;
     fn abs(self) -> Self;
+    fn copy_sign(self, sign: Self) -> Self {
+        crate::algorithms::base::copy_sign(self.ctx(), self, sign)
+    }
     fn trunc(self) -> Self;
     fn ceil(self) -> Self;
     fn floor(self) -> Self;
@@ -218,6 +221,33 @@ pub trait Float:
         let mask = self.ctx().make(Self::PrimFloat::MANTISSA_FIELD_MASK);
         self.to_bits() & mask
     }
+    fn is_sign_negative(self) -> Self::Bool {
+        let mask = self.ctx().make(Self::PrimFloat::SIGN_FIELD_MASK);
+        self.ctx()
+            .make::<Self::BitsType>(0.to())
+            .ne(self.to_bits() & mask)
+    }
+    fn is_sign_positive(self) -> Self::Bool {
+        let mask = self.ctx().make(Self::PrimFloat::SIGN_FIELD_MASK);
+        self.ctx()
+            .make::<Self::BitsType>(0.to())
+            .eq(self.to_bits() & mask)
+    }
+    fn extract_sign_field(self) -> Self::BitsType {
+        let shift = self.ctx().make(Self::PrimFloat::SIGN_FIELD_SHIFT);
+        self.to_bits() >> shift
+    }
+    fn from_fields(
+        sign_field: Self::BitsType,
+        exponent_field: Self::BitsType,
+        mantissa_field: Self::BitsType,
+    ) -> Self {
+        let sign_shift = sign_field.ctx().make(Self::PrimFloat::SIGN_FIELD_SHIFT);
+        let exponent_shift = sign_field.ctx().make(Self::PrimFloat::EXPONENT_FIELD_SHIFT);
+        Self::from_bits(
+            (sign_field << sign_shift) | (exponent_field << exponent_shift) | mantissa_field,
+        )
+    }
     fn sub_exponent_bias(exponent_field: Self::BitsType) -> Self::SignedBitsType {
         Self::SignedBitsType::cvt_from(exponent_field)
             - exponent_field
@@ -229,14 +259,14 @@ pub trait Float:
     }
 }
 
-pub trait Bool: Make + BitOps {}
+pub trait Bool: Make<Prim = bool> + BitOps + Select<Self> {}
 
-pub trait Select<T>: Bool {
+pub trait Select<T> {
     fn select(self, true_v: T, false_v: T) -> T;
 }
 
 pub trait Compare: Make {
-    type Bool: Bool + Select<Self>;
+    type Bool: Bool + Select<Self> + Make<Context = Self::Context>;
     fn eq(self, rhs: Self) -> Self::Bool;
     fn ne(self, rhs: Self) -> Self::Bool;
     fn lt(self, rhs: Self) -> Self::Bool;