[jets] add mix jet

This commit is contained in:
Philip Monk 2023-02-14 20:48:17 -07:00
parent 9825db95ff
commit 0569a26142
2 changed files with 39 additions and 0 deletions

View File

@ -44,6 +44,7 @@ pub fn get_jet(jet_name: Noun) -> Result<Jet, ()> {
tas!(b"rsh") => Ok(jet_rsh),
tas!(b"con") => Ok(jet_con),
tas!(b"dis") => Ok(jet_dis),
tas!(b"mix") => Ok(jet_mix),
tas!(b"cut") => Ok(jet_cut),
tas!(b"met") => Ok(jet_met),
tas!(b"mug") => Ok(jet_mug),

View File

@ -394,6 +394,22 @@ pub fn jet_dis(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
}
}
pub fn jet_mix(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
let arg = raw_slot(subject, 6);
let a = raw_slot(arg, 2).as_atom()?;
let b = raw_slot(arg, 3).as_atom()?;
let new_size = cmp::max(a.size(), b.size());
unsafe {
let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, new_size);
let a_bit = a.as_bitslice();
dest[..a_bit.len()].copy_from_bitslice(a_bit);
*dest ^= b.as_bitslice();
Ok(atom.normalize_as_atom().as_noun())
}
}
pub fn jet_cut(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
let arg = raw_slot(subject, 6);
let bloq = raw_slot(arg, 2).as_direct()?.data() as usize;
@ -925,6 +941,28 @@ mod tests {
assert_math_jet_noun(s, jet_dis, &[atom_128, atom_0], a0);
}
#[test]
fn test_mix() {
let ref mut s = init();
let (_a0, _a24, _a63, _a96, a128) = atoms(s);
assert_math_jet(s, jet_mix, &[atom_0, atom_0], ubig!(0));
assert_math_jet(
s,
jet_mix,
&[atom_24, atom_96],
ubig!(0xfaceb00c15deadbeef955115),
);
assert_math_jet(
s,
jet_mix,
&[atom_96, atom_128],
ubig!(0xdeadbeefe8fae674eb02172699460646),
);
assert_math_jet(s, jet_mix, &[atom_24, atom_63], ubig!(0x7fffffffff789abc));
assert_math_jet_noun(s, jet_mix, &[atom_0, atom_128], a128);
assert_math_jet_noun(s, jet_mix, &[atom_128, atom_0], a128);
}
#[test]
fn test_cut() {
let ref mut s = init();