use std::sync::Arc;
use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};
pub const OP_ID: &str = "vyre-primitives::bitset::equal";
#[must_use]
pub fn bitset_equal(lhs: &str, rhs: &str, out_scalar: &str, words: u32) -> Program {
let t = Expr::InvocationId { axis: 0 };
let body = vec![
Node::if_then(
Expr::lt(t.clone(), Expr::u32(words)),
vec![Node::let_bind(
"_diff",
Expr::atomic_or(
out_scalar,
Expr::u32(0),
Expr::ne(Expr::load(lhs, t.clone()), Expr::load(rhs, t.clone())),
),
)],
),
Node::if_then(
Expr::eq(t.clone(), Expr::u32(0)),
vec![Node::store(
out_scalar,
Expr::u32(0),
Expr::eq(Expr::load(out_scalar, Expr::u32(0)), Expr::u32(0)),
)],
),
];
Program::wrapped(
vec![
BufferDecl::storage(lhs, 0, BufferAccess::ReadOnly, DataType::U32).with_count(words),
BufferDecl::storage(rhs, 1, BufferAccess::ReadOnly, DataType::U32).with_count(words),
BufferDecl::storage(out_scalar, 2, BufferAccess::ReadWrite, DataType::U32)
.with_count(1),
],
[256, 1, 1],
vec![Node::Region {
generator: Ident::from(OP_ID),
source_region: None,
body: Arc::new(body),
}],
)
}
#[must_use]
pub fn cpu_ref(lhs: &[u32], rhs: &[u32]) -> u32 {
if lhs.len() != rhs.len() {
return 0;
}
if lhs.iter().zip(rhs.iter()).all(|(a, b)| a == b) {
1
} else {
0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identical_returns_one() {
assert_eq!(cpu_ref(&[0xDEAD, 0xBEEF], &[0xDEAD, 0xBEEF]), 1);
}
#[test]
fn differs_in_first_word_returns_zero() {
assert_eq!(cpu_ref(&[0xDEAD, 0xBEEF], &[0xDEAE, 0xBEEF]), 0);
}
#[test]
fn differs_in_last_word_returns_zero() {
assert_eq!(cpu_ref(&[0, 0, 1], &[0, 0, 0]), 0);
}
#[test]
fn empty_pair_returns_one() {
assert_eq!(cpu_ref(&[], &[]), 1);
}
#[test]
fn length_mismatch_returns_zero() {
assert_eq!(cpu_ref(&[0], &[0, 0]), 0);
}
}