jets: reorganize jets

This commit is contained in:
Alex Shelkovnykov 2023-09-04 13:20:45 -06:00
parent e9f203e4bd
commit d187f64d5a
4 changed files with 1054 additions and 921 deletions

View File

@ -1,10 +1,14 @@
pub mod math;
pub mod mink;
pub mod tree;
pub mod bits;
pub mod hash;
pub mod mink;
use crate::jets::math::*;
use crate::jets::mink::*;
use crate::jets::tree::*;
use crate::jets::bits::*;
use crate::jets::hash::*;
use crate::jets::mink::*;
use crate::mem::NockStack;
use crate::newt::Newt;
use crate::noun::{self, Noun, Slots};
@ -40,37 +44,40 @@ impl From<JetErr> for () {
pub fn get_jet(jet_name: Noun) -> Option<Jet> {
match jet_name.as_direct().ok()?.data() {
tas!(b"dec") => Some(jet_dec),
tas!(b"add") => Some(jet_add),
tas!(b"sub") => Some(jet_sub),
tas!(b"mul") => Some(jet_mul),
tas!(b"dec") => Some(jet_dec),
tas!(b"div") => Some(jet_div),
tas!(b"mod") => Some(jet_mod),
tas!(b"dvr") => Some(jet_dvr),
tas!(b"lth") => Some(jet_lth),
tas!(b"lte") => Some(jet_lte),
tas!(b"gth") => Some(jet_gth),
tas!(b"gte") => Some(jet_gte),
tas!(b"bex") => Some(jet_bex),
tas!(b"lsh") => Some(jet_lsh),
tas!(b"rsh") => Some(jet_rsh),
tas!(b"con") => Some(jet_con),
tas!(b"dis") => Some(jet_dis),
tas!(b"mix") => Some(jet_mix),
tas!(b"end") => Some(jet_end),
tas!(b"cat") => Some(jet_cat),
tas!(b"cut") => Some(jet_cut),
tas!(b"can") => Some(jet_can),
tas!(b"rap") => Some(jet_rap),
tas!(b"rep") => Some(jet_rep),
tas!(b"rip") => Some(jet_rip),
tas!(b"met") => Some(jet_met),
tas!(b"mug") => Some(jet_mug),
tas!(b"rev") => Some(jet_rev),
tas!(b"gth") => Some(jet_gth),
tas!(b"lte") => Some(jet_lte),
tas!(b"lth") => Some(jet_lth),
tas!(b"mod") => Some(jet_mod),
tas!(b"mul") => Some(jet_mul),
tas!(b"sub") => Some(jet_sub),
//
tas!(b"cap") => Some(jet_cap),
tas!(b"mas") => Some(jet_mas),
//
tas!(b"bex") => Some(jet_bex),
tas!(b"can") => Some(jet_can),
tas!(b"cat") => Some(jet_cat),
tas!(b"cut") => Some(jet_cut),
tas!(b"end") => Some(jet_end),
tas!(b"lsh") => Some(jet_lsh),
tas!(b"met") => Some(jet_met),
tas!(b"rap") => Some(jet_rap),
tas!(b"rep") => Some(jet_rep),
tas!(b"rev") => Some(jet_rev),
tas!(b"rip") => Some(jet_rip),
tas!(b"rsh") => Some(jet_rsh),
//
tas!(b"con") => Some(jet_con),
tas!(b"dis") => Some(jet_dis),
tas!(b"mix") => Some(jet_mix),
//
tas!(b"mug") => Some(jet_mug),
//
tas!(b"mink") => Some(jet_mink),
_ => {
// eprintln!("Unknown jet: {:?}", jet_name);

797
rust/ares/src/jets/bits.rs Normal file
View File

@ -0,0 +1,797 @@
/** Bit arithmetic & logic jets
*/
use crate::jets;
use crate::jets::JetErr::*;
use crate::jets::util::*;
use crate::mem::NockStack;
use crate::newt::Newt;
use crate::noun::{Cell, DirectAtom, IndirectAtom, Noun, D};
use std::cmp;
crate::gdb!();
/*
* Bit arithmetic
*/
pub fn jet_bex(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?.as_direct()?.data() as usize;
Ok(bex(stack, arg).as_noun())
}
pub fn jet_can(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let bloq = bloq(slot(arg, 2)?)?;
let original_list = slot(arg, 3)?;
let mut len = 0usize;
let mut list = original_list;
loop {
if unsafe { list.raw_equals(D(0)) } {
break;
}
let cell = list.as_cell()?;
let item = cell.head().as_cell()?;
let step = item.head().as_direct()?.data() as usize;
len = checked_add(len, step)?;
list = cell.tail();
}
if len == 0 {
Ok(D(0))
} else {
unsafe {
let (mut new_indirect, new_slice) = IndirectAtom::new_raw_mut_bitslice(stack, bite_to_word(bloq, len)?);
let mut pos = 0;
let mut list = original_list;
loop {
if list.raw_equals(D(0)) {
break;
}
let cell = list.as_cell()?;
let item = cell.head().as_cell()?;
let step = item.head().as_direct()?.data() as usize;
let atom = item.tail().as_atom()?;
chop(bloq, 0, step, pos, new_slice, atom.as_bitslice())?;
pos += step;
list = cell.tail();
}
Ok(new_indirect.normalize_as_atom().as_noun())
}
}
}
pub fn jet_cat(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let bloq = bloq(slot(arg, 2)?)?;
let a = slot(arg, 6)?.as_atom()?;
let b = slot(arg, 7)?.as_atom()?;
let len_a = met(bloq, a);
let len_b = met(bloq, b);
let new_len = bite_to_word(bloq, checked_add(len_a, len_b)?)?;
if new_len == 0 {
Ok(a.as_noun())
} else {
unsafe {
let (mut new_indirect, new_slice) = IndirectAtom::new_raw_mut_bitslice(stack, new_len);
chop(bloq, 0, len_a, 0, new_slice, a.as_bitslice())?;
chop(bloq, 0, len_b, len_a, new_slice, b.as_bitslice())?;
Ok(new_indirect.normalize_as_atom().as_noun())
}
}
}
pub fn jet_cut(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let bloq = bloq(slot(arg, 2)?)?;
let start = slot(arg, 12)?.as_direct()?.data() as usize;
let run = slot(arg, 13)?.as_direct()?.data() as usize;
let atom = slot(arg, 7)?.as_atom()?;
let new_indirect = unsafe {
let (mut new_indirect, new_slice) = IndirectAtom::new_raw_mut_bitslice(stack, bite_to_word(bloq, run)?);
chop(bloq, start, run, 0, new_slice, atom.as_bitslice())?;
new_indirect.normalize_as_atom()
};
Ok(new_indirect.as_noun())
}
pub fn jet_end(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let (bloq, step) = bite(slot(arg, 2)?)?;
let a = slot(arg, 3)?.as_atom()?;
if step == 0 {
Ok(D(0))
} else if step >= met(bloq, a) {
Ok(a.as_noun())
} else {
unsafe {
let (mut new_indirect, new_slice) = IndirectAtom::new_raw_mut_bitslice(stack, bite_to_word(bloq, step)?);
chop(bloq, 0, step, 0, new_slice, a.as_bitslice())?;
Ok(new_indirect.normalize_as_atom().as_noun())
}
}
}
pub fn jet_lsh(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let (bloq, step) = bite(slot(arg, 2)?)?;
let a = slot(arg, 3)?.as_atom()?;
let len = met(bloq, a);
if len == 0 {
return Ok(D(0));
}
let new_size = bits_to_word(checked_add(a.bit_size(), checked_left_shift(bloq, step)?)?)?;
unsafe {
let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, new_size);
chop(bloq, 0, len, step, dest, a.as_bitslice())?;
Ok(atom.normalize_as_atom().as_noun())
}
}
pub fn jet_met(
_stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let bloq = bloq(slot(arg, 2)?)?;
let a = slot(arg, 3)?.as_atom()?;
Ok(D(met(bloq, a) as u64))
}
pub fn jet_rap(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let bloq = bloq(slot(arg, 2)?)?;
let original_list = slot(arg, 3)?;
let mut len = 0usize;
let mut list = original_list;
loop {
if unsafe { list.raw_equals(D(0)) } {
break;
}
let cell = list.as_cell()?;
len = checked_add(len, met(bloq, cell.head().as_atom()?))?;
list = cell.tail();
}
if len == 0 {
Ok(D(0))
} else {
unsafe {
let (mut new_indirect, new_slice) = IndirectAtom::new_raw_mut_bitslice(stack, bite_to_word(bloq, len)?);
let mut pos = 0;
let mut list = original_list;
loop {
if list.raw_equals(D(0)) {
break;
}
let cell = list.as_cell()?;
let atom = cell.head().as_atom()?;
let step = met(bloq, atom);
chop(bloq, 0, step, pos, new_slice, atom.as_bitslice())?;
pos += step;
list = cell.tail();
}
Ok(new_indirect.normalize_as_atom().as_noun())
}
}
}
pub fn jet_rep(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let (bloq, step) = bite(slot(arg, 2)?)?;
let original_list = slot(arg, 3)?;
let mut len = 0usize;
let mut list = original_list;
loop {
if unsafe { list.raw_equals(D(0)) } {
break;
}
let cell = list.as_cell()?;
len = checked_add(len, step)?;
list = cell.tail();
}
if len == 0 {
Ok(D(0))
} else {
unsafe {
let (mut new_indirect, new_slice) = IndirectAtom::new_raw_mut_bitslice(stack, bite_to_word(bloq, len)?);
let mut pos = 0;
let mut list = original_list;
loop {
if list.raw_equals(D(0)) {
break;
}
let cell = list.as_cell()?;
let atom = cell.head().as_atom()?;
chop(bloq, 0, step, pos, new_slice, atom.as_bitslice())?;
pos += step;
list = cell.tail();
}
Ok(new_indirect.normalize_as_atom().as_noun())
}
}
}
pub fn jet_rev(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let boz = slot(arg, 2)?.as_atom()?.as_direct()?.data();
if boz >= 64 {
return Err(NonDeterministic);
}
let boz = boz as usize;
let len = slot(arg, 6)?.as_atom()?.as_direct()?.data();
let dat = slot(arg, 7)?.as_atom()?;
let bits = len << boz;
/* 63 is the maximum number of bits for a direct atom */
let mut output = if dat.is_direct() && bits < 64 {
unsafe { DirectAtom::new_unchecked(0).as_atom() }
} else {
unsafe { IndirectAtom::new_raw(stack, ((bits + 7) / 8) as usize, &0).as_atom() }
};
let src = dat.as_bitslice();
let dest = output.as_bitslice_mut();
let len = len as usize;
let total_len = len << boz;
for (start, end) in (0..len).map(|b| (b << boz, (b + 1) << boz)) {
dest[start..end].copy_from_bitslice(&src[(total_len - end)..(total_len - start)]);
}
Ok(unsafe { output.normalize() }.as_noun())
}
pub fn jet_rip(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let (bloq, step) = bite(slot(arg, 2)?)?;
let atom = slot(arg, 3)?.as_atom()?;
let len = (met(bloq, atom) + step - 1) / step;
let mut list = D(0);
for i in (0..len).rev() {
let new_atom = unsafe {
let (mut new_indirect, new_slice) =
IndirectAtom::new_raw_mut_bitslice(stack, step << bloq);
chop(bloq, i * step, step, 0, new_slice, atom.as_bitslice())?;
new_indirect.normalize_as_atom()
};
list = Cell::new(stack, new_atom.as_noun(), list).as_noun();
}
Ok(list)
}
pub fn jet_rsh(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let (bloq, step) = bite(slot(arg, 2)?)?;
let a = slot(arg, 3)?.as_atom()?;
let len = met(bloq, a);
if step >= len {
return Ok(D(0));
}
let new_size = bits_to_word(checked_sub(a.bit_size(), checked_left_shift(bloq, step)?)?)?;
unsafe {
let (mut atom, dest) = IndirectAtom::new_raw_mut_bitslice(stack, new_size);
chop(bloq, step, len - step, 0, dest, a.as_bitslice())?;
Ok(atom.normalize_as_atom().as_noun())
}
}
/*
* Bit logic
*/
pub fn jet_con(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let a = slot(arg, 2)?.as_atom()?;
let b = slot(arg, 3)?.as_atom()?;
Ok(con(stack, a, b).as_noun())
}
pub fn jet_dis(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let a = slot(arg, 2)?.as_atom()?;
let b = 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_mix(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> jets::Result {
let arg = slot(subject, 6)?;
let a = slot(arg, 2)?.as_atom()?;
let b = 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())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::jets::Jet;
use crate::jets::util::test::{assert_jet, assert_jet_ubig, assert_nary_jet_ubig, init_stack, A};
use crate::mem::NockStack;
use crate::noun::{Noun, D, T};
use ibig::{UBig, ubig};
fn atoms(s: &mut NockStack) -> (Noun, Noun, Noun, Noun, Noun) {
(atom_0(s), atom_24(s), atom_63(s), atom_96(s), atom_128(s))
}
fn atom_0(_stack: &mut NockStack) -> Noun {
D(0)
}
fn atom_24(_stack: &mut NockStack) -> Noun {
D(0x876543)
}
fn atom_63(_stack: &mut NockStack) -> Noun {
D(0x7fffffffffffffff)
}
fn atom_96(stack: &mut NockStack) -> Noun {
A(stack, &ubig!(0xfaceb00c15deadbeef123456))
}
fn atom_128(stack: &mut NockStack) -> Noun {
A(stack, &ubig!(0xdeadbeef12345678fedcba9876543210))
}
fn assert_math_jet(
stack: &mut NockStack,
jet: Jet,
sam: &[fn(&mut NockStack) -> Noun],
res: UBig,
) {
let sam: Vec<Noun> = sam.iter().map(|f| f(stack)).collect();
assert_nary_jet_ubig(stack, jet, &sam, res);
}
fn assert_math_jet_noun(
stack: &mut NockStack,
jet: Jet,
sam: &[fn(&mut NockStack) -> Noun],
res: Noun,
) {
let sam: Vec<Noun> = sam.iter().map(|f| f(stack)).collect();
let sam = T(stack, &sam);
assert_jet(stack, jet, sam, res);
}
/*
* Bit arithmetic
*/
#[test]
fn test_bex() {
let s = &mut init_stack();
assert_jet(s, jet_bex, D(0), D(1));
assert_jet(s, jet_bex, D(5), D(32));
assert_jet(s, jet_bex, D(62), D(0x4000000000000000));
assert_jet_ubig(
s,
jet_bex,
D(256),
ubig!(_0x10000000000000000000000000000000000000000000000000000000000000000),
);
}
#[test]
fn test_can() {
let s = &mut init_stack();
let (a0, _a24, _a63, _a96, a128) = atoms(s);
let bloq0 = D(0);
let bloq3 = D(3);
let bloq4 = D(4);
let sam = T(s, &[bloq0, D(0)]);
assert_jet(s, jet_can, sam, D(0));
let sam = T(s, &[bloq3, D(0)]);
assert_jet(s, jet_can, sam, D(0));
let run1 = T(s, &[D(0), a0]);
let run2 = T(s, &[D(1), a0]);
let run3 = T(s, &[D(2), a0]);
let sam = T(s, &[bloq0, run1, run2, run3, D(0)]);
assert_jet(s, jet_can, sam, D(0));
let sam = T(s, &[bloq3, run1, run2, run3, D(0)]);
assert_jet(s, jet_can, sam, D(0));
let run1 = T(s, &[D(1), a128]);
let run2 = T(s, &[D(3), a0]);
let sam = T(s, &[bloq3, run1, run2, D(0)]);
assert_jet(s, jet_can, sam, D(0x10));
let run1 = T(s, &[D(3), a0]);
let run2 = T(s, &[D(1), a128]);
let sam = T(s, &[bloq3, run1, run2, D(0)]);
assert_jet(s, jet_can, sam, D(0x10000000));
let run1 = T(s, &[D(8), D(0xfe)]);
let run2 = T(s, &[D(4), D(0xa)]);
let run3 = T(s, &[D(0), D(0xbbbb)]);
let run4 = T(s, &[D(1), D(0)]);
let run5 = T(s, &[D(1), D(0)]);
let run6 = T(s, &[D(1), D(1)]);
let run7 = T(s, &[D(1), D(1)]);
let sam = T(s, &[bloq0, run1, run2, run3, run4, run5, run6, run7, D(0)]);
assert_jet(s, jet_can, sam, D(0xcafe));
let run1 = T(s, &[D(1), D(0xfe)]);
let run2 = T(s, &[D(1), D(0xca)]);
let sam = T(s, &[bloq4, run1, run2, D(0)]);
assert_jet(s, jet_can, sam, D(0xca00fe));
}
#[test]
fn test_cat() {
let s = &mut init_stack();
let (a0, a24, _a63, _a96, a128) = atoms(s);
let bloq0 = D(0);
let bloq3 = D(3);
let bloq4 = D(4);
let sam = T(s, &[bloq0, a0, a0]);
assert_jet(s, jet_cat, sam, D(0));
let sam = T(s, &[bloq3, a0, a0]);
assert_jet(s, jet_cat, sam, D(0));
let sam = T(s, &[bloq0, a24, a128]);
let res = A(s, &ubig!(_0xdeadbeef12345678fedcba9876543210876543));
assert_jet(s, jet_cat, sam, res);
let sam = T(s, &[bloq3, a24, a128]);
let res = A(s, &ubig!(_0xdeadbeef12345678fedcba9876543210876543));
assert_jet(s, jet_cat, sam, res);
let sam = T(s, &[bloq4, a24, a128]);
let res = A(s, &ubig!(_0xdeadbeef12345678fedcba987654321000876543));
assert_jet(s, jet_cat, sam, res);
}
#[test]
fn test_cut() {
let s = &mut init_stack();
let (_a0, a24, _a63, a96, a128) = atoms(s);
let run = T(s, &[D(0), D(5)]);
let sam = T(s, &[D(0), run, a24]);
assert_jet(s, jet_cut, sam, D(0x3));
let run = T(s, &[D(4), D(6)]);
let sam = T(s, &[D(3), run, a96]);
assert_jet(s, jet_cut, sam, D(0xb00c15deadbe));
let run = T(s, &[D(4), D(1)]);
let sam = T(s, &[D(4), run, a24]);
assert_jet(s, jet_cut, sam, D(0));
let run = T(s, &[D(2), D(10)]);
let sam = T(s, &[D(4), run, a128]);
let res = A(s, &ubig!(0xdeadbeef12345678fedcba98));
assert_jet(s, jet_cut, sam, res);
}
#[test]
fn test_end() {
let s = &mut init_stack();
let (a0, a24, _a63, a96, a128) = atoms(s);
let sam = T(s, &[a0, a24]);
assert_jet(s, jet_end, sam, D(0x1));
let sam = T(s, &[D(3), a24]);
assert_jet(s, jet_end, sam, D(0x43));
let sam = T(s, &[D(7), a24]);
assert_jet(s, jet_end, sam, a24);
let sam = T(s, &[D(6), a128]);
let res = A(s, &ubig!(0xfedcba9876543210));
assert_jet(s, jet_end, sam, res);
let bit = T(s, &[D(0), D(5)]);
let sam = T(s, &[bit, a24]);
assert_jet(s, jet_end, sam, D(0x3));
let bit = T(s, &[D(4), D(6)]);
let sam = T(s, &[bit, a96]);
assert_jet(s, jet_end, sam, a96);
}
#[test]
fn test_lsh() {
let s = &mut init_stack();
let (a0, a24, _a63, a96, a128) = atoms(s);
let sam = T(s, &[a0, a24]);
assert_jet(s, jet_lsh, sam, D(0x10eca86));
let sam = T(s, &[D(3), a24]);
assert_jet(s, jet_lsh, sam, D(0x87654300));
let sam = T(s, &[D(7), a24]);
let res = A(s, &ubig!(_0x87654300000000000000000000000000000000));
assert_jet(s, jet_lsh, sam, res);
let sam = T(s, &[D(6), a128]);
let res = A(
s,
&ubig!(_0xdeadbeef12345678fedcba98765432100000000000000000),
);
assert_jet(s, jet_lsh, sam, res);
let bit = T(s, &[D(0), D(5)]);
let sam = T(s, &[bit, a24]);
assert_jet(s, jet_lsh, sam, D(0x10eca860));
let bit = T(s, &[D(4), D(6)]);
let sam = T(s, &[bit, a96]);
let res = A(
s,
&ubig!(_0xfaceb00c15deadbeef123456000000000000000000000000),
);
assert_jet(s, jet_lsh, sam, res);
}
#[test]
fn test_met() {
let s = &mut init_stack();
let (a0, a24, _a63, _a96, a128) = atoms(s);
let sam = T(s, &[a0, a0]);
assert_jet(s, jet_met, sam, D(0));
let sam = T(s, &[a0, a24]);
assert_jet(s, jet_met, sam, D(24));
let sam = T(s, &[D(3), a24]);
assert_jet(s, jet_met, sam, D(3));
let sam = T(s, &[D(1), a128]);
assert_jet(s, jet_met, sam, D(64));
}
#[test]
fn test_rap() {
let s = &mut init_stack();
let bloq0 = D(0);
let bloq2 = D(2);
let bloq3 = D(3);
let empty_list = D(0);
let zero_list = T(s, &[D(0), D(0), D(0)]);
let test_list = T(s, &[D(0xe), D(0xf), D(0xa), D(0xc), D(0)]);
let wide_list = T(s, &[D(0xafe), D(0xc), D(0)]);
let sam = T(s, &[bloq0, empty_list]);
assert_jet(s, jet_rap, sam, D(0));
let sam = T(s, &[bloq0, zero_list]);
assert_jet(s, jet_rap, sam, D(0));
let sam = T(s, &[bloq3, zero_list]);
assert_jet(s, jet_rap, sam, D(0));
let sam = T(s, &[bloq0, test_list]);
assert_jet(s, jet_rap, sam, D(0xcafe));
let sam = T(s, &[bloq2, test_list]);
assert_jet(s, jet_rap, sam, D(0xcafe));
let sam = T(s, &[bloq2, wide_list]);
assert_jet(s, jet_rap, sam, D(0xcafe));
let sam = T(s, &[bloq3, test_list]);
let res = A(s, &ubig!(0xc0a0f0e));
assert_jet(s, jet_rap, sam, res);
let sam = T(s, &[bloq3, wide_list]);
assert_jet(s, jet_rap, sam, D(0xc0afe));
}
#[test]
fn test_rep() {
let s = &mut init_stack();
let (a0, a24, a63, a96, a128) = atoms(s);
let sam = T(s, &[D(0), D(0)]);
assert_jet(s, jet_rep, sam, D(0));
let bit = T(s, &[D(3), D(2)]);
let sam = T(s, &[bit, a0, a24, a63, a96, a128, D(0)]);
let res = A(s, &ubig!(0x32103456ffff65430000));
assert_jet(s, jet_rep, sam, res);
}
#[test]
fn test_rev() {
let s = &mut init_stack();
let (_a0, a24, _a63, _a96, _a128) = atoms(s);
let sam = T(s, &[D(0), D(60), a24]);
assert_jet(s, jet_rev, sam, D(0xc2a6e1000000000));
let test = 0x1234567890123u64;
let sam = T(s, &[D(3), D(7), D(test)]);
assert_jet(s, jet_rev, sam, D(test.swap_bytes() >> 8));
}
#[test]
fn test_rip() {
let s = &mut init_stack();
let (_a0, _a24, _a63, _a96, a128) = atoms(s);
let sam = T(s, &[D(0), D(0)]);
assert_jet(s, jet_rip, sam, D(0));
let bit = T(s, &[D(1), D(2)]);
let sam = T(s, &[bit, a128]);
#[rustfmt::skip]
let res = T(
s,
&[
D(0x0), D(0x1), D(0x2), D(0x3), D(0x4), D(0x5), D(0x6), D(0x7),
D(0x8), D(0x9), D(0xa), D(0xb), D(0xc), D(0xd), D(0xe), D(0xf),
D(0x8), D(0x7), D(0x6), D(0x5), D(0x4), D(0x3), D(0x2), D(0x1),
D(0xf), D(0xe), D(0xe), D(0xb), D(0xd), D(0xa), D(0xe), D(0xd),
D(0x0),
],
);
assert_jet(s, jet_rip, sam, res);
}
#[test]
fn test_rsh() {
let s = &mut init_stack();
let (a0, a24, _a63, a96, a128) = atoms(s);
let sam = T(s, &[a0, a24]);
assert_jet(s, jet_rsh, sam, D(0x43b2a1));
let sam = T(s, &[D(3), a24]);
assert_jet(s, jet_rsh, sam, D(0x8765));
let sam = T(s, &[D(7), a24]);
assert_jet(s, jet_rsh, sam, D(0));
let sam = T(s, &[D(2), a128]);
let res = A(s, &ubig!(0xdeadbeef12345678fedcba987654321));
assert_jet(s, jet_rsh, sam, res);
let sam = T(s, &[D(6), a128]);
let res = A(s, &ubig!(0xdeadbeef12345678));
assert_jet(s, jet_rsh, sam, res);
let bit = T(s, &[D(0), D(5)]);
let sam = T(s, &[bit, a24]);
assert_jet(s, jet_rsh, sam, D(0x43b2a));
let bit = T(s, &[D(4), D(6)]);
let sam = T(s, &[bit, a96]);
assert_jet(s, jet_rsh, sam, D(0));
}
/*
* Bit logic
*/
#[test]
fn test_con() {
let s = &mut init_stack();
let (_a0, _a24, a63, _a96, a128) = atoms(s);
assert_math_jet(s, jet_con, &[atom_0, atom_0], ubig!(0));
assert_math_jet(
s,
jet_con,
&[atom_24, atom_96],
ubig!(0xfaceb00c15deadbeef977557),
);
assert_math_jet(
s,
jet_con,
&[atom_96, atom_128],
ubig!(0xdeadbeeffafef67cffdebfbeff563656),
);
assert_math_jet_noun(s, jet_con, &[atom_24, atom_63], a63);
assert_math_jet_noun(s, jet_con, &[atom_0, atom_128], a128);
assert_math_jet_noun(s, jet_con, &[atom_128, atom_0], a128);
}
#[test]
fn test_dis() {
let s = &mut init_stack();
let (a0, a24, _a63, _a96, _a128) = atoms(s);
assert_math_jet(s, jet_dis, &[atom_0, atom_0], ubig!(0));
assert_math_jet(s, jet_dis, &[atom_24, atom_96], ubig!(0x22442));
assert_math_jet(
s,
jet_dis,
&[atom_96, atom_128],
ubig!(0x1204100814dca89866103010),
);
assert_math_jet_noun(s, jet_dis, &[atom_24, atom_63], a24);
assert_math_jet_noun(s, jet_dis, &[atom_0, atom_128], a0);
assert_math_jet_noun(s, jet_dis, &[atom_128, atom_0], a0);
}
#[test]
fn test_mix() {
let s = &mut init_stack();
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);
}
}

View File

@ -0,0 +1,73 @@
/** Hash jets
*/
use crate::jets::Result;
use crate::jets::util::*;
use crate::mem::NockStack;
use crate::mug::mug;
use crate::newt::Newt;
use crate::noun::Noun;
crate::gdb!();
pub fn jet_mug(
stack: &mut NockStack,
_newt: &mut Option<&mut Newt>,
subject: Noun,
) -> Result {
let arg = slot(subject, 6)?;
Ok(mug(stack, arg).as_noun())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::jets::util::test::{assert_jet, init_stack, A};
use crate::mem::NockStack;
use crate::noun::{Noun, D, T};
use ibig::ubig;
fn atoms(s: &mut NockStack) -> (Noun, Noun, Noun, Noun, Noun) {
(atom_0(s), atom_24(s), atom_63(s), atom_96(s), atom_128(s))
}
fn atom_0(_stack: &mut NockStack) -> Noun {
D(0)
}
fn atom_24(_stack: &mut NockStack) -> Noun {
D(0x876543)
}
fn atom_63(_stack: &mut NockStack) -> Noun {
D(0x7fffffffffffffff)
}
fn atom_96(stack: &mut NockStack) -> Noun {
A(stack, &ubig!(0xfaceb00c15deadbeef123456))
}
fn atom_128(stack: &mut NockStack) -> Noun {
A(stack, &ubig!(0xdeadbeef12345678fedcba9876543210))
}
#[test]
fn test_mug() {
let s = &mut init_stack();
let (a0, a24, a63, a96, a128) = atoms(s);
assert_jet(s, jet_mug, a0, D(0x79ff04e8));
assert_jet(s, jet_mug, a24, D(0x69d59d90));
assert_jet(s, jet_mug, a63, D(0x7a9f252e));
assert_jet(s, jet_mug, a96, D(0x2aa4c8fb));
assert_jet(s, jet_mug, a128, D(0x44fb2c0c));
let sam = T(s, &[a128, a128]);
assert_jet(s, jet_mug, sam, D(0x61c0ea5c));
let sam = T(s, &[a96, a128]);
assert_jet(s, jet_mug, sam, D(0x20fb143f));
let sam = T(s, &[a0, a0]);
assert_jet(s, jet_mug, sam, D(0x192f5588));
let sam = T(s, &[a0, a24, a63, a96, a128]);
let sam = T(s, &[sam, a0, a24, a63, a96, a128]);
let sam = T(s, &[sam, a0, a24, a63, a96, a128]);
assert_jet(s, jet_mug, sam, D(0x7543cac7));
}
}

File diff suppressed because it is too large Load Diff