Skip to main content

f16b

Struct f16b 

Source
pub struct f16b(/* private fields */);
🔬This is a nightly-only experimental API. (f16b #160630)
Expand description

A 16-bit brain floating-point value.

This type stores values using the bfloat16 encoding. It deliberately exposes only raw-bit construction, comparison, formatting, and lossless widening to f32.

The 16-bit brain floating-point intends to preserve the dynamic range of a 32-bit floating-point value while using half the storage. It does this by using 8 bits for the exponent, the same as f32, but only using 7 bits for the mantissa. See Wikipedia on bfloat16 for more information.

Implementations§

Source§

impl f16b

Source

pub const fn from_bits(bits: u16) -> Self

🔬This is a nightly-only experimental API. (f16b #160630)

Raw transmutation from u16.

This is currently identical to transmute::<u16, f16b>(v) on all platforms. It turns out this is incredibly portable, for two reasons:

  • Floats and Ints have the same endianness on all supported platforms.
  • IEEE 754 very precisely specifies the bit layout of floats.

However there is one caveat: prior to the 2008 version of IEEE 754, how to interpret the NaN signaling bit wasn’t actually specified. Most platforms (notably x86 and ARM) picked the interpretation that was ultimately standardized in 2008, but some didn’t (notably MIPS). As a result, all signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.

Rather than trying to preserve signaling-ness cross-platform, this implementation favors preserving the exact bits. This means that any payloads encoded in NaNs will be preserved even if the result of this method is sent over the network from an x86 machine to a MIPS one.

If the results of this method are only manipulated by the same architecture that produced them, then there is no portability concern.

If the input isn’t NaN, then there is no portability concern.

If you don’t care about signalingness (very likely), then there is no portability concern.

Note that this function is distinct from as casting, which attempts to preserve the numeric value, and not the bitwise value.

#![feature(f16b)]
use core::num::f16b;

let v = f16b::from_bits(0x4148);
assert_eq!(f32::from(v), 12.5);
Source

pub const fn to_bits(self) -> u16

🔬This is a nightly-only experimental API. (f16b #160630)

Raw transmutation to u16.

This is currently identical to transmute::<f16b, u16>(self) on all platforms.

See from_bits for some discussion of the portability of this operation (there are almost no issues).

Note that this function is distinct from as casting, which attempts to preserve the numeric value, and not the bitwise value.

#![feature(f16b)]
use core::num::f16b;

assert_eq!(f16b::from_bits(0x4148).to_bits(), 0x4148);

Trait Implementations§

Source§

impl Clone for f16b

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)
where Self:,

Performs copy-assignment from source. Read more
Source§

impl Copy for f16b

Source§

impl Debug for f16b

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for f16b

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<f16b> for f32

Source§

fn from(value: f16b) -> Self

Converts to this type from the input type.
Source§

impl LowerExp for f16b

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for f16b

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for f16b

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl UpperExp for f16b

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for f16b

§

impl RefUnwindSafe for f16b

§

impl Send for f16b

§

impl Sync for f16b

§

impl Unpin for f16b

§

impl UnsafeUnpin for f16b

§

impl UnwindSafe for f16b

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.