Skip to main content

buffer_size_bytes

Function buffer_size_bytes 

Source
pub fn buffer_size_bytes(ty: ElementType, element_count: u64) -> u64
Expand description

Returns the minimum buffer size in bytes required to store element_count contiguous elements of the given type.

This implements the buffer-size formulas defined in docs/spec/element-types.md § Buffer Size Calculation:

  • Whole-byte types (bit_width ≥ 8): element_count × (bit_width / 8)
  • 6-bit types (float6_e2m3, float6_e3m2): ⌈N / 4⌉ × 3
  • Other sub-byte types (bit_width ∈ {1, 2, 4}): ⌈N × bit_width / 8⌉

Uses 128-bit intermediate arithmetic to avoid overflow for large element_count.

§Examples

use hurray_core::{ElementType, buffer_size_bytes};

// float32: 60 elements × 4 bytes = 240 bytes
assert_eq!(buffer_size_bytes(ElementType::Float32, 60), 240);

// int4: 7 elements → ⌈7/2⌉ = 4 bytes
assert_eq!(buffer_size_bytes(ElementType::Int4, 7), 4);

// float6_e2m3: 5 elements → ⌈5/4⌉ × 3 = 6 bytes
assert_eq!(buffer_size_bytes(ElementType::Float6E2M3, 5), 6);

// bool: 9 elements → ⌈9/8⌉ = 2 bytes
assert_eq!(buffer_size_bytes(ElementType::Bool, 9), 2);

// Zero elements always yields zero bytes.
assert_eq!(buffer_size_bytes(ElementType::Float32, 0), 0);