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//! - **`nightly`** -- Enable nightly-only features.
107//!
108//! [`alloc`]: https://doc.rust-lang.org/alloc/
109//! [`std`]: https://doc.rust-lang.org/std/
110//! [`binary16`]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
111//! [`bfloat16`]: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
112//! [`serde`]: https://crates.io/crates/serde
113//! [`bytemuck`]: https://crates.io/crates/bytemuck
114//! [`num-traits`]: https://crates.io/crates/num-traits
115//! [`zerocopy`]: https://crates.io/crates/zerocopy
116//! [`rand_distr`]: https://crates.io/crates/rand_distr
117//! [`rkyv`]: (https://crates.io/crates/rkyv)
118//! [`arbitrary`]: (https://crates.io/crates/arbitrary)
119#![cfg_attr(
120 feature = "alloc",
121 doc = "
122[`vec`]: mod@vec"
123)]
124#![cfg_attr(
125 not(feature = "alloc"),
126 doc = "
127[`vec`]: #
128[`Vec`]: https://docs.rust-lang.org/stable/alloc/vec/struct.Vec.html"
129)]
130#![cfg_attr(
131 feature = "serde",
132 doc = "
133[`Serialize`]: serde::Serialize
134[`Deserialize`]: serde::Deserialize"
135)]
136#![cfg_attr(
137 not(feature = "serde"),
138 doc = "
139[`Serialize`]: https://docs.rs/serde/*/serde/trait.Serialize.html
140[`Deserialize`]: https://docs.rs/serde/*/serde/trait.Deserialize.html"
141)]
142#![cfg_attr(
143 feature = "num-traits",
144 doc = "
145[`ToPrimitive`]: ::num_traits::ToPrimitive
146[`FromPrimitive`]: ::num_traits::FromPrimitive
147[`ToBytes`]: ::num_traits::ToBytes
148[`AsPrimitive`]: ::num_traits::AsPrimitive
149[`Num`]: ::num_traits::Num
150[`Float`]: ::num_traits::Float
151[`FloatCore`]: ::num_traits::float::FloatCore
152[`Signed`]: ::num_traits::Signed
153[`Bounded`]: ::num_traits::Bounded"
154)]
155#![cfg_attr(
156 not(feature = "num-traits"),
157 doc = "
158[`ToPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.ToPrimitive.html
159[`FromPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.FromPrimitive.html
160[`ToBytes`]: https://docs.rs/num-traits/*/num_traits/ops/bytes/trait.ToBytes.html
161[`AsPrimitive`]: https://docs.rs/num-traits/*/num_traits/cast/trait.AsPrimitive.html
162[`Num`]: https://docs.rs/num-traits/*/num_traits/trait.Num.html
163[`Float`]: https://docs.rs/num-traits/*/num_traits/float/trait.Float.html
164[`FloatCore`]: https://docs.rs/num-traits/*/num_traits/float/trait.FloatCore.html
165[`Bounded`]: https://docs.rs/num-traits/*/num_traits/bounds/trait.Bounded.html"
166)]
167#![cfg_attr(
168 feature = "bytemuck",
169 doc = "
170[`Zeroable`]: bytemuck::Zeroable
171[`Pod`]: bytemuck::Pod"
172)]
173#![cfg_attr(
174 not(feature = "bytemuck"),
175 doc = "
176[`Zeroable`]: https://docs.rs/bytemuck/*/bytemuck/trait.Zeroable.html
177[`Pod`]: https://docs.rs/bytemuck/*bytemuck/trait.Pod.html"
178)]
179#![cfg_attr(
180 feature = "zerocopy",
181 doc = "
182[`IntoBytes`]: zerocopy::IntoBytes
183[`FromBytes`]: zerocopy::FromBytes"
184)]
185#![cfg_attr(
186 not(feature = "zerocopy"),
187 doc = "
188[`IntoBytes`]: https://docs.rs/zerocopy/*/zerocopy/trait.IntoBytes.html
189[`FromBytes`]: https://docs.rs/zerocopy/*/zerocopy/trait.FromBytes.html"
190)]
191#![cfg_attr(
192 feature = "rand_distr",
193 doc = "
194[`Distribution`]: rand::distr::Distribution"
195)]
196#![cfg_attr(
197 not(feature = "rand_distr"),
198 doc = "
199[`Distribution`]: https://docs.rs/rand/*/rand/distr/trait.Distribution.html"
200)]
201#![cfg_attr(
202 feature = "arbitrary",
203 doc = "
204[`Arbitrary`]: arbitrary::Arbitrary"
205)]
206#![cfg_attr(
207 not(feature = "arbitrary"),
208 doc = "
209[`Arbitrary`]: https://docs.rs/arbitrary/*/arbitrary/trait.Arbitrary.html"
210)]
211#![warn(
212 missing_docs,
213 missing_copy_implementations,
214 trivial_numeric_casts,
215 future_incompatible
216)]
217#![cfg_attr(not(target_arch = "spirv"), warn(missing_debug_implementations))]
218#![cfg_attr(
219 all(feature = "nightly", target_arch = "loongarch64"),
220 feature(
221 stdarch_loongarch,
222 stdarch_loongarch_feature_detection,
223 loongarch_target_feature
224 )
225)]
226#![allow(clippy::verbose_bit_mask, clippy::cast_lossless, unexpected_cfgs)]
227#![cfg_attr(not(feature = "std"), no_std)]
228#![doc(html_root_url = "https://docs.rs/half/2.7.1")]
229#![doc(test(attr(deny(warnings), allow(unused))))]
230// Until updated to use newly stabilized `from_bits`, disable new lint warning about the transmutes
231#![allow(unknown_lints, unnecessary_transmutes)]
232#![warn(unknown_lints)]
233
234#[cfg(feature = "alloc")]
235extern crate alloc;
236
237mod bfloat;
238mod binary16;
239mod leading_zeros;
240#[cfg(feature = "num-traits")]
241mod num_traits;
242
243#[cfg(not(target_arch = "spirv"))]
244pub mod slice;
245#[cfg(feature = "alloc")]
246pub mod vec;
247
248pub use bfloat::bf16;
249pub use binary16::f16;
250
251#[cfg(feature = "rand_distr")]
252mod rand_distr;
253
254/// A collection of the most used items and traits in this crate for easy importing.
255///
256/// # Examples
257///
258/// ```rust
259/// use half::prelude::*;
260/// ```
261pub mod prelude {
262 #[doc(no_inline)]
263 pub use crate::{bf16, f16};
264
265 #[cfg(not(target_arch = "spirv"))]
266 #[doc(no_inline)]
267 pub use crate::slice::{HalfBitsSliceExt, HalfFloatSliceExt};
268
269 #[cfg(feature = "alloc")]
270 #[doc(no_inline)]
271 pub use crate::vec::{HalfBitsVecExt, HalfFloatVecExt};
272}
273
274// Keep this module private to crate
275mod private {
276 use crate::{bf16, f16};
277 use zerocopy::{FromBytes, Immutable, IntoBytes};
278
279 pub trait SealedHalf: FromBytes + IntoBytes + Immutable {}
280
281 impl SealedHalf for f16 {}
282 impl SealedHalf for bf16 {}
283}