Struct f16b

#[repr(transparent)]
pub struct f16b(pub(in ::num::bfloat) u16);

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.

Fields

0: u16

Implementations

impl f16b

const fn from_bits(bits: u16) -> Self

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)]
# #[cfg(target_has_reliable_f16b)] {
use core::num::f16b;

let v = f16b::from_bits(0x4148);
assert_eq!(f32::from(v), 12.5);
# }
const fn to_bits(self) -> u16

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)]
# #[cfg(target_has_reliable_f16b)] {
use core::num::f16b;

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

Trait Implementations

impl Clone for f16b

fn clone(&self) -> Self

impl Copy for f16b

impl Debug for f16b

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

impl Default for f16b

fn default() -> Self

impl LowerExp for f16b

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

impl PartialEq for f16b

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

impl PartialOrd for f16b

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

impl UpperExp for f16b

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

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

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for f16b

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for f16b where T: Copy + Debug,

impl<T> SizeHint for f16b where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for f16b

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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