half
IEEE 754 f16 and bf16 half-precision floating-point types for Rust
Repository Health
Technical Analysis
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.
Used by 4 apps in this directory
Fyrox
Game Development
A production-ready 2D/3D game engine written in Rust with a built-in scene editor, physics, and hot-reloading game scripts
Laminar
AI Development · Monitoring
Open-source observability platform purpose-built for AI agents — trace, evaluate, debug, and monitor at scale with SQL access and real-time replay.
ParadeDB
Search · Databases · Analytics
Born out of Y Combinator's S2023 batch, ParadeDB is a Postgres extension that delivers Elasticsearch-quality BM25 search and real-time analytics without a separate search cluster to manage.
WrenAI
Analytics · AI Agents · Data Engineering
Open-source GenBI engine that lets AI agents turn natural-language questions into governed SQL, charts, and shareable dashboards across 20+ data sources — no vendor lock-in, no black-box prompts.