1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
use std::{fmt, ops}; #[allow(non_camel_case_types)] #[derive(Copy, Clone, Default, PartialEq, PartialOrd, Debug)] #[cfg_attr(feature = "serialize", derive(Serialize))] pub struct f16(pub half::f16); macro_rules! binary_f16 { ($f:ident) => { fn $f(self, other:f16) -> f16 { (self.0).to_f32().$f((other.0).to_f32()).into() } } } macro_rules! unary_as_f32 { ($f:ident) => { fn $f(self) -> f16 { (self.0).to_f32().$f().into() } }; } macro_rules! unary_f16 { ($f:ident, $t:ty) => { fn $f(self) -> $t { (self.0).$f() } } } macro_rules! const_f16 { ($f:ident, $c:ident) => { fn $f() -> f16 { f16(half::consts::$c) } }; } #[allow(deprecated)] impl num_traits::Float for f16 { unary_as_f32!(floor); unary_as_f32!(ceil); unary_as_f32!(round); unary_as_f32!(trunc); unary_as_f32!(fract); unary_as_f32!(abs); unary_as_f32!(recip); unary_as_f32!(sqrt); unary_as_f32!(exp); unary_as_f32!(exp2); unary_as_f32!(ln); unary_as_f32!(log2); unary_as_f32!(log10); unary_as_f32!(cbrt); unary_as_f32!(sin); unary_as_f32!(cos); unary_as_f32!(tan); unary_as_f32!(sinh); unary_as_f32!(cosh); unary_as_f32!(tanh); unary_as_f32!(asin); unary_as_f32!(acos); unary_as_f32!(atan); unary_as_f32!(asinh); unary_as_f32!(acosh); unary_as_f32!(atanh); unary_as_f32!(exp_m1); unary_as_f32!(ln_1p); unary_f16!(classify, ::std::num::FpCategory); unary_f16!(is_nan, bool); unary_f16!(is_infinite, bool); unary_f16!(is_finite, bool); unary_f16!(is_normal, bool); unary_f16!(is_sign_positive, bool); unary_f16!(is_sign_negative, bool); binary_f16!(powf); binary_f16!(log); binary_f16!(max); binary_f16!(min); binary_f16!(abs_sub); binary_f16!(hypot); binary_f16!(atan2); const_f16!(nan, NAN); const_f16!(infinity, INFINITY); const_f16!(neg_infinity, NEG_INFINITY); const_f16!(neg_zero, NEG_ZERO); const_f16!(max_value, MAX); const_f16!(min_value, MIN); const_f16!(min_positive_value, MIN_POSITIVE); fn signum(self) -> f16 { f16(self.0.signum()) } fn mul_add(self, a: f16, b: f16) -> f16 { (self.0).to_f32().mul_add((a.0).to_f32(), (b.0).to_f32()).into() } fn powi(self, i: i32) -> f16 { (self.0).to_f32().powi(i).into() } fn sin_cos(self) -> (f16, f16) { let (s, c) = (self.0).to_f32().sin_cos(); (s.into(), c.into()) } fn integer_decode(self) -> (u64, i16, i8) { (self.0).to_f32().integer_decode() } } impl num_traits::Num for f16 { type FromStrRadixErr = <f32 as num_traits::Num>::FromStrRadixErr; fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> { f32::from_str_radix(str, radix).map(|it| it.into()) } } impl num_traits::Zero for f16 { fn is_zero(&self) -> bool { f32::from(self.0).is_zero() } fn zero() -> f16 { 0.0f32.into() } } impl num_traits::One for f16 { fn one() -> f16 { 1.0f32.into() } } impl num_traits::ToPrimitive for f16 { fn to_i64(&self) -> Option<i64> { f32::from(self.0).to_i64() } fn to_u64(&self) -> Option<u64> { f32::from(self.0).to_u64() } } impl num_traits::AsPrimitive<usize> for f16 { fn as_(self) -> usize { self.0.to_f32() as usize } } impl num_traits::AsPrimitive<f32> for f16 { fn as_(self) -> f32 { self.0.to_f32() } } impl num_traits::AsPrimitive<f16> for f32 { fn as_(self) -> f16 { f16(half::f16::from_f32(self)) } } impl num_traits::AsPrimitive<f64> for f16 { fn as_(self) -> f64 { self.0.to_f64() } } impl num_traits::AsPrimitive<f16> for f64 { fn as_(self) -> f16 { f16(half::f16::from_f64(self)) } } impl num_traits::NumCast for f16 { fn from<T: num_traits::ToPrimitive>(n: T) -> Option<Self> { n.to_f32().map(|f| f16(half::f16::from_f32(f))) } } impl num_traits::Bounded for f16 { fn min_value() -> f16 { f16(half::consts::MIN) } fn max_value() -> f16 { f16(half::consts::MAX) } } impl ops::Neg for f16 { type Output = f16; fn neg(self) -> f16 { self.0.to_f32().neg().into() } } impl From<f32> for f16 { fn from(f: f32) -> f16 { f16(half::f16::from_f32(f)) } } impl fmt::Display for f16 { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(fmt) } } impl num_traits::AsPrimitive<f16> for usize { fn as_(self) -> f16 { f16(half::f16::from_f64(self as f64)) } } impl ops::Add<f16> for f16 { type Output = f16; fn add(self, other: f16) -> f16 { (self.0.to_f32() + other.0.to_f32()).into() } } impl ops::Sub<f16> for f16 { type Output = f16; fn sub(self, other: f16) -> f16 { (self.0.to_f32() - other.0.to_f32()).into() } } impl ops::Mul<f16> for f16 { type Output = f16; fn mul(self, other: f16) -> f16 { (self.0.to_f32() * other.0.to_f32()).into() } } impl ops::Div<f16> for f16 { type Output = f16; fn div(self, other: f16) -> f16 { (self.0.to_f32() / other.0.to_f32()).into() } } impl ops::Rem<f16> for f16 { type Output = f16; fn rem(self, other: f16) -> f16 { (self.0.to_f32() % other.0.to_f32()).into() } } impl std::iter::Sum for f16 { fn sum<I>(iter: I) -> Self where I: Iterator<Item = f16>, { iter.fold(0.0f32, |acc, i| acc + i.0.to_f32()).into() } } impl<'a> std::iter::Sum<&'a f16> for f16 { fn sum<I>(iter: I) -> Self where I: Iterator<Item = &'a f16>, { iter.fold(0.0f32, |acc, i| acc + i.0.to_f32()).into() } } impl std::str::FromStr for f16 { type Err = std::num::ParseFloatError; fn from_str(s: &str) -> Result<f16, Self::Err> { s.parse::<f32>().map(|f| f.into()) } }