r/wg21
P3973R0 - bit_cast_as: Element type reinterpretation for std::simd WG21
Posted by u/simd_ergonomics_watcher · 5 hr. ago

Document: P3973R0
Author: Daniel Towner (Intel)
Date: 2026-01-19
Audience: LEWG, SG1

Proposes std::bit_cast_as<T>(simd_vec) for reinterpreting simd objects at different element granularities. Instead of std::bit_cast<vec<uint16_t, 8>>(bytes) with manually computed element counts, you write std::bit_cast_as<uint16_t>(bytes) and the count is inferred automatically. Brings std::simd to parity with platform intrinsics like _mm256_castps_si256. Generalizes naturally to std::array and std::span.

▲ 31 points (93% upvoted) · 7 comments
sorted by: best
u/intrinsics_refugee 24 points 4 hr. ago

This is long overdue. Platform intrinsics have had _mm256_castps_si256 forever - same bits, different element type, zero overhead. std::simd made you spell out the entire target type with manual count computation. bit_cast_as<uint32_t>(floats) is the natural C++ equivalent and it's embarrassing we didn't have it from the start.

u/compile_time_safety_fan 18 points 4 hr. ago

The compile-time size verification is the key feature. bit_cast_as<uint32_t>(vec<uint8_t, 15>) won't compile because 15 bytes doesn't divide evenly into 4-byte elements. You catch the error at compile time instead of at runtime or in a code review.

u/array_generalization_fan 14 points 3 hr. ago

The generalization to std::array is natural and useful. bit_cast_as<uint16_t>(std::array<uint8_t, 16>) returning std::array<uint16_t, 8> is exactly what protocol parsing code needs. The std::span variant returning a view (not a copy) is the right semantic choice too.

u/p3983_dependency 11 points 3 hr. ago

Depends on P3983R0 for array-like layout guarantees in simd. Without guaranteed contiguous element storage, bit_cast between simd types isn't portable. The dependency is well-documented and the prerequisite paper is in the pipeline.

u/naming_bikeshed 8 points 2 hr. ago

bit_cast_as vs as_elements - the paper chose bit_cast_as because the relationship to std::bit_cast should be obvious. I agree. When you see bit_cast_as you know this is bit-level reinterpretation, not a semantic conversion.

u/intel_simd_team 5 points 1 hr. ago

Intel's simd implementation has had this internally since early development. It's widely used both in the library internals (interfacing with compiler intrinsics) and in user code (wireless signal processing, packet handling). Production-tested.