use super::*;
#[cfg(target_arch = "x86_64")]
pub(super) type Avx512GfniTable = (__m512i, __m512i, [u16; 256], [u16; 256]);
#[cfg(target_arch = "x86_64")]
pub(super) type Avx512ShuffleTable = (
__m512i,
__m512i,
__m512i,
__m512i,
__m512i,
__m512i,
__m512i,
__m512i,
[u16; 256],
[u16; 256],
);
#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
pub(super) type Avx2GfniTable = (__m256i, __m256i, [u16; 256], [u16; 256]);
#[cfg(target_arch = "x86_64")]
pub(super) type Ssse3Table = (
__m128i,
__m128i,
__m128i,
__m128i,
__m128i,
__m128i,
__m128i,
__m128i,
[u16; 256],
[u16; 256],
);
#[cfg(target_arch = "x86_64")]
#[allow(dead_code)]
pub(super) type Avx2Shuffle2xTable = (
__m256i, __m256i, __m256i, __m256i, [u16; 256], [u16; 256], );
#[cfg(target_arch = "x86_64")]
pub(super) type Avx2Table = (
__m256i,
__m256i,
__m256i,
__m256i,
__m256i,
__m256i,
__m256i,
__m256i,
[u16; 256],
[u16; 256],
);
#[cfg(target_arch = "x86_64")]
#[derive(Clone, Copy)]
pub(super) struct PlaneOutDeps {
pub(super) plane_out: u8,
pub(super) plane_ins: [u8; 16],
pub(super) n_ins: u8,
}
#[cfg(target_arch = "x86_64")]
pub(super) fn decode_plane_deps(deps: &[u16; 16]) -> ([PlaneOutDeps; 16], usize) {
let mut out = [PlaneOutDeps {
plane_out: 0,
plane_ins: [0; 16],
n_ins: 0,
}; 16];
let mut count = 0;
for (plane_out, &mask) in deps.iter().enumerate() {
if mask == 0 {
continue;
}
let mut plane_ins = [0u8; 16];
let mut n_ins = 0u8;
for plane_in in 0..16u8 {
if (mask >> plane_in) & 1 == 1 {
plane_ins[n_ins as usize] = plane_in;
n_ins += 1;
}
}
out[count] = PlaneOutDeps {
plane_out: plane_out as u8,
plane_ins,
n_ins,
};
count += 1;
}
(out, count)
}
#[cfg_attr(not(target_arch = "x86_64"), allow(dead_code))]
pub(super) fn gfni_affine_u64_mats(gf: &Gf16, coeff: u16) -> (u64, u64, u64, u64) {
let mut m_ll = 0u64;
let mut m_lh = 0u64;
let mut m_hl = 0u64;
let mut m_hh = 0u64;
for row in 0..8usize {
let mut row_ll = 0u8;
let mut row_lh = 0u8;
let mut row_hl = 0u8;
let mut row_hh = 0u8;
for j in 0..8usize {
let r_lo = gf.mul(1u16 << j, coeff);
if (r_lo >> row) & 1 == 1 {
row_ll |= 1 << j;
}
if (r_lo >> (row + 8)) & 1 == 1 {
row_hl |= 1 << j;
}
let r_hi = gf.mul(1u16 << (j + 8), coeff);
if (r_hi >> row) & 1 == 1 {
row_lh |= 1 << j;
}
if (r_hi >> (row + 8)) & 1 == 1 {
row_hh |= 1 << j;
}
}
let shift = (7 - row) * 8;
m_ll |= (row_ll as u64) << shift;
m_lh |= (row_lh as u64) << shift;
m_hl |= (row_hl as u64) << shift;
m_hh |= (row_hh as u64) << shift;
}
(m_ll, m_lh, m_hl, m_hh)
}
#[cfg_attr(not(target_arch = "x86_64"), allow(dead_code))]
#[repr(align(32))]
pub(super) struct AffineNibbleScratch {
pub(super) mats: [[(u64, u64, u64, u64); 4]; 16],
}
#[cfg_attr(not(target_arch = "x86_64"), allow(dead_code))]
impl AffineNibbleScratch {
pub(super) fn new(gf: &Gf16) -> Self {
let mut mats = [[(0u64, 0u64, 0u64, 0u64); 4]; 16];
for n in 0..16u16 {
for slot in 0..4u16 {
let (ll, lh, hl, hh) = gfni_affine_u64_mats(gf, n << (4 * slot));
mats[n as usize][slot as usize] = (ll, hh, hl, lh);
}
}
Self { mats }
}
pub(super) fn load(&self, coeff: u16) -> (u64, u64, u64, u64) {
let xor = |a: (u64, u64, u64, u64), b: (u64, u64, u64, u64)| {
(a.0 ^ b.0, a.1 ^ b.1, a.2 ^ b.2, a.3 ^ b.3)
};
let mut m = self.mats[(coeff & 0xf) as usize][0];
m = xor(m, self.mats[((coeff >> 4) & 0xf) as usize][1]);
m = xor(m, self.mats[((coeff >> 8) & 0xf) as usize][2]);
m = xor(m, self.mats[((coeff >> 12) & 0xf) as usize][3]);
(m.0, m.3, m.2, m.1)
}
}