Skip to content

Commit

Permalink
wip: retrieve_tests almost done; u3i_words usage throughout codebas…
Browse files Browse the repository at this point in the history
…e going to be tricky
  • Loading branch information
matthew-levan committed Sep 10, 2024
1 parent f8cd976 commit ed3bc81
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
21 changes: 21 additions & 0 deletions pkg/noun/imprison.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,21 @@ u3i_slab_from(u3i_slab* sab_u, u3_atom a, c3_g met_g, c3_d len_d)
// if necessary, mask off extra most-significant bits
// from most-significant word
//
#ifdef VERE_64
if ( (6 > met_g) && (u3r_met(6, a) >= sab_u->len_w) ) {
#else
if ( (5 > met_g) && (u3r_met(5, a) >= sab_u->len_w) ) {
#endif
// NB: overflow already checked in _ci_slab_size()
//
c3_d bit_d = len_d << met_g;
#ifdef VERE_64
c3_w wor_w = bit_d >> 6;
c3_w bit_w = bit_d & 0x3f;
#else
c3_w wor_w = bit_d >> 5;
c3_w bit_w = bit_d & 0x1f;
#endif

if ( bit_w ) {
sab_u->buf_w[wor_w] &= ((c3_w)1 << bit_w) - 1;
Expand Down Expand Up @@ -338,6 +347,9 @@ u3i_word(c3_w dat_w)
u3_atom
u3i_chub(c3_d dat_d)
{
#ifdef VERE_64
return u3i_word(dat_d);
#else
if ( c3y == u3a_is_cat(dat_d) ) {
return (u3_atom)dat_d;
}
Expand All @@ -349,6 +361,7 @@ u3i_chub(c3_d dat_d)

return u3i_words(2, dat_w);
}
#endif
}

/* u3i_bytes(): Copy [a] bytes from [b] to an LSB first atom.
Expand Down Expand Up @@ -401,10 +414,18 @@ u3i_words(c3_w a_w,
}
else {
u3i_slab sab_u;
#ifdef VERE_64
u3i_slab_bare(&sab_u, 6, a_w);
#else
u3i_slab_bare(&sab_u, 5, a_w);
#endif

u3t_on(mal_o);
#ifdef VERE_64
memcpy(sab_u.buf_w, b_w, (size_t)8 * a_w);
#else
memcpy(sab_u.buf_w, b_w, (size_t)4 * a_w);
#endif
u3t_off(mal_o);

return u3i_slab_moot(&sab_u);
Expand Down
4 changes: 4 additions & 0 deletions pkg/noun/jets/c/mix.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
c3_w len_w = c3_max(lna_w, lnb_w);
c3_w i_w;
u3i_slab sab_u;
#ifdef VERE_64
u3i_slab_from(&sab_u, a, 6, len_w);
#else
u3i_slab_from(&sab_u, a, 5, len_w);
#endif

// XX use u3r_chop for XOR?
//
Expand Down
28 changes: 27 additions & 1 deletion pkg/noun/retrieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -1294,10 +1294,14 @@ c3_d
u3r_chub(c3_w a_w,
u3_atom b)
{
#ifdef VERE_64
return u3r_word(a_w, b);
#else
c3_w wlo_w = u3r_word(a_w * 2, b);
c3_w whi_w = u3r_word(1 + (a_w * 2), b);

return (((uint64_t)whi_w) << 32ULL) | ((uint64_t)wlo_w);
#endif
}

/* u3r_words():
Expand All @@ -1319,23 +1323,41 @@ u3r_words(c3_w a_w,
if ( _(u3a_is_cat(d)) ) {
if ( a_w == 0 ) {
*c_w = d;
#ifdef VERE_64
memset((c3_y*)(c_w + 1), 0, (b_w - 1) << 3);
#else
memset((c3_y*)(c_w + 1), 0, (b_w - 1) << 2);
#endif
}
else {
#ifdef VERE_64
memset((c3_y*)c_w, 0, b_w << 3);
#else
memset((c3_y*)c_w, 0, b_w << 2);
#endif
}
}
else {
u3a_atom* d_u = u3a_to_ptr(d);
if ( a_w >= d_u->len_w ) {
#ifdef VERE_64
memset((c3_y*)c_w, 0, b_w << 3);
#else
memset((c3_y*)c_w, 0, b_w << 2);
#endif
}
else {
c3_w z_w = c3_min(b_w, d_u->len_w - a_w);
c3_w* x_w = d_u->buf_w + a_w;
#ifdef VERE_64
memcpy((c3_y*)c_w, (c3_y*)x_w, z_w << 3);
if ( b_w > d_u->len_w - a_w ) {
memset((c3_y*)(c_w + z_w), 0, (b_w + a_w - d_u->len_w) << 3);
#else
memcpy((c3_y*)c_w, (c3_y*)x_w, z_w << 2);
if ( b_w > d_u->len_w - a_w ) {
memset((c3_y*)(c_w + z_w), 0, (b_w + a_w - d_u->len_w) << 2);
#endif
}
}
}
Expand Down Expand Up @@ -1487,7 +1509,7 @@ u3r_chop_bits(c3_g bif_g,
hig_w ^= src_w[1] << fib_y;
}

*dst_w ^= (hig_w & ((1 << wid_d) - 1)) << bit_g;
*dst_w ^= (hig_w & ((1ULL << wid_d) - 1)) << bit_g;
}
}

Expand Down Expand Up @@ -1760,12 +1782,16 @@ u3r_mug_cell(u3_noun hed,
c3_l
u3r_mug_chub(c3_d num_d)
{
#ifdef VERE_64
return u3r_mug_words((c3_w*)&num_d, 1);
#else
c3_w buf_w[2];

buf_w[0] = (c3_w)(num_d & 0xffffffffULL);
buf_w[1] = (c3_w)(num_d >> 32);

return u3r_mug_words(buf_w, 2);
#endif
}

/* u3r_mug_words(): 31-bit nonzero MurmurHash3 on raw words.
Expand Down
36 changes: 27 additions & 9 deletions pkg/noun/retrieve_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,9 @@ _test_mug(void)
{
// stick some zero bytes in a string
//
u3m_p("(bex 212)", u3qc_bex(212));
u3m_p("u3i_string", u3i_string("abcdefjhijklmnopqrstuvwxyz"));
u3_noun str = u3kc_lsh(3, 1,
u3kc_mix(u3qc_bex(212),
u3i_string("abcdefjhijklmnopqrstuvwxyz")));
u3m_p("lsh", str);

c3_w byt_w = u3r_met(3, str);
#ifdef VERE_64
Expand All @@ -158,25 +155,37 @@ _test_mug(void)
u3r_bytes(0, byt_w, str_y, str);
u3r_words(0, wor_w, str_w, str);

#ifdef VERE_64
str_d |= str_w[0];
#else
str_d |= str_w[0];
str_d |= ((c3_d)str_w[1] << 32ULL);
#endif

gud = 0x34d08717;
res = u3r_mug(str);
if ( gud != res ) {
fprintf(stderr, "fail (i) (1) %x %x\r\n", gud, res);
ret_i = 0;
}
if ( 0x34d08717 != u3r_mug_bytes(str_y, byt_w) ) {
fprintf(stderr, "fail (i) (2)\r\n");
res = u3r_mug_bytes(str_y, byt_w);
if ( gud != res ) {
fprintf(stderr, "fail (i) (2) %x %x\r\n", gud, res);
ret_i = 0;
}
if ( 0x34d08717 != u3r_mug_words(str_w, wor_w) ) {
fprintf(stderr, "fail (i) (3)\r\n");
res = u3r_mug_words(str_w, wor_w);
if ( gud != res ) {
fprintf(stderr, "fail (i) (3) %x %x\r\n", gud, res);
ret_i = 0;
}
if ( u3r_mug_words(str_w, 2) != u3r_mug_chub(str_d) ) {
fprintf(stderr, "fail (i) (4)\r\n");
#ifdef VERE_64
gud = u3r_mug_words(str_w, 1);
#else
gud = u3r_mug_words(str_w, 2);
#endif
res = u3r_mug_chub(str_d);
if ( gud != res ) {
fprintf(stderr, "fail (i) (4) %x %x\r\n", gud, res);
ret_i = 0;
}

Expand All @@ -186,15 +195,24 @@ _test_mug(void)
}

{
#ifdef VERE_64
c3_y som_y[13] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
u3_noun som = u3i_bytes(13, som_y);
#else
c3_w som_w[4] = { 0, 0, 0, 1 };
u3_noun som = u3i_words(4, som_w);
#endif

if ( 0x519bd45c != u3r_mug(som) ) {
fprintf(stderr, "fail (j) (1)\r\n");
ret_i = 0;
}

#ifdef VERE_64
if ( 0x519bd45c != u3r_mug_bytes(som_y, 13) ) {
#else
if ( 0x519bd45c != u3r_mug_words(som_w, 4) ) {
#endif
fprintf(stderr, "fail (j) (2)\r\n");
ret_i = 0;
}
Expand Down

0 comments on commit ed3bc81

Please sign in to comment.