Skip to main content

trueno/tiling/
packing.rs

1//! Memory packing layout utilities.
2
3/// Memory layout for packed matrices
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum PackingLayout {
6    /// Row-major (C-style)
7    RowMajor,
8    /// Column-major (Fortran-style)
9    ColumnMajor,
10    /// Panel-major for A (Goto algorithm)
11    PanelMajorA,
12    /// Panel-major for B (Goto algorithm)
13    PanelMajorB,
14}
15
16/// Calculate packed index for panel-major A layout
17///
18/// Panel-major stores micro-panels contiguously for sequential access.
19#[must_use]
20#[inline]
21pub fn pack_a_index(row: usize, col: usize, mr: usize, kc: usize, _mc: usize) -> usize {
22    let panel = row / mr;
23    let row_in_panel = row % mr;
24    panel * mr * kc + col * mr + row_in_panel
25}
26
27/// Calculate packed index for panel-major B layout
28#[must_use]
29#[inline]
30pub fn pack_b_index(row: usize, col: usize, nr: usize, kc: usize, _nc: usize) -> usize {
31    let panel = col / nr;
32    let col_in_panel = col % nr;
33    panel * kc * nr + row * nr + col_in_panel
34}
35
36/// Apply XOR swizzling for shared memory bank conflict avoidance
37///
38/// Pattern: idx_swizzled = idx ^ (idx >> 5) for 32-bank architectures.
39#[must_use]
40#[inline]
41pub fn swizzle_index(idx: usize) -> usize {
42    idx ^ (idx >> 5)
43}