use crate::sat::{Cnf, Lit};
use crate::smt::{Ctx, Op, Term};
pub const MAX_BITS: usize = 64;
pub(crate) fn blast(ctx: &mut Ctx, cnf: &mut Cnf, t: Term, out: &mut [Lit; MAX_BITS]) {
if ctx.charge_walk_depth() {
cnf.mark_overflow();
let f = cnf.false_lit();
out.fill(f);
ctx.release_walk_depth();
return;
}
blast_bounded(ctx, cnf, t, out);
ctx.release_walk_depth();
}
fn blast_bounded(ctx: &mut Ctx, cnf: &mut Cnf, t: Term, out: &mut [Lit; MAX_BITS]) {
if ctx.charge_sat_step() {
cnf.mark_overflow();
let f = cnf.false_lit();
out.fill(f);
return;
}
if ctx.is_stale(t) {
ctx.mark_stale();
let f = cnf.false_lit();
out.fill(f);
return;
}
let n = ctx.peek(t);
let w = n.width as usize;
if w == 0 || w > MAX_BITS {
ctx.mark_ill_formed();
let f = cnf.false_lit();
out.fill(f);
return;
}
match n.op {
Op::Var => {
let idx = n.a;
for (bit, slot) in out[..w].iter_mut().enumerate() {
*slot = cnf.input_bit(idx, bit as u32);
}
}
Op::Const => {
let v = ctx.const_of(t);
for (bit, slot) in out[..w].iter_mut().enumerate() {
*slot = if (v >> bit) & 1 == 1 { cnf.true_lit() } else { cnf.false_lit() };
}
}
Op::Not => {
let mut a = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
for bit in 0..w {
out[bit] = a[bit].not();
}
}
Op::And | Op::Or | Op::Xor => {
let mut a = [cnf.false_lit(); MAX_BITS];
let mut b = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
blast(ctx, cnf, Term(n.b), &mut b);
for bit in 0..w {
out[bit] = match n.op {
Op::And => cnf.and(a[bit], b[bit]),
Op::Or => cnf.or(a[bit], b[bit]),
_ => cnf.xor(a[bit], b[bit]),
};
}
}
Op::Shl | Op::LShr | Op::Shr => {
let mut a = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
let sh = n.b as usize;
let sign = a[w - 1];
let f = cnf.false_lit();
for (bit, slot) in out[..w].iter_mut().enumerate() {
*slot = match n.op {
Op::Shl if bit >= sh => a[bit - sh],
Op::Shl => f,
Op::LShr if sh < w - bit => a[bit + sh],
Op::LShr => f,
_ if sh < w - bit => a[bit + sh],
_ => sign,
};
}
}
Op::Add => {
let mut a = [cnf.false_lit(); MAX_BITS];
let mut b = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
blast(ctx, cnf, Term(n.b), &mut b);
let mut carry = cnf.false_lit();
for bit in 0..w {
let axb = cnf.xor(a[bit], b[bit]);
out[bit] = cnf.xor(axb, carry);
let ab = cnf.and(a[bit], b[bit]);
let cx = cnf.and(carry, axb);
carry = cnf.or(ab, cx);
}
}
Op::Sub => {
let mut a = [cnf.false_lit(); MAX_BITS];
let mut b = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
blast(ctx, cnf, Term(n.b), &mut b);
let mut borrow = cnf.true_lit(); for bit in 0..w {
let nb = b[bit].not();
let axb = cnf.xor(a[bit], nb);
out[bit] = cnf.xor(axb, borrow);
let ab = cnf.and(a[bit], nb);
let cx = cnf.and(borrow, axb);
borrow = cnf.or(ab, cx);
}
}
Op::Mul => {
let mut a = [cnf.false_lit(); MAX_BITS];
let mut b = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
blast(ctx, cnf, Term(n.b), &mut b);
let f = cnf.false_lit();
out[..w].fill(f);
for j in 0..w {
let mut carry = f;
for i in j..w {
let partial = cnf.and(a[i - j], b[j]);
let s1 = cnf.xor(out[i], partial);
let s2 = cnf.xor(s1, carry);
let c1 = cnf.and(out[i], partial);
let c2 = cnf.and(carry, s1);
carry = cnf.or(c1, c2);
out[i] = s2;
}
}
}
Op::Eq => {
let mut a = [cnf.false_lit(); MAX_BITS];
let mut b = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
blast(ctx, cnf, Term(n.b), &mut b);
let mut acc = cnf.true_lit();
for bit in 0..w {
let x = cnf.xor(a[bit], b[bit]);
acc = cnf.and(acc, x.not());
}
out[0] = acc;
}
Op::Ult | Op::Ule => {
let mut a = [cnf.false_lit(); MAX_BITS];
let mut b = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
blast(ctx, cnf, Term(n.b), &mut b);
let mut lt = cnf.false_lit();
let mut eq = cnf.true_lit();
for bit in (0..w).rev() {
let na = a[bit].not();
let nab = cnf.and(na, b[bit]);
let lt_here = cnf.and(eq, nab);
lt = cnf.or(lt, lt_here);
let e = cnf.xor(a[bit], b[bit]).not();
eq = cnf.and(eq, e);
}
out[0] = if n.op == Op::Ule { cnf.or(lt, eq) } else { lt };
}
Op::Ite => {
let mut c = [cnf.false_lit(); MAX_BITS];
let mut t2 = [cnf.false_lit(); MAX_BITS];
let mut e2 = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut c);
blast(ctx, cnf, Term(n.b), &mut t2);
blast(ctx, cnf, Term(n.c), &mut e2);
for bit in 0..w {
out[bit] = cnf.mux(c[0], t2[bit], e2[bit]);
}
}
Op::Trunc => {
let mut a = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, Term(n.a), &mut a);
let src = n.b as usize;
let f = cnf.false_lit();
for (bit, slot) in out[..w].iter_mut().enumerate() {
*slot = if bit < src { a[bit] } else { f };
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sat::{self, Sat};
use crate::smt;
fn mask(w: u8) -> u64 {
if w >= 64 {
u64::MAX
} else {
(1u64 << w) - 1
}
}
fn circuit_can_differ(
w: u8,
res_w: u8,
build: impl Fn(smt::Var, smt::Var) -> smt::Term,
a: u64,
b: u64,
want: u64,
) -> bool {
smt::with_fresh(|| {
let x = smt::var_named(0, w);
let y = smt::var_named(1, w);
let t = build(x, y);
smt::with_ctx(|ctx| {
sat::query(|cnf| {
let mut out = [cnf.false_lit(); MAX_BITS];
blast(ctx, cnf, t, &mut out);
for (idx, val) in [(0u32, a), (1u32, b)] {
for bit in 0..w {
let lit = cnf.input_bit(idx, bit as u32);
let one = (val >> bit) & 1 == 1;
cnf.add_clause(&[if one { lit } else { lit.not() }]);
}
}
let mut diff = cnf.false_lit();
for (bit, lit) in out.iter().enumerate().take(res_w as usize) {
let one = (want >> bit) & 1 == 1;
let mism = if one { lit.not() } else { *lit };
diff = cnf.or(diff, mism);
}
diff
})
})
.0 != Sat::Unsat
})
}
fn word_op(
w: u8,
name: &str,
build: impl Fn(smt::Var, smt::Var) -> smt::Term,
rust: impl Fn(u64, u64) -> u64,
) {
let m = mask(w);
for a in 0..=m {
for b in 0..=m {
let want = rust(a, b) & m;
assert!(
!circuit_can_differ(w, w, &build, a, b, want),
"{name} at {w} bits: the SAT blaster disagrees with Rust on a={a} b={b} (want {want})"
);
}
}
}
fn cmp_op(
w: u8,
name: &str,
build: impl Fn(smt::Var, smt::Var) -> smt::Term,
rust: impl Fn(u64, u64) -> bool,
) {
let m = mask(w);
for a in 0..=m {
for b in 0..=m {
let want = u64::from(rust(a, b));
assert!(
!circuit_can_differ(w, 1, &build, a, b, want),
"{name} at {w} bits: the SAT blaster disagrees with Rust on a={a} b={b} (want {want})"
);
}
}
}
fn sign_extend(v: u64, w: u8) -> i64 {
let v = v & mask(w);
if w < 64 && (v >> (w - 1)) & 1 == 1 {
(v | !mask(w)) as i64
} else {
v as i64
}
}
#[test]
fn the_sat_adder_matches_rust() {
for w in 1..=4 {
word_op(w, "add", |x, y| smt::add(x, y).0, |a, b| a.wrapping_add(b));
}
}
#[test]
fn the_sat_subtractor_matches_rust() {
for w in 1..=4 {
word_op(w, "sub", |x, y| smt::sub(x, y).0, |a, b| a.wrapping_sub(b));
}
}
#[test]
fn the_sat_multiplier_matches_rust() {
for w in 1..=4 {
word_op(w, "mul", |x, y| smt::mul(x, y).0, |a, b| a.wrapping_mul(b));
}
}
#[test]
fn the_sat_bitwise_ops_match_rust() {
for w in 1..=4 {
word_op(w, "and", |x, y| smt::band(x, y).0, |a, b| a & b);
word_op(w, "or", |x, y| smt::bor(x, y).0, |a, b| a | b);
word_op(w, "xor", |x, y| smt::bxor(x, y).0, |a, b| a ^ b);
}
}
#[test]
fn the_sat_comparators_match_rust() {
for w in 1..=4 {
cmp_op(w, "eq", |x, y| smt::eq(x, y).0, |a, b| a == b);
cmp_op(w, "ult", |x, y| smt::ult(x, y).0, |a, b| a < b);
cmp_op(w, "ule", |x, y| smt::ule(x, y).0, |a, b| a <= b);
}
}
#[test]
fn the_sat_shifters_match_rust() {
for w in 1..=4u8 {
for k in 0..w {
word_op(w, "shl", |x, _| smt::shl(x, k as u32).0, |a, _| a << k);
word_op(w, "lshr", |x, _| smt::lshr(x, k as u32).0, |a, _| a >> k);
word_op(
w,
"shr",
|x, _| smt::ashr(x, k as u32).0,
|a, _| (sign_extend(a, w) >> k) as u64,
);
}
}
}
#[test]
fn the_divergence_check_has_teeth() {
let m = mask(4);
let mut found = false;
for a in 0..=m {
for b in 0..=m {
let xor = a ^ b;
let add = a.wrapping_add(b) & m;
if xor != add
&& circuit_can_differ(4, 4, |x, y| smt::bxor(x, y).0, a, b, add)
{
found = true;
}
}
}
assert!(
found,
"the divergence check never reported a difference, so it cannot be detecting any"
);
}
#[test]
fn a_query_past_the_bdd_capacity_returns_instead_of_hanging() {
smt::with_fresh(|| {
let x = smt::var(8);
let y = smt::var(8);
let q = smt::udiv(x, y);
let _ = smt::prove(smt::eq(q, smt::word(1, 8)));
});
smt::with_fresh(|| {
let base = smt::var(8);
let r = crate::mir::intrinsics::eval(
crate::mir::intrinsics::Intrinsic::Pow(crate::mir::intrinsics::Family::Wrapping),
base,
smt::word(7, 32),
8,
false,
8,
);
let Some(crate::mir::intrinsics::Outcome::Value(out)) = r else {
panic!("`wrapping_pow` at a `u8` must be modelled");
};
let _ = smt::prove(smt::eq(out, smt::word(128, 8)));
});
}
}