use super::forms::Parts;
use super::limbs::same_low;
use super::{Fin, Runner, Stop, casts, discard, linear, linear_mba};
use crate::engine::budget::Counter;
use crate::expr::{Context, OpCode};
use crate::ops::{BinOp, UnOp};
use crate::{BitVec, Width};
const MAX_TERMS: usize = 24;
fn readings(cx: &Context, x: u32) -> Vec<(u32, u16)> {
let (mut p, mut k) = (x, cx.wid(x));
let mut out = vec![(p, k)];
loop {
let n = cx.node(p);
let (below, bits) = match n.op {
OpCode::Extract if n.b == 0 => (n.a, n.width),
OpCode::Zext => (n.a, cx.wid(n.a)),
OpCode::And if n.width <= 64 => {
let (v, c) = match (cx.const_val(n.a), cx.const_val(n.b)) {
(Some(c), None) => (n.b, c.limbs()[0]),
(None, Some(c)) => (n.a, c.limbs()[0]),
_ => break,
};
let m = c.trailing_ones() as u16;
if m == 0 || m >= n.width || c >> m != 0 {
break;
}
(v, m)
}
_ => break,
};
if cx.wid(below) > 64 {
break;
}
p = below;
k = k.min(bits);
out.push((p, k));
}
out
}
fn borrow(cx: &Context, a: u32) -> Option<(Option<u32>, u32, bool)> {
let n = cx.node(a);
if !matches!(n.op, OpCode::Zext | OpCode::Sext) || n.width < 2 {
return None;
}
let c = cx.node(n.a);
let minus = n.op == OpCode::Sext;
match c.op {
OpCode::Ult => Some((Some(c.a), c.b, minus)),
OpCode::Ne if cx.const_val(c.b).is_some_and(|k| k.is_zero()) => Some((None, c.a, minus)),
OpCode::Ne if cx.const_val(c.a).is_some_and(|k| k.is_zero()) => Some((None, c.b, minus)),
OpCode::Ne => {
let (x, k) = match (cx.const_val(c.a), cx.const_val(c.b)) {
(None, Some(_)) => (c.a, c.b),
(Some(_), None) => (c.b, c.a),
_ => return None,
};
if cx.wid(x) > 64 {
return None;
}
let bits = readings(cx, x).last()?.1;
let ones = u64::MAX >> (64 - bits);
(cx.const_val(k)?.limbs()[0] == ones).then_some((Some(x), k, minus))
}
_ => None,
}
}
const MAX_LIFT: u32 = 24;
fn lifted(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
w: u16,
seen: &mut Vec<(u32, u32)>,
) -> Result<u32, Stop> {
if let Some(&(_, v)) = seen.iter().find(|s| s.0 == x) {
return Ok(v);
}
let n = cx.node(x);
let bin = match n.op {
OpCode::Add => Some(BinOp::Add),
OpCode::Sub => Some(BinOp::Sub),
OpCode::Mul => Some(BinOp::Mul),
OpCode::And => Some(BinOp::And),
OpCode::Or => Some(BinOp::Or),
OpCode::Xor => Some(BinOp::Xor),
_ => None,
};
let un = match n.op {
OpCode::Neg => Some(UnOp::Neg),
OpCode::Not => Some(UnOp::Not),
_ => None,
};
let room = (seen.len() as u32) < MAX_LIFT;
let v = match n.op {
OpCode::Extract if n.b == 0 && cx.wid(n.a) >= w => {
r.build(cx, |cx| cx.c_extract(n.a, 0, w))?
}
_ => match (bin, un) {
(Some(op), _) if room => {
let a = lifted(r, cx, n.a, w, seen)?;
let b = lifted(r, cx, n.b, w, seen)?;
r.build(cx, |cx| cx.c_bin(op, a, b))?
}
(_, Some(op)) if room => {
let a = lifted(r, cx, n.a, w, seen)?;
r.build(cx, |cx| cx.c_un(op, a))?
}
_ => r.build(cx, |cx| cx.c_zext(x, w))?,
},
};
seen.push((x, v));
Ok(v)
}
#[derive(Clone, Copy)]
struct Digit {
of: u32,
small: bool,
}
fn lift_width(cx: &Context, x: u32) -> u16 {
let mut stack = vec![x];
let (mut seen, mut w) = (0, 0);
while let Some(i) = stack.pop() {
seen += 1;
if seen > MAX_LIFT {
break;
}
let n = cx.node(i);
match n.op {
OpCode::Extract if n.b == 0 => {
let s = cx.wid(n.a);
if s <= 64 {
w = w.max(s);
}
}
OpCode::Add
| OpCode::Sub
| OpCode::Mul
| OpCode::And
| OpCode::Or
| OpCode::Xor
| OpCode::Neg
| OpCode::Not => stack.extend(n.children()),
_ => {}
}
}
w
}
fn below(r: &mut Runner<'_, '_>, cx: &mut Context, v: u32, k: u16) -> Result<(bool, Fin), Stop> {
let w = cx.wid(v);
if k >= w {
return Ok((true, Fin::FINAL));
}
let (known, fin) = super::known_words(r, cx, v)?;
let high = (u64::MAX >> (64 - w)) & !((1u64 << k) - 1);
Ok((known.is_some_and(|[zero, _]| zero & high == high), fin))
}
#[allow(clippy::too_many_arguments)]
fn side(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
p: u32,
kp: u16,
k: u16,
w: u16,
fin: &mut Fin,
) -> Result<Option<Digit>, Stop> {
let wp = cx.wid(p);
if kp > k {
let (fits, f) = below(r, cx, x, k)?;
if !fits {
return Ok(None);
}
*fin = fin.and(f);
} else if kp < k {
if wp != kp || wp > w {
return Ok(None);
}
let of = r.build(cx, |cx| cx.c_zext(p, w))?;
return Ok(Some(Digit { of, small: true }));
}
let of = match wp.cmp(&w) {
core::cmp::Ordering::Less => lifted(r, cx, p, w, &mut Vec::new())?,
core::cmp::Ordering::Equal => p,
core::cmp::Ordering::Greater => r.build(cx, |cx| cx.c_extract(p, 0, w))?,
};
let (small, f) = below(r, cx, of, k)?;
if small {
*fin = fin.and(f);
}
Ok(Some(Digit { of, small }))
}
#[derive(Clone, Copy)]
struct Anchor {
low: u32,
of: u32,
k: u16,
fin: Fin,
}
const MAX_ANCHORS: usize = 4;
fn anchors(
r: &mut Runner<'_, '_>,
cx: &mut Context,
terms: &[(u32, BitVec)],
) -> Result<Vec<Anchor>, Stop> {
let mut out: Vec<Anchor> = Vec::new();
let mut words: Vec<(u32, u16)> = Vec::new();
for &(a, _) in terms {
let n = cx.node(a);
let s = if n.op == OpCode::Zext {
cx.node(n.a)
} else {
n
};
let (of, k) = match s.op {
OpCode::Extract if s.b > 0 => (s.a, u64::from(s.b)),
OpCode::LShr => match cx.const_val(s.b).and_then(|k| k.to_u64()) {
Some(k) => (s.a, k),
None => continue,
},
_ => continue,
};
let w = cx.wid(of);
if w > 64 || k == 0 || k >= u64::from(w) || words.contains(&(of, k as u16)) {
continue;
}
if words.len() == MAX_ANCHORS {
break;
}
let k = k as u16;
words.push((of, k));
if let Some((low, fin)) = casts::narrowed(r, cx, of, 0, k)? {
out.push(Anchor { low, of, k, fin });
}
if let Some((low, fin)) = casts::narrowed_live(r, cx, of, 0, k)?
&& out
.last()
.is_none_or(|a| a.of != of || a.k != k || a.low != low)
{
out.push(Anchor { low, of, k, fin });
}
}
Ok(out)
}
fn holds(cx: &Context, x: u32, lows: &[u32]) -> bool {
let mut stack = vec![x];
let mut seen = 0;
while let Some(i) = stack.pop() {
if lows.contains(&i) {
return true;
}
seen += 1;
if seen > MAX_LIFT {
break;
}
let n = cx.node(i);
if matches!(
n.op,
OpCode::Add
| OpCode::Sub
| OpCode::Mul
| OpCode::And
| OpCode::Or
| OpCode::Xor
| OpCode::Neg
| OpCode::Not
) {
stack.extend(n.children());
}
}
false
}
fn anchored(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
y: u32,
anchors: &[Anchor],
) -> Result<Option<(Digit, Digit, u16, Fin)>, Stop> {
let (rx, ry) = (readings(cx, x), readings(cx, y));
for (at, first) in anchors.iter().enumerate() {
let (k, w) = (first.k, cx.wid(first.of));
if anchors[..at].iter().any(|a| a.k == k && cx.wid(a.of) == w) {
continue;
}
let same: Vec<&Anchor> = anchors
.iter()
.filter(|a| a.k == k && cx.wid(a.of) == w)
.collect();
let lows: Vec<u32> = same.iter().map(|a| a.low).collect();
let side = |rs: &[(u32, u16)]| {
let (p, _) = rs.iter().rev().find(|v| v.1 == k).copied()?;
let wp = cx.wid(p);
if wp == k {
Some((p, true, holds(cx, p, &lows)))
} else {
(wp == w).then_some((p, false, false))
}
};
let (Some((px, nx, hx)), Some((py, ny, hy))) = (side(&rx), side(&ry)) else {
continue;
};
if !(hx || hy) {
continue;
}
let seed: Vec<(u32, u32)> = same.iter().map(|a| (a.low, a.of)).collect();
let mut fin = same.iter().fold(Fin::FINAL, |f, a| f.and(a.fin));
let p = if nx {
lifted(r, cx, px, w, &mut seed.clone())?
} else {
px
};
let q = if ny {
lifted(r, cx, py, w, &mut seed.clone())?
} else {
py
};
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32| {
let (small, f) = below(r, cx, of, k)?;
if small {
fin = fin.and(f);
}
Ok::<_, Stop>(Digit { of, small })
};
let (a, b) = (digit(r, cx, p)?, digit(r, cx, q)?);
return Ok(Some((a, b, k, fin)));
}
Ok(None)
}
#[derive(Clone, Copy)]
struct Sides {
p: Digit,
q: Digit,
at: u16,
fin: Fin,
guard: Option<(u16, u16)>,
}
fn narrow_word(r: &mut Runner<'_, '_>, cx: &mut Context, word: u32, top: u16) -> Result<u32, Stop> {
if !matches!(cx.node(word).op, OpCode::Add | OpCode::Sub) {
return Ok(word);
}
let form = linear::form_of(r, cx, word)?;
let (konst, terms, _) = form.parts();
let mut w = 0;
for &(t, _) in terms {
let n = cx.node(t);
if n.op != OpCode::Zext || w != 0 && cx.wid(n.a) != w {
return Ok(word);
}
w = cx.wid(n.a);
}
if w < top || w == 0 {
return Ok(word);
}
let ww = Width::new(w).map_err(|e| Stop::Error(e.into()))?;
let low = |v: &BitVec| v.trunc(ww).unwrap_or(*v);
let narrow: Vec<(u32, BitVec)> = terms
.iter()
.map(|&(t, k)| (cx.node(t).a, low(&k)))
.collect();
let e = linear::emit(r, cx, &linear::Form::of(low(konst), &narrow))?;
let form = linear::form_of(r, cx, e)?;
linear::emit(r, cx, &form)
}
fn middle_of(r: &mut Runner<'_, '_>, cx: &mut Context, x: u32) -> Result<Option<(u32, u16)>, Stop> {
let n = cx.node(x);
let slice = |cx: &Context, i: u32| {
let s = cx.node(i);
(s.op == OpCode::Extract && s.b > 0 && cx.wid(s.a) <= 64).then_some((s.a, s.b as u16))
};
if let Some((a, o)) = slice(cx, x) {
return Ok(Some((narrow_word(r, cx, a, o + n.width)?, o)));
}
if n.op != OpCode::Add || n.width > 64 {
return Ok(None);
}
let (e, c) = match (cx.const_val(n.a), cx.const_val(n.b)) {
(None, Some(c)) => (n.a, c),
(Some(c), None) => (n.b, c),
_ => return Ok(None),
};
let Some((a, o)) = slice(cx, e) else {
return Ok(None);
};
let a = narrow_word(r, cx, a, o + n.width)?;
let w = cx.width_of(a);
let k = (c.limbs()[0] << o) & (u64::MAX >> (64 - w.bits()));
let k = BitVec::from_u64(w, k).map_err(|e| Stop::Error(e.into()))?;
let of = r.build(cx, |cx| {
let k = cx.mk_const(&k)?;
cx.c_bin(BinOp::Add, a, k)
})?;
let form = linear::form_of(r, cx, of)?;
Ok(Some((linear::emit(r, cx, &form)?, o)))
}
fn middle(r: &mut Runner<'_, '_>, cx: &mut Context, x: u32, y: u32) -> Result<Option<Sides>, Stop> {
let n = cx.wid(x);
if n != cx.wid(y) {
return Ok(None);
}
let (Some((p, o)), Some((q, oq))) = (middle_of(r, cx, x)?, middle_of(r, cx, y)?) else {
return Ok(None);
};
let w = cx.wid(p);
if o != oq || w != cx.wid(q) || o + n >= w {
return Ok(None);
}
middle_sides(r, cx, p, q, o, n, Fin::FINAL).map(Some)
}
fn unextended(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
y: u32,
) -> Result<Option<(u32, u32, Fin)>, Stop> {
let m = cx.wid(x);
if m != cx.wid(y) || m > 64 {
return Ok(None);
}
let inner = |cx: &Context, v: u32| {
let nd = cx.node(v);
(nd.op == OpCode::Zext).then(|| cx.wid(nd.a))
};
let n = match (inner(cx, x), inner(cx, y)) {
(None, None) => return Ok(None),
(a, b) => a.unwrap_or(0).max(b.unwrap_or(0)),
};
if n >= m {
return Ok(None);
}
let mut fin = Fin::FINAL;
let mut out = [0; 2];
for (i, v) in [x, y].into_iter().enumerate() {
let nd = cx.node(v);
out[i] = if nd.op == OpCode::Zext {
if cx.wid(nd.a) == n {
nd.a
} else {
r.build(cx, |cx| cx.c_zext(nd.a, n))?
}
} else {
let (fits, f) = below(r, cx, v, n)?;
if !fits {
return Ok(None);
}
fin = fin.and(f);
if nd.op == OpCode::Extract && nd.b > 0 && cx.wid(nd.a) <= 64 {
let a = cx.node(nd.a);
let keep = (u64::MAX >> (64 - n)) << nd.b;
let under = match (a.op, cx.const_val(a.a), cx.const_val(a.b)) {
(OpCode::And, None, Some(c)) if c.limbs()[0] & keep == keep => a.a,
(OpCode::And, Some(c), None) if c.limbs()[0] & keep == keep => a.b,
_ => nd.a,
};
r.build(cx, |cx| cx.c_extract(under, nd.b as u16, n))?
} else {
let Some((h, f2, _)) = casts::narrowed_from(r, cx, v, 0, n)? else {
return Ok(None);
};
fin = fin.and(f2);
h
}
};
}
Ok(Some((out[0], out[1], fin)))
}
fn middle_sides(
r: &mut Runner<'_, '_>,
cx: &mut Context,
p: u32,
q: u32,
o: u16,
n: u16,
mut fin: Fin,
) -> Result<Sides, Stop> {
let at = o + n;
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32| {
let (small, f) = below(r, cx, of, at)?;
if small {
fin = fin.and(f);
}
Ok::<_, Stop>(Digit { of, small })
};
let (p, q) = (digit(r, cx, p)?, digit(r, cx, q)?);
Ok(Sides {
p,
q,
at,
fin,
guard: Some((o, n)),
})
}
fn shifted(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
y: u32,
words: Option<&[Anchor]>,
) -> Result<Option<Sides>, Stop> {
use crate::facts::known::bv_shl;
let n = cx.wid(x);
if n != cx.wid(y) || n > 64 {
return Ok(None);
}
let nw = cx.width_of(x);
let (zero, one) = (BitVec::zero(nw), BitVec::one(nw));
let (fx, fy) = (linear::form_of(r, cx, x)?, linear::form_of(r, cx, y)?);
let ((kx, tx, relx), (ky, ty, rely)) = (fx.parts(), fy.parts());
r.meter
.charge(Counter::PassWork, (tx.len() + ty.len()) as u64)?;
let slice = |cx: &Context, t: &(u32, BitVec)| {
let s = cx.node(t.0);
t.1 == one
&& s.op == OpCode::Extract
&& s.b > 0
&& cx.wid(s.a) <= 64
&& s.b as u16 + n < cx.wid(s.a)
&& words.is_none_or(|ws| ws.iter().any(|a| a.of == s.a && a.k == s.b as u16 + n))
};
let without = |ts: &[(u32, BitVec)], s: u32| -> Vec<(u32, BitVec)> {
ts.iter().filter(|t| t.0 != s).copied().collect()
};
let mut own: Vec<u32> = tx.iter().filter(|t| slice(cx, t)).map(|t| t.0).collect();
own.sort_by(|a, b| cx.order(*a, *b));
let shared = own.iter().copied().find(|s| ty.contains(&(*s, one)));
let (s, both, consts, values) = if let Some(s) = shared {
let mut values = [None; 2];
for (v, ts) in values.iter_mut().zip([tx, ty]) {
let rest = without(ts, s);
if !rest.is_empty() {
*v = Some(linear::emit(r, cx, &linear::Form::of(zero, &rest))?);
}
}
(s, true, [*kx, *ky], values)
} else if ty.iter().any(|t| slice(cx, t)) {
return Ok(None);
} else {
let found = own.iter().copied().find(|s| {
let rest = without(tx, *s);
rest.is_empty() || rest == ty
});
let Some(s) = found else {
return Ok(None);
};
let t = (!ty.is_empty()).then_some(y);
if tx.len() == 1 {
let k = if ty.is_empty() { *ky } else { zero };
(s, false, [*kx, k], [None, t])
} else {
let k = BitVec::bin_unchecked(BinOp::Sub, kx, ky);
(s, false, [k, zero], [t, t])
}
};
let (a, o) = (cx.node(s).a, cx.node(s).b as u16);
let wide = cx.width_of(a);
let w = wide.bits();
let up = |v: &BitVec| bv_shl(&v.zext(wide).unwrap_or(*v), u32::from(o));
let mut fin = relx.and(rely);
let mut words = [0u32; 2];
for i in 0..2 {
let mut word = if i == 0 || both {
linear::form_of(r, cx, a)?
} else {
linear::Form::of(BitVec::zero(wide), &[])
};
word = word.add(&linear::Form::of(up(&consts[i]), &[]));
if let Some(t) = values[i] {
let v = cx.node(t);
let z = if v.op == OpCode::Extract && v.b == 0 && cx.wid(v.a) == w {
let m = BitVec::wrapping_from_u64(wide, u64::MAX >> (64 - n));
r.build(cx, |cx| {
let m = cx.mk_const(&m)?;
cx.c_bin(BinOp::And, v.a, m)
})?
} else {
r.build(cx, |cx| cx.c_zext(t, w))?
};
word = word.add(&linear::form_of(r, cx, z)?.scale(&up(&one)));
}
fin = fin.and(word.fin);
words[i] = linear::emit(r, cx, &word)?;
}
middle_sides(r, cx, words[0], words[1], o, n, fin).map(Some)
}
fn digits(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
y: u32,
anchors: &[Anchor],
need: u16,
) -> Result<Option<Sides>, Stop> {
if cx.wid(x) > 64 {
return Ok(None);
}
if !anchors.is_empty()
&& let Some((p, q, at, fin)) = anchored(r, cx, x, y, anchors)?
{
return Ok(Some(Sides {
p,
q,
at,
fin,
guard: None,
}));
}
if let Some(found) = middle(r, cx, x, y)? {
return Ok(Some(found));
}
let narrow = unextended(r, cx, x, y)?;
if let Some((x, y, f)) = narrow
&& let Some(mut found) = middle(r, cx, x, y)?
{
found.fin = found.fin.and(f);
return Ok(Some(found));
}
let (mx, my, mf) = narrow.unwrap_or((x, y, Fin::FINAL));
let other = |r: &mut Runner<'_, '_>, cx: &mut Context, words: Option<&[Anchor]>| {
Ok::<_, Stop>(shifted(r, cx, mx, my, words)?.map(|mut s| {
s.fin = s.fin.and(mf);
s
}))
};
if let Some(found) = other(r, cx, Some(anchors))? {
return Ok(Some(found));
}
let (rx, ry) = (readings(cx, x), readings(cx, y));
let at = |rs: &[(u32, u16)], w: u16| rs.iter().rev().find(|v| cx.wid(v.0) == w).copied();
let common = rx
.iter()
.filter_map(|&(p, _)| {
let w = cx.wid(p);
let (a, b) = (at(&rx, w)?, at(&ry, w)?);
(w > a.1.min(b.1)).then_some((w, a, b))
})
.max_by_key(|c| c.0);
let ((p, kp), (q, kq), k, w) = match common {
Some((w, a, b)) => (a, b, a.1.min(b.1), w),
None => {
let (a, b) = (rx[rx.len() - 1], ry[ry.len() - 1]);
let (wp, wq) = (cx.wid(a.0), cx.wid(b.0));
let (k, w) = match (wp > a.1, wq > b.1) {
(true, true) => (a.1.min(b.1), wp.max(wq)),
(true, false) => (a.1, wp),
(false, true) => (b.1, wq),
(false, false) => {
let k = a.1.max(b.1);
let lift = |v: (u32, u16)| if v.1 == k { lift_width(cx, v.0) } else { 0 };
(k, lift(a).max(lift(b)))
}
};
(a, b, k, w)
}
};
let wide = lift_width(cx, p).max(lift_width(cx, q));
let w = if w > k
&& w - k < need
&& wide > w
&& anchors.iter().any(|a| a.k == k && cx.wid(a.of) == wide)
{
wide
} else {
w
};
if w <= k {
return other(r, cx, None);
}
let mut fin = Fin::FINAL;
let Some(a) = side(r, cx, x, p, kp, k, w, &mut fin)? else {
return other(r, cx, None);
};
let Some(b) = side(r, cx, y, q, kq, k, w, &mut fin)? else {
return other(r, cx, None);
};
Ok(Some(Sides {
p: a,
q: b,
at: k,
fin,
guard: None,
}))
}
fn high_digit(cx: &Context, i: u32) -> bool {
let n = cx.node(i);
match n.op {
OpCode::Extract => n.b > 0,
OpCode::Zext => {
let s = cx.node(n.a);
s.op == OpCode::Extract && s.b > 0
}
OpCode::LShr => cx.const_val(n.b).is_some(),
_ => false,
}
}
pub(super) fn may_expand(cx: &Context, x: u32) -> bool {
let mut stack = vec![x];
let (mut seen, mut high) = (0, 0);
while let Some(i) = stack.pop() {
seen += 1;
if seen > 4 * MAX_TERMS {
return false;
}
let n = cx.node(i);
match n.op {
OpCode::Add | OpCode::Sub | OpCode::Neg | OpCode::Not => stack.extend(n.children()),
_ if borrow(cx, i).is_some() || detects(cx, i).is_some() => return true,
_ if high_digit(cx, i) => {
high += 1;
if high == 2 {
return true;
}
}
_ => {}
}
}
false
}
fn difference(
r: &mut Runner<'_, '_>,
cx: &mut Context,
p: u32,
q: u32,
fin: &mut Fin,
) -> Result<u32, Stop> {
let raw = r.build(cx, |cx| cx.c_bin(BinOp::Sub, p, q))?;
let mut form = linear::form_of(r, cx, raw)?;
let (konst, terms, _) = form.parts();
let (konst, mut terms) = (*konst, terms.to_vec());
let mut pushed = false;
for t in &mut terms {
let n = cx.node(t.0);
if n.op == OpCode::Extract
&& let Some((h, f, _)) = casts::narrowed_from(r, cx, n.a, n.b as u16, n.width)?
&& h != t.0
{
*fin = fin.and(f);
t.0 = h;
pushed = true;
}
}
if pushed {
let relied = form.fin;
let e = linear::emit(r, cx, &linear::Form::of(konst, &terms))?;
form = linear::form_of(r, cx, e)?;
form.fin = form.fin.and(relied);
}
if let Some((f, _)) = super::limbs::joined(r, cx, &form, cx.wid(raw))? {
form = f;
}
if form.parts().1.len() > 1
&& let Some(f) = linear::unmasked(r, cx, &form)?
{
form = f;
}
*fin = fin.and(form.fin);
let d = linear::emit(r, cx, &form)?;
let (mut best, mut left) = (d, form.parts().1.len());
if left > 1 {
for e in [Some(d), (raw != d).then_some(raw)].into_iter().flatten() {
if let Some(mixed) = linear_mba::normal(r, cx, e)? {
let n = linear::form_of(r, cx, mixed)?.parts().1.len();
if n < left {
(best, left) = (mixed, n);
}
}
}
}
Ok(best)
}
fn no_wrap(r: &mut Runner<'_, '_>, cx: &mut Context, q: u32, d: u32) -> Result<(bool, Fin), Stop> {
let mask = u64::MAX >> (64 - cx.wid(q));
let (kq, fq) = super::known_words(r, cx, q)?;
let (kd, fd) = super::known_words(r, cx, d)?;
let (Some([zq, _]), Some([zd, _])) = (kq, kd) else {
return Ok((false, Fin::FINAL));
};
let most = u128::from(!zq & mask) + u128::from(!zd & mask);
Ok((most <= u128::from(mask), fq.and(fd)))
}
fn trimmed(r: &mut Runner<'_, '_>, cx: &mut Context, t: u32, fin: &mut Fin) -> Result<u32, Stop> {
let w = cx.wid(t);
let (Some([zero, _]), f) = super::known_words(r, cx, t)? else {
return Ok(t);
};
let open = !zero & (u64::MAX >> (64 - w));
let bits = (u64::BITS - open.leading_zeros()) as u16;
if bits == w {
return Ok(t);
}
let e = if bits == 0 {
let zero = BitVec::zero(cx.width_of(t));
r.build(cx, |cx| cx.mk_const(&zero))?
} else {
let Some((h, fh, _)) = casts::narrowed_from(r, cx, t, 0, bits)? else {
return Ok(t);
};
*fin = fin.and(fh);
r.build(cx, |cx| cx.c_zext(h, w))?
};
*fin = fin.and(f);
Ok(e)
}
fn rebased(
r: &mut Runner<'_, '_>,
cx: &mut Context,
t: u32,
fin: &mut Fin,
) -> Result<(u32, BitVec), Stop> {
use crate::facts::known::{bv_lshr, bv_shl, trailing_zeros};
let none = (t, BitVec::zero(cx.width_of(t)));
let outer = cx.node(t);
let slice = if outer.op == OpCode::Zext { outer.a } else { t };
let s = cx.node(slice);
if s.op != OpCode::Extract {
return Ok(none);
}
let of = cx.node(s.a).op;
if !matches!(
of,
OpCode::Add | OpCode::Sub | OpCode::Neg | OpCode::Mul | OpCode::Shl
) {
return Ok(none);
}
let (mut lo, n) = (s.b, s.width);
let form = linear::form_of(r, cx, s.a)?;
let mut relied = form.fin;
let z = form
.parts()
.1
.iter()
.map(|t| trailing_zeros(&t.1))
.min()
.unwrap_or(0)
.min(lo);
let down = if z > 0 {
let (konst, terms, _) = form.parts();
let down: Vec<(u32, BitVec)> = terms.iter().map(|&(a, k)| (a, bv_lshr(&k, z))).collect();
linear::Form::of(bv_lshr(konst, z), &down)
} else {
form
};
lo -= z;
let mut base = linear::emit(r, cx, &down)?;
let mut above = BitVec::zero(cx.width_of(t));
if lo > 0 && outer.width == n {
let form = linear::form_of(r, cx, base)?;
let (konst, terms, f) = form.parts();
let high = bv_lshr(konst, lo);
if !terms.is_empty() && !high.is_zero() {
let low = BitVec::bin_unchecked(BinOp::Sub, konst, &bv_shl(&high, lo));
base = linear::emit(r, cx, &linear::Form::of(low, terms))?;
relied = relied.and(f);
above = high.trunc(cx.width_of(t)).unwrap_or(above);
}
}
if base == s.a && lo == s.b {
return Ok(none);
}
*fin = fin.and(relied);
let key = r.build(cx, |cx| {
let e = cx.c_extract(base, lo as u16, n)?;
cx.c_zext(e, outer.width)
})?;
Ok((key, above))
}
fn key_of(r: &mut Runner<'_, '_>, cx: &mut Context, t: u32) -> Result<(u32, BitVec, Fin), Stop> {
let mut f = Fin::FINAL;
let e = trimmed(r, cx, t, &mut f)?;
let (key, above) = rebased(r, cx, e, &mut f)?;
let key = match casts::spelled(r, cx, key, 0, cx.wid(key))? {
Some((s, fs, _)) => {
f = f.and(fs);
s
}
None => key,
};
Ok((key, above, f))
}
fn merged(
r: &mut Runner<'_, '_>,
cx: &mut Context,
sum: &linear::Form,
fin: &mut Fin,
) -> Result<linear::Form, Stop> {
let (konst, terms, relied) = sum.parts();
let w = konst.width();
let mut read = Vec::with_capacity(terms.len());
for &(t, k) in terms {
let (key, above, f) = key_of(r, cx, t)?;
read.push((key, t, k, f, above));
}
read.sort_by_key(|v| v.0);
let mut konst = *konst;
let mut out: Vec<(u32, BitVec)> = Vec::with_capacity(read.len());
for group in read.chunk_by(|a, b| a.0 == b.0) {
if let [(_, t, k, ..)] = group {
out.push((*t, *k));
continue;
}
let add = |a: &BitVec, b: &BitVec| BitVec::bin_unchecked(BinOp::Add, a, b);
let mul = |a: &BitVec, b: &BitVec| BitVec::bin_unchecked(BinOp::Mul, a, b);
let k = group.iter().fold(BitVec::zero(w), |k, v| add(&k, &v.2));
for v in group {
*fin = fin.and(v.3);
konst = add(&konst, &mul(&v.2, &v.4));
}
let key = group[0].0;
if let Some(c) = cx.const_val(key) {
konst = add(&konst, &mul(&k, &c));
continue;
}
out.push((key, k));
}
let mut form = linear::Form::of(konst, &out);
form.fin = relied;
Ok(form)
}
#[derive(Clone, Copy)]
struct Carry {
p: Digit,
q: Digit,
d: u32,
at: u16,
minus: bool,
fin: Fin,
met: Option<(usize, bool)>,
gone: Option<usize>,
}
fn absorbed(
r: &mut Runner<'_, '_>,
cx: &mut Context,
terms: &[(u32, BitVec)],
carries: &[Option<Carry>],
sum: &linear::Form,
j: u16,
fin: &mut Fin,
) -> Result<Option<(u32, usize, Vec<u32>)>, Stop> {
use crate::facts::known::bv_shl;
let (konst, left, _) = sum.parts();
let jw = konst.width();
let low = |v: &BitVec| v.trunc(jw).unwrap_or(*v);
let all: Vec<(u32, BitVec)> = terms
.iter()
.zip(carries)
.filter(|(_, c)| c.is_none())
.map(|(t, _)| (t.0, low(&t.1)))
.collect();
let plain = &all;
let one = BitVec::one(jw);
let mut digits: Vec<u32> = left
.iter()
.filter(|t| {
let n = cx.node(t.0);
t.1 == one
&& n.op == OpCode::Extract
&& cx.wid(n.a) <= 64
&& carries.iter().flatten().any(|c| u32::from(c.at) == n.b)
})
.map(|t| t.0)
.collect();
if plain.is_empty() || digits.is_empty() {
return Ok(None);
}
digits.sort_by(|a, b| cx.order(*a, *b));
digits.truncate(2);
let mut narrow = Vec::with_capacity(plain.len() + 1);
for &(a, ka) in plain {
let e = if cx.wid(a) == j {
a
} else {
let Some((e, f, _)) = casts::narrowed_from(r, cx, a, 0, j)? else {
return Ok(None);
};
*fin = fin.and(f);
e
};
narrow.push((e, ka));
}
let zero = BitVec::zero(jw);
let mut found = None;
for h in digits {
let mut over = linear::Form::of(zero, left).add(&linear::Form::of(zero, &[(h, one)]).neg());
for &(e, ka) in &narrow {
over = over.add(&linear::form_of(r, cx, e)?.scale(&ka).neg());
}
let mut by_order: Vec<usize> = (0..narrow.len()).collect();
by_order.sort_by(|a, b| cx.order(plain[*a].0, plain[*b].0));
let mut kept: Vec<usize> = Vec::with_capacity(narrow.len());
for i in by_order {
let (e, ka) = narrow[i];
if high_digit(cx, plain[i].0) {
let with = over.add(&linear::form_of(r, cx, e)?.scale(&ka));
if with.parts().1.len() < over.parts().1.len() {
over = with;
continue;
}
}
kept.push(i);
}
if kept.is_empty() {
continue;
}
let mut check: Vec<(u32, BitVec)> = kept.iter().map(|&i| narrow[i]).collect();
check.push((h, one));
let e = linear::emit(r, cx, &linear::Form::of(*konst, &check))?;
let same = linear::form_of(r, cx, e)?;
if same.parts().0 == konst && same.parts().1 == left {
found = Some((h, same.fin, kept));
break;
}
}
let Some((h, same, kept)) = found else {
return Ok(None);
};
let plain: Vec<(u32, BitVec)> = kept.iter().map(|&i| all[i]).collect();
let (p, k) = (cx.node(h).a, cx.node(h).b);
let w = cx.wid(p);
let wide = cx.width_of(p);
let up = |c: &BitVec| bv_shl(&c.zext(wide).unwrap_or(*c), k);
let form = linear::form_of(r, cx, p)?;
let mut relied = same.and(form.fin);
let mut word = form.parts().1.to_vec();
let mut konst = BitVec::bin_unchecked(BinOp::Add, form.parts().0, &up(konst));
for &(a, ka) in &plain {
let wa = cx.wid(a);
let at = match wa.cmp(&w) {
core::cmp::Ordering::Equal => a,
core::cmp::Ordering::Less => r.build(cx, |cx| cx.c_zext(a, w))?,
core::cmp::Ordering::Greater => r.build(cx, |cx| cx.c_extract(a, 0, w))?,
};
let fa = linear::form_of(r, cx, at)?;
relied = relied.and(fa.fin);
let ka = up(&ka);
let (ca, ta, _) = fa.parts();
konst = BitVec::bin_unchecked(
BinOp::Add,
&konst,
&BitVec::bin_unchecked(BinOp::Mul, ca, &ka),
);
word.extend(
ta.iter()
.map(|&(t, c)| (t, BitVec::bin_unchecked(BinOp::Mul, &c, &ka))),
);
}
let total = linear::Form::of(konst, &word);
let m = (k as u16 + j).min(w);
let Some(mut found) = super::limbs::joined(r, cx, &total, m)? else {
return Ok(None);
};
if m < w
&& let Some(whole) = super::limbs::joined(r, cx, &total, w)?
&& whole.0.parts().1.len() <= found.0.parts().1.len()
{
found = whole;
}
let (joined, leaves) = found;
*fin = fin.and(relied).and(joined.fin);
let base = linear::emit(r, cx, &joined)?;
let e = r.build(cx, |cx| cx.c_extract(base, k as u16, j))?;
Ok(Some((e, joined.parts().1.len(), leaves)))
}
fn narrow_term(
r: &mut Runner<'_, '_>,
cx: &mut Context,
a: u32,
j: u16,
w: u16,
) -> Result<Option<(u32, Fin, Vec<u32>)>, Stop> {
let n = cx.node(a);
let (from, lo) = match n.op {
OpCode::Extract if j == w => (n.a, n.b as u16),
_ if j == w => return Ok(Some((a, Fin::FINAL, vec![a]))),
_ => (a, 0),
};
casts::narrowed_from(r, cx, from, lo, j)
}
type Read = (Vec<(u32, BitVec)>, Fin, Vec<u32>);
struct Settled {
e: u32,
left: usize,
plain: usize,
fin: Fin,
stops: Vec<u32>,
}
#[allow(clippy::too_many_arguments)]
fn settled(
r: &mut Runner<'_, '_>,
cx: &mut Context,
terms: &[(u32, BitVec)],
konst: BitVec,
carries: &[Option<Carry>],
digs: &[Option<Read>],
owns: &[Option<(u32, Fin, Vec<u32>)>],
j: u16,
) -> Result<Settled, Stop> {
let jw = konst.width();
let low = |v: &BitVec| v.trunc(jw).unwrap_or(*v);
let mut out: Vec<(u32, BitVec)> = Vec::with_capacity(terms.len() + 4);
let mut rest: Vec<(u32, BitVec)> = Vec::with_capacity(terms.len());
let mut read = 0;
let mut stops = Vec::new();
let mut fin = Fin::FINAL;
for (i, &(_, k)) in terms.iter().enumerate() {
let k = low(&k);
if let Some((ds, f, s)) = &digs[i] {
read += usize::from(!k.is_zero());
fin = fin.and(*f);
stops.extend(s);
out.extend(ds);
} else if let Some((h, f, s)) = &owns[i] {
fin = fin.and(*f);
stops.extend(s);
out.push((*h, k));
rest.push((*h, k));
}
}
let flat = |r: &mut Runner<'_, '_>, cx: &mut Context, terms: &[(u32, BitVec)]| {
let e = linear::emit(r, cx, &linear::Form::of(konst, terms))?;
linear::form_of(r, cx, e)
};
let plain = if read == 0 {
terms.len()
} else {
flat(r, cx, &rest)?.parts().1.len() + read
};
let sum = flat(r, cx, &out)?;
let sum = merged(r, cx, &sum, &mut fin)?;
let mut left = sum.parts().1.len();
let mut e = linear::emit(r, cx, &sum)?;
if left > 1
&& let Some(e2) = linear_mba::normal(r, cx, e)?
{
let f = linear::form_of(r, cx, e2)?;
if f.parts().1.len() < left {
left = f.parts().1.len();
e = e2;
}
}
if left > 1
&& read > 0
&& let Some((e2, count, leaves)) = absorbed(r, cx, terms, carries, &sum, j, &mut fin)?
&& count < left
{
left = count;
e = e2;
stops.extend(leaves);
}
if left > 1
&& read > 0
&& let Some((f, _)) = super::limbs::joined_over(r, cx, &sum, j, true)?
&& f.parts().1.len() < left
&& f.parts()
.1
.iter()
.all(|t| sum.parts().1.iter().any(|s| s.0 == t.0))
{
left = f.parts().1.len();
fin = fin.and(f.fin);
e = linear::emit(r, cx, &f)?;
}
Ok(Settled {
e,
left,
plain,
fin,
stops,
})
}
fn detects(cx: &Context, a: u32) -> Option<(u32, bool)> {
let n = cx.node(a);
if !matches!(n.op, OpCode::Zext | OpCode::Sext) || n.width < 2 {
return None;
}
let c = cx.node(n.a);
if c.op != OpCode::Or || c.width != 1 {
return None;
}
let and = |i: u32| cx.node(i).op == OpCode::And;
let test = |i: u32| matches!(cx.node(i).op, OpCode::Ult | OpCode::Ule | OpCode::Ne);
(and(c.a) && test(c.b) || and(c.b) && test(c.a)).then_some((n.a, n.op == OpCode::Sext))
}
struct Mid {
word: u32,
o: u16,
extra: linear::Form,
fin: Fin,
}
fn limb_digit(
r: &mut Runner<'_, '_>,
cx: &mut Context,
s: u32,
n: u16,
depth: u32,
) -> Result<Option<(u32, Fin)>, Stop> {
if matches!(cx.node(s).op, OpCode::Add | OpCode::Sub | OpCode::Neg)
&& let Some((e, f, _, _)) = read_sum(r, cx, s, n, depth, true, false)?
{
return Ok(Some((e, f)));
}
Ok(casts::narrowed_from(r, cx, s, 0, n)?.map(|(h, f, _)| (h, f)))
}
const MAX_BASES: usize = 3;
fn middle_words(
r: &mut Runner<'_, '_>,
cx: &mut Context,
v: u32,
n: u16,
depth: u32,
) -> Result<Vec<Mid>, Stop> {
if cx.wid(v) > 64 {
return Ok(Vec::new());
}
let Some(&(s, _)) = readings(cx, v).iter().rev().find(|x| x.1 == n) else {
return Ok(Vec::new());
};
let Some((d, fin)) = limb_digit(r, cx, s, n, depth)? else {
return Ok(Vec::new());
};
let nw = Width::new(n).map_err(|e| Stop::Error(e.into()))?;
let none = linear::Form::of(BitVec::zero(nw), &[]);
if let Some((word, o)) = middle_of(r, cx, d)? {
return Ok(vec![Mid {
word,
o,
extra: none,
fin,
}]);
}
if cx.wid(d) != n {
return Ok(Vec::new());
}
let form = linear::form_of(r, cx, d)?;
let (konst, terms, relied) = form.parts();
let fin = fin.and(relied);
let one = BitVec::one(nw);
let mut bases: Vec<u32> = terms
.iter()
.filter(|t| {
let s = cx.node(t.0);
t.1 == one && s.op == OpCode::Extract && s.b > 0 && cx.wid(s.a) <= 64
})
.map(|t| t.0)
.collect();
bases.sort_by(|a, b| cx.order(*a, *b));
bases.truncate(MAX_BASES);
let mut out = Vec::with_capacity(bases.len());
for h in bases {
r.meter.charge(Counter::PassWork, terms.len() as u64)?;
let (a, o) = (cx.node(h).a, cx.node(h).b as u16);
let w = cx.width_of(a);
let k = (konst.limbs()[0] << o) & (u64::MAX >> (64 - w.bits()));
let mut f = fin;
let word = if k == 0 {
a
} else {
let k = BitVec::from_u64(w, k).map_err(|e| Stop::Error(e.into()))?;
let of = r.build(cx, |cx| {
let k = cx.mk_const(&k)?;
cx.c_bin(BinOp::Add, a, k)
})?;
let form = linear::form_of(r, cx, of)?;
f = f.and(form.fin);
linear::emit(r, cx, &form)?
};
let rest: Vec<(u32, BitVec)> = terms.iter().filter(|t| t.0 != h).copied().collect();
out.push(Mid {
word,
o,
extra: linear::Form::of(BitVec::zero(nw), &rest),
fin: f,
});
}
Ok(out)
}
fn mid_word(r: &mut Runner<'_, '_>, cx: &mut Context, m: &Mid, fin: &mut Fin) -> Result<u32, Stop> {
use crate::facts::known::bv_shl;
let (_, extra, _) = m.extra.parts();
if extra.is_empty() {
return Ok(m.word);
}
let w = cx.width_of(m.word);
let form = linear::form_of(r, cx, m.word)?;
let (konst, terms, relied) = form.parts();
*fin = fin.and(relied);
let mut all = terms.to_vec();
for &(t, c) in extra {
let n = cx.node(t);
let z = if n.op == OpCode::Extract && n.b == 0 && cx.wid(n.a) == w.bits() && w.bits() <= 64
{
let m = BitVec::wrapping_from_u64(w, u64::MAX >> (64 - n.width));
r.build(cx, |cx| {
let m = cx.mk_const(&m)?;
cx.c_bin(BinOp::And, n.a, m)
})?
} else {
r.build(cx, |cx| cx.c_zext(t, w.bits()))?
};
let c = c.sext(w).map_err(|e| Stop::Error(e.into()))?;
all.push((z, bv_shl(&c, u32::from(m.o))));
}
linear::emit(r, cx, &linear::Form::of(*konst, &all))
}
fn carried(
r: &mut Runner<'_, '_>,
cx: &mut Context,
c: u32,
anchors: &[Anchor],
need: u16,
depth: u32,
) -> Result<Option<Sides>, Stop> {
let n = cx.node(c);
let zero = |cx: &Context, i: u32| cx.const_val(i).is_some_and(|k| k.is_zero());
let y = match n.op {
OpCode::Ult => return digits(r, cx, n.a, n.b, anchors, need),
OpCode::Or if depth > 0 => return detector(r, cx, c, anchors, need, depth - 1),
OpCode::Ne if zero(cx, n.b) => n.a,
OpCode::Ne if zero(cx, n.a) => n.b,
_ => return Ok(None),
};
let wy = cx.width_of(y);
let x = r.build(cx, |cx| cx.mk_const(&BitVec::zero(wy)))?;
digits(r, cx, x, y, anchors, need)
}
fn detector(
r: &mut Runner<'_, '_>,
cx: &mut Context,
c: u32,
anchors: &[Anchor],
need: u16,
depth: u32,
) -> Result<Option<Sides>, Stop> {
r.meter.charge(Counter::PassWork, 16)?;
let node = cx.node(c);
let and = |cx: &Context, i: u32| cx.node(i).op == OpCode::And;
let (both, test) = if and(cx, node.a) {
(node.a, node.b)
} else {
(node.b, node.a)
};
if node.op != OpCode::Or || !and(cx, both) {
return Ok(None);
}
let b = cx.node(both);
let (eq, cin) = if cx.node(b.a).op == OpCode::Eq {
(b.a, b.b)
} else {
(b.b, b.a)
};
let e = cx.node(eq);
if e.op != OpCode::Eq {
return Ok(None);
}
let konst = |v: BitVec| linear::Form::of(v, &[]);
let mut fin = Fin::FINAL;
let (tested, n) = match (cx.const_val(e.a), cx.const_val(e.b)) {
(None, None) => {
let n = cx.wid(e.a);
if n > 64 {
return Ok(None);
}
let (Some((ha, fa)), Some((hb, fb))) = (
limb_digit(r, cx, e.a, n, depth)?,
limb_digit(r, cx, e.b, n, depth)?,
) else {
return Ok(None);
};
fin = fin.and(fa).and(fb);
let tested = linear::form_of(r, cx, ha)?.add(&linear::form_of(r, cx, hb)?.neg());
(tested, n)
}
(None, Some(k)) | (Some(k), None) => {
let v = if cx.const_val(e.a).is_some() {
e.b
} else {
e.a
};
let k = k.limbs()[0];
if cx.wid(v) > 64 {
return Ok(None);
}
let rs = readings(cx, v);
let (g, n) = rs[rs.len() - 1];
if n < 64 && k >> n != 0 {
return Ok(None);
}
let Some((h, fh)) = limb_digit(r, cx, g, n, depth)? else {
return Ok(None);
};
fin = fin.and(fh);
let nw = Width::new(n).map_err(|e| Stop::Error(e.into()))?;
let tested =
linear::form_of(r, cx, h)?.add(&konst(BitVec::wrapping_from_u64(nw, k)).neg());
(tested, n)
}
_ => return Ok(None),
};
let nw = Width::new(n).map_err(|e| Stop::Error(e.into()))?;
let one = BitVec::one(nw);
fin = fin.and(tested.fin);
let same = |a: &linear::Form, b: &linear::Form| {
a.parts().0 == b.parts().0 && a.parts().1 == b.parts().1
};
let mask = u64::MAX >> (64 - n);
let konst_of = |cx: &Context, i: u32| cx.const_val(i).map(|k| k.limbs()[0]);
let t = cx.node(test);
if cx.wid(t.a) > 64 {
return Ok(None);
}
let (x, y, kd) = match (t.op, konst_of(cx, t.a), konst_of(cx, t.b)) {
(OpCode::Ne, None, Some(0)) => (None, Some(t.a), 0),
(OpCode::Ne | OpCode::Ult, Some(0), None) => (None, Some(t.b), 0),
(OpCode::Ult, None, None) => (Some(t.a), Some(t.b), 0),
(OpCode::Ult, None, Some(kd)) if kd <= mask => (Some(t.a), None, kd),
(OpCode::Ult | OpCode::Ule, Some(c), None) => {
let c = c.wrapping_add(u64::from(t.op == OpCode::Ult));
if c == 0 || c > mask {
return Ok(None);
}
let kd = c.wrapping_neg() & mask;
let Some(&(s, _)) = readings(cx, t.b).iter().rev().find(|x| x.1 == n) else {
return Ok(None);
};
let Some((d, fd, _)) = casts::narrowed_from(r, cx, s, 0, n)? else {
return Ok(None);
};
fin = fin.and(fd);
let kv = BitVec::wrapping_from_u64(nw, kd);
let x = r.build(cx, |cx| {
let k = cx.mk_const(&kv)?;
cx.c_bin(BinOp::Add, d, k)
})?;
(Some(x), None, kd)
}
_ => return Ok(None),
};
let (p, yw, o, lows) = match (x, y, kd) {
(Some(x), None, kd) => {
let xs = middle_words(r, cx, x, n, depth)?;
if xs.is_empty() {
return Ok(None);
}
let mut found = None;
for pm in &xs {
r.meter.charge(Counter::PassWork, 16)?;
let (p, o) = (pm.word, pm.o);
let w = cx.wid(p);
if o + n >= w {
continue;
}
let minus = konst(one).neg().add(&pm.extra.neg());
let digit = [tested.add(&minus), tested.neg().add(&minus)]
.iter()
.find_map(|f| {
let (k0, ts, _) = f.parts();
(k0.is_zero() && ts.len() == 1 && ts[0].1 == one).then(|| ts[0].0)
});
let Some(digit) = digit else {
continue;
};
let s = cx.node(digit);
if s.op != OpCode::Extract || s.b != u32::from(o) || s.width != n || cx.wid(s.a) < w
{
continue;
}
let z = if cx.wid(s.a) == w {
s.a
} else {
r.build(cx, |cx| cx.c_extract(s.a, 0, w))?
};
let mut f = pm.fin;
let yw = difference(r, cx, p, z, &mut f)?;
let Some(k) = cx.const_val(yw) else {
continue;
};
if (k.limbs()[0] >> o) & mask != kd {
continue;
}
let word = mid_word(r, cx, pm, &mut f)?;
found = Some((word, yw, o, [p, yw, z], f));
break;
}
let Some((p, yw, o, lows, f)) = found else {
return Ok(None);
};
fin = fin.and(f);
(p, yw, o, lows)
}
(None, None, _) => return Ok(None),
(x, Some(y), _) => {
let ys = middle_words(r, cx, y, n, depth)?;
if ys.is_empty() {
return Ok(None);
}
let xs = match x {
Some(x) => {
let xs = middle_words(r, cx, x, n, depth)?;
if xs.is_empty() {
return Ok(None);
}
Some(xs)
}
None => None,
};
let mut found = None;
'pairs: for ym in &ys {
let (yb, o) = (ym.word, ym.o);
let w = cx.wid(yb);
if o + n >= w {
continue;
}
let nothing;
let ps: &[Mid] = match &xs {
Some(xs) => xs,
None => {
let zero = BitVec::zero(cx.width_of(yb));
nothing = [Mid {
word: r.build(cx, |cx| cx.mk_const(&zero))?,
o,
extra: konst(BitVec::zero(nw)),
fin: Fin::FINAL,
}];
¬hing
}
};
for pm in ps {
if pm.o != o || cx.wid(pm.word) != w {
continue;
}
r.meter.charge(Counter::PassWork, 16)?;
let mut f = pm.fin.and(ym.fin);
let z = difference(r, cx, pm.word, yb, &mut f)?;
let Some((hz, fz, _)) = casts::narrowed_from(r, cx, z, o, n)? else {
continue;
};
let digit = linear::form_of(r, cx, hz)?
.add(&pm.extra)
.add(&ym.extra.neg())
.add(&konst(one));
if !(same(&tested, &digit) || same(&tested, &digit.neg())) {
continue;
}
f = f.and(fz).and(digit.fin);
let p = mid_word(r, cx, pm, &mut f)?;
let yw = mid_word(r, cx, ym, &mut f)?;
found = Some((p, yw, o, [pm.word, yb, z], f));
break 'pairs;
}
}
let Some((p, yw, o, lows, f)) = found else {
return Ok(None);
};
fin = fin.and(f);
(p, yw, o, lows)
}
};
let Some(inner) = carried(r, cx, cin, anchors, need, depth)? else {
return Ok(None);
};
if inner.guard.is_some() || inner.at != o || !same_low(r, cx, inner.p.of, lows[0], o, &mut fin)?
{
return Ok(None);
}
if !(same_low(r, cx, inner.q.of, lows[1], o, &mut fin)?
|| same_low(r, cx, inner.q.of, lows[2], o, &mut fin)?)
{
return Ok(None);
}
fin = fin.and(inner.fin);
let at = o + n;
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32| {
let (small, f) = below(r, cx, of, at)?;
if small {
fin = fin.and(f);
}
Ok::<_, Stop>(Digit { of, small })
};
let (p, q) = (digit(r, cx, p)?, digit(r, cx, yw)?);
Ok(Some(Sides {
p,
q,
at,
fin,
guard: None,
}))
}
const MAX_WORDS: usize = 4;
#[allow(clippy::too_many_arguments)]
fn reanchored(
r: &mut Runner<'_, '_>,
cx: &mut Context,
terms: &[(u32, BitVec)],
p: Digit,
q: Digit,
d: u32,
at: u16,
fin: &mut Fin,
) -> Result<Option<(Digit, Digit, u32)>, Stop> {
let w = cx.wid(p.of);
let mut words: Vec<u32> = Vec::new();
for &(a, _) in terms {
let n = cx.node(a);
let s = if n.op == OpCode::Zext {
cx.node(n.a)
} else {
n
};
let (of, k) = match s.op {
OpCode::Extract if s.b > 0 => (s.a, u64::from(s.b)),
OpCode::LShr => match cx.const_val(s.b).and_then(|k| k.to_u64()) {
Some(k) => (s.a, k),
None => continue,
},
_ => continue,
};
let wa = cx.wid(of);
if k != u64::from(at) || wa > 64 || wa <= at || words.contains(&of) {
continue;
}
if wa > w {
words.push(of);
}
}
if words.is_empty() {
return Ok(None);
}
words.sort_by(|a, b| cx.order(*a, *b));
words.truncate(MAX_WORDS);
let olds = [d, q.of, p.of];
let mut news: [Option<u32>; 3] = [None; 3];
let mut wide = 0;
let mut f = Fin::FINAL;
for &a in &words {
let wa = cx.wid(a);
if wide != 0 && wa != wide {
continue;
}
for (old, new) in olds.iter().zip(&mut news) {
if new.is_some() {
continue;
}
r.meter.charge(Counter::PassWork, 16)?;
if a == *old || same_low(r, cx, a, *old, at, &mut f)? {
*new = Some(a);
wide = wa;
break;
}
}
}
if (0..3).all(|i| news[i].is_none_or(|a| a == olds[i])) {
return Ok(None);
}
let resized = |r: &mut Runner<'_, '_>, cx: &mut Context, v: u32| {
let wv = cx.wid(v);
match wv.cmp(&wide) {
core::cmp::Ordering::Equal => Ok(v),
core::cmp::Ordering::Less => r.build(cx, |cx| cx.c_zext(v, wide)),
core::cmp::Ordering::Greater => r.build(cx, |cx| cx.c_extract(v, 0, wide)),
}
};
let sum = |r: &mut Runner<'_, '_>, cx: &mut Context, a: u32, b: u32, f: &mut Fin| {
let (fa, fb) = (linear::form_of(r, cx, a)?, linear::form_of(r, cx, b)?);
*f = f.and(fa.fin).and(fb.fin);
linear::emit(r, cx, &fa.add(&fb))
};
let (p2, q2, d2) = match news {
[Some(d2), Some(q2), _] => (sum(r, cx, q2, d2, &mut f)?, q2, d2),
[Some(d2), None, Some(p2)] => (p2, difference(r, cx, p2, d2, &mut f)?, d2),
[None, Some(q2), Some(p2)] => (p2, q2, difference(r, cx, p2, q2, &mut f)?),
[Some(d2), None, None] => {
let q2 = resized(r, cx, q.of)?;
(sum(r, cx, q2, d2, &mut f)?, q2, d2)
}
[None, Some(q2), None] => {
let d2 = resized(r, cx, d)?;
(sum(r, cx, q2, d2, &mut f)?, q2, d2)
}
[None, None, Some(p2)] => {
let q2 = resized(r, cx, q.of)?;
let d2 = match summands(r, cx, p2, p.of, &mut f)? {
Some((a, b)) => conjunction(r, cx, a, b, d, wide, &mut f)?,
None => None,
};
let d2 = match d2 {
Some(d2) => d2,
None => difference(r, cx, p2, q2, &mut f)?,
};
(p2, q2, d2)
}
[None, None, None] => return Ok(None),
};
*fin = fin.and(f);
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32| {
let (small, f) = below(r, cx, of, at)?;
if small {
*fin = fin.and(f);
}
Ok::<_, Stop>(Digit { of, small })
};
Ok(Some((digit(r, cx, p2)?, digit(r, cx, q2)?, d2)))
}
fn congruent(
r: &mut Runner<'_, '_>,
cx: &mut Context,
a: u32,
b: u32,
o: u16,
fin: &mut Fin,
) -> Result<bool, Stop> {
if o == 0 || o > cx.wid(a) || cx.wid(a) != cx.wid(b) {
return Ok(false);
}
let ow = Width::new(o).map_err(|e| Stop::Error(e.into()))?;
let nothing = |h: &linear::Form| h.parts().0.is_zero() && h.parts().1.is_empty();
let zero = |r: &mut Runner<'_, '_>, cx: &mut Context, x: u32, y: u32| {
let (f, g) = (linear::form_of(r, cx, x)?, linear::form_of(r, cx, y)?);
let relied = f.fin.and(g.fin);
let over = f.add(&g.neg()).reduce(ow);
if x == y || nothing(&over) {
return Ok::<_, Stop>(Some(relied));
}
Ok(match super::limbs::joined(r, cx, &over, o)? {
Some((h, _)) if nothing(&h) => Some(relied.and(h.fin)),
_ => None,
})
};
if let Some(relied) = zero(r, cx, a, b)? {
*fin = fin.and(relied);
return Ok(true);
}
if o == cx.wid(a) {
return Ok(false);
}
let (Some((x, fx, _)), Some((y, fy, _))) = (
casts::narrowed_from(r, cx, a, 0, o)?,
casts::narrowed_from(r, cx, b, 0, o)?,
) else {
return Ok(false);
};
let Some(relied) = zero(r, cx, x, y)? else {
return Ok(false);
};
*fin = fin.and(relied).and(fx).and(fy);
Ok(true)
}
fn digit_words(
cx: &Context,
terms: &[(u32, BitVec)],
k: &BitVec,
w: u16,
at: u16,
) -> Vec<(u32, bool, usize)> {
let jw = k.width();
let minus = BitVec::bin_unchecked(BinOp::Sub, &BitVec::zero(jw), k);
let mut words: Vec<(u32, bool, usize)> = Vec::new();
for (i, &(a, c)) in terms.iter().enumerate() {
let n = cx.node(a);
let s = if n.op == OpCode::Zext {
cx.node(n.a)
} else {
n
};
let (of, lo) = match s.op {
OpCode::Extract if s.b > 0 => (s.a, u64::from(s.b)),
OpCode::LShr => match cx.const_val(s.b).and_then(|k| k.to_u64()) {
Some(k) => (s.a, k),
None => continue,
},
_ => continue,
};
let c = c.trunc(jw).unwrap_or(c);
let bits = if s.op == OpCode::Extract {
s.width
} else {
w.saturating_sub(at)
};
if lo != u64::from(at) || cx.wid(of) != w || bits < jw.bits() {
continue;
}
if c == *k || c == minus {
words.push((of, c == *k, i));
}
}
words.sort_by(|a, b| {
cx.order(a.0, b.0)
.then_with(|| cx.order(terms[a.2].0, terms[b.2].0))
});
words.dedup_by_key(|v| v.0);
words
}
fn masked_shift(cx: &Context, x: u32, at: u16) -> Option<(u32, u16)> {
let n = cx.node(x);
if n.op != OpCode::Shl || n.width > 64 {
return None;
}
let s = cx.const_val(n.b)?.to_u64().filter(|s| *s < u64::from(at))? as u16;
let a = cx.node(n.a);
if a.op != OpCode::And {
return None;
}
let (v, m) = match (cx.const_val(a.a), cx.const_val(a.b)) {
(Some(m), None) => (a.b, m.limbs()[0]),
(None, Some(m)) => (a.a, m.limbs()[0]),
_ => return None,
};
let low = (1u64 << (at - s)) - 1;
(m & low == low).then_some((v, s))
}
fn unmasked(
r: &mut Runner<'_, '_>,
cx: &mut Context,
p: Digit,
q: Digit,
d: u32,
at: u16,
fin: &mut Fin,
) -> Result<Option<(Digit, Digit, u32)>, Stop> {
let (sp, sq) = (masked_shift(cx, p.of, at), masked_shift(cx, q.of, at));
if sp.is_none() && sq.is_none() {
return Ok(None);
}
r.meter.charge(Counter::PassWork, 16)?;
let w = cx.width_of(p.of);
let whole = |r: &mut Runner<'_, '_>, cx: &mut Context, old: u32, s: Option<(u32, u16)>| {
let Some((v, s)) = s else {
return Ok::<_, Stop>(old);
};
r.build(cx, |cx| {
let k = cx.mk_const(&BitVec::wrapping_from_u64(w, u64::from(s)))?;
cx.c_bin(BinOp::Shl, v, k)
})
};
let (p2, q2) = (whole(r, cx, p.of, sp)?, whole(r, cx, q.of, sq)?);
let mut relied = Fin::FINAL;
let over = difference(r, cx, p2, q2, &mut relied)?;
let (fo, fd) = (linear::form_of(r, cx, over)?, linear::form_of(r, cx, d)?);
if fo.parts().1.len() >= fd.parts().1.len() {
return Ok(None);
}
*fin = fin.and(relied).and(fo.fin);
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32| {
let (small, f) = below(r, cx, of, at)?;
if small {
*fin = fin.and(f);
}
Ok::<_, Stop>(Digit { of, small })
};
Ok(Some((digit(r, cx, p2)?, digit(r, cx, q2)?, over)))
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
fn respelled(
r: &mut Runner<'_, '_>,
cx: &mut Context,
terms: &[(u32, BitVec)],
k: &BitVec,
p: Digit,
q: Digit,
d: u32,
at: u16,
fin: &mut Fin,
) -> Result<Option<(Digit, Digit, u32, (usize, bool))>, Stop> {
let w = cx.wid(p.of);
let mut words = digit_words(cx, terms, k, w, at);
words.retain(|v| {
v.0 != p.of
&& v.0 != q.of
&& matches!(
cx.node(v.0).op,
OpCode::Add | OpCode::Sub | OpCode::Neg | OpCode::Mul | OpCode::Shl
)
});
words.truncate(MAX_WORDS);
for (a, with_q, term) in words {
r.meter.charge(Counter::PassWork, 16)?;
let mut f = Fin::FINAL;
let old = if with_q { q.of } else { p.of };
if !congruent(r, cx, a, old, at, &mut f)? {
continue;
}
let (fa, fd) = (linear::form_of(r, cx, a)?, linear::form_of(r, cx, d)?);
let mut relied = f;
let over = if with_q {
difference(r, cx, p.of, a, &mut relied)?
} else {
difference(r, cx, a, q.of, &mut relied)?
};
let fo = linear::form_of(r, cx, over)?;
let (p2, q2, d2) = if fo.parts().1.len() < fd.parts().1.len() {
f = relied.and(fo.fin);
if with_q {
(p.of, a, over)
} else {
(a, q.of, over)
}
} else {
f = f.and(fa.fin).and(fd.fin);
let other = if with_q {
fa.add(&fd)
} else {
fa.add(&fd.neg())
};
let other = linear::emit(r, cx, &other)?;
if with_q { (other, a, d) } else { (a, other, d) }
};
*fin = fin.and(f);
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32| {
let (small, f) = below(r, cx, of, at)?;
if small {
*fin = fin.and(f);
}
Ok::<_, Stop>(Digit { of, small })
};
return Ok(Some((
digit(r, cx, p2)?,
digit(r, cx, q2)?,
d2,
(term, with_q),
)));
}
Ok(None)
}
fn makes(r: &mut Runner<'_, '_>, cx: &mut Context, m: &Wide) -> Result<bool, Stop> {
if m.minus {
return Ok(false);
}
let np = linear::form_of(r, cx, m.p.of)?.parts().1.len();
let nq = linear::form_of(r, cx, m.q.of)?.parts().1.len();
let nd = linear::form_of(r, cx, m.d)?.parts().1.len();
Ok(nd < np + nq || nq == 0 || nd == 0)
}
#[derive(Clone, Copy)]
struct Wide {
i: usize,
p: Digit,
q: Digit,
d: u32,
at: u16,
minus: bool,
fin: Fin,
}
const MAX_WIDE: usize = 4;
fn summands(
r: &mut Runner<'_, '_>,
cx: &mut Context,
word: u32,
p: u32,
fin: &mut Fin,
) -> Result<Option<(u32, u32)>, Stop> {
let w = cx.wid(p);
let form = linear::form_of(r, cx, word)?;
let (konst, terms, relied) = form.parts();
let &[(za, ka), (zb, kb)] = terms else {
return Ok(None);
};
let one = BitVec::one(konst.width());
let (na, nb) = (cx.node(za), cx.node(zb));
if !konst.is_zero()
|| ka != one
|| kb != one
|| na.op != OpCode::Zext
|| nb.op != OpCode::Zext
|| cx.wid(na.a) != w
|| cx.wid(nb.a) != w
{
return Ok(None);
}
let (fa, fb) = (linear::form_of(r, cx, na.a)?, linear::form_of(r, cx, nb.a)?);
let (sum, fp) = (fa.add(&fb), linear::form_of(r, cx, p)?);
if sum.parts().0 != fp.parts().0 || sum.parts().1 != fp.parts().1 {
return Ok(None);
}
*fin = fin.and(relied).and(sum.fin).and(fp.fin);
Ok(Some((na.a, nb.a)))
}
fn conjunction(
r: &mut Runner<'_, '_>,
cx: &mut Context,
a: u32,
b: u32,
d: u32,
wide: u16,
fin: &mut Fin,
) -> Result<Option<u32>, Stop> {
let w = cx.width_of(d);
let form = linear::form_of(r, cx, d)?;
let (konst, terms, relied) = form.parts();
let &[(m, c)] = terms else {
return Ok(None);
};
let node = cx.node(m);
let twice = c == BitVec::wrapping_from_u64(w, 2);
if !konst.is_zero() || node.op != OpCode::And || !(twice || c == BitVec::one(w)) {
return Ok(None);
}
let mut f = relied;
let mut same = |r: &mut Runner<'_, '_>, cx: &mut Context, u: u32, v: u32| {
if u == v {
return Ok::<_, Stop>(true);
}
let (fu, fv) = (linear::form_of(r, cx, u)?, linear::form_of(r, cx, v)?);
if fu.parts().0 != fv.parts().0 || fu.parts().1 != fv.parts().1 {
return Ok(false);
}
f = f.and(fu.fin).and(fv.fin);
Ok(true)
};
if !(same(r, cx, node.a, a)? && same(r, cx, node.b, b)?
|| same(r, cx, node.a, b)? && same(r, cx, node.b, a)?)
{
return Ok(None);
}
*fin = fin.and(f);
let ww = Width::new(wide).map_err(|e| Stop::Error(e.into()))?;
let zm = r.build(cx, |cx| cx.c_zext(m, wide))?;
let k = BitVec::wrapping_from_u64(ww, if twice { 2 } else { 1 });
let out = linear::form_of(r, cx, zm)?.scale(&k);
*fin = fin.and(out.fin);
Ok(Some(linear::emit(r, cx, &out)?))
}
fn widened(
r: &mut Runner<'_, '_>,
cx: &mut Context,
group: &[Wide],
g: usize,
j: u16,
) -> Result<Vec<Option<Carry>>, Stop> {
let (w, at) = (cx.wid(group[0].p.of), group[0].at);
let wide = if at + j <= 2 * w && 2 * w <= 64 {
2 * w
} else {
(at + j).min(64)
};
let mut out = vec![None; group.len()];
if wide <= w {
return Ok(out);
}
let modular = j <= wide - at;
let maker = &group[g];
let (zq, zd) = (
r.build(cx, |cx| cx.c_zext(maker.q.of, wide))?,
r.build(cx, |cx| cx.c_zext(maker.d, wide))?,
);
let (fq, fd) = (linear::form_of(r, cx, zq)?, linear::form_of(r, cx, zd)?);
let mut relied = fq.fin.and(fd.fin);
let word = linear::emit(r, cx, &fq.add(&fd))?;
let (small, fs) = below(r, cx, word, at)?;
if small {
relied = relied.and(fs);
}
for (n, m) in group.iter().enumerate() {
let mut fin = m.fin.and(relied);
let d = if n == g {
m.d
} else if !modular {
continue;
} else if let Some(d) = conjunction(r, cx, maker.q.of, maker.d, m.d, wide, &mut fin)? {
d
} else {
let zq = r.build(cx, |cx| cx.c_zext(m.q.of, wide))?;
difference(r, cx, word, zq, &mut fin)?
};
out[n] = Some(Carry {
p: Digit { of: word, small },
q: m.q,
d,
at,
minus: m.minus,
fin,
met: None,
gone: None,
});
}
Ok(out)
}
fn carry_digits(
r: &mut Runner<'_, '_>,
cx: &mut Context,
c: &Carry,
k: BitVec,
j: u16,
terms: &[(u32, BitVec)],
w: u16,
) -> Result<Option<Read>, Stop> {
let neg = |v: &BitVec| BitVec::bin_unchecked(BinOp::Sub, &BitVec::zero(v.width()), v);
let k = if c.minus { neg(&k) } else { k };
let own = |with_q: bool| c.met.filter(|m| m.1 == with_q).map(|m| terms[m.0].0);
let highs = [
(!c.p.small).then_some((c.p.of, k, own(false))),
(!c.q.small).then_some((c.q.of, neg(&k), own(true))),
Some((c.d, neg(&k), c.gone.map(|t| terms[t].0))),
];
let mut ds = Vec::with_capacity(3);
let (mut relied, mut stopped) = (c.fin, Vec::new());
for (v, k, own) in highs.into_iter().flatten() {
if let Some(t) = own {
let Some((h, f, s)) = narrow_term(r, cx, t, j, w)? else {
return Ok(None);
};
relied = relied.and(f);
stopped.extend(s);
ds.push((h, k));
continue;
}
let bits = j.min(cx.wid(v) - c.at);
let Some((h, f, s)) = casts::narrowed_from(r, cx, v, c.at, bits)? else {
return Ok(None);
};
relied = relied.and(f);
stopped.extend(s);
ds.push((r.build(cx, |cx| cx.c_zext(h, j))?, k));
}
Ok(Some((ds, relied, stopped)))
}
fn over_difference(
r: &mut Runner<'_, '_>,
cx: &mut Context,
terms: &[(u32, BitVec)],
carries: &[Option<Carry>],
i: usize,
j: u16,
) -> Result<Option<Carry>, Stop> {
let Some(c) = carries[i] else {
return Ok(None);
};
let (w, at) = (cx.wid(c.p.of), c.at);
if c.gone.is_some() || j > w - at {
return Ok(None);
}
let jw = Width::new(j).map_err(|e| Stop::Error(e.into()))?;
let low = |v: &BitVec| v.trunc(jw).unwrap_or(*v);
let k = low(&terms[i].1);
let k = if c.minus {
BitVec::bin_unchecked(BinOp::Sub, &BitVec::zero(jw), &k)
} else {
k
};
let mut words: Vec<(u32, u16, usize)> = Vec::new();
for (t, &(a, ka)) in terms.iter().enumerate() {
if carries[t].is_some()
|| low(&ka) != k
|| carries.iter().flatten().any(|o| o.gone == Some(t))
{
continue;
}
let n = cx.node(a);
let s = if n.op == OpCode::Zext {
cx.node(n.a)
} else {
n
};
let (of, o) = match s.op {
OpCode::Extract if s.b > 0 && s.width >= j => (s.a, u64::from(s.b)),
OpCode::LShr => match cx.const_val(s.b).and_then(|k| k.to_u64()) {
Some(k) => (s.a, k),
None => continue,
},
_ => continue,
};
if o == 0 || o > u64::from(at) || cx.wid(of) != w {
continue;
}
words.push((of, o as u16, t));
}
words.sort_by(|a, b| {
cx.order(a.0, b.0)
.then(a.1.cmp(&b.1))
.then_with(|| cx.order(terms[a.2].0, terms[b.2].0))
});
words.truncate(MAX_WORDS);
let wide = cx.width_of(c.p.of);
for (a, o, term) in words {
r.meter.charge(Counter::PassWork, 16)?;
let mut f = c.fin;
let fa = linear::form_of(r, cx, a)?;
let up = BitVec::from_u64(wide, 1u64 << (at - o)).map_err(|e| Stop::Error(e.into()))?;
let fd = fa.scale(&up);
let d = linear::emit(r, cx, &fd)?;
let same = d == c.d || {
let over = difference(r, cx, d, c.d, &mut f)?;
match cx.const_val(over) {
Some(k) if k.is_zero() => true,
Some(_) => return Ok(None),
None => false,
}
};
if same {
return Ok(Some(Carry {
fin: f.and(fd.fin),
gone: Some(term),
..c
}));
}
if !congruent(r, cx, c.d, d, at, &mut f)? {
continue;
}
f = f.and(fd.fin);
let shared = |v: u32| {
carries
.iter()
.enumerate()
.any(|(n, o)| n != i && o.is_some_and(|o| o.p.of == v || o.q.of == v))
};
let (fp, fq) = (
linear::form_of(r, cx, c.p.of)?,
linear::form_of(r, cx, c.q.of)?,
);
let (with_p, with_q) = (fp.add(&fd.neg()), fq.add(&fd));
let keep_p = match c.met {
Some((_, with_q)) => !with_q,
None => {
let (hp, hq) = (shared(c.p.of), shared(c.q.of));
if hp != hq {
hp
} else {
with_p.parts().1.len() < with_q.parts().1.len()
}
}
};
let (p, q) = if keep_p {
f = f.and(fp.fin);
(c.p.of, linear::emit(r, cx, &with_p)?)
} else {
f = f.and(fq.fin);
(linear::emit(r, cx, &with_q)?, c.q.of)
};
let mut digit = |r: &mut Runner<'_, '_>, cx: &mut Context, of: u32, was: Digit| {
if of == was.of {
return Ok::<_, Stop>(was);
}
let (small, fs) = below(r, cx, of, at)?;
if small {
f = f.and(fs);
}
Ok(Digit { of, small })
};
let (p, q) = (digit(r, cx, p, c.p)?, digit(r, cx, q, c.q)?);
return Ok(Some(Carry {
p,
q,
d,
fin: f,
gone: Some(term),
..c
}));
}
Ok(None)
}
#[allow(clippy::type_complexity)]
pub(super) fn expanded(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
j: u16,
ties: bool,
) -> Result<Option<(u32, Fin, Vec<u32>, bool)>, Stop> {
read_sum(r, cx, x, j, DETECTORS, false, ties)
}
const DETECTORS: u32 = 3;
#[allow(clippy::type_complexity)]
fn read_sum(
r: &mut Runner<'_, '_>,
cx: &mut Context,
x: u32,
j: u16,
depth: u32,
any: bool,
ties: bool,
) -> Result<Option<(u32, Fin, Vec<u32>, bool)>, Stop> {
let w = cx.wid(x);
if j == 0 || j > w || !may_expand(cx, x) {
return Ok(None);
}
let form = linear::form_of(r, cx, x)?;
let (konst, terms, fin) = form.parts();
let (konst, terms, mut fin) = (*konst, terms.to_vec(), fin);
if terms.len() > MAX_TERMS {
return Ok(None);
}
let jw = Width::new(j).map_err(|e| Stop::Error(e.into()))?;
let low = |v: &BitVec| v.trunc(jw).unwrap_or(*v);
let neg = |v: &BitVec| BitVec::bin_unchecked(BinOp::Sub, &BitVec::zero(jw), v);
let before = cx.len() as u32;
let anchors = if terms
.iter()
.any(|t| borrow(cx, t.0).is_some() || detects(cx, t.0).is_some())
{
anchors(r, cx, &terms)?
} else {
Vec::new()
};
let mut pending: Vec<Wide> = Vec::new();
let mut carries = Vec::with_capacity(terms.len());
for &(a, k) in &terms {
let mut c = None;
let sides = match borrow(cx, a) {
Some((Some(x), y, minus)) => Some((x, y, minus)),
Some((None, y, minus)) => {
let wy = cx.width_of(y);
Some((r.build(cx, |cx| cx.mk_const(&BitVec::zero(wy)))?, y, minus))
}
None => None,
};
let found = match sides {
Some((x, y, minus)) => digits(r, cx, x, y, &anchors, j)?.map(|s| (s, minus)),
None => match detects(cx, a) {
Some((c, minus)) if depth > 0 => {
detector(r, cx, c, &anchors, j, depth - 1)?.map(|s| (s, minus))
}
_ => None,
},
};
if let Some((
Sides {
p,
q,
at,
fin: relied,
guard,
},
minus,
)) = found
{
let mut f = relied;
let mut d = difference(r, cx, p.of, q.of, &mut f)?;
let (mut p, mut q) = (p, q);
if guard.is_none()
&& j <= cx.wid(p.of) - at
&& let Some((p2, q2, d2)) = unmasked(r, cx, p, q, d, at, &mut f)?
{
(p, q, d) = (p2, q2, d2);
}
let wide = j > cx.wid(p.of) - at && !no_wrap(r, cx, q.of, d)?.0;
if guard.is_none()
&& wide
&& let Some((p2, q2, d2)) = reanchored(r, cx, &terms, p, q, d, at, &mut f)?
{
(p, q, d) = (p2, q2, d2);
}
let room = cx.wid(p.of) - at;
let mut reads = j <= room || {
let (exact, fe) = no_wrap(r, cx, q.of, d)?;
f = f.and(fe);
exact
};
if !reads && guard.is_none() {
pending.push(Wide {
i: carries.len(),
p,
q,
d,
at,
minus,
fin: f,
});
}
if reads && let Some((o, n)) = guard {
let (known, fk) = super::known_words(r, cx, d)?;
let digit = (u64::MAX >> (64 - n)) << o;
let lows = (1u64 << o) - 1;
reads = known.is_some_and(|[zero, _]| zero & digit != 0 || zero & lows == lows);
f = f.and(fk);
if !reads {
let (known, fq) = super::known_words(r, cx, q.of)?;
reads = known.is_some_and(|[zero, _]| zero & lows == lows);
f = f.and(fq);
}
}
let kc = if minus { neg(&low(&k)) } else { low(&k) };
let mut met = digit_words(cx, &terms, &kc, cx.wid(p.of), at)
.into_iter()
.find(|v| v.0 == if v.1 { q.of } else { p.of })
.map(|v| (v.2, v.1));
let natural = guard.is_some()
|| sides.is_none_or(|(x, y, _)| {
let widest = |v: u32| readings(cx, v).iter().map(|s| cx.wid(s.0)).max();
widest(x).max(widest(y)).is_some_and(|n| cx.wid(p.of) <= n)
});
if reads
&& met.is_none()
&& j <= room
&& natural
&& let Some((p2, q2, d2, m)) = respelled(r, cx, &terms, &kc, p, q, d, at, &mut f)?
{
(p, q, d, met) = (p2, q2, d2, Some(m));
}
if reads
&& j <= room
&& let Some((_, with_q)) = met
{
let fd = linear::form_of(r, cx, d)?;
let low = fd.reduce(Width::new(at).map_err(|e| Stop::Error(e.into()))?);
if low.parts().1.len() < fd.parts().1.len() {
let kept = linear::form_of(r, cx, if with_q { q.of } else { p.of })?;
let other = if with_q {
kept.add(&low)
} else {
kept.add(&low.neg())
};
f = f.and(fd.fin).and(kept.fin);
let of = linear::emit(r, cx, &other)?;
let (small, fs) = below(r, cx, of, at)?;
if small {
f = f.and(fs);
}
d = linear::emit(r, cx, &low)?;
if with_q {
p = Digit { of, small };
} else {
q = Digit { of, small };
}
}
}
if reads {
c = Some(Carry {
p,
q,
d,
at,
minus,
fin: f,
met,
gone: None,
});
}
}
carries.push(c);
}
for i in 0..carries.len() {
if let Some(c) = over_difference(r, cx, &terms, &carries, i, j)? {
carries[i] = Some(c);
}
}
let highs = terms.iter().filter(|t| high_digit(cx, t.0)).count();
if carries.iter().all(Option::is_none) && highs < 2 && pending.is_empty() {
discard(r, cx, before);
return Ok(None);
}
let mut digs: Vec<Option<Read>> = Vec::with_capacity(terms.len());
let mut owns: Vec<Option<(u32, Fin, Vec<u32>)>> = Vec::with_capacity(terms.len());
let mut lowered = false;
for (&(a, k), c) in terms.iter().zip(&carries) {
let Some(c) = *c else {
let Some(own) = narrow_term(r, cx, a, j, w)? else {
discard(r, cx, before);
return Ok(None);
};
let n = cx.node(a);
if j < w
&& n.op == OpCode::Extract
&& n.b > 0
&& let Some(m) = casts::zext_sum(cx, n.a, 1)
{
let lo = n.b as u16;
lowered |= lo + j <= m && m < lo + n.width;
}
owns.push(Some(own));
digs.push(None);
continue;
};
let Some(read) = carry_digits(r, cx, &c, low(&k), j, &terms, w)? else {
discard(r, cx, before);
return Ok(None);
};
owns.push(None);
digs.push(Some(read));
}
let konst = low(&konst);
let base = settled(r, cx, &terms, konst, &carries, &digs, &owns, j)?;
pending.sort_by(|a, b| {
cx.order(a.p.of, b.p.of)
.then(a.at.cmp(&b.at))
.then(cx.order(terms[a.i].0, terms[b.i].0))
});
let mut wide: Option<Settled> = None;
for group in pending
.chunk_by(|a, b| a.p.of == b.p.of && a.at == b.at)
.take(MAX_WIDE)
{
for g in 0..group.len().min(MAX_WIDE) {
if !makes(r, cx, &group[g])? {
continue;
}
r.meter.charge(Counter::PassWork, 4 * terms.len() as u64)?;
let read = widened(r, cx, group, g, j)?;
let (mut c2, mut d2, mut o2) = (carries.clone(), digs.clone(), owns.clone());
let mut whole = true;
for (c, m) in read.iter().zip(group) {
let Some(c) = c else {
continue;
};
let Some(ds) = carry_digits(r, cx, c, low(&terms[m.i].1), j, &terms, w)? else {
whole = false;
break;
};
c2[m.i] = Some(*c);
d2[m.i] = Some(ds);
o2[m.i] = None;
}
if !whole || read.iter().all(Option::is_none) {
continue;
}
let other = settled(r, cx, &terms, konst, &c2, &d2, &o2, j)?;
if other.left < wide.as_ref().map_or(base.left, |s| s.left) {
wide = Some(other);
(carries, digs, owns) = (c2, d2, o2);
}
}
}
let taken = wide
.as_ref()
.is_some_and(|s| s.left + 2 <= base.left || pending.iter().all(|m| carries[m.i].is_some()));
let best = match wide {
Some(s) if taken => s,
Some(_) => {
for m in &pending {
carries[m.i] = None;
}
base
}
None => base,
};
if carries.iter().all(Option::is_none) && highs < 2 {
discard(r, cx, before);
return Ok(None);
}
let Settled {
e,
left,
plain,
fin: relied,
stops,
} = best;
fin = fin.and(relied);
let lowered = lowered && carries.iter().all(Option::is_none) && pending.is_empty();
let tied = left >= plain && !(lowered && left == plain);
if tied && !any && !(ties && carries.iter().any(Option::is_some)) {
discard(r, cx, before);
return Ok(None);
}
Ok(Some((e, fin, stops, tied)))
}