stdsimd bindings work!
[vector-math.git] / src / f16.rs
index 5bf2e69c213a401a12faf75b6dc8953a8cad9229..5bc1119443466e949640876b70e54c6bf15f24d6 100644 (file)
@@ -8,13 +8,18 @@ use crate::traits::{ConvertTo, Float};
 use half::f16 as F16Impl;
 
 #[cfg(not(feature = "f16"))]
-#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
-enum F16Impl {}
+type F16Impl = u16;
 
 #[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
-#[cfg_attr(feature = "f16", repr(transparent))]
+#[repr(transparent)]
 pub struct F16(F16Impl);
 
+#[cfg(not(feature = "f16"))]
+#[track_caller]
+pub(crate) fn panic_f16_feature_disabled() -> ! {
+    panic!("f16 feature is not enabled")
+}
+
 #[cfg(feature = "f16")]
 macro_rules! f16_impl {
     ($v:expr, [$($vars:ident),*]) => {
@@ -27,11 +32,17 @@ macro_rules! f16_impl {
     ($v:expr, [$($vars:ident),*]) => {
         {
             $(let _ = $vars;)*
-            panic!("f16 feature is not enabled")
+            panic_f16_feature_disabled()
         }
     };
 }
 
+impl Default for F16 {
+    fn default() -> Self {
+        f16_impl!(F16(F16Impl::default()), [])
+    }
+}
+
 impl From<F16Impl> for F16 {
     fn from(v: F16Impl) -> Self {
         F16(v)
@@ -67,10 +78,7 @@ macro_rules! impl_from_f16 {
         $(
             impl From<F16> for $ty {
                 fn from(v: F16) -> Self {
-                    #[cfg(feature = "f16")]
-                    return v.0.into();
-                    #[cfg(not(feature = "f16"))]
-                    match v.0 {}
+                    f16_impl!(v.0.into(), [v])
                 }
             }
 
@@ -133,7 +141,7 @@ impl Neg for F16 {
     type Output = Self;
 
     fn neg(self) -> Self::Output {
-        Self::from_bits(self.to_bits() ^ 0x8000)
+        f16_impl!(Self::from_bits(self.to_bits() ^ 0x8000), [])
     }
 }
 
@@ -165,11 +173,13 @@ impl_bin_op_using_f32! {
     Rem, rem, RemAssign, rem_assign;
 }
 
-impl Float<u32> for F16 {
+impl Float for F16 {
+    type FloatEncoding = F16;
     type BitsType = u16;
+    type SignedBitsType = i16;
 
     fn abs(self) -> Self {
-        Self::from_bits(self.to_bits() & 0x7FFF)
+        f16_impl!(Self::from_bits(self.to_bits() & 0x7FFF), [])
     }
 
     fn trunc(self) -> Self {
@@ -206,11 +216,17 @@ impl Float<u32> for F16 {
     }
 
     fn from_bits(v: Self::BitsType) -> Self {
-        f16_impl!(F16(F16Impl::from_bits(v)), [v])
+        #[cfg(feature = "f16")]
+        return F16(F16Impl::from_bits(v));
+        #[cfg(not(feature = "f16"))]
+        return F16(v);
     }
 
     fn to_bits(self) -> Self::BitsType {
-        f16_impl!(self.0.to_bits(), [])
+        #[cfg(feature = "f16")]
+        return self.0.to_bits();
+        #[cfg(not(feature = "f16"))]
+        return self.0;
     }
 }