pub struct f16b(/* private fields */);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
impl f16b
Sourcepub const fn from_bits(bits: u16) -> Self
๐ฌThis is a nightly-only experimental API. (f16b #160630)
pub const fn from_bits(bits: u16) -> Self
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.
Sourcepub const fn to_bits(self) -> u16
๐ฌThis is a nightly-only experimental API. (f16b #160630)
pub const fn to_bits(self) -> u16
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.