repn jets pass without jet.

This commit is contained in:
Benjamin Summers 2019-12-09 16:03:43 -08:00 committed by Logan Allen
parent 0912b28fc4
commit 95e307c927
2 changed files with 132 additions and 4 deletions

View File

@ -1,9 +1,15 @@
/+ *test /+ *test
|% |%
++ repn
|= [bits=@ud x=(list @)]
=| c=@ud
|- ^- @
?~ x 0
(add (lsh 0 (mul bits c) (end 0 bits i.x)) $(c +(c), x t.x))
++ test-bits ++ test-bits
;: weld ;: weld
:: ::
:: Random sanity testing :: ripn: Random sanity testing
:: ::
%+ expect-eq %+ expect-eq
!> ~[0x3 0x7 0x7] !> ~[0x3 0x7 0x7]
@ -15,13 +21,13 @@
!> ~[0x1 0xe 0xe 0xf 0xf] !> ~[0x1 0xe 0xe 0xf 0xf]
!> (flop (ripn 4 0x1.eeff)) !> (flop (ripn 4 0x1.eeff))
:: ::
:: Typical use-cases :: ripn: Typical use-cases
:: ::
%+ expect-eq %+ expect-eq
!> ~[0x1 0x23.4567 0x89.abcd] !> ~[0x1 0x23.4567 0x89.abcd]
!> (flop (ripn 24 0x1.2345.6789.abcd)) !> (flop (ripn 24 0x1.2345.6789.abcd))
:: ::
:: Edge cases :: ripn: Edge cases
:: ::
%+ expect-eq %+ expect-eq
!> ~ !> ~
@ -30,7 +36,7 @@
!> ~ !> ~
!> (flop (ripn 1 0x0)) !> (flop (ripn 1 0x0))
:: ::
:: Word boundaries :: ripn: Word boundaries
:: ::
%+ expect-eq %+ expect-eq
!> ~[0x7fff.ffff] !> ~[0x7fff.ffff]
@ -50,6 +56,54 @@
%+ expect-eq %+ expect-eq
!> ~[0x123 0x456 0x789 0xabc 0xdef 0x12 0x345 0x678] !> ~[0x123 0x456 0x789 0xabc 0xdef 0x12 0x345 0x678]
!> (flop (ripn 12 0x1234.5678.9abc.def0.1234.5678)) !> (flop (ripn 12 0x1234.5678.9abc.def0.1234.5678))
::
:: repn: Random sanity testing
::
%+ expect-eq
!> 0xff
!> (repn 3 (flop ~[0x3 0x7 0x7]))
%+ expect-eq
!> 0x1.eeff
!> (repn 8 (flop ~[0x1 0xee 0xff]))
%+ expect-eq
!> 0x1.eeff
!> (repn 4 (flop ~[0x1 0xe 0xe 0xf 0xf]))
::
:: repn: Typical use-cases
::
%+ expect-eq
!> 0x1.2345.6789.abcd
!> (repn 24 (flop ~[0x1 0x23.4567 0x89.abcd]))
::
:: repn: Edge cases
::
%+ expect-eq
!> 0x0
!> (repn 31 (flop ~))
%+ expect-eq
!> 0x0
!> (repn 1 (flop ~))
::
:: repn: Word boundaries
::
%+ expect-eq
!> 0x7fff.ffff
!> (repn 31 (flop ~[0x7fff.ffff]))
%+ expect-eq
!> 0xffff.ffff
!> (repn 31 (flop ~[0x1 0x7fff.ffff]))
%+ expect-eq
!> 0x1.ffff.ffff
!> (repn 31 (flop ~[0x3 0x7fff.ffff]))
%+ expect-eq
!> 0xffff.ffff.ffff.ffff
!> (repn 31 (flop ~[0x3 0x7fff.ffff 0x7fff.ffff]))
%+ expect-eq
!> 0x7.ffff.ffff
!> (repn 17 (flop ~[0x1 0x1.ffff 0x1.ffff]))
%+ expect-eq
!> 0x1234.5678.9abc.def0.1234.5678
!> (repn 12 (flop ~[0x123 0x456 0x789 0xabc 0xdef 0x12 0x345 0x678]))
== ==
:: ::
-- --

74
pkg/urbit/jets/c/repn.c Normal file
View File

@ -0,0 +1,74 @@
#include "all.h"
/*
Get the lowest `n` bits of a word `w` using a bitmask.
*/
#define TAKEBITS(n,w) \
((n)==32) ? (w) : \
((n)==0) ? 0 : \
((w) & ((1 << (n)) - 1))
/*
Divide, rounding up.
*/
#define DIVCEIL(x,y) \
(x==0) ? 0 : \
1 + ((x - 1) / y);
u3_noun u3qc_repn(u3_atom bits, u3_atom atom) {
if ( !_(u3a_is_cat(bits) || bits==0 || bits>31) ) {
return u3m_bail(c3__fail);
}
c3_w bit_width = u3r_met(0, atom);
c3_w num_blocks = DIVCEIL(bit_width, bits);
u3_noun res = u3_nul;
for ( c3_w blk = 0; blk < num_blocks; blk++ ) {
c3_w next_blk = blk + 1;
c3_w blks_rem = num_blocks - next_blk;
c3_w bits_rem = blks_rem * bits;
c3_w ins_idx = bits_rem / 32;
c3_w sig_idx = ins_idx + 1;
c3_w bits_rem_in_ins_word = bits_rem % 32;
c3_w ins_word = u3r_word(ins_idx, atom);
c3_w sig_word = u3r_word(sig_idx, atom);
c3_w nbits_ins = c3_min(bits, 32 - bits_rem_in_ins_word);
c3_w nbits_sig = bits - nbits_ins;
c3_w ins_word_bits = TAKEBITS(nbits_ins, ins_word >> bits_rem_in_ins_word);
c3_w sig_word_bits = TAKEBITS(nbits_sig, sig_word);
c3_w item = ins_word_bits | (sig_word_bits << nbits_ins);
res = u3nc(item, res);
}
return res;
}
u3_noun u3wc_repn(u3_noun cor) {
u3_noun bits, atom=0;
u3l_log("WE'RE ALL GOING TO DIE\n");
return 0;
if ( (c3n == u3r_mean(cor, u3x_sam_2, &bits, u3x_sam_3, &atom, 0)) ||
(c3n == u3ud(bits)) ||
(c3n == u3ud(atom)) )
{
return u3m_bail(c3__exit);
}
return u3qc_repn(bits, atom);
}
u3_noun u3kc_repn(u3_atom bits, u3_atom atom) {
u3_noun res = u3qc_repn(bits, atom);
u3z(bits), u3z(atom);
return res;
}