use super::slices::Read;
use super::{
Counts, Fin, PassKind, Runner, Step, Stop, finish, finish_within, linear, worth_building,
};
use crate::engine::Exhausted;
use crate::engine::budget::Counter;
use crate::expr::{Context, OpCode};
use crate::hash::IdMap;
use crate::ops::{BinOp, CmpOp, UnOp};
use crate::prove::{self, Outcome, Unknown};
use crate::{BitVec, Error, SymbolKey};
const MAX_ATOMS: usize = 3;
const MIN_COUNT_REGION: usize = 6;
const MAX_REGION: u32 = 256;
const LANES: usize = 64;
const QUICK: usize = 16;
const LANES_PER_UNIT: u64 = 8;
pub(super) const MAX_AIG_NODES: usize = 1 << 16;
const PROPAGATIONS: u64 = 1 << 22;
const PROPAGATION_RATE: u64 = 4;
const MAX_CONSTS: usize = 4;
const MIN_JOIN: u16 = 8;
#[derive(Default)]
pub(crate) struct Scratch {
seen: Counts,
stack: Vec<(u32, bool)>,
order: Vec<u32>,
atoms: Vec<u32>,
consts: Vec<u64>,
rows: Vec<u64>,
undecided: IdMap<u32, ()>,
pub(super) unfit: IdMap<u32, ()>,
pub(super) waiting: IdMap<u32, ()>,
pub(super) fits: [(u32, u32); 2],
pub(super) hard: IdMap<u32, ()>,
pub(super) shapes: IdMap<u64, ()>,
pub(super) uncut: IdMap<u32, ()>,
}
fn interior(cx: &Context, i: u32) -> bool {
let n = cx.node(i);
match n.op {
OpCode::And | OpCode::Or | OpCode::Xor | OpCode::Not => true,
OpCode::Shl => cx.is_const(n.b),
_ => false,
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
enum Mode {
Bitwise,
Slices,
Counts,
}
fn counted(cx: &Context, i: u32) -> bool {
let n = cx.node(i);
match n.op {
OpCode::Shl | OpCode::LShr | OpCode::AShr => cx.is_const(n.b) && cx.wid(i) <= 64,
OpCode::And
| OpCode::Or
| OpCode::Xor
| OpCode::Not
| OpCode::Add
| OpCode::Sub
| OpCode::Neg
| OpCode::Mul => cx.wid(i) <= 64,
_ => false,
}
}
fn sliced(cx: &Context, i: u32) -> bool {
let n = cx.node(i);
let small = |j: u32| cx.wid(j) <= 64;
match n.op {
OpCode::Add
| OpCode::Sub
| OpCode::Neg
| OpCode::Concat
| OpCode::Extract
| OpCode::Zext
| OpCode::Sext => small(i) && small(n.a),
OpCode::Ult => small(n.a),
OpCode::LShr => small(i) && cx.is_const(n.b),
_ => interior(cx, i),
}
}
fn walk(cx: &Context, n: u32, s: &mut Scratch, mode: Mode) -> (bool, bool, u32) {
let slices = mode == Mode::Slices;
let joined = slices && cx.node(n).op != OpCode::Concat;
s.seen.begin(cx.len());
s.order.clear();
s.atoms.clear();
s.consts.clear();
s.stack.clear();
s.stack.push((n, false));
let mut reached = 0u32;
let (mut carried, mut bitwise) = (false, false);
let (mut sliced_any, mut added, mut compared) = (false, false, false);
let (mut multiplied, mut lowered) = (false, false);
let w = cx.wid(n);
while let Some((i, expanded)) = s.stack.pop() {
if expanded {
s.order.push(i);
continue;
}
if s.seen.get(i).is_some() {
continue;
}
s.seen.set(i, u32::MAX);
reached += 1;
if reached > MAX_REGION {
return (false, false, reached);
}
if let Some(v) = cx.const_val(i) {
if !v.is_zero() && !v.is_ones() {
s.consts.push(v.limbs()[0]);
}
continue;
}
if !(match mode {
Mode::Slices => sliced(cx, i),
Mode::Counts => counted(cx, i),
Mode::Bitwise => interior(cx, i),
}) {
s.atoms.push(i);
let most = if mode == Mode::Counts { 1 } else { MAX_ATOMS };
if s.atoms.len() > most
|| (slices && cx.wid(i) > 64)
|| (mode == Mode::Counts && cx.wid(i) != w)
{
return (false, false, reached);
}
continue;
}
if s.undecided.contains_key(&i) {
return (false, false, reached);
}
s.stack.push((i, true));
let node = cx.node(i);
if mode == Mode::Counts {
multiplied |= node.op == OpCode::Mul;
lowered |= matches!(node.op, OpCode::LShr | OpCode::AShr);
added |= matches!(node.op, OpCode::Add | OpCode::Sub);
if matches!(node.op, OpCode::Shl | OpCode::LShr | OpCode::AShr) {
s.stack.push((node.a, false));
} else {
for c in node.children() {
s.stack.push((c, false));
}
}
} else if slices {
sliced_any |= matches!(node.op, OpCode::Extract | OpCode::LShr);
added |= matches!(node.op, OpCode::Add | OpCode::Sub | OpCode::Ult);
compared |= node.op == OpCode::Ult;
for c in node.children() {
s.stack.push((c, false));
}
} else if node.op == OpCode::Shl {
s.stack.push((node.a, false));
} else {
bitwise = true;
for c in node.children() {
carried |= cx.node(c).op == OpCode::Shl && interior(cx, c);
s.stack.push((c, false));
}
}
}
s.consts.sort_unstable();
s.consts.dedup();
s.consts.truncate(MAX_CONSTS);
if slices {
let fits = sliced_any && added && (compared || !joined) && !s.atoms.is_empty();
return (fits, false, reached);
}
if mode == Mode::Counts {
let fits = s.atoms.len() == 1
&& s.order.len() >= MIN_COUNT_REGION
&& (multiplied || (lowered && added));
return (fits, false, reached);
}
(
(carried || !bitwise) && !s.atoms.is_empty(),
!bitwise,
reached,
)
}
fn mix(z: u64) -> u64 {
let mut z = z.wrapping_add(0x9e37_79b9_7f4a_7c15);
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
z ^ (z >> 31)
}
fn lane_value(t: usize, j: usize, k: usize, mask: u64, consts: &[u64]) -> u64 {
if k <= t {
return u64::from(k == j + 1);
}
let m = k - t - 1;
let sign = (mask >> 1) + 1;
let boundary = [0, 1, mask, sign, mask >> 1];
let grid = if t == 1 { 5 } else { 25 };
if m < grid {
let digit = match j {
0 => m % 5,
1 => m / 5,
_ => (m % 5 + m / 5) % 5,
};
return boundary[digit];
}
let q = m - grid;
if q < 4 * consts.len() {
let c = consts[q / 4];
let v = [c, c.wrapping_neg(), c.wrapping_add(1), !c][q % 4] & mask;
return if j == q % t { v } else { 0 };
}
let seed = (k as u64) << 8 | j as u64;
let (a, b) = (mix(seed), mix(seed ^ 0x5555_0000));
(match m % 3 {
0 => a,
1 => a & b,
_ => a | b,
}) & mask
}
fn evaluate(cx: &Context, s: &mut Scratch, lanes: core::ops::Range<usize>) {
let t = s.atoms.len();
if lanes.start == 0 {
s.rows.clear();
s.rows.resize((t + s.order.len()) * LANES, 0);
for (j, &a) in s.atoms.iter().enumerate() {
s.seen.set(a, j as u32);
}
for (row, &i) in s.order.iter().enumerate() {
s.seen.set(i, (t + row) as u32);
}
}
for j in 0..t {
let own = u64::MAX >> (64 - u64::from(cx.wid(s.atoms[j])));
for k in lanes.clone() {
s.rows[j * LANES + k] = lane_value(t, j, k, own, &s.consts);
}
}
for (row, &i) in s.order.iter().enumerate() {
let row = t + row;
let n = cx.node(i);
let bits = u64::from(n.width);
let mask = u64::MAX >> (64 - bits);
let (done, rest) = s.rows.split_at_mut(row * LANES);
let out = &mut rest[lanes.clone()];
let operand = |x: u32| -> Result<&[u64], u64> {
match cx.const_val(x) {
Some(v) => Err(v.limbs()[0]),
None => {
let r = s.seen.get(x).unwrap_or(0) as usize * LANES;
Ok(&done[r + lanes.start..r + lanes.end])
}
}
};
match n.op {
OpCode::Not => match operand(n.a) {
Ok(a) => out.iter_mut().zip(a).for_each(|(o, &x)| *o = !x & mask),
Err(c) => out.fill(!c & mask),
},
OpCode::Shl => {
let c = cx.const_val(n.b).map_or(bits, |v| v.limbs()[0]);
match operand(n.a) {
Ok(a) if c < bits => out
.iter_mut()
.zip(a)
.for_each(|(o, &x)| *o = (x << c) & mask),
Err(x) if c < bits => out.fill((x << c) & mask),
_ => out.fill(0),
}
}
OpCode::Add
| OpCode::Sub
| OpCode::Neg
| OpCode::Mul
| OpCode::LShr
| OpCode::AShr
| OpCode::Concat
| OpCode::Extract
| OpCode::Zext
| OpCode::Sext
| OpCode::Ult => {
let at = |x: u32, k: usize| match operand(x) {
Ok(a) => a[k - lanes.start],
Err(c) => c,
};
let wa = u64::from(cx.wid(n.a));
for k in lanes.clone() {
let x = at(n.a, k);
let v = match n.op {
OpCode::Add => x.wrapping_add(at(n.b, k)),
OpCode::Sub => x.wrapping_sub(at(n.b, k)),
OpCode::Neg => x.wrapping_neg(),
OpCode::Mul => x.wrapping_mul(at(n.b, k)),
OpCode::LShr => {
let c = at(n.b, k);
if c >= bits { 0 } else { x >> c }
}
OpCode::AShr => {
let signed = ((x << (64 - bits)) as i64) >> (64 - bits);
(signed >> at(n.b, k).min(bits - 1)) as u64
}
OpCode::Concat => (x << cx.wid(n.b)) | at(n.b, k),
OpCode::Extract => x >> n.b,
OpCode::Sext if x >> (wa - 1) & 1 == 1 => x | !(u64::MAX >> (64 - wa)),
OpCode::Ult => u64::from(x < at(n.b, k)),
_ => x,
};
out[k - lanes.start] = v & mask;
}
}
op => {
let f: fn(u64, u64) -> u64 = match op {
OpCode::And => |x, y| x & y,
OpCode::Or => |x, y| x | y,
_ => |x, y| x ^ y,
};
match (operand(n.a), operand(n.b)) {
(Ok(a), Ok(b)) => out
.iter_mut()
.zip(a.iter().zip(b))
.for_each(|(o, (&x, &y))| *o = f(x, y)),
(Ok(a), Err(c)) | (Err(c), Ok(a)) => {
out.iter_mut().zip(a).for_each(|(o, &x)| *o = f(x, c));
}
(Err(x), Err(y)) => out.fill(f(x, y) & mask),
}
}
}
}
}
fn count<'x>(r: &'x mut Runner<'_, '_>) -> &'x mut crate::engine::PassCounts {
r.stats.passes.entry(PassKind::Carries.name()).or_default()
}
enum Verdict {
Proved,
No,
Unpaid,
}
fn resized(cx: &mut Context, a: u32, to: u16) -> Result<u32, Error> {
match cx.wid(a) {
w if w > to => cx.c_extract(a, 0, to),
w if w < to => cx.c_zext(a, to),
_ => Ok(a),
}
}
#[derive(Clone, Copy)]
enum Target<'a> {
Affine(u64, &'a [u64]),
Count(UnOp),
}
fn copy(
cx: &Context,
s: &Scratch,
scx: &mut Context,
w: crate::Width,
target: Target<'_>,
) -> Result<(u32, u32), Error> {
let mut map: Vec<u32> = Vec::with_capacity(s.atoms.len() + s.order.len());
for (j, &a) in s.atoms.iter().enumerate() {
let e = scx.symbol(SymbolKey::from(j as u64), cx.width_of(a))?;
map.push(scx.id(e)?);
}
let operand = |scx: &mut Context, map: &[u32], x: u32| -> Result<u32, Error> {
match cx.const_val(x) {
Some(v) => scx.mk_const(&v),
None => Ok(map[s.seen.get(x).unwrap_or(0) as usize]),
}
};
for &i in &s.order {
let node = cx.node(i);
let a = operand(scx, &map, node.a)?;
let e = match node.op {
OpCode::Not => scx.c_un(UnOp::Not, a)?,
OpCode::Neg => scx.c_un(UnOp::Neg, a)?,
OpCode::Extract => scx.c_extract(a, node.b as u16, node.width)?,
OpCode::Zext => scx.c_zext(a, node.width)?,
OpCode::Sext => scx.c_sext(a, node.width)?,
op => {
let b = operand(scx, &map, node.b)?;
match op {
OpCode::Concat => scx.c_concat(a, b)?,
OpCode::Ult => scx.c_cmp(CmpOp::Ult, a, b)?,
op => {
let bop = match op {
OpCode::And => BinOp::And,
OpCode::Or => BinOp::Or,
OpCode::Xor => BinOp::Xor,
OpCode::Add => BinOp::Add,
OpCode::Sub => BinOp::Sub,
OpCode::Mul => BinOp::Mul,
OpCode::LShr => BinOp::LShr,
OpCode::AShr => BinOp::AShr,
_ => BinOp::Shl,
};
scx.c_bin(bop, a, b)?
}
}
}
};
map.push(e);
}
let lhs = *map.last().unwrap_or(&0);
let rhs = match target {
Target::Count(op) => scx.c_un(op, map[0])?,
Target::Affine(konst, coeffs) => {
let mut rhs = scx.mk_const(&BitVec::wrapping_from_u64(w, konst))?;
for (j, &k) in coeffs.iter().enumerate() {
if k != 0 {
let k = scx.mk_const(&BitVec::wrapping_from_u64(w, k))?;
let x = resized(scx, map[j], w.bits())?;
let term = scx.c_bin(BinOp::Mul, x, k)?;
rhs = scx.c_bin(BinOp::Add, rhs, term)?;
}
}
rhs
}
};
Ok((lhs, rhs))
}
fn proved(
r: &mut Runner<'_, '_>,
cx: &Context,
n: u32,
target: Target<'_>,
) -> Result<Verdict, Stop> {
let left = r.meter.left_in(|b| b.pass_work);
let propagations =
PROPAGATIONS.min(left.saturating_sub(MAX_AIG_NODES as u64) / PROPAGATION_RATE);
if propagations == 0 {
return Ok(Verdict::Unpaid);
}
count(r).proofs += 1;
let w = cx.width_of(n);
let mut scx = Context::new();
let (lhs, rhs) = copy(cx, &r.carries, &mut scx, w, target).map_err(Stop::Error)?;
let cfg = prove::Config::default()
.with_simplify(false)
.with_certificate(false)
.with_samples(0)
.with_max_nodes(MAX_AIG_NODES)
.with_max_conflicts(u64::MAX)
.with_max_propagations(propagations);
let (lhs, rhs) = (scx.handle(lhs), scx.handle(rhs));
let mut q = prove::Question::equal(&mut scx, lhs, rhs, &cfg).map_err(Stop::Error)?;
let out = q.solve(&mut scx, cfg.limits()).map_err(Stop::Error)?;
let stats = q.stats();
let nodes = match &out {
Outcome::Unknown(Unknown::TooLarge { nodes }) => *nodes,
_ => stats.nodes,
};
let cost = (nodes as u64)
.saturating_add(stats.propagations.saturating_mul(PROPAGATION_RATE))
.min(left);
r.meter.charge(Counter::PassWork, cost)?;
let c = count(r);
Ok(match out {
Outcome::Proved(_) => {
c.proved += 1;
Verdict::Proved
}
Outcome::Refuted(_) => {
c.refuted += 1;
Verdict::No
}
Outcome::Unknown(Unknown::Budget { .. }) if propagations < PROPAGATIONS => {
c.unknown += 1;
Verdict::Unpaid
}
Outcome::Unknown(_) => {
c.unknown += 1;
r.carries.undecided.insert(n, ());
Verdict::No
}
})
}
fn fit(r: &mut Runner<'_, '_>, cx: &Context, mask: u64) -> Result<Option<(u64, Vec<u64>)>, Stop> {
let t = r.carries.atoms.len();
let rows = (t + r.carries.order.len()) as u64;
r.meter.charge(
Counter::PassWork,
(rows * QUICK as u64).div_ceil(LANES_PER_UNIT),
)?;
evaluate(cx, &mut r.carries, 0..QUICK);
let s = &r.carries;
let f = (t + s.order.len() - 1) * LANES;
let konst = s.rows[f];
let coeffs: Vec<u64> = (0..t)
.map(|j| s.rows[f + 1 + j].wrapping_sub(konst) & mask)
.collect();
let affine = |s: &Scratch, mut lanes: core::ops::Range<usize>| {
lanes.all(|k| {
let v = coeffs.iter().enumerate().fold(konst, |acc, (j, &a)| {
acc.wrapping_add(a.wrapping_mul(s.rows[j * LANES + k]))
});
v & mask == s.rows[f + k]
})
};
if !affine(s, t + 1..QUICK) {
return Ok(None);
}
r.meter.charge(
Counter::PassWork,
(rows * (LANES - QUICK) as u64).div_ceil(LANES_PER_UNIT),
)?;
evaluate(cx, &mut r.carries, QUICK..LANES);
Ok(affine(&r.carries, QUICK..LANES).then_some((konst, coeffs)))
}
fn fit_count(r: &mut Runner<'_, '_>, cx: &Context, bits: u16) -> Result<Option<UnOp>, Stop> {
let mask = u64::MAX >> (64 - u32::from(bits));
let rows = (1 + r.carries.order.len()) as u64;
let w = u32::from(bits);
let count = |op: UnOp, v: u64| -> u64 {
match op {
UnOp::Popcnt => u64::from(v.count_ones()),
UnOp::Ctz if v == 0 => u64::from(w),
UnOp::Ctz => u64::from(v.trailing_zeros()),
UnOp::Clz if v == 0 => u64::from(w),
_ => u64::from(v.leading_zeros() - (64 - w)),
}
};
let agree = |s: &Scratch, op: UnOp, lanes: core::ops::Range<usize>| {
let f = s.order.len() * LANES;
lanes
.into_iter()
.all(|k| count(op, s.rows[k] & mask) == s.rows[f + k] & mask)
};
r.meter.charge(
Counter::PassWork,
(rows * QUICK as u64).div_ceil(LANES_PER_UNIT),
)?;
evaluate(cx, &mut r.carries, 0..QUICK);
let Some(op) = [UnOp::Popcnt, UnOp::Ctz, UnOp::Clz]
.into_iter()
.find(|&op| agree(&r.carries, op, 0..QUICK))
else {
return Ok(None);
};
r.meter.charge(
Counter::PassWork,
(rows * (LANES - QUICK) as u64).div_ceil(LANES_PER_UNIT),
)?;
evaluate(cx, &mut r.carries, QUICK..LANES);
Ok(agree(&r.carries, op, QUICK..LANES).then_some(op))
}
fn step_count(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Step, Stop> {
let bits = cx.wid(n);
let (fits, _, reached) = walk(cx, n, &mut r.carries, Mode::Counts);
r.meter.charge(Counter::PassWork, u64::from(reached))?;
if !fits {
return Ok(Step::Normal(Fin::FINAL));
}
let Some(op) = fit_count(r, cx, bits)? else {
return Ok(Step::Normal(Fin::FINAL));
};
let atoms = r.carries.atoms.clone();
if let Some(fin) = worth_building(r, cx, n, &atoms, 1)? {
count(r).rejected_cost += 1;
return Ok(Step::Normal(fin));
}
match proved(r, cx, n, Target::Count(op))? {
Verdict::Proved => {}
Verdict::No => return Ok(Step::Normal(Fin::FINAL)),
Verdict::Unpaid => return Ok(Step::Normal(Fin::capped(Exhausted::PassWork))),
}
let before = cx.len() as u32;
let x = atoms[0];
let e = r.build(cx, |cx| cx.c_un(op, x))?;
finish(r, cx, PassKind::Carries, n, e, before, &atoms, Fin::FINAL)
}
fn ends_count(cx: &Context, n: u32) -> bool {
let node = cx.node(n);
matches!(node.op, OpCode::LShr | OpCode::And) && cx.is_const(node.b)
}
fn low_read(cx: &Context, n: u32) -> Option<(u32, u16, u16)> {
let node = cx.node(n);
let (s, j, k) = match node.op {
OpCode::Extract if node.b == 0 => (node.a, node.width, 0),
OpCode::And if node.width <= 64 => {
let (s, c) = match (cx.const_val(node.a), cx.const_val(node.b)) {
(Some(c), None) => (node.b, c.limbs()[0]),
(None, Some(c)) => (node.a, c.limbs()[0]),
_ => return None,
};
let j = c.trailing_ones() as u16;
if j == 0 || j >= node.width || c >> j != 0 {
return None;
}
(s, j, 0)
}
OpCode::Shl if node.width <= 64 => {
let k = cx.const_val(node.b)?.limbs()[0];
if k == 0 || k >= u64::from(node.width) {
return None;
}
(node.a, node.width - k as u16, k as u16)
}
_ => return None,
};
(cx.wid(s) <= 64 && matches!(cx.node(s).op, OpCode::Add | OpCode::Sub)).then_some((s, j, k))
}
pub(super) fn slack(cx: &Context, n: u32, e: u32, before: u32) -> u32 {
let mut outer = cx.node(e);
if outer.op == OpCode::Shl && cx.const_val(outer.b).is_some() {
outer = cx.node(outer.a);
}
let slice = if outer.op == OpCode::Zext {
cx.node(outer.a)
} else {
outer
};
if slice.op == OpCode::Extract && slice.a < before {
return 2;
}
let tree = |i: u32| cx.meta[i as usize].tree;
u32::from(tree(n) != u32::MAX && tree(e) < tree(n))
}
pub(super) fn step(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Step, Stop> {
let bits = cx.wid(n);
let op = cx.node(n).op;
if let Some((s, j, k)) = low_read(cx, n) {
let before = cx.len() as u32;
let mut own = Fin::FINAL;
if let Some((e, fin, stops, _)) = super::borrows::expanded(r, cx, s, j, false)? {
let e = r.build(cx, |cx| super::slices::held(cx, e, bits, k))?;
let slack = slack(cx, n, e, before);
match finish_within(r, cx, PassKind::Carries, n, e, before, &stops, fin, slack)? {
Step::Normal(f) => own = f,
step => return Ok(step),
}
}
let read = Read {
n,
s,
lo: 0,
len: j,
k,
apart: false,
};
match super::slices::fitted(r, cx, read)? {
Ok(step) => return Ok(step),
Err(f) => own = own.and(f),
}
if !own.done {
return Ok(Step::Normal(own));
}
}
if op == OpCode::Extract {
let node = cx.node(n);
let s = node.a;
if node.b > 0 && cx.wid(s) <= 64 && matches!(cx.node(s).op, OpCode::Add | OpCode::Sub) {
let read = Read {
n,
s,
lo: node.b as u16,
len: bits,
k: 0,
apart: true,
};
return Ok(match super::slices::fitted(r, cx, read)? {
Ok(step) => step,
Err(f) => Step::Normal(f),
});
}
}
let slices = op == OpCode::Concat;
let joined = matches!(op, OpCode::Or | OpCode::Xor | OpCode::Add | OpCode::Sub);
if bits > 64 {
return Ok(Step::Normal(Fin::FINAL));
}
if !(slices || joined || interior(cx, n)) {
if ends_count(cx, n) {
return step_count(r, cx, n);
}
return Ok(Step::Normal(Fin::FINAL));
}
let mask = u64::MAX >> (64 - bits);
let mut found = None;
if slices || interior(cx, n) {
let mode = if slices { Mode::Slices } else { Mode::Bitwise };
let (fits, chain, reached) = walk(cx, n, &mut r.carries, mode);
r.meter.charge(Counter::PassWork, u64::from(reached))?;
if fits {
r.carries.atoms.sort_by(|a, b| cx.order(*a, *b));
if chain {
let k = r.carries.order.iter().fold(0u64, |k, &i| {
let c = cx.const_val(cx.node(i).b);
k.saturating_add(c.map_or(u64::from(bits), |v| v.limbs()[0]))
});
if r.carries.order.len() == 1 && (1..u64::from(bits)).contains(&k) {
return Ok(Step::Normal(Fin::FINAL));
}
let coeff = if k < u64::from(bits) { 1u64 << k } else { 0 };
found = Some((0, vec![coeff], true));
} else {
found = fit(r, cx, mask)?.map(|(konst, coeffs)| (konst, coeffs, false));
}
}
}
if found.is_none() && joined {
let (fits, _, reached) = walk(cx, n, &mut r.carries, Mode::Slices);
r.meter.charge(Counter::PassWork, u64::from(reached))?;
if fits {
r.carries.atoms.sort_by(|a, b| cx.order(*a, *b));
found = fit(r, cx, mask)?.map(|(konst, coeffs)| (konst, coeffs, false));
}
}
let Some((konst, coeffs, exact)) = found else {
let mut own = Fin::FINAL;
if matches!(op, OpCode::Add | OpCode::Sub) {
let before = cx.len() as u32;
if let Some((e, fin, stops, _)) = super::borrows::expanded(r, cx, n, bits, false)? {
let slack = slack(cx, n, e, before);
match finish_within(r, cx, PassKind::Carries, n, e, before, &stops, fin, slack)? {
Step::Normal(f) => own = f,
step => return Ok(step),
}
}
} else if bits < MIN_JOIN || !matches!(op, OpCode::Or | OpCode::Concat) {
if ends_count(cx, n) {
return step_count(r, cx, n);
}
return Ok(Step::Normal(Fin::FINAL));
}
let (known, fk) = super::known_words(r, cx, n)?;
let open = known.map_or(mask, |[zero, _]| !zero & mask);
if open == 0 {
return Ok(Step::Normal(own.and(fk)));
}
let read = Read {
n,
s: n,
lo: 0,
len: (64 - open.leading_zeros()) as u16,
k: 0,
apart: !matches!(op, OpCode::Add | OpCode::Sub),
};
return Ok(match super::slices::fitted(r, cx, read)? {
Ok(Step::To(e, f)) => Step::To(e, f.and(fk)),
Ok(Step::Normal(f)) => Step::Normal(f.and(fk)),
Err(f) => Step::Normal(own.and(f).and(fk)),
});
};
let w = cx.width_of(n);
let atoms = r.carries.atoms.clone();
let before = cx.len() as u32;
let mut terms: Vec<(u32, BitVec)> = Vec::with_capacity(atoms.len());
for (&a, &k) in atoms.iter().zip(&coeffs) {
if k != 0 {
let x = r.build(cx, |cx| resized(cx, a, bits))?;
terms.push((x, BitVec::wrapping_from_u64(w, k)));
}
}
let form = linear::Form::of(BitVec::wrapping_from_u64(w, konst), &terms);
if let Some(fin) = worth_building(r, cx, n, &atoms, linear::estimate(&form))? {
count(r).rejected_cost += 1;
return Ok(Step::Normal(fin));
}
if !exact {
match proved(r, cx, n, Target::Affine(konst, &coeffs))? {
Verdict::Proved => {}
Verdict::No => return Ok(Step::Normal(Fin::FINAL)),
Verdict::Unpaid => return Ok(Step::Normal(Fin::capped(Exhausted::PassWork))),
}
}
let e = linear::emit(r, cx, &form)?;
r.explain_with(|| crate::engine::Explanation::Carries {
konst: BitVec::wrapping_from_u64(w, konst),
terms: terms.iter().map(|&(a, k)| (cx.handle(a), k)).collect(),
proved: !exact,
});
finish(r, cx, PassKind::Carries, n, e, before, &atoms, Fin::FINAL)
}