MCPcopy Index your code
hub / github.com/VoidStarKat/half-rs

github.com/VoidStarKat/half-rs @v2.7.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.7.1 ↗ · + Follow
402 symbols 717 edges 14 files 4 documented · 1%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.81 or greater. If you need support for older versions of Rust, use previous 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, ToBytes, FromBytes, Num, Float, FloatCore, Signed, and Bounded trait implementations from the num-traits crate.

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

  • rand_distr — Enable sampling from distributions like StandardUniform and StandardNormal from the rand_distr crate.

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

  • aribtrary -- Enable fuzzing support with arbitrary crate by implementing Arbitrary trait.

  • nightly -- Enable nightly-only features (currently loongarch64 intrinsics).

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.
loongarch64 lsx (nightly feature only) This supports conversion to/from f16 only (including vector SIMD) and does not support any bf16 or arithmetic operations.

More Documentation

License

All files in this library are dual-licensed and distributed under the terms of either of:

at your option.

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.

Extension points exported contracts — how you extend this code

HalfFloatVecExt (Interface)
Extensions to [`Vec `] and [`Vec `] to support reinterpret operations. This trait is sealed and cannot be impl [2 implementers]
src/vec.rs
HalfFloatSliceExt (Interface)
Extensions to `[f16]` and `[bf16]` slices to support conversion and reinterpret operations. This trait is sealed and ca [2 …
src/slice.rs
SealedHalf (Interface)
(no doc) [2 implementers]
src/lib.rs
HalfBitsVecExt (Interface)
Extensions to [`Vec `] to support reinterpret operations. This trait is sealed and cannot be implemented outside of [1 …
src/vec.rs
HalfBitsSliceExt (Interface)
Extensions to `[u16]` slices to support reinterpret operations. This trait is sealed and cannot be implemented outside [1 …
src/slice.rs
SealedHalfFloatVec (Interface)
(no doc) [2 implementers]
src/vec.rs
SealedHalfFloatSlice (Interface)
(no doc) [2 implementers]
src/slice.rs
SealedHalfBitsVec (Interface)
(no doc) [1 implementers]
src/vec.rs

Core symbols most depended-on inside this repo

to_f32
called by 134
src/num_traits.rs
to_f32
called by 21
src/bfloat.rs
is_nan
called by 19
src/binary16.rs
is_nan
called by 19
src/bfloat.rs
to_bits
called by 16
src/bfloat.rs
f32_to_f16_fallback
called by 13
src/binary16/arch.rs
f64_to_f16_fallback
called by 13
src/binary16/arch.rs
f16_to_f32_fallback
called by 13
src/binary16/arch.rs

Shape

Method 242
Function 145
Interface 9
Class 6

Languages

Rust100%

Modules by API surface

src/num_traits.rs107 symbols
src/binary16.rs82 symbols
src/bfloat.rs80 symbols
src/binary16/arch.rs45 symbols
src/slice.rs24 symbols
src/binary16/arch/aarch64.rs12 symbols
benches/convert.rs12 symbols
src/binary16/arch/x86.rs10 symbols
src/vec.rs9 symbols
src/rand_distr.rs8 symbols
src/binary16/arch/loongarch64.rs6 symbols
src/bfloat/convert.rs4 symbols

For agents

$ claude mcp add half-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact