feature. See also
. The project being documented here (as the example) is the Zig library itself.
soft.table_lookup
fn table_lookup(table: *align(64) const [4][256]u32, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u32
File
Code
fn table_lookup(table: *align(64) const [4][256]u32, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u32 {
if (side_channels_mitigations == .none) {
return [4]u32{
table[0][idx0],
table[1][idx1],
table[2][idx2],
table[3][idx3],
};
} else {
const table_len: usize = 256;
const stride = switch (side_channels_mitigations) {
.none => unreachable,
.basic => table_len / 4,
.medium => @max(1, @min(table_len, 2 * cache_line_bytes / 4)),
.full => @max(1, @min(table_len, cache_line_bytes / 4)),
};
const of0 = idx0 % stride;
const of1 = idx1 % stride;
const of2 = idx2 % stride;
const of3 = idx3 % stride;
var t: [4][table_len / stride]u32 align(64) = undefined;
var i: usize = 0;
while (i < t[0].len) : (i += 1) {
const tx = table[0][i * stride ..];
t[0][i] = tx[of0];
t[1][i] = tx[of1];
t[2][i] = tx[of2];
t[3][i] = tx[of3];
}
std.mem.doNotOptimizeAway(t);
return [4]u32{
t[0][idx0 / stride],
math.rotl(u32, (&t[1])[idx1 / stride], 8),
math.rotl(u32, (&t[2])[idx2 / stride], 16),
math.rotl(u32, (&t[3])[idx3 / stride], 24),
};
}
}