#![warn(missing_docs,
missing_copy_implementations,
missing_debug_implementations,
trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "use-intrinsics", feature(link_llvm_intrinsics))]
#[cfg(feature = "serialize")]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "std")]
extern crate core;
use core::num::{FpCategory, ParseFloatError};
use core::cmp::Ordering;
use core::str::FromStr;
use core::fmt::{Debug, Display, LowerExp, UpperExp, Formatter, Error};
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Default)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct f16(u16);
pub mod consts {
use super::f16;
pub const DIGITS: u32 = 3;
pub const EPSILON: f16 = f16(0x1700u16);
pub const INFINITY: f16 = f16(0x7C00u16);
pub const MANTISSA_DIGITS: u32 = 11;
pub const MAX: f16 = f16(0x7BFF);
pub const MAX_10_EXP: i32 = 9;
pub const MAX_EXP: i32 = 15;
pub const MIN: f16 = f16(0xFBFF);
pub const MIN_10_EXP: i32 = -9;
pub const MIN_EXP: i32 = -14;
pub const MIN_POSITIVE: f16 = f16(0x0400u16);
pub const NAN: f16 = f16(0xFE00u16);
pub const NEG_INFINITY: f16 = f16(0xFC00u16);
pub const RADIX: u32 = 2;
pub const MIN_POSITIVE_SUBNORMAL: f16 = f16(0x0001u16);
pub const MAX_SUBNORMAL: f16 = f16(0x03FFu16);
pub const ONE: f16 = f16(0x3C00u16);
pub const ZERO: f16 = f16(0x0000u16);
pub const NEG_ZERO: f16 = f16(0x8000u16);
pub const E: f16 = f16(0x4170u16);
pub const PI: f16 = f16(0x4248u16);
pub const FRAC_1_PI: f16 = f16(0x3518u16);
pub const FRAC_1_SQRT_2: f16 = f16(0x39A8u16);
pub const FRAC_2_PI: f16 = f16(0x3918u16);
pub const FRAC_2_SQRT_PI: f16 = f16(0x3C83u16);
pub const FRAC_PI_2: f16 = f16(0x3E48u16);
pub const FRAC_PI_3: f16 = f16(0x3C30u16);
pub const FRAC_PI_4: f16 = f16(0x3A48u16);
pub const FRAC_PI_6: f16 = f16(0x3830u16);
pub const FRAC_PI_8: f16 = f16(0x3648u16);
pub const LN_10: f16 = f16(0x409Bu16);
pub const LN_2: f16 = f16(0x398Cu16);
pub const LOG10_E: f16 = f16(0x36F3u16);
pub const LOG2_E: f16 = f16(0x3DC5u16);
pub const SQRT_2: f16 = f16(0x3DA8u16);
}
impl f16 {
#[inline]
pub fn from_bits(bits: u16) -> f16 {
f16(bits)
}
#[inline]
pub fn from_f32(value: f32) -> f16 {
f16(convert::f32_to_f16(value))
}
#[inline]
pub fn from_f64(value: f64) -> f16 {
f16(convert::f64_to_f16(value))
}
#[inline]
pub fn as_bits(self) -> u16 {
self.0
}
#[inline]
fn to_f32(self) -> f32 {
convert::f16_to_f32(self.0)
}
#[inline]
fn to_f64(self) -> f64 {
convert::f16_to_f64(self.0)
}
#[inline]
pub fn is_nan(self) -> bool {
(self.0 & 0x7C00u16 == 0x7C00u16) && (self.0 & 0x03FFu16 != 0)
}
#[inline]
pub fn is_infinite(self) -> bool {
(self.0 & 0x7C00u16 == 0x7C00u16) && (self.0 & 0x03FFu16 == 0)
}
#[inline]
pub fn is_finite(self) -> bool {
self.0 & 0x7C00u16 != 0x7C00u16
}
#[inline]
pub fn is_normal(self) -> bool {
let exp = self.0 & 0x7C00u16;
exp != 0x7C00u16 && exp != 0
}
pub fn classify(self) -> FpCategory {
let exp = self.0 & 0x7C00u16;
let man = self.0 & 0x03FFu16;
if exp == 0 {
if man == 0 {
FpCategory::Zero
} else {
FpCategory::Subnormal
}
} else if exp == 0x7C00u16 {
if man == 0 {
FpCategory::Infinite
} else {
FpCategory::Nan
}
} else {
FpCategory::Normal
}
}
pub fn signum(self) -> f16 {
if self.is_nan() {
self
} else if self.0 & 0x8000u16 != 0 {
f16::from_f32(-1.0)
} else {
f16::from_f32(1.0)
}
}
#[inline]
pub fn is_sign_positive(self) -> bool {
!self.is_nan() && self.0 & 0x8000u16 == 0
}
#[inline]
pub fn is_sign_negative(self) -> bool {
!self.is_nan() && self.0 & 0x8000u16 != 0
}
}
impl From<f16> for f32 {
fn from(x: f16) -> f32 {
x.to_f32()
}
}
impl From<f16> for f64 {
fn from(x: f16) -> f64 {
x.to_f64()
}
}
impl From<i8> for f16 {
fn from(x: i8) -> f16 {
f16::from_f32(f32::from(x))
}
}
impl From<u8> for f16 {
fn from(x: u8) -> f16 {
f16::from_f32(f32::from(x))
}
}
impl PartialEq for f16 {
fn eq(&self, other: &f16) -> bool {
!self.is_nan() && !other.is_nan() && self.0 == other.0
}
}
impl PartialOrd for f16 {
fn partial_cmp(&self, other: &f16) -> Option<Ordering> {
if self.is_nan() || other.is_nan() {
None
} else if self.0 == other.0 {
Some(Ordering::Equal)
} else if self.0 < other.0 {
Some(Ordering::Less)
} else {
Some(Ordering::Greater)
}
}
fn lt(&self, other: &f16) -> bool {
!self.is_nan() && !other.is_nan() && self.0 < other.0
}
fn le(&self, other: &f16) -> bool {
!self.is_nan() && !other.is_nan() && self.0 <= other.0
}
fn gt(&self, other: &f16) -> bool {
!self.is_nan() && !other.is_nan() && self.0 > other.0
}
fn ge(&self, other: &f16) -> bool {
!self.is_nan() && !other.is_nan() && self.0 >= other.0
}
}
impl FromStr for f16 {
type Err = ParseFloatError;
fn from_str(src: &str) -> Result<f16, ParseFloatError> {
f32::from_str(src).map(|x| f16::from_f32(x))
}
}
impl Debug for f16 {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "0x{:X}", self.0)
}
}
impl Display for f16 {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.to_f32())
}
}
impl LowerExp for f16 {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{:e}", self.to_f32())
}
}
impl UpperExp for f16 {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{:E}", self.to_f32())
}
}
#[cfg(feature = "use-intrinsics")]
mod convert {
extern "C" {
#[link_name = "llvm.convert.to.fp16.f32"]
fn convert_to_fp16_f32(f: f32) -> u16;
#[link_name = "llvm.convert.to.fp16.f64"]
fn convert_to_fp16_f64(f: f64) -> u16;
#[link_name = "llvm.convert.from.fp16.f32"]
fn convert_from_fp16_f32(i: u16) -> f32;
#[link_name = "llvm.convert.from.fp16.f64"]
fn convert_from_fp16_f64(i: u16) -> f64;
}
#[inline(always)]
pub fn f32_to_f16(f: f32) -> u16 {
unsafe { convert_to_fp16_f32(f) }
}
#[inline(always)]
pub fn f64_to_f16(f: f64) -> u16 {
unsafe { convert_to_fp16_f64(f) }
}
#[inline(always)]
pub fn f16_to_f32(i: u16) -> f32 {
unsafe { convert_from_fp16_f32(i) }
}
#[inline(always)]
pub fn f16_to_f64(i: u16) -> f64 {
unsafe { convert_from_fp16_f64(i) }
}
}
#[cfg(not(feature = "use-intrinsics"))]
mod convert {
use core;
use core::mem;
use super::*;
pub fn f32_to_f16(value: f32) -> u16 {
let x: u32 = unsafe { mem::transmute(value) };
if x & 0x7FFFFFFFu32 == 0 {
return (x >> 16) as u16;
}
let sign = x & 0x80000000u32;
let exp = x & 0x7F800000u32;
let man = x & 0x007FFFFFu32;
if exp == 0 {
return (sign >> 16) as u16;
}
if exp == 0x7F800000u32 {
if man == 0 {
return ((sign >> 16) | 0x7C00u32) as u16;
}
return consts::NAN.0;
}
let half_sign = sign >> 16;
let unbiased_exp = ((exp >> 23) as i32) - 127;
let half_exp = unbiased_exp + 15;
if half_exp >= 0x1F {
return (half_sign | 0x7C00u32) as u16;
}
if half_exp <= 0 {
if 14 - half_exp > 24 {
return half_sign as u16;
}
let man = man | 0x00800000u32;
let mut half_man = man >> (14 - half_exp);
if (man >> (13 - half_exp)) & 0x1u32 != 0 {
half_man += 1;
}
return (half_sign | half_man) as u16;
}
let half_exp = (half_exp as u32) << 10;
let half_man = man >> 13;
if man & 0x00001000u32 != 0 {
((half_sign | half_exp | half_man) + 1) as u16
} else {
(half_sign | half_exp | half_man) as u16
}
}
pub fn f64_to_f16(value: f64) -> u16 {
let val: u64 = unsafe { mem::transmute(value) };
let x = (val >> 32) as u32;
if x & 0x7FFFFFFFu32 == 0 {
return (x >> 16) as u16;
}
let sign = x & 0x80000000u32;
let exp = x & 0x7FF00000u32;
let man = x & 0x000FFFFFu32;
if exp == 0 {
return (sign >> 16) as u16;
}
if exp == 0x7FF00000u32 {
if man == 0 {
return ((sign >> 16) | 0x7C00u32) as u16;
}
return consts::NAN.0;
}
let half_sign = sign >> 16;
let unbiased_exp = ((exp >> 20) as i64) - 1023;
let half_exp = unbiased_exp + 15;
if half_exp >= 0x1F {
return (half_sign | 0x7C00u32) as u16;
}
if half_exp <= 0 {
if 10 - half_exp > 21 {
return half_sign as u16;
}
let man = man | 0x00100000u32;
let mut half_man = man >> (11 - half_exp);
if (man >> (10 - half_exp)) & 0x1u32 != 0 {
half_man += 1;
}
return (half_sign | half_man) as u16;
}
let half_exp = (half_exp as u32) << 10;
let half_man = man >> 10;
if man & 0x00000200u32 != 0 {
((half_sign | half_exp | half_man) + 1) as u16
} else {
(half_sign | half_exp | half_man) as u16
}
}
pub fn f16_to_f32(i: u16) -> f32 {
if i & 0x7FFFu16 == 0 {
return unsafe { mem::transmute((i as u32) << 16) };
}
let half_sign = (i & 0x8000u16) as u32;
let half_exp = (i & 0x7C00u16) as u32;
let half_man = (i & 0x03FFu16) as u32;
if half_exp == 0x7C00u32 {
if half_man == 0 {
return unsafe { mem::transmute((half_sign << 16) | 0x7F800000u32) };
} else {
return core::f32::NAN;
}
}
let sign = half_sign << 16;
let unbiased_exp = ((half_exp as i32) >> 10) - 15;
let man = (half_man & 0x03FFu32) << 13;
if half_exp == 0 {
let e = {
let mut e_adj = 0;
let mut hm_adj = half_man << 1;
while hm_adj & 0x0400u32 == 0 {
e_adj += 1;
hm_adj <<= 1;
}
e_adj
};
let exp = ((unbiased_exp + 127 - e) << 23) as u32;
return unsafe { mem::transmute(sign | exp | man) };
}
let exp = ((unbiased_exp + 127) << 23) as u32;
unsafe { mem::transmute(sign | exp | man) }
}
pub fn f16_to_f64(i: u16) -> f64 {
if i & 0x7FFFu16 == 0 {
return unsafe { mem::transmute((i as u64) << 48) };
}
let half_sign = (i & 0x8000u16) as u64;
let half_exp = (i & 0x7C00u16) as u64;
let half_man = (i & 0x03FFu16) as u64;
if half_exp == 0x7C00u64 {
if half_man == 0 {
return unsafe { mem::transmute((half_sign << 48) | 0x7FF0000000000000u64) };
} else {
return core::f64::NAN;
}
}
let sign = half_sign << 48;
let unbiased_exp = ((half_exp as i64) >> 10) - 15;
let man = (half_man & 0x03FFu64) << 42;
if half_exp == 0 {
let e = {
let mut e_adj = 0;
let mut hm_adj = half_man << 1;
while hm_adj & 0x0400u64 == 0 {
e_adj += 1;
hm_adj <<= 1;
}
e_adj
};
let exp = ((unbiased_exp + 1023 - e) << 52) as u64;
return unsafe { mem::transmute(sign | exp | man) };
}
let exp = ((unbiased_exp + 1023) << 52) as u64;
unsafe { mem::transmute(sign | exp | man) }
}
}
#[cfg(test)]
mod test {
use core;
use super::*;
#[test]
fn test_f16_consts_from_f32() {
let one = f16::from_f32(1.0);
let zero = f16::from_f32(0.0);
let neg_zero = f16::from_f32(-0.0);
let inf = f16::from_f32(core::f32::INFINITY);
let neg_inf = f16::from_f32(core::f32::NEG_INFINITY);
let nan = f16::from_f32(core::f32::NAN);
assert_eq!(consts::ONE, one);
assert_eq!(consts::ZERO, zero);
assert_eq!(consts::NEG_ZERO, neg_zero);
assert_eq!(consts::INFINITY, inf);
assert_eq!(consts::NEG_INFINITY, neg_inf);
assert!(nan.is_nan());
assert!(consts::NAN.is_nan());
let e = f16::from_f32(core::f32::consts::E);
let pi = f16::from_f32(core::f32::consts::PI);
let frac_1_pi = f16::from_f32(core::f32::consts::FRAC_1_PI);
let frac_1_sqrt_2 = f16::from_f32(core::f32::consts::FRAC_1_SQRT_2);
let frac_2_pi = f16::from_f32(core::f32::consts::FRAC_2_PI);
let frac_2_sqrt_pi = f16::from_f32(core::f32::consts::FRAC_2_SQRT_PI);
let frac_pi_2 = f16::from_f32(core::f32::consts::FRAC_PI_2);
let frac_pi_3 = f16::from_f32(core::f32::consts::FRAC_PI_3);
let frac_pi_4 = f16::from_f32(core::f32::consts::FRAC_PI_4);
let frac_pi_6 = f16::from_f32(core::f32::consts::FRAC_PI_6);
let frac_pi_8 = f16::from_f32(core::f32::consts::FRAC_PI_8);
let ln_10 = f16::from_f32(core::f32::consts::LN_10);
let ln_2 = f16::from_f32(core::f32::consts::LN_2);
let log10_e = f16::from_f32(core::f32::consts::LOG10_E);
let log2_e = f16::from_f32(core::f32::consts::LOG2_E);
let sqrt_2 = f16::from_f32(core::f32::consts::SQRT_2);
assert_eq!(consts::E, e);
assert_eq!(consts::PI, pi);
assert_eq!(consts::FRAC_1_PI, frac_1_pi);
assert_eq!(consts::FRAC_1_SQRT_2, frac_1_sqrt_2);
assert_eq!(consts::FRAC_2_PI, frac_2_pi);
assert_eq!(consts::FRAC_2_SQRT_PI, frac_2_sqrt_pi);
assert_eq!(consts::FRAC_PI_2, frac_pi_2);
assert_eq!(consts::FRAC_PI_3, frac_pi_3);
assert_eq!(consts::FRAC_PI_4, frac_pi_4);
assert_eq!(consts::FRAC_PI_6, frac_pi_6);
assert_eq!(consts::FRAC_PI_8, frac_pi_8);
assert_eq!(consts::LN_10, ln_10);
assert_eq!(consts::LN_2, ln_2);
assert_eq!(consts::LOG10_E, log10_e);
assert_eq!(consts::LOG2_E, log2_e);
assert_eq!(consts::SQRT_2, sqrt_2);
}
#[test]
fn test_f16_consts_from_f64() {
let one = f16::from_f64(1.0);
let zero = f16::from_f64(0.0);
let neg_zero = f16::from_f64(-0.0);
let inf = f16::from_f64(core::f64::INFINITY);
let neg_inf = f16::from_f64(core::f64::NEG_INFINITY);
let nan = f16::from_f64(core::f64::NAN);
assert_eq!(consts::ONE, one);
assert_eq!(consts::ZERO, zero);
assert_eq!(consts::NEG_ZERO, neg_zero);
assert_eq!(consts::INFINITY, inf);
assert_eq!(consts::NEG_INFINITY, neg_inf);
assert!(nan.is_nan());
assert!(consts::NAN.is_nan());
let e = f16::from_f64(core::f64::consts::E);
let pi = f16::from_f64(core::f64::consts::PI);
let frac_1_pi = f16::from_f64(core::f64::consts::FRAC_1_PI);
let frac_1_sqrt_2 = f16::from_f64(core::f64::consts::FRAC_1_SQRT_2);
let frac_2_pi = f16::from_f64(core::f64::consts::FRAC_2_PI);
let frac_2_sqrt_pi = f16::from_f64(core::f64::consts::FRAC_2_SQRT_PI);
let frac_pi_2 = f16::from_f64(core::f64::consts::FRAC_PI_2);
let frac_pi_3 = f16::from_f64(core::f64::consts::FRAC_PI_3);
let frac_pi_4 = f16::from_f64(core::f64::consts::FRAC_PI_4);
let frac_pi_6 = f16::from_f64(core::f64::consts::FRAC_PI_6);
let frac_pi_8 = f16::from_f64(core::f64::consts::FRAC_PI_8);
let ln_10 = f16::from_f64(core::f64::consts::LN_10);
let ln_2 = f16::from_f64(core::f64::consts::LN_2);
let log10_e = f16::from_f64(core::f64::consts::LOG10_E);
let log2_e = f16::from_f64(core::f64::consts::LOG2_E);
let sqrt_2 = f16::from_f64(core::f64::consts::SQRT_2);
assert_eq!(consts::E, e);
assert_eq!(consts::PI, pi);
assert_eq!(consts::FRAC_1_PI, frac_1_pi);
assert_eq!(consts::FRAC_1_SQRT_2, frac_1_sqrt_2);
assert_eq!(consts::FRAC_2_PI, frac_2_pi);
assert_eq!(consts::FRAC_2_SQRT_PI, frac_2_sqrt_pi);
assert_eq!(consts::FRAC_PI_2, frac_pi_2);
assert_eq!(consts::FRAC_PI_3, frac_pi_3);
assert_eq!(consts::FRAC_PI_4, frac_pi_4);
assert_eq!(consts::FRAC_PI_6, frac_pi_6);
assert_eq!(consts::FRAC_PI_8, frac_pi_8);
assert_eq!(consts::LN_10, ln_10);
assert_eq!(consts::LN_2, ln_2);
assert_eq!(consts::LOG10_E, log10_e);
assert_eq!(consts::LOG2_E, log2_e);
assert_eq!(consts::SQRT_2, sqrt_2);
}
#[test]
fn test_f16_to_f32() {
let f = f16::from_f32(7.0);
assert_eq!(f.to_f32(), 7.0f32);
let f = f16::from_f32(7.1);
let diff = (f.to_f32() - 7.1f32).abs();
assert!(diff <= consts::EPSILON.to_f32());
}
#[test]
fn test_f16_to_f64() {
let f = f16::from_f64(7.0);
assert_eq!(f.to_f64(), 7.0f64);
let f = f16::from_f64(7.1);
let diff = (f.to_f64() - 7.1f64).abs();
assert!(diff <= consts::EPSILON.to_f64());
}
}