#![warn(
missing_docs, missing_copy_implementations, missing_debug_implementations, trivial_casts,
trivial_numeric_casts, unused_extern_crates, unused_import_braces
)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "use-intrinsics", feature(link_llvm_intrinsics))]
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
#[cfg(feature = "std")]
extern crate core;
use core::cmp::Ordering;
use core::fmt::{Debug, Display, Error, Formatter, LowerExp, UpperExp};
use core::num::{FpCategory, ParseFloatError};
use core::str::FromStr;
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Default)]
#[cfg_attr(feature = "serde", 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 to_bits(self) -> u16 {
self.0
}
#[deprecated(since = "1.2.0", note = "renamed to to_bits")]
#[inline]
pub fn as_bits(self) -> u16 {
self.to_bits()
}
#[inline]
pub fn to_f32(self) -> f32 {
convert::f16_to_f32(self.0)
}
#[inline]
pub fn to_f64(self) -> f64 {
convert::f16_to_f64(self.0)
}
#[inline]
pub fn is_nan(self) -> bool {
self.0 & 0x7FFFu16 > 0x7C00u16
}
#[inline]
pub fn is_infinite(self) -> bool {
self.0 & 0x7FFFu16 == 0x7C00u16
}
#[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.0 & 0x8000u16 == 0
}
#[inline]
pub fn is_sign_negative(self) -> bool {
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 {
if self.is_nan() || other.is_nan() {
false
} else {
(self.0 == other.0) || ((self.0 | other.0) & 0x7FFFu16 == 0)
}
}
}
impl PartialOrd for f16 {
fn partial_cmp(&self, other: &f16) -> Option<Ordering> {
if self.is_nan() || other.is_nan() {
None
} else {
let neg = self.0 & 0x8000u16 != 0;
let other_neg = other.0 & 0x8000u16 != 0;
match (neg, other_neg) {
(false, false) => Some(self.0.cmp(&other.0)),
(false, true) => {
if (self.0 | other.0) & 0x7FFFu16 == 0 {
Some(Ordering::Equal)
} else {
Some(Ordering::Greater)
}
}
(true, false) => {
if (self.0 | other.0) & 0x7FFFu16 == 0 {
Some(Ordering::Equal)
} else {
Some(Ordering::Less)
}
}
(true, true) => Some(other.0.cmp(&self.0)),
}
}
}
fn lt(&self, other: &f16) -> bool {
if self.is_nan() || other.is_nan() {
false
} else {
let neg = self.0 & 0x8000u16 != 0;
let other_neg = other.0 & 0x8000u16 != 0;
match (neg, other_neg) {
(false, false) => self.0 < other.0,
(false, true) => false,
(true, false) => (self.0 | other.0) & 0x7FFFu16 != 0,
(true, true) => self.0 > other.0,
}
}
}
fn le(&self, other: &f16) -> bool {
if self.is_nan() || other.is_nan() {
false
} else {
let neg = self.0 & 0x8000u16 != 0;
let other_neg = other.0 & 0x8000u16 != 0;
match (neg, other_neg) {
(false, false) => self.0 <= other.0,
(false, true) => (self.0 | other.0) & 0x7FFFu16 == 0,
(true, false) => true,
(true, true) => self.0 >= other.0,
}
}
}
fn gt(&self, other: &f16) -> bool {
if self.is_nan() || other.is_nan() {
false
} else {
let neg = self.0 & 0x8000u16 != 0;
let other_neg = other.0 & 0x8000u16 != 0;
match (neg, other_neg) {
(false, false) => self.0 > other.0,
(false, true) => (self.0 | other.0) & 0x7FFFu16 != 0,
(true, false) => false,
(true, true) => self.0 < other.0,
}
}
}
fn ge(&self, other: &f16) -> bool {
if self.is_nan() || other.is_nan() {
false
} else {
let neg = self.0 & 0x8000u16 != 0;
let other_neg = other.0 & 0x8000u16 != 0;
match (neg, other_neg) {
(false, false) => self.0 >= other.0,
(false, true) => true,
(true, false) => (self.0 | other.0) & 0x7FFFu16 == 0,
(true, true) => 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;
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 ((sign >> 16) | 0x7E00u32) as u16;
}
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) && (val as u32 == 0) {
return ((sign >> 16) | 0x7C00u32) as u16;
}
return ((sign >> 16) | 0x7E00u32) as u16;
}
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;
if half_exp == 0 {
let e = (half_man as u16).leading_zeros() - 6;
let exp = (127 - 15 - e) << 23;
let man = (half_man << (14 + e)) & 0x7F_FF_FFu32;
return unsafe { mem::transmute(sign | exp | man) };
}
let exp = ((unbiased_exp + 127) as u32) << 23;
let man = (half_man & 0x03FFu32) << 13;
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;
if half_exp == 0 {
let e = (half_man as u16).leading_zeros() - 6;
let exp = ((1023 - 15 - e) as u64) << 52;
let man = (half_man << (43 + e)) & 0xF_FFFF_FFFF_FFFFu64;
return unsafe { mem::transmute(sign | exp | man) };
}
let exp = ((unbiased_exp + 1023) as u64) << 52;
let man = (half_man & 0x03FFu64) << 42;
unsafe { mem::transmute(sign | exp | man) }
}
}
pub mod slice {
use super::f16;
use core::slice;
#[inline]
pub fn from_bits_mut<'s>(bits: &'s mut [u16]) -> &'s mut [f16] {
let pointer = bits.as_ptr() as *mut f16;
let length = bits.len();
unsafe { slice::from_raw_parts_mut(pointer, length) }
}
#[inline]
pub fn to_bits_mut<'s>(bits: &'s mut [f16]) -> &'s mut [u16] {
let pointer = bits.as_ptr() as *mut u16;
let length = bits.len();
unsafe { slice::from_raw_parts_mut(pointer, length) }
}
#[inline]
pub fn from_bits<'s>(bits: &'s [u16]) -> &'s [f16] {
let pointer = bits.as_ptr() as *const f16;
let length = bits.len();
unsafe { slice::from_raw_parts(pointer, length) }
}
#[inline]
pub fn to_bits<'s>(bits: &'s [f16]) -> &'s [u16] {
let pointer = bits.as_ptr() as *const u16;
let length = bits.len();
unsafe { slice::from_raw_parts(pointer, length) }
}
}
#[cfg(feature = "std")]
pub mod vec {
use super::f16;
use core::mem;
#[inline]
pub fn from_bits(bits: Vec<u16>) -> Vec<f16> {
let mut bits = bits;
let length = bits.len();
let capacity = bits.capacity();
let pointer = bits.as_mut_ptr() as *mut f16;
mem::forget(bits);
unsafe { Vec::from_raw_parts(pointer, length, capacity) }
}
#[inline]
pub fn to_bits(numbers: Vec<f16>) -> Vec<u16> {
let mut numbers = numbers;
let length = numbers.len();
let capacity = numbers.capacity();
let pointer = numbers.as_mut_ptr() as *mut u16;
mem::forget(numbers);
unsafe { Vec::from_raw_parts(pointer, length, capacity) }
}
}
#[cfg(test)]
mod test {
use super::*;
use core;
use core::cmp::Ordering;
#[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_nan_conversion() {
use core::mem;
let nan64: f64;
let neg_nan64: f64;
let nan32: f32;
let neg_nan32: f32;
unsafe {
nan64 = mem::transmute(0x7ff0_0000_0000_0001u64);
neg_nan64 = mem::transmute(0xfff0_0000_0000_0001u64);
nan32 = mem::transmute(0x7f80_0001u32);
neg_nan32 = mem::transmute(0xff80_0001u32);
}
let nan32_from_64 = nan64 as f32;
let neg_nan32_from_64 = neg_nan64 as f32;
let nan16_from_64 = f16::from_f64(nan64);
let neg_nan16_from_64 = f16::from_f64(neg_nan64);
let nan16_from_32 = f16::from_f32(nan32);
let neg_nan16_from_32 = f16::from_f32(neg_nan32);
assert!(nan64.is_nan());
assert!(neg_nan64.is_nan());
assert!(nan32.is_nan());
assert!(neg_nan32.is_nan());
assert!(nan32_from_64.is_nan());
assert!(neg_nan32_from_64.is_nan());
assert!(nan16_from_64.is_nan());
assert!(neg_nan16_from_64.is_nan());
assert!(nan16_from_32.is_nan());
assert!(neg_nan16_from_32.is_nan());
let sign64 = 1u64 << 63;
let sign32 = 1u32 << 31;
let sign16 = 1u16 << 15;
let nan64_u: u64;
let neg_nan64_u: u64;
let nan32_u: u32;
let neg_nan32_u: u32;
let nan32_from_64_u: u32;
let neg_nan32_from_64_u: u32;
let nan16_from_64_u: u16;
let neg_nan16_from_64_u: u16;
let nan16_from_32_u: u16;
let neg_nan16_from_32_u: u16;
unsafe {
nan64_u = mem::transmute(nan64);
neg_nan64_u = mem::transmute(neg_nan64);
nan32_u = mem::transmute(nan32);
neg_nan32_u = mem::transmute(neg_nan32);
nan32_from_64_u = mem::transmute(nan32_from_64);
neg_nan32_from_64_u = mem::transmute(neg_nan32_from_64);
nan16_from_64_u = mem::transmute(nan16_from_64);
neg_nan16_from_64_u = mem::transmute(neg_nan16_from_64);
nan16_from_32_u = mem::transmute(nan16_from_32);
neg_nan16_from_32_u = mem::transmute(neg_nan16_from_32);
}
assert_eq!(nan64_u & sign64, 0);
assert_eq!(neg_nan64_u & sign64, sign64);
assert_eq!(nan32_u & sign32, 0);
assert_eq!(neg_nan32_u & sign32, sign32);
assert_eq!(nan32_from_64_u & sign32, 0);
assert_eq!(neg_nan32_from_64_u & sign32, sign32);
assert_eq!(nan16_from_64_u & sign16, 0);
assert_eq!(neg_nan16_from_64_u & sign16, sign16);
assert_eq!(nan16_from_32_u & sign16, 0);
assert_eq!(neg_nan16_from_32_u & sign16, sign16);
}
#[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());
assert_eq!(f16::from_bits(0x0000_0001).to_f32(), 2.0f32.powi(-24));
assert_eq!(f16::from_bits(0x0000_0005).to_f32(), 5.0 * 2.0f32.powi(-24));
assert_eq!(f16::from_bits(0x0000_0001), f16::from_f32(2.0f32.powi(-24)));
assert_eq!(
f16::from_bits(0x0000_0005),
f16::from_f32(5.0 * 2.0f32.powi(-24))
);
}
#[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());
assert_eq!(f16::from_bits(0x0000_0001).to_f64(), 2.0f64.powi(-24));
assert_eq!(f16::from_bits(0x0000_0005).to_f64(), 5.0 * 2.0f64.powi(-24));
assert_eq!(f16::from_bits(0x0000_0001), f16::from_f64(2.0f64.powi(-24)));
assert_eq!(
f16::from_bits(0x0000_0005),
f16::from_f64(5.0 * 2.0f64.powi(-24))
);
}
#[test]
fn test_comparisons() {
let zero = f16::from_f64(0.0);
let one = f16::from_f64(1.0);
let neg_zero = f16::from_f64(-0.0);
let neg_one = f16::from_f64(-1.0);
assert_eq!(zero.partial_cmp(&neg_zero), Some(Ordering::Equal));
assert_eq!(neg_zero.partial_cmp(&zero), Some(Ordering::Equal));
assert!(zero == neg_zero);
assert!(neg_zero == zero);
assert!(!(zero != neg_zero));
assert!(!(neg_zero != zero));
assert!(!(zero < neg_zero));
assert!(!(neg_zero < zero));
assert!(zero <= neg_zero);
assert!(neg_zero <= zero);
assert!(!(zero > neg_zero));
assert!(!(neg_zero > zero));
assert!(zero >= neg_zero);
assert!(neg_zero >= zero);
assert_eq!(one.partial_cmp(&neg_zero), Some(Ordering::Greater));
assert_eq!(neg_zero.partial_cmp(&one), Some(Ordering::Less));
assert!(!(one == neg_zero));
assert!(!(neg_zero == one));
assert!(one != neg_zero);
assert!(neg_zero != one);
assert!(!(one < neg_zero));
assert!(neg_zero < one);
assert!(!(one <= neg_zero));
assert!(neg_zero <= one);
assert!(one > neg_zero);
assert!(!(neg_zero > one));
assert!(one >= neg_zero);
assert!(!(neg_zero >= one));
assert_eq!(one.partial_cmp(&neg_one), Some(Ordering::Greater));
assert_eq!(neg_one.partial_cmp(&one), Some(Ordering::Less));
assert!(!(one == neg_one));
assert!(!(neg_one == one));
assert!(one != neg_one);
assert!(neg_one != one);
assert!(!(one < neg_one));
assert!(neg_one < one);
assert!(!(one <= neg_one));
assert!(neg_one <= one);
assert!(one > neg_one);
assert!(!(neg_one > one));
assert!(one >= neg_one);
assert!(!(neg_one >= one));
}
#[test]
fn test_slice_conversions() {
use consts::*;
let bits = &[
E.to_bits(),
PI.to_bits(),
EPSILON.to_bits(),
FRAC_1_SQRT_2.to_bits(),
];
let numbers = &[E, PI, EPSILON, FRAC_1_SQRT_2];
let from_bits = slice::from_bits(bits);
assert_slice_contents_eq(from_bits, numbers);
let to_bits = slice::to_bits(from_bits);
assert_slice_contents_eq(to_bits, bits);
}
#[test]
#[cfg(feature = "std")]
fn test_vec_conversions() {
use consts::*;
let numbers = vec![E, PI, EPSILON, FRAC_1_SQRT_2];
let bits = vec![
E.to_bits(),
PI.to_bits(),
EPSILON.to_bits(),
FRAC_1_SQRT_2.to_bits(),
];
let bits_cloned = bits.clone();
let from_bits = vec::from_bits(bits);
assert_slice_contents_eq(&from_bits, &numbers);
let to_bits = vec::to_bits(from_bits);
assert_slice_contents_eq(&to_bits, &bits_cloned);
}
fn assert_slice_contents_eq<T: PartialEq + core::fmt::Debug>(a: &[T], b: &[T]) {
assert_eq!(a, b);
for (a, b) in a.iter().zip(b.iter()) {
assert_eq!(a, b);
}
}
#[test]
fn test_mutablility(){
use consts::*;
let mut bits_array = [ PI.to_bits() ];
let bits = &mut bits_array[..];
{
let numbers = slice::from_bits_mut(bits);
numbers[0] = E;
}
assert_eq!(bits, &[ E.to_bits() ]);
bits[0] = LN_2.to_bits();
assert_eq!(bits, &[ LN_2.to_bits() ]);
}
}