use crate::kernel::gf16::{mul_add_scalar, mul_assign_scalar, mul_into_scalar};
use crate::kernel::tables::{NibbleFactors, TowerTables};
use super::{swap_mask128, swap_mask256};
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
pub(crate) struct NibbleAvx2 {
pub(crate) lo: [__m256i; 4],
pub(crate) hi: [__m256i; 4],
pub(crate) nibble: __m256i,
pub(crate) even: __m256i,
pub(crate) swap: __m256i,
}
#[archmage::rite(v3, import_intrinsics)]
pub(crate) fn nibble_avx2<T: NibbleFactors>(tables: &T) -> NibbleAvx2 {
let mut lo = [_mm256_setzero_si256(); 4];
let mut hi = [_mm256_setzero_si256(); 4];
for slot in 0..4 {
lo[slot] = _mm256_broadcastsi128_si256(_mm_loadu_si128(tables.lo(slot)));
hi[slot] = _mm256_broadcastsi128_si256(_mm_loadu_si128(tables.hi(slot)));
}
NibbleAvx2 {
lo,
hi,
nibble: _mm256_set1_epi8(0x0f),
even: _mm256_set1_epi16(0x00ff),
swap: swap_mask256(),
}
}
#[archmage::rite(v3)]
pub(crate) fn split_avx2(value: __m256i, nibble: __m256i) -> (__m256i, __m256i) {
(
_mm256_and_si256(value, nibble),
_mm256_and_si256(_mm256_srli_epi16(value, 4), nibble),
)
}
#[archmage::rite(v3)]
pub(crate) fn lookup_avx2(lo: __m256i, hi: __m256i, split: (__m256i, __m256i)) -> __m256i {
_mm256_xor_si256(
_mm256_shuffle_epi8(lo, split.0),
_mm256_shuffle_epi8(hi, split.1),
)
}
#[archmage::rite(v3)]
pub(crate) fn scale_avx2(src: __m256i, tables: &NibbleAvx2) -> __m256i {
let swapped = _mm256_shuffle_epi8(src, tables.swap);
let direct = split_avx2(src, tables.nibble);
let crossed = split_avx2(swapped, tables.nibble);
let even = _mm256_xor_si256(
lookup_avx2(tables.lo[0], tables.hi[0], direct),
lookup_avx2(tables.lo[2], tables.hi[2], crossed),
);
let odd = _mm256_xor_si256(
lookup_avx2(tables.lo[1], tables.hi[1], direct),
lookup_avx2(tables.lo[3], tables.hi[3], crossed),
);
_mm256_xor_si256(
_mm256_and_si256(even, tables.even),
_mm256_andnot_si256(tables.even, odd),
)
}
#[allow(clippy::used_underscore_binding)]
#[archmage::arcane(import_intrinsics)]
pub fn mul_add_avx2(
_token: archmage::X64V3Token,
dst: &mut [u8],
tables: &TowerTables,
src: &[u8],
) {
super::check_elements("gf16::mul_add_avx2", dst.len());
assert_eq!(
dst.len(),
src.len(),
"gf16::mul_add_avx2: dst is {} bytes but src is {} bytes",
dst.len(),
src.len(),
);
let vectors = nibble_avx2(tables);
let (dst_lanes, dst_rest) = dst.as_chunks_mut::<32>();
let (src_lanes, src_rest) = src.as_chunks::<32>();
for (dst_lane, src_lane) in dst_lanes.iter_mut().zip(src_lanes) {
let x = _mm256_loadu_si256(src_lane);
let d = _mm256_loadu_si256(&*dst_lane);
_mm256_storeu_si256(dst_lane, _mm256_xor_si256(d, scale_avx2(x, &vectors)));
}
let (dst_lanes, dst_tail) = dst_rest.as_chunks_mut::<16>();
let (src_lanes, src_tail) = src_rest.as_chunks::<16>();
if !(dst_lanes.is_empty() && src_lanes.is_empty()) {
let narrow = nibble_ssse3(tables);
for (dst_lane, src_lane) in dst_lanes.iter_mut().zip(src_lanes) {
let x = _mm_loadu_si128(src_lane);
let d = _mm_loadu_si128(&*dst_lane);
_mm_storeu_si128(dst_lane, _mm_xor_si128(d, scale_ssse3(x, &narrow)));
}
}
mul_add_scalar(dst_tail, tables.coeff, src_tail);
}
#[allow(clippy::used_underscore_binding)]
#[archmage::arcane(import_intrinsics)]
pub fn mul_assign_avx2(_token: archmage::X64V3Token, dst: &mut [u8], tables: &TowerTables) {
super::check_elements("gf16::mul_assign_avx2", dst.len());
let vectors = nibble_avx2(tables);
let (dst_lanes, dst_rest) = dst.as_chunks_mut::<32>();
for dst_lane in dst_lanes {
let x = _mm256_loadu_si256(&*dst_lane);
_mm256_storeu_si256(dst_lane, scale_avx2(x, &vectors));
}
let (dst_lanes, dst_tail) = dst_rest.as_chunks_mut::<16>();
if !dst_lanes.is_empty() {
let narrow = nibble_ssse3(tables);
for dst_lane in dst_lanes {
let x = _mm_loadu_si128(&*dst_lane);
_mm_storeu_si128(dst_lane, scale_ssse3(x, &narrow));
}
}
mul_assign_scalar(dst_tail, tables.coeff);
}
#[allow(clippy::used_underscore_binding)]
#[archmage::arcane(import_intrinsics)]
pub fn mul_into_avx2(
_token: archmage::X64V3Token,
dst: &mut [u8],
tables: &TowerTables,
src: &[u8],
) {
super::check_elements("gf16::mul_into_avx2", dst.len());
assert_eq!(
dst.len(),
src.len(),
"gf16::mul_into_avx2: dst is {} bytes but src is {} bytes",
dst.len(),
src.len(),
);
match super::nt_split(dst, 2) {
Some(peel) => {
let (head, body) = dst.split_at_mut(peel);
let (src_head, src_body) = src.split_at(peel);
mul_into_avx2_lane::<false>(head, tables, src_head);
mul_into_avx2_lane::<true>(body, tables, src_body);
_mm_sfence();
}
None => mul_into_avx2_lane::<false>(dst, tables, src),
}
}
#[allow(unsafe_code)]
#[archmage::rite(v3, import_intrinsics)]
fn mul_into_avx2_lane<const NT: bool>(dst: &mut [u8], tables: &TowerTables, src: &[u8]) {
let vectors = nibble_avx2(tables);
let (dst_lanes, dst_rest) = dst.as_chunks_mut::<32>();
let (src_lanes, src_rest) = src.as_chunks::<32>();
for (dst_lane, src_lane) in dst_lanes.iter_mut().zip(src_lanes) {
let x = _mm256_loadu_si256(src_lane);
unsafe { super::store256::<NT>(dst_lane.as_mut_ptr(), scale_avx2(x, &vectors)) };
}
let (dst_lanes, dst_tail) = dst_rest.as_chunks_mut::<16>();
let (src_lanes, src_tail) = src_rest.as_chunks::<16>();
if !(dst_lanes.is_empty() && src_lanes.is_empty()) {
let narrow = nibble_ssse3(tables);
for (dst_lane, src_lane) in dst_lanes.iter_mut().zip(src_lanes) {
let x = _mm_loadu_si128(src_lane);
_mm_storeu_si128(dst_lane, scale_ssse3(x, &narrow));
}
}
mul_into_scalar(dst_tail, tables.coeff, src_tail);
}
pub(crate) struct NibbleSsse3 {
pub(crate) lo: [__m128i; 4],
pub(crate) hi: [__m128i; 4],
pub(crate) nibble: __m128i,
pub(crate) even: __m128i,
pub(crate) swap: __m128i,
}
#[archmage::rite(v2, import_intrinsics)]
pub(crate) fn nibble_ssse3<T: NibbleFactors>(tables: &T) -> NibbleSsse3 {
let mut lo = [_mm_setzero_si128(); 4];
let mut hi = [_mm_setzero_si128(); 4];
for slot in 0..4 {
lo[slot] = _mm_loadu_si128(tables.lo(slot));
hi[slot] = _mm_loadu_si128(tables.hi(slot));
}
NibbleSsse3 {
lo,
hi,
nibble: _mm_set1_epi8(0x0f),
even: _mm_set1_epi16(0x00ff),
swap: swap_mask128(),
}
}
#[archmage::rite(v2)]
pub(crate) fn split_ssse3(value: __m128i, nibble: __m128i) -> (__m128i, __m128i) {
(
_mm_and_si128(value, nibble),
_mm_and_si128(_mm_srli_epi16(value, 4), nibble),
)
}
#[archmage::rite(v2)]
pub(crate) fn lookup_ssse3(lo: __m128i, hi: __m128i, split: (__m128i, __m128i)) -> __m128i {
_mm_xor_si128(_mm_shuffle_epi8(lo, split.0), _mm_shuffle_epi8(hi, split.1))
}
#[archmage::rite(v2)]
pub(crate) fn scale_ssse3(src: __m128i, tables: &NibbleSsse3) -> __m128i {
let swapped = _mm_shuffle_epi8(src, tables.swap);
let direct = split_ssse3(src, tables.nibble);
let crossed = split_ssse3(swapped, tables.nibble);
let even = _mm_xor_si128(
lookup_ssse3(tables.lo[0], tables.hi[0], direct),
lookup_ssse3(tables.lo[2], tables.hi[2], crossed),
);
let odd = _mm_xor_si128(
lookup_ssse3(tables.lo[1], tables.hi[1], direct),
lookup_ssse3(tables.lo[3], tables.hi[3], crossed),
);
_mm_xor_si128(
_mm_and_si128(even, tables.even),
_mm_andnot_si128(tables.even, odd),
)
}
#[allow(clippy::used_underscore_binding)]
#[archmage::arcane(import_intrinsics)]
pub fn mul_add_ssse3(
_token: archmage::X64V2Token,
dst: &mut [u8],
tables: &TowerTables,
src: &[u8],
) {
super::check_elements("gf16::mul_add_ssse3", dst.len());
assert_eq!(
dst.len(),
src.len(),
"gf16::mul_add_ssse3: dst is {} bytes but src is {} bytes",
dst.len(),
src.len(),
);
let vectors = nibble_ssse3(tables);
let (dst_lanes, dst_tail) = dst.as_chunks_mut::<16>();
let (src_lanes, src_tail) = src.as_chunks::<16>();
for (dst_lane, src_lane) in dst_lanes.iter_mut().zip(src_lanes) {
let x = _mm_loadu_si128(src_lane);
let d = _mm_loadu_si128(&*dst_lane);
_mm_storeu_si128(dst_lane, _mm_xor_si128(d, scale_ssse3(x, &vectors)));
}
mul_add_scalar(dst_tail, tables.coeff, src_tail);
}
#[allow(clippy::used_underscore_binding)]
#[archmage::arcane(import_intrinsics)]
pub fn mul_assign_ssse3(_token: archmage::X64V2Token, dst: &mut [u8], tables: &TowerTables) {
super::check_elements("gf16::mul_assign_ssse3", dst.len());
let vectors = nibble_ssse3(tables);
let (dst_lanes, dst_tail) = dst.as_chunks_mut::<16>();
for dst_lane in dst_lanes {
let x = _mm_loadu_si128(&*dst_lane);
_mm_storeu_si128(dst_lane, scale_ssse3(x, &vectors));
}
mul_assign_scalar(dst_tail, tables.coeff);
}
#[allow(clippy::used_underscore_binding)]
#[archmage::arcane(import_intrinsics)]
pub fn mul_into_ssse3(
_token: archmage::X64V2Token,
dst: &mut [u8],
tables: &TowerTables,
src: &[u8],
) {
super::check_elements("gf16::mul_into_ssse3", dst.len());
assert_eq!(
dst.len(),
src.len(),
"gf16::mul_into_ssse3: dst is {} bytes but src is {} bytes",
dst.len(),
src.len(),
);
let vectors = nibble_ssse3(tables);
let (dst_lanes, dst_tail) = dst.as_chunks_mut::<16>();
let (src_lanes, src_tail) = src.as_chunks::<16>();
for (dst_lane, src_lane) in dst_lanes.iter_mut().zip(src_lanes) {
let x = _mm_loadu_si128(src_lane);
_mm_storeu_si128(dst_lane, scale_ssse3(x, &vectors));
}
mul_into_scalar(dst_tail, tables.coeff, src_tail);
}