half

IEEE 754 f16 and bf16 half-precision floating-point types for Rust

Library
Cargo
v2.7.1
281stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
51/100Fair
Development Activity16
Maintenance32
Community76
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture75
Code Quality78
Innovation68
Learning Curve70

half is a Rust crate that implements the IEEE 754-2008 binary16 (f16) format and the bfloat16 (bf16) format as first-class numeric types. It targets code that needs compact float storage or hardware-accelerated half-precision math — machine learning tensors, graphics pipelines, and embedded targets — while staying no_std-friendly.

The crate leans on CPU intrinsics (f16c on x86/x86_64, fp16 on aarch64) when available and falls back to portable software conversion routines otherwise, so the same API works whether or not the target has hardware half-float support. Optional feature flags add interop with serde, bytemuck, num-traits, rand_distr, rkyv, and arbitrary without forcing those dependencies on users who don’t need them.

What You Get

  • f16 (IEEE 754 binary16) and bf16 (bfloat16) types with to/from f32 and f64 conversions
  • Hardware-accelerated conversion via f16c intrinsics on x86/x86_64 and fp16 on aarch64, with a portable software fallback
  • no_std support for embedded and bare-metal targets, with an optional alloc feature for Vec-based zero-copy conversions
  • Optional trait implementations for serde, bytemuck, num-traits, rand_distr, rkyv, and arbitrary behind feature flags
  • Basic arithmetic operations on half-precision values without needing to round-trip through f32

Common Use Cases

  • Storing and converting machine learning model weights and tensors in half precision to cut memory footprint
  • Interchanging half-precision pixel or vertex data with GPU/graphics APIs that use f16 natively
  • Running numeric code on embedded/no_std targets where a compact float representation matters
  • Serializing half-precision values to disk or over the wire via serde without manual bit manipulation

Under The Hood

Architecture — The crate is organized around two core numeric types, f16 (src/binary16.rs) and bf16 (src/bfloat.rs), each a thin newtype wrapper over u16 bit patterns. Conversion logic is split per-architecture under src/binary16/arch/ (x86.rs, aarch64.rs, loongarch64.rs) with cfg-if-gated dispatch that picks hardware intrinsics when the target supports them and falls back to portable bit-manipulation routines otherwise. Bulk operations live in src/slice.rs and src/vec.rs, providing zero-copy reinterpretation between [u16]/Vec<u16> and [f16]/Vec<f16> where safe. Tech Stack — Pure Rust, edition 2021, minimum Rust 1.81. Core dependency is cfg-if for target-conditional compilation; everything else (bytemuck, serde, num-traits, rand/rand_distr, rkyv, arbitrary, zerocopy) is optional and feature-gated, keeping the default build lean and no_std-compatible (with alloc and std as additive features). Criterion drives the benchmark suite in benches/convert.rs. Code Quality — Tests are inline #[cfg(test)] modules across the core source files (51 #[test] functions found in binary16.rs, bfloat.rs, slice.rs, vec.rs, leading_zeros.rs, rand_distr.rs) plus quickcheck property tests for conversion round-tripping, rather than a separate tests/ directory. Naming is consistent with Rust numeric-type conventions (mirroring f32/f64 method names), and per-architecture intrinsic code is isolated to keep the safe/unsafe boundary narrow. API Design — The public API deliberately mirrors std::f32/f64 conventions (to_f32, from_f32, arithmetic traits), so developers already familiar with Rust’s float types get near-zero learning curve; optional features are additive and don’t change the core API surface, and the README documents hardware support per architecture in a clear table.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search