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),*]) => {
($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)
$(
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])
}
}
type Output = Self;
fn neg(self) -> Self::Output {
- Self::from_bits(self.to_bits() ^ 0x8000)
+ f16_impl!(Self::from_bits(self.to_bits() ^ 0x8000), [])
}
}
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 {
}
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;
}
}