half/
lib.rs

1//! A crate that provides support for half-precision 16-bit floating point types.
2//!
3//! This crate provides the [`struct@f16`] type, which is an implementation of the IEEE 754-2008 standard
4//! [`binary16`] a.k.a "half" floating point type. This 16-bit floating point type is intended for
5//! efficient storage where the full range and precision of a larger floating point value is not
6//! required. This is especially useful for image storage formats.
7//!
8//! This crate also provides a [`struct@bf16`] type, an alternative 16-bit floating point format. The
9//! [`bfloat16`] format is a truncated IEEE 754 standard `binary32` float that preserves the
10//! exponent to allow the same range as [`f32`] but with only 8 bits of precision (instead of 11
11//! bits for [`struct@f16`]). See the [`struct@bf16`] type for details.
12//!
13//! Because [`struct@f16`] and [`struct@bf16`] are primarily for efficient storage, floating point operations such
14//! as addition, multiplication, etc. are not always implemented by hardware. When hardware does not
15//! support these operations, this crate emulates them by converting the value to
16//! [`f32`] before performing the operation and then back afterward.
17//!
18//! Note that conversion from [`f32`]/[`f64`] to both [`struct@f16`] and [`struct@bf16`] are lossy operations, and
19//! just as converting a [`f64`] to [`f32`] is lossy and does not have `Into`/`From` trait
20//! implementations, so too do these smaller types not have those trait implementations either.
21//! Instead, use `from_f32`/`from_f64` functions for the types in this crate. If you don't care
22//! about lossy conversions and need trait conversions, use the appropriate [`num-traits`]
23//! traits that are implemented.
24//!
25//! This crate also provides a [`slice`][mod@slice] module for zero-copy in-place conversions of
26//! [`u16`] slices to both [`struct@f16`] and [`struct@bf16`], as well as efficient vectorized conversions of
27//! larger buffers of floating point values to and from these half formats.
28//!
29//! The crate supports `#[no_std]` when the `std` cargo feature is not enabled, so can be used in
30//! embedded environments without using the Rust [`std`] library. The `std` feature enables support
31//! for the standard library and is enabled by default, see the [Cargo Features](#cargo-features)
32//! section below.
33//!
34//! A [`prelude`] module is provided for easy importing of available utility traits.
35//!
36//! # Serialization
37//!
38//! When the `serde` feature is enabled, [`struct@f16`] and [`struct@bf16`] will be serialized as a newtype of
39//! [`u16`] by default. In binary formats this is ideal, as it will generally use just two bytes for
40//! storage. For string formats like JSON, however, this isn't as useful, and due to design
41//! limitations of serde, it's not possible for the default `Serialize` implementation to support
42//! different serialization for different formats.
43//!
44//! Instead, it's up to the containter type of the floats to control how it is serialized. This can
45//! easily be controlled when using the derive macros using `#[serde(serialize_with="")]`
46//! attributes. For both [`struct@f16`] and [`struct@bf16`] a `serialize_as_f32` and `serialize_as_string` are
47//! provided for use with this attribute.
48//!
49//! Deserialization of both float types supports deserializing from the default serialization,
50//! strings, and `f32`/`f64` values, so no additional work is required.
51//!
52//! # Hardware support
53//!
54//! Hardware support for these conversions and arithmetic will be used
55//! whenever hardware support is available—either through instrinsics or targeted assembly—although
56//! a nightly Rust toolchain may be required for some hardware. When hardware supports it the
57//! functions and traits in the [`slice`][mod@slice] and [`vec`] modules will also use vectorized
58//! SIMD intructions for increased efficiency.
59//!
60//! The following list details hardware support for floating point types in this crate. When using
61//! `std` cargo feature, runtime CPU target detection will be used. To get the most performance
62//! benefits, compile for specific CPU features which avoids the runtime overhead and works in a
63//! `no_std` environment.
64//!
65//! | Architecture | CPU Target Feature | Notes |
66//! | ------------ | ------------------ | ----- |
67//! | `x86`/`x86_64` | `f16c` | This supports conversion to/from [`struct@f16`] only (including vector SIMD) and does not support any [`struct@bf16`] or arithmetic operations. |
68//! | `aarch64` | `fp16` | This supports all operations on [`struct@f16`] only. |
69//! | `loongarch64` | `lsx` | This supports conversion to/from [`struct@f16`] only (including vector SIMD) and does not support any [`struct@bf16`] or arithmetic operations. |
70//!
71//! # Cargo Features
72//!
73//! This crate supports a number of optional cargo features. None of these features are enabled by
74//! default, even `std`.
75//!
76//! - **`alloc`** — Enable use of the [`alloc`] crate when not using the `std` library.
77//!
78//!   Among other functions, this enables the [`vec`] module, which contains zero-copy
79//!   conversions for the [`Vec`] type. This allows fast conversion between raw `Vec<u16>` bits and
80//!   `Vec<f16>` or `Vec<bf16>` arrays, and vice versa.
81//!
82//! - **`std`** — Enable features that depend on the Rust [`std`] library. This also enables the
83//!   `alloc` feature automatically.
84//!
85//!   Enabling the `std` feature enables runtime CPU feature detection of hardware support.
86//!   Without this feature detection, harware is only used when compiler target supports them.
87//!
88//! - **`serde`** — Adds support for the [`serde`] crate by implementing [`Serialize`] and
89//!   [`Deserialize`] traits for both [`struct@f16`] and [`struct@bf16`].
90//!
91//! - **`num-traits`** — Adds support for the [`num-traits`] crate by implementing [`ToPrimitive`],
92//!   [`FromPrimitive`], [`ToBytes`], `FromBytes`, [`AsPrimitive`], [`Num`], [`Float`],
93//!   [`FloatCore`], [`Signed`], and [`Bounded`] traits for both [`struct@f16`] and [`struct@bf16`].
94//!
95//! - **`bytemuck`** — Adds support for the [`bytemuck`] crate by implementing [`Zeroable`] and
96//!   [`Pod`] traits for both [`struct@f16`] and [`struct@bf16`].
97//!
98//! - **`rand_distr`** — Adds support for the [`rand_distr`] crate by implementing [`Distribution`]
99//!   and other traits for both [`struct@f16`] and [`struct@bf16`].
100//!
101//! - **`rkyv`** -- Enable zero-copy deserializtion with [`rkyv`] crate.
102//!
103//! - **`aribtrary`** -- Enable fuzzing support with [`arbitrary`] crate by implementing
104//!   [`Arbitrary`] trait.
105//!
106//! [`alloc`]: https://doc.rust-lang.org/alloc/
107//! [`std`]: https://doc.rust-lang.org/std/
108//! [`binary16`]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
109//! [`bfloat16`]: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
110//! [`serde`]: https://crates.io/crates/serde
111//! [`bytemuck`]: https://crates.io/crates/bytemuck
112//! [`num-traits`]: https://crates.io/crates/num-traits
113//! [`zerocopy`]: https://crates.io/crates/zerocopy
114//! [`rand_distr`]: https://crates.io/crates/rand_distr
115//! [`rkyv`]: (https://crates.io/crates/rkyv)
116//! [`arbitrary`]: (https://crates.io/crates/arbitrary)
117#![cfg_attr(
118    feature = "alloc",
119    doc = "
120[`vec`]: mod@vec"
121)]
122#![cfg_attr(
123    not(feature = "alloc"),
124    doc = "
125[`vec`]: #
126[`Vec`]: https://docs.rust-lang.org/stable/alloc/vec/struct.Vec.html"
127)]
128#![cfg_attr(
129    feature = "serde",
130    doc = "
131[`Serialize`]: serde::Serialize
132[`Deserialize`]: serde::Deserialize"
133)]
134#![cfg_attr(
135    not(feature = "serde"),
136    doc = "
137[`Serialize`]: https://docs.rs/serde/*/serde/trait.Serialize.html
138[`Deserialize`]: https://docs.rs/serde/*/serde/trait.Deserialize.html"
139)]
140#![cfg_attr(
141    feature = "num-traits",
142    doc = "
143[`ToPrimitive`]: ::num_traits::ToPrimitive
144[`FromPrimitive`]: ::num_traits::FromPrimitive
145[`ToBytes`]: ::num_traits::ToBytes
146[`AsPrimitive`]: ::num_traits::AsPrimitive
147[`Num`]: ::num_traits::Num
148[`Float`]: ::num_traits::Float
149[`FloatCore`]: ::num_traits::float::FloatCore
150[`Signed`]: ::num_traits::Signed
151[`Bounded`]: ::num_traits::Bounded"
152)]
153#![cfg_attr(
154    not(feature = "num-traits"),
155    doc = "
156[`ToPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.ToPrimitive.html
157[`FromPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.FromPrimitive.html
158[`ToBytes`]: https://docs.rs/num-traits/*/num_traits/ops/bytes/trait.ToBytes.html
159[`AsPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.AsPrimitive.html
160[`Num`]: https://docs.rs/num-traits/*/num_traits/trait.Num.html
161[`Float`]: https://docs.rs/num-traits/*/num_traits/float/trait.Float.html
162[`FloatCore`]: https://docs.rs/num-traits/*/num_traits/float/trait.FloatCore.html
163[`Bounded`]: https://docs.rs/num-traits/*/num_traits/bounds/trait.Bounded.html"
164)]
165#![cfg_attr(
166    feature = "bytemuck",
167    doc = "
168[`Zeroable`]: bytemuck::Zeroable
169[`Pod`]: bytemuck::Pod"
170)]
171#![cfg_attr(
172    not(feature = "bytemuck"),
173    doc = "
174[`Zeroable`]: https://docs.rs/bytemuck/*/bytemuck/trait.Zeroable.html
175[`Pod`]: https://docs.rs/bytemuck/*bytemuck/trait.Pod.html"
176)]
177#![cfg_attr(
178    feature = "zerocopy",
179    doc = "
180[`IntoBytes`]: zerocopy::IntoBytes
181[`FromBytes`]: zerocopy::FromBytes"
182)]
183#![cfg_attr(
184    not(feature = "zerocopy"),
185    doc = "
186[`IntoBytes`]: https://docs.rs/zerocopy/*/zerocopy/trait.IntoBytes.html
187[`FromBytes`]: https://docs.rs/zerocopy/*/zerocopy/trait.FromBytes.html"
188)]
189#![cfg_attr(
190    feature = "rand_distr",
191    doc = "
192[`Distribution`]: rand::distr::Distribution"
193)]
194#![cfg_attr(
195    not(feature = "rand_distr"),
196    doc = "
197[`Distribution`]: https://docs.rs/rand/*/rand/distr/trait.Distribution.html"
198)]
199#![cfg_attr(
200    feature = "arbitrary",
201    doc = "
202[`Arbitrary`]: arbitrary::Arbitrary"
203)]
204#![cfg_attr(
205    not(feature = "arbitrary"),
206    doc = "
207[`Arbitrary`]: https://docs.rs/arbitrary/*/arbitrary/trait.Arbitrary.html"
208)]
209#![warn(
210    missing_docs,
211    missing_copy_implementations,
212    trivial_numeric_casts,
213    future_incompatible
214)]
215#![cfg_attr(not(target_arch = "spirv"), warn(missing_debug_implementations))]
216#![cfg_attr(
217    target_arch = "loongarch64",
218    feature(
219        stdarch_loongarch,
220        stdarch_loongarch_feature_detection,
221        loongarch_target_feature
222    )
223)]
224#![allow(clippy::verbose_bit_mask, clippy::cast_lossless, unexpected_cfgs)]
225#![cfg_attr(not(feature = "std"), no_std)]
226#![doc(html_root_url = "https://docs.rs/half/2.7.0")]
227#![doc(test(attr(deny(warnings), allow(unused))))]
228// Until updated to use newly stabilized `from_bits`, disable new lint warning about the transmutes
229#![allow(unknown_lints, unnecessary_transmutes)]
230#![warn(unknown_lints)]
231
232#[cfg(feature = "alloc")]
233extern crate alloc;
234
235mod bfloat;
236mod binary16;
237mod leading_zeros;
238#[cfg(feature = "num-traits")]
239mod num_traits;
240
241#[cfg(not(target_arch = "spirv"))]
242pub mod slice;
243#[cfg(feature = "alloc")]
244pub mod vec;
245
246pub use bfloat::bf16;
247pub use binary16::f16;
248
249#[cfg(feature = "rand_distr")]
250mod rand_distr;
251
252/// A collection of the most used items and traits in this crate for easy importing.
253///
254/// # Examples
255///
256/// ```rust
257/// use half::prelude::*;
258/// ```
259pub mod prelude {
260    #[doc(no_inline)]
261    pub use crate::{bf16, f16};
262
263    #[cfg(not(target_arch = "spirv"))]
264    #[doc(no_inline)]
265    pub use crate::slice::{HalfBitsSliceExt, HalfFloatSliceExt};
266
267    #[cfg(feature = "alloc")]
268    #[doc(no_inline)]
269    pub use crate::vec::{HalfBitsVecExt, HalfFloatVecExt};
270}
271
272// Keep this module private to crate
273mod private {
274    use crate::{bf16, f16};
275    use zerocopy::{FromBytes, Immutable, IntoBytes};
276
277    pub trait SealedHalf: FromBytes + IntoBytes + Immutable {}
278
279    impl SealedHalf for f16 {}
280    impl SealedHalf for bf16 {}
281}