#[cfg(feature = "bitslice")]
use macerator::{Scalar, Simd, Vector, with_simd};
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn and_words<S: Simd>(dst: &mut [u64], a: &[u64], b: &[u64]) {
let lanes = u64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
for i in (0..whole).step_by(lanes) {
(Vector::<S, u64>::load(&a[i..]) & Vector::<S, u64>::load(&b[i..])).store(&mut dst[i..]);
}
for i in whole..dst.len() {
dst[i] = a[i] & b[i];
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn or_words<S: Simd>(dst: &mut [u64], a: &[u64], b: &[u64]) {
let lanes = u64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
for i in (0..whole).step_by(lanes) {
(Vector::<S, u64>::load(&a[i..]) | Vector::<S, u64>::load(&b[i..])).store(&mut dst[i..]);
}
for i in whole..dst.len() {
dst[i] = a[i] | b[i];
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn or_assign_words<S: Simd>(dst: &mut [u64], src: &[u64]) {
let lanes = u64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
for i in (0..whole).step_by(lanes) {
(Vector::<S, u64>::load(&dst[i..]) | Vector::<S, u64>::load(&src[i..]))
.store(&mut dst[i..]);
}
for i in whole..dst.len() {
dst[i] |= src[i];
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn or_and_assign_words<S: Simd>(dst: &mut [u64], a: &[u64], b: &[u64]) {
let lanes = u64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
for i in (0..whole).step_by(lanes) {
let cur = Vector::<S, u64>::load(&dst[i..]);
let prod = Vector::<S, u64>::load(&a[i..]) & Vector::<S, u64>::load(&b[i..]);
(cur | prod).store(&mut dst[i..]);
}
for i in whole..dst.len() {
dst[i] |= a[i] & b[i];
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn andnot_words<S: Simd>(dst: &mut [u64], a: &[u64], b: &[u64]) {
let lanes = u64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
for i in (0..whole).step_by(lanes) {
(Vector::<S, u64>::load(&a[i..]) & !Vector::<S, u64>::load(&b[i..])).store(&mut dst[i..]);
}
for i in whole..dst.len() {
dst[i] = a[i] & !b[i];
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn not_words<S: Simd>(dst: &mut [u64], a: &[u64]) {
let lanes = u64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
for i in (0..whole).step_by(lanes) {
(!Vector::<S, u64>::load(&a[i..])).store(&mut dst[i..]);
}
for i in whole..dst.len() {
dst[i] = !a[i];
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn is_all_zero_words<S: Simd>(a: &[u64]) -> bool {
let lanes = u64::lanes::<S>();
let whole = a.len() / lanes * lanes;
let mut acc = 0u64.splat::<S>();
for i in (0..whole).step_by(lanes) {
acc |= Vector::<S, u64>::load(&a[i..]);
}
let mut tail = 0u64;
for &w in &a[whole..] {
tail |= w;
}
if tail != 0 {
return false;
}
let mut buf = [0u64; 16];
debug_assert!(lanes <= buf.len());
acc.store(&mut buf[..lanes]);
buf[..lanes].iter().all(|&w| w == 0)
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn scale_f64<S: Simd>(dst: &mut [f64], factor: f64) {
let lanes = f64::lanes::<S>();
let whole = dst.len() / lanes * lanes;
let f = factor.splat::<S>();
for i in (0..whole).step_by(lanes) {
(Vector::<S, f64>::load(&dst[i..]) * f).store(&mut dst[i..]);
}
for x in &mut dst[whole..] {
*x *= factor;
}
}
#[cfg(not(feature = "bitslice"))]
pub(crate) fn scale_f64(dst: &mut [f64], factor: f64) {
for x in dst {
*x *= factor;
}
}
#[cfg(feature = "bitslice")]
#[with_simd]
pub(crate) fn diff_and_merge_u8<S: Simd>(missing: &mut [u8], encoded: &mut [u8], need: &[u8]) {
let len = need.len();
let lanes = u8::lanes::<S>();
let whole = len / lanes * lanes;
for i in (0..whole).step_by(lanes) {
let n = Vector::<S, u8>::load(&need[i..]);
let e = Vector::<S, u8>::load(&encoded[i..]);
(n & !e).store(&mut missing[i..]);
(e | n).store(&mut encoded[i..]);
}
for i in whole..len {
let n = need[i];
let e = encoded[i];
missing[i] = n & !e;
encoded[i] = e | n;
}
}
#[cfg(not(feature = "bitslice"))]
pub(crate) fn diff_and_merge_u8(missing: &mut [u8], encoded: &mut [u8], need: &[u8]) {
for i in 0..need.len() {
let n = need[i];
let e = encoded[i];
missing[i] = n & !e;
encoded[i] = e | n;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scale_and_diff_merge_match_scalar() {
for len in [0, 1, 3, 7, 8, 15, 16, 31, 32, 65] {
let mut vals: Vec<f64> = (0..len).map(|i| (i as f64) + 1.5).collect();
let expected: Vec<f64> = vals.iter().map(|&x| x * 0.25).collect();
scale_f64(&mut vals, 0.25);
assert_eq!(vals, expected);
let need: Vec<u8> = (0..len).map(|i| (i % 4) as u8).collect();
let mut enc: Vec<u8> = (0..len).map(|i| ((i / 2) % 4) as u8).collect();
let mut miss = vec![0u8; len];
let exp_miss: Vec<u8> = need.iter().zip(&enc).map(|(&n, &e)| n & !e).collect();
let exp_enc: Vec<u8> = need.iter().zip(&enc).map(|(&n, &e)| e | n).collect();
diff_and_merge_u8(&mut miss, &mut enc, &need);
assert_eq!(miss, exp_miss);
assert_eq!(enc, exp_enc);
}
}
#[cfg(feature = "bitslice")]
#[test]
fn word_ops_match_scalar() {
for len in [0, 1, 2, 4, 7, 8, 9, 64] {
let a: Vec<u64> = (0..len)
.map(|i| (i as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15))
.collect();
let b: Vec<u64> = (0..len)
.map(|i| !(i as u64).wrapping_mul(0x517c_c1b7_2722_0a95))
.collect();
let mut dst = vec![0u64; len];
not_words(&mut dst, &a);
assert_eq!(dst, a.iter().map(|&x| !x).collect::<Vec<_>>());
let mut acc = vec![0u64; len];
assert!(is_all_zero_words(&acc));
or_and_assign_words(&mut acc, &a, &b);
let expected: Vec<u64> = a.iter().zip(&b).map(|(&x, &y)| x & y).collect();
assert_eq!(acc, expected);
if expected.iter().any(|&w| w != 0) {
assert!(!is_all_zero_words(&acc));
}
}
}
}