2 Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
5 use crate::traits::{ConvertTo, Float};
7 #[cfg(feature = "f16")]
8 use half::f16 as F16Impl;
10 #[cfg(not(feature = "f16"))]
13 #[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
15 pub struct F16(F16Impl);
17 #[cfg(not(feature = "f16"))]
19 pub(crate) fn panic_f16_feature_disabled() -> ! {
20 panic!("f16 feature is not enabled")
23 #[cfg(feature = "f16")]
24 macro_rules! f16_impl {
25 ($v:expr, [$($vars:ident),*]) => {
30 #[cfg(not(feature = "f16"))]
31 macro_rules! f16_impl {
32 ($v:expr, [$($vars:ident),*]) => {
35 panic_f16_feature_disabled()
40 impl Default for F16 {
41 fn default() -> Self {
42 f16_impl!(F16(F16Impl::default()), [])
46 impl From<F16Impl> for F16 {
47 fn from(v: F16Impl) -> Self {
52 impl From<F16> for F16Impl {
53 fn from(v: F16) -> Self {
58 macro_rules! impl_f16_from {
61 impl From<$ty> for F16 {
62 fn from(v: $ty) -> Self {
63 f16_impl!(F16(F16Impl::from(v)), [v])
67 impl ConvertTo<F16> for $ty {
76 macro_rules! impl_from_f16 {
79 impl From<F16> for $ty {
80 fn from(v: F16) -> Self {
81 f16_impl!(v.0.into(), [v])
85 impl ConvertTo<$ty> for F16 {
94 impl_f16_from![i8, u8,];
96 impl_from_f16![f32, f64,];
98 macro_rules! impl_int_to_f16 {
99 ($($int:ident),*) => {
101 impl ConvertTo<F16> for $int {
103 // f32 has enough mantissa bits such that f16 overflows to
104 // infinity before f32 stops being able to properly
105 // represent integer values, making the below conversion correct.
113 macro_rules! impl_f16_to_int {
114 ($($int:ident),*) => {
116 impl ConvertTo<$int> for F16 {
117 fn to(self) -> $int {
118 f32::from(self) as $int
125 impl_int_to_f16![i16, u16, i32, u32, i64, u64, i128, u128];
126 impl_f16_to_int![i8, u8, i16, u16, i32, u32, i64, u64, i128, u128];
128 impl ConvertTo<F16> for f32 {
130 f16_impl!(F16(F16Impl::from_f32(self)), [])
134 impl ConvertTo<F16> for f64 {
136 f16_impl!(F16(F16Impl::from_f64(self)), [])
143 fn neg(self) -> Self::Output {
144 f16_impl!(Self::from_bits(self.to_bits() ^ 0x8000), [])
148 macro_rules! impl_bin_op_using_f32 {
149 ($($op:ident, $op_fn:ident, $op_assign:ident, $op_assign_fn:ident;)*) => {
154 fn $op_fn(self, rhs: Self) -> Self::Output {
155 f32::from(self).$op_fn(f32::from(rhs)).to()
159 impl $op_assign for F16 {
160 fn $op_assign_fn(&mut self, rhs: Self) {
161 *self = (*self).$op_fn(rhs);
168 impl_bin_op_using_f32! {
169 Add, add, AddAssign, add_assign;
170 Sub, sub, SubAssign, sub_assign;
171 Mul, mul, MulAssign, mul_assign;
172 Div, div, DivAssign, div_assign;
173 Rem, rem, RemAssign, rem_assign;
177 type FloatEncoding = F16;
179 type SignedBitsType = i16;
181 fn abs(self) -> Self {
182 f16_impl!(Self::from_bits(self.to_bits() & 0x7FFF), [])
185 fn trunc(self) -> Self {
186 f32::from(self).trunc().to()
189 fn ceil(self) -> Self {
190 f32::from(self).ceil().to()
193 fn floor(self) -> Self {
194 f32::from(self).floor().to()
197 fn round(self) -> Self {
198 f32::from(self).round().to()
201 #[cfg(feature = "fma")]
202 fn fma(self, a: Self, b: Self) -> Self {
203 (f64::from(self) * f64::from(a) + f64::from(b)).to()
206 fn is_nan(self) -> Self::Bool {
207 f16_impl!(self.0.is_nan(), [])
210 fn is_infinite(self) -> Self::Bool {
211 f16_impl!(self.0.is_infinite(), [])
214 fn is_finite(self) -> Self::Bool {
215 f16_impl!(self.0.is_finite(), [])
218 fn from_bits(v: Self::BitsType) -> Self {
219 #[cfg(feature = "f16")]
220 return F16(F16Impl::from_bits(v));
221 #[cfg(not(feature = "f16"))]
225 fn to_bits(self) -> Self::BitsType {
226 #[cfg(feature = "f16")]
227 return self.0.to_bits();
228 #[cfg(not(feature = "f16"))]
236 use core::cmp::Ordering;
240 not(feature = "f16"),
241 should_panic(expected = "f16 feature is not enabled")
244 assert_eq!(F16::from_bits(0x8000).abs().to_bits(), 0);
245 assert_eq!(F16::from_bits(0).abs().to_bits(), 0);
246 assert_eq!(F16::from_bits(0x8ABC).abs().to_bits(), 0xABC);
247 assert_eq!(F16::from_bits(0xFE00).abs().to_bits(), 0x7E00);
248 assert_eq!(F16::from_bits(0x7E00).abs().to_bits(), 0x7E00);
253 not(feature = "f16"),
254 should_panic(expected = "f16 feature is not enabled")
257 assert_eq!(F16::from_bits(0x8000).neg().to_bits(), 0);
258 assert_eq!(F16::from_bits(0).neg().to_bits(), 0x8000);
259 assert_eq!(F16::from_bits(0x8ABC).neg().to_bits(), 0xABC);
260 assert_eq!(F16::from_bits(0xFE00).neg().to_bits(), 0x7E00);
261 assert_eq!(F16::from_bits(0x7E00).neg().to_bits(), 0xFE00);
266 not(feature = "f16"),
267 should_panic(expected = "f16 feature is not enabled")
269 fn test_int_to_f16() {
270 assert_eq!(F16::to_bits(0u32.to()), 0);
271 for v in 1..0x20000u32 {
272 let leading_zeros = u32::leading_zeros(v);
273 let shifted_v = v << leading_zeros;
274 // round to nearest, ties to even
275 let round_up = match (shifted_v & 0x1FFFFF).cmp(&0x100000) {
276 Ordering::Less => false,
277 Ordering::Equal => (shifted_v & 0x200000) != 0,
278 Ordering::Greater => true,
280 let (rounded, carry) =
281 (shifted_v & !0x1FFFFF).overflowing_add(round_up.then(|| 0x200000).unwrap_or(0));
284 mantissa = (rounded >> 22) as u16 + 0x400;
286 mantissa = (rounded >> 21) as u16;
288 assert_eq!((mantissa & !0x3FF), 0x400);
289 let exponent = 31 - leading_zeros as u16 + 15 + carry as u16;
290 let expected = if exponent < 0x1F {
291 (mantissa & 0x3FF) + (exponent << 10)
295 let actual = F16::to_bits(v.to());
298 "actual = {:#X}, expected = {:#X}, v = {:#X}",