#f16 #bfloat16 #arithmetic-operations #no-std

no-std half

Half-precision floating point f16 and bf16 types for Rust implementing the IEEE 754-2008 standard binary16 and bfloat16 types

24 stable releases

2.4.1 Apr 6, 2024
2.4.0 Feb 26, 2024
2.3.1 Jun 26, 2023
2.2.1 Jan 9, 2023
0.1.0 Mar 18, 2016

#6 in Data structures

Download history 1351933/week @ 2024-08-22 1206984/week @ 2024-08-29 1357440/week @ 2024-09-05 1276511/week @ 2024-09-12 1247150/week @ 2024-09-19 1323002/week @ 2024-09-26 1370959/week @ 2024-10-03 1311252/week @ 2024-10-10 1396281/week @ 2024-10-17 1429628/week @ 2024-10-24 1318083/week @ 2024-10-31 1348647/week @ 2024-11-07 1434776/week @ 2024-11-14 1304780/week @ 2024-11-21 1108954/week @ 2024-11-28 1114395/week @ 2024-12-05

5,213,098 downloads per month
Used in 9,203 crates (313 directly)

MIT/Apache

255KB
5.5K SLoC

f16 and bf16 floating point types for Rust

Crates.io Documentation Crates.io Build status CircleCI

This crate implements a half-precision floating point f16 type for Rust implementing the IEEE 754-2008 standard binary16 a.k.a "half" format, as well as a bf16 type implementing the bfloat16 format.

Usage

The f16 and bf16 types attempt to match existing Rust floating point type functionality where possible, and provides both conversion operations (such as to/from f32 and f64) and basic arithmetic operations. Hardware support for these operations will be used whenever hardware support is available—either through instrinsics or targeted assembly—although a nightly Rust toolchain may be required for some hardware.

This crate provides no_std support so can easily be used in embedded code where a smaller float format is most useful.

Requires Rust 1.70 or greater. If you need support for older versions of Rust, use 1.x versions of this crate.

See the crate documentation for more details.

Optional Features

  • alloc — Enable use of the alloc crate when not using the std library.

    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, including everything in the alloc feature.

    Enabling the std feature enables runtime CPU feature detection of hardware support. Without this feature detection, harware is only used when compiler target supports them.

  • serde - Implement Serialize and Deserialize traits for f16 and bf16. This adds a dependency on the serde crate.

  • num-traits — Enable ToPrimitive, FromPrimitive, Num, Float, FloatCore and Bounded trait implementations from the num-traits crate.

  • bytemuck — Enable Zeroable and Pod trait implementations from the bytemuck crate.

  • zerocopy — Enable AsBytes and FromBytes trait implementations from the zerocopy crate.

  • rand_distr — Enable sampling from distributions like Uniform and Normal from the rand_distr crate.

  • rkyv -- Enable zero-copy deserializtion with rkyv crate.

Hardware support

The following list details hardware support for floating point types in this crate. When using std library, runtime CPU target detection will be used. To get the most performance benefits, compile for specific CPU features which avoids the runtime overhead and works in a no_std environment.

Architecture CPU Target Feature Notes
x86/x86_64 f16c This supports conversion to/from f16 only (including vector SIMD) and does not support any bf16 or arithmetic operations.
aarch64 fp16 This supports all operations on f16 only.

More Documentation

License

This library is distributed under the terms of either of:

at your option.

This project is REUSE-compliant. Copyrights are retained by their contributors. Some files may include explicit copyright notices and/or license SPDX identifiers. For full authorship information, see the version control history.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–0.9MB
~16K SLoC