Expand description
A crate that provides support for half-precision 16-bit floating point types.
This crate provides the f16
type, which is an implementation of the IEEE 754-2008 standard
binary16
a.k.a half
floating point type. This 16-bit floating point type is intended for
efficient storage where the full range and precision of a larger floating point value is not
required. This is especially useful for image storage formats.
This crate also provides a bf16
type, an alternative 16-bit floating point format. The
bfloat16
format is a truncated IEEE 754 standard binary32
float that preserves the
exponent to allow the same range as f32
but with only 8 bits of precision (instead of 11
bits for f16
). See the bf16
type for details.
Because f16
and bf16
are primarily for efficient storage, floating point operations such
as addition, multiplication, etc. are not implemented by hardware. While this crate does provide
the appropriate trait implementations for basic operations, they each convert the value to
f32
before performing the operation and then back afterward. When performing complex
arithmetic, manually convert to and from f32
before and after to reduce repeated conversions
for each operation.
This crate also provides a slice
module for zero-copy in-place conversions of
u16
slices to both f16
and bf16
, as well as efficient vectorized conversions of
larger buffers of floating point values to and from these half formats.
The crate uses #[no_std]
by default, so can be used in embedded environments without using the
Rust std
library. A std
feature to enable support for the standard library is available,
see the Cargo Features section below.
A prelude
module is provided for easy importing of available utility traits.
Cargo Features
This crate supports a number of optional cargo features. None of these features are enabled by
default, even std
.
-
use-intrinsics
– Usecore::arch
hardware intrinsics forf16
andbf16
conversions if available on the compiler target. This feature currently only works on nightly Rust until the corresponding intrinsics are stabilized.When this feature is enabled and the hardware supports it, the functions and traits in the
slice
module will use vectorized SIMD intructions for increased efficiency.By default, without this feature, conversions are done only in software, which will also be the fallback if the target does not have hardware support. Note that without the
std
feature enabled, no runtime CPU feature detection is used, so the hardware support is only compiled if the compiler target supports the CPU feature. -
alloc
– Enable use of thealloc
crate when not using thestd
library.Among other functions, this enables the
vec
module, which contains zero-copy conversions for theVec
type. This allows fast conversion between rawVec<u16>
bits andVec<f16>
orVec<bf16>
arrays, and vice versa. -
std
– Enable features that depend on the Ruststd
library. This also enables thealloc
feature automatically.Enabling the
std
feature also enables runtime CPU feature detection when theuse-intrsincis
feature is also enabled. Without this feature detection, intrinsics are only used when compiler target supports the target feature. -
serde
– Adds support for theserde
crate by implementingSerialize
andDeserialize
traits for bothf16
andbf16
. -
num-traits
– Adds support for thenum-traits
crate by implementingToPrimitive
,FromPrimitive
,AsPrimitive
,Num
,Float
,FloatCore
, andBounded
traits for bothf16
andbf16
. -
bytemuck
– Adds support for thebytemuck
crate by implementingZeroable
andPod
traits for bothf16
andbf16
. -
zerocopy
– Adds support for thezerocopy
crate by implementingAsBytes
andFromBytes
traits for bothf16
andbf16
.
Modules
- A collection of the most used items and traits in this crate for easy importing.