Name Description Size
bfloat
bfloat.rs 49264
binary16
binary16.rs 56062
lib.rs 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`][mod@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](#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`** -- Use [`core::arch`] hardware intrinsics for `f16` and `bf16` 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`][mod@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 the [`alloc`] crate when not using the `std` library. Among other functions, this enables the [`vec`] module, which contains zero-copy conversions for the [`Vec`] type. This allows fast conversion between raw `Vec<u16>` bits and `Vec<f16>` or `Vec<bf16>` arrays, and vice versa. - **`std`** -- Enable features that depend on the Rust [`std`] library. This also enables the `alloc` feature automatically. Enabling the `std` feature also enables runtime CPU feature detection when the `use-intrsincis` feature is also enabled. Without this feature detection, intrinsics are only used when compiler target supports the target feature. - **`serde`** -- Adds support for the [`serde`] crate by implementing [`Serialize`] and [`Deserialize`] traits for both [`f16`] and [`bf16`]. - **`num-traits`** -- Adds support for the [`num-traits`] crate by implementing [`ToPrimitive`], [`FromPrimitive`], [`AsPrimitive`], [`Num`], [`Float`], [`FloatCore`], and [`Bounded`] traits for both [`f16`] and [`bf16`]. - **`bytemuck`** -- Adds support for the [`bytemuck`] crate by implementing [`Zeroable`] and [`Pod`] traits for both [`f16`] and [`bf16`]. - **`zerocopy`** -- Adds support for the [`zerocopy`] crate by implementing [`AsBytes`] and [`FromBytes`] traits for both [`f16`] and [`bf16`]. [`alloc`]: https://doc.rust-lang.org/alloc/ [`std`]: https://doc.rust-lang.org/std/ [`binary16`]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format [`bfloat16`]: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format [`serde`]: https://crates.io/crates/serde [`bytemuck`]: https://crates.io/crates/bytemuck [`num-traits`]: https://crates.io/crates/num-traits [`zerocopy`]: https://crates.io/crates/zerocopy 8431
num_traits.rs 28568
slice.rs Contains utility functions and traits to convert between slices of [`u16`] bits and [`f16`] or [`bf16`] numbers. The utility [`HalfBitsSliceExt`] sealed extension trait is implemented for `[u16]` slices, while the utility [`HalfFloatSliceExt`] sealed extension trait is implemented for both `[f16]` and `[bf16]` slices. These traits provide efficient conversions and reinterpret casting of larger buffers of floating point values, and are automatically included in the [`prelude`][crate::prelude] module. 32300
vec.rs Contains utility functions and traits to convert between vectors of [`u16`] bits and [`f16`] or [`bf16`] vectors. The utility [`HalfBitsVecExt`] sealed extension trait is implemented for [`Vec<u16>`] vectors, while the utility [`HalfFloatVecExt`] sealed extension trait is implemented for both [`Vec<f16>`] and [`Vec<bf16>`] vectors. These traits provide efficient conversions and reinterpret casting of larger buffers of floating point values, and are automatically included in the [`prelude`][crate::prelude] module. This module is only available with the `std` or `alloc` feature. 10866