Crate half

source ·
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. Operations should be performed with f32 or higher-precision types and converted to/from f16 or bf16 as necessary.

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.

A prelude module is provided for easy importing of available utility traits.

Some hardware architectures provide support for 16-bit floating point conversions. Enable the use-intrinsics feature to use LLVM intrinsics for hardware conversions. This crate does no checks on whether the hardware supports the feature. This feature currently only works on nightly Rust due to a compiler feature gate. When this feature is enabled and the hardware supports it, the slice trait conversions will use vectorized SIMD intructions for increased efficiency.

Support for serde crate Serialize and Deserialize traits is provided when the serde feature is enabled. This adds a dependency on serde crate so is an optional cargo feature.

The crate uses #[no_std] by default, so can be used in embedded environments without using the Rust std library. A std feature is available, which enables additional utilities using the std library, such as the vec module that provides zero-copy Vec conversions. The alloc feature may be used to enable the vec module without adding a dependency to the std library.


Useful f16 constants.
A collection of the most used items and traits in this crate for easy importing.
Contains utility functions and traits to convert between slices of u16 bits and f16 or bf16 numbers.


A 16-bit floating point type implementing the bfloat16 format.
A 16-bit floating point type implementing the IEEE 754-2008 standard binary16 a.k.a half format.