use super::{
BinOp, Expr, LaneOp, LaneParity, Lifter, Operand, Operation, RegRef, VectorHalf, a64, mask_bits,
};
pub(super) fn simd(operand: Operand) -> Option<(u16, u16)> {
let Operand::SIMDRegister(size, number) = operand else {
return None;
};
let bits = match size {
a64::SIMDSizeCode::B => 8,
a64::SIMDSizeCode::H => 16,
a64::SIMDSizeCode::S => 32,
a64::SIMDSizeCode::D => 64,
a64::SIMDSizeCode::Q => 128,
};
Some((vlk_arch::arch::aarch64::V0 + 2 * number, bits))
}
pub(super) fn lane(operand: Operand) -> Option<(u16, u64, u64)> {
let Operand::SIMDRegisterElementsLane(_, number, size, index) = operand else {
return None;
};
let bits: u64 = match size {
a64::SIMDSizeCode::B => 8,
a64::SIMDSizeCode::H => 16,
a64::SIMDSizeCode::S => 32,
a64::SIMDSizeCode::D => 64,
a64::SIMDSizeCode::Q => return None,
};
let start = u64::from(index) * bits;
let slot = vlk_arch::arch::aarch64::V0 + 2 * number + (start / 64) as u16;
Some((slot, start % 64, bits))
}
pub(super) fn lane_value((slot, shift, bits): (u16, u64, u64)) -> Expr {
let value = Expr::bin(BinOp::Shr, Expr::reg(slot, 64), Expr::constant(shift));
if bits == 64 {
value
} else {
Expr::bin(BinOp::And, value, Expr::constant((1 << bits) - 1))
}
}
pub(super) fn vector(operand: Operand) -> Option<(u16, bool, u8)> {
let (size, number, element) = match operand {
Operand::SIMDRegisterElements(size, number, element) => (size, number, element),
Operand::SIMDRegister(a64::SIMDSizeCode::D, number) => {
(a64::SIMDSizeCode::D, number, a64::SIMDSizeCode::D)
}
_ => return None,
};
let bits = match element {
a64::SIMDSizeCode::B => 8,
a64::SIMDSizeCode::H => 16,
a64::SIMDSizeCode::S => 32,
a64::SIMDSizeCode::D => 64,
a64::SIMDSizeCode::Q => return None,
};
Some((
vlk_arch::arch::aarch64::V0 + 2 * number,
size == a64::SIMDSizeCode::Q,
bits,
))
}
pub(super) fn halves((slot, full, _): (u16, bool, u8)) -> (Expr, Expr) {
let high = if full {
Expr::reg(slot + 1, 64)
} else {
Expr::constant(0)
};
(Expr::reg(slot, 64), high)
}
pub(super) fn immediate_lane(operand: Operand, complement: bool, bits: u8) -> Option<u64> {
let lane = match operand {
Operand::ImmShift(value, shift) => u64::from(value) << shift,
Operand::ImmShiftMSL(value, shift) => (u64::from(value) << shift) | ((1 << shift) - 1),
_ => return None,
};
let lane = if complement { !lane } else { lane };
Some(if bits == 64 {
lane
} else {
lane & ((1u64 << bits) - 1)
})
}
pub(super) fn replicated(lane: u64, bits: u8) -> u64 {
(0..64 / u32::from(bits)).fold(0, |out, index| out | lane << (index * u32::from(bits)))
}
pub(super) fn replicate(value: Expr, bits: u8) -> Expr {
if bits == 64 {
return value;
}
let lane = (1u64 << bits) - 1;
let ones = replicated(1, bits);
Expr::bin(
BinOp::Mul,
Expr::bin(BinOp::And, value, Expr::constant(lane)),
Expr::constant(ones),
)
}
pub(super) fn window(parts: &[Expr], start: u64) -> Expr {
let (index, offset) = ((start / 64) as usize, start % 64);
super::super::windows::window(&parts[index], parts.get(index + 1), offset)
}
pub(super) fn shift_count(operand: Operand) -> Option<u64> {
match operand {
Operand::Immediate(count) => Some(u64::from(count)),
Operand::Imm16(count) => Some(u64::from(count)),
_ => None,
}
}
pub(super) fn source_halves(operand: Operand) -> Option<(Expr, Expr)> {
match operand {
Operand::Immediate(0) | Operand::Imm64(0) | Operand::ImmediateDouble(0.0) => {
Some((Expr::constant(0), Expr::constant(0)))
}
Operand::SIMDRegisterElementsLane(_, number, element, lane) => {
let bits = match element {
a64::SIMDSizeCode::H => 16u64,
a64::SIMDSizeCode::S => 32,
a64::SIMDSizeCode::D => 64,
_ => return None,
};
let bit_offset = u64::from(lane) * bits;
let half = Expr::reg(
vlk_arch::arch::aarch64::V0 + 2 * number + (bit_offset / 64) as u16,
64,
);
let within = bit_offset % 64;
let value = if bits == 64 {
half
} else {
Expr::bin(
BinOp::And,
Expr::bin(BinOp::Shr, half, Expr::constant(within)),
Expr::constant((1u64 << bits) - 1),
)
};
let copied = Expr::bin(BinOp::Mul, value, Expr::constant(replicated(1, bits as u8)));
Some((copied.clone(), copied))
}
operand => {
let Some(vector_register) = vector(operand) else {
return None;
};
Some(halves(vector_register))
}
}
}
pub(super) fn lanewise(op: LaneOp, bits: u8, a: Expr, b: Expr) -> Expr {
Expr::apply(Operation::Lanes(op, bits), vec![a, b])
}
pub(super) fn lane_shift(op: LaneOp, bits: u8, value: Expr, count: u64) -> Expr {
match op {
LaneOp::ShiftLeft if bits == 64 && count < 64 => {
Expr::bin(BinOp::Shl, value, Expr::constant(count))
}
LaneOp::ShiftRight if bits == 64 && count < 64 => {
Expr::bin(BinOp::Shr, value, Expr::constant(count))
}
_ => lanewise(op, bits, value, Expr::constant(count)),
}
}
pub(super) fn by_halves(
(a0, a1): (Expr, Expr),
(b0, b1): (Expr, Expr),
f: impl Fn(Expr, Expr) -> Expr,
) -> (Expr, Expr) {
(f(a0, b0), f(a1, b1))
}
pub(super) fn widened(
(low, high): (Expr, Expr),
upper: VectorHalf,
bits: u8,
signed: bool,
) -> (Expr, Expr) {
let upper = matches!(upper, VectorHalf::Upper);
let half = if upper { high } else { low };
let widen = |high| Expr::apply(Operation::Widen { bits, signed, high }, vec![half.clone()]);
(widen(false), widen(true))
}
pub(super) fn narrowed((low, high): (Expr, Expr), from: u8) -> Expr {
Expr::apply(Operation::Narrow(from), vec![low, high])
}
pub(super) fn doubling_mul_high(a: Expr, b: Expr, bits: u8) -> Expr {
let wide = bits * 2;
let a = widened(
(a, Expr::constant(0)),
VectorHalf::Lower,
bits,
true,
);
let b = widened(
(b, Expr::constant(0)),
VectorHalf::Lower,
bits,
true,
);
let limit = Expr::constant(replicated((1u64 << (bits - 1)) - 1, wide));
let product = |a, b| {
let multiplied = lanewise(LaneOp::Mul, wide, a, b);
let high = lane_shift(
LaneOp::ShiftRightSigned,
wide,
multiplied,
u64::from(bits - 1),
);
lanewise(LaneOp::MinSigned, wide, high, limit.clone())
};
narrowed((product(a.0, b.0), product(a.1, b.1)), wide)
}
pub(super) fn unzip(
a: (Expr, Expr),
b: (Expr, Expr),
bits: u8,
full: bool,
odd: LaneParity,
) -> (Expr, Expr) {
let odd = matches!(odd, LaneParity::Odd);
if bits == 64 {
return if odd { (a.1, b.1) } else { (a.0, b.0) };
}
let wide = 2 * bits;
let pick = |half: Expr| {
if odd {
lane_shift(LaneOp::ShiftRight, wide, half, u64::from(bits))
} else {
half
}
};
let narrow = |x: Expr, y: Expr| narrowed((pick(x), pick(y)), wide);
if full {
(narrow(a.0, a.1), narrow(b.0, b.1))
} else {
(narrow(a.0, b.0), Expr::constant(0))
}
}
pub(super) fn rounding_shift(bits: u8, value: Expr, count: u64, signed: bool) -> Expr {
let op = if signed {
LaneOp::ShiftRightSigned
} else {
LaneOp::ShiftRight
};
let shifted = lane_shift(op, bits, value.clone(), count);
let last = Expr::bin(
BinOp::And,
lane_shift(LaneOp::ShiftRight, bits, value, count - 1),
Expr::constant(replicated(1, bits)),
);
lanewise(LaneOp::Add, bits, shifted, last)
}
pub(super) fn absolute_difference(bits: u8, signed: bool, a: Expr, b: Expr) -> Expr {
let (max, min) = if signed {
(LaneOp::MaxSigned, LaneOp::MinSigned)
} else {
(LaneOp::MaxUnsigned, LaneOp::MinUnsigned)
};
lanewise(
LaneOp::Sub,
bits,
lanewise(max, bits, a.clone(), b.clone()),
lanewise(min, bits, a, b),
)
}
pub(super) fn complement(value: Expr) -> Expr {
Expr::bin(BinOp::Xor, value, Expr::constant(u64::MAX))
}
pub(super) fn write_narrow(
lifter: &mut Lifter,
(slot, _, _): (u16, bool, u8),
upper: bool,
value: Expr,
) {
if upper {
lifter.assign(
RegRef {
index: slot + 1,
width: 64,
},
value,
);
} else {
lifter.write_simd(slot, 128, (value, Expr::constant(0)));
}
}
pub(super) fn reverse_lanes(half: Expr, bits: u8, part: u8) -> Expr {
let (bits, per_part) = (u64::from(bits), u64::from(part / bits));
(0..64 / bits)
.map(|index| {
let target = index - index % per_part + (per_part - 1 - index % per_part);
let lane = Expr::bin(
BinOp::And,
Expr::bin(BinOp::Shr, half.clone(), Expr::constant(bits * index)),
Expr::constant(mask_bits(bits)),
);
Expr::bin(BinOp::Shl, lane, Expr::constant(bits * target))
})
.reduce(|all, lane| Expr::bin(BinOp::Or, all, lane))
.unwrap_or(half)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn windows_match_adjacent_half_expressions() {
let parts = [Expr::reg(0, 64), Expr::reg(1, 64), Expr::reg(2, 64)];
for count in 1..=parts.len() {
for start in 0..(count as u64 * 64) {
let (index, offset) = ((start / 64) as usize, start % 64);
let low = Expr::bin(BinOp::Shr, parts[index].clone(), Expr::Const(offset));
let expected = match parts[..count].get(index + 1) {
Some(next) if offset > 0 => Expr::bin(
BinOp::Or,
low,
Expr::bin(BinOp::Shl, next.clone(), Expr::Const(64 - offset)),
),
_ => low,
};
assert_eq!(window(&parts[..count], start), expected);
}
}
}
#[test]
#[should_panic(expected = "index out of bounds")]
fn windows_require_a_first_half() {
window(&[], 0);
}
}