Before jet renaming.

This commit is contained in:
C. Guy Yarvin 2014-09-02 19:58:36 -07:00
parent b75189b53e
commit 2d9af9f04e
4 changed files with 420 additions and 734 deletions

View File

@ -328,7 +328,8 @@ $(BIN)/vere: $(LIBCRE) $(VERE_OFILES) $(LIBUV) $(LIBRE2) $(LIBED25519) $(BPT_O)
meme: $(BIN)/meme
MEME_OFILES=f/meme.o f/zock.o $(J164_OFILES) $(CRE2_OFILES)
MEME_OFILES=f/meme.o f/zock.o f/test.o f/kjet.o \
$(J164_OFILES) $(CRE2_OFILES)
$(BIN)/meme: $(MEME_OFILES) $(LIBED25519)
$(CLD) -g $(CLDOSFLAGS) -o $(BIN)/meme $(MEME_OFILES) $(LIBRE2) $(LIBED25519) -lgmp

732
f/meme.c
View File

@ -3,58 +3,7 @@
** This file is in the public domain.
*/
#define C3_GLOBAL
// delete me
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <setjmp.h>
#include <gmp.h>
#include <dirent.h>
#include <stdint.h>
#include <uv.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include <errno.h>
#include "f/meme.h"
#include "../gen164/pit.h"
/** Jet dependencies. Minimize these.
**/
# define Pt5Y k_164__mood__hoon
/** Jet dependencies. Minimize these.
**/
# define Pt3Y k_164__mood__hoon
# define Pt4Y k_164__mood__hoon
# define Pt5Y k_164__mood__hoon
u2_noun j2_mbc(Pt3Y, gor)(u2_noun a, u2_noun b);
u2_noun j2_mcc(Pt4Y, by, get)(u2_noun a, u2_noun b);
u2_noun j2_mcc(Pt4Y, by, put)(u2_noun a, u2_noun b, u2_noun c);
u2_noun j2_mby(Pt5Y, cue)(u2_noun a);
u2_noun j2_mby(Pt5Y, jam)(u2_noun a);
u2_noun j2_mby(Pt5Y, trip)(u2_noun a);
# define _coal_cue j2_mby(Pt5Y, cue)
# define _coal_jam j2_mby(Pt5Y, jam)
# define _coal_trip j2_mby(Pt5Y, trip)
# define _coal_gor j2_mbc(Pt3Y, gor)
# define _coal_by_gas j2_mcc(Pt4Y, by, gas)
# define _coal_by_get j2_mcc(Pt4Y, by, get)
# define _coal_by_has j2_mcc(Pt4Y, in, has)
# define _coal_by_put j2_mcc(Pt4Y, by, put)
# define _coal_in_gas j2_mcc(Pt4Y, in, gas)
# define _coal_in_has j2_mcc(Pt4Y, in, has)
# define _coal_in_tap j2_mcc(Pt4Y, in, tap)
/* _boot_north(): install a north road.
*/
@ -178,44 +127,6 @@ _me_road_all_cap(c3_w len_w)
}
#endif
static void
_road_dump(void)
{
c3_w hat_w;
c3_w fre_w = 0;
c3_w i_w;
hat_w = u2_so(u2_co_is_north) ? u2R->hat_w - u2R->rut_w
: u2R->hat_w - u2R->rut_w;
for ( i_w = 0; i_w < u2_cc_fbox_no; i_w++ ) {
u2_cs_fbox* fre_u = u2R->all.fre_u[i_w];
while ( fre_u ) {
fre_w += fre_u->box_u.siz_w;
fre_u = fre_u->nex_u;
}
}
printf("dump: hat_w %x, fre_w %x, allocated %x\n",
hat_w, fre_w, (hat_w - fre_w));
if ( 0 != (hat_w - fre_w) ) {
c3_w* box_w = u2R->rut_w;
c3_w mem_w = 0;
while ( box_w < u2R->hat_w ) {
u2_cs_box* box_u = (void *)box_w;
if ( 0 != box_u->use_w ) {
mem_w += box_u->siz_w;
}
box_w += box_u->siz_w;
}
printf("second count: %x\n", mem_w);
}
}
#if 1
static void
_road_sane(void)
@ -3964,647 +3875,4 @@ u2_cn_nock_an(u2_noun bus, u2_noun fol)
return u2_cn_nock_un(bus, fol);
}
#if 0
static c3_w*
_test_walloc(c3_w siz_w)
{
c3_w *ptr_w = u2_ca_walloc(siz_w);
c3_w i_w;
c3_assert(siz_w >= 1);
*ptr_w = siz_w;
for ( i_w = 1; i_w < siz_w; i_w++ ) {
ptr_w[i_w] = u2_cr_mug((0xffff & (c3_p)(ptr_w)) + i_w);
}
return ptr_w;
}
static void
_test_free(c3_w* ptr_w)
{
c3_w i_w, siz_w = *ptr_w;
for ( i_w = 1; i_w < siz_w; i_w++ ) {
c3_assert(ptr_w[i_w] == u2_cr_mug((0xffff & (c3_p)(ptr_w)) + i_w));
}
u2_ca_free(ptr_w);
}
#define NUM 16384
// Simple allocation test.
//
void
test(void)
{
c3_w* one_w[NUM];
c3_w* two_w[NUM];
c3_w i_w;
for ( i_w = 0; i_w < NUM; i_w++ ) {
c3_w siz_w = c3_max(1, u2_cr_mug(i_w) & 0xff);
one_w[i_w] = _test_walloc(siz_w);
two_w[i_w] = _test_walloc(siz_w);
}
_road_sane();
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(two_w[NUM - (i_w + 1)]);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
c3_w siz_w = c3_max(1, u2_cr_mug(i_w + 1) & 0xff);
two_w[i_w] = _test_walloc(siz_w);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(one_w[NUM - (i_w + 1)]);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
c3_w siz_w = c3_max(1, u2_cr_mug(i_w + 2) & 0xff);
one_w[i_w] = _test_walloc(siz_w);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(one_w[NUM - (i_w + 1)]);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(two_w[NUM - (i_w + 1)]);
_road_sane();
}
printf("allocations %d, iterations %d\n", ALL_w, ITE_w);
}
#endif
#if 1
/* u2_walk_load(): load file or bail.
*/
static u2_noun
u2_walk_load(c3_c* pas_c)
{
struct stat buf_b;
c3_i fid_i = open(pas_c, O_RDONLY, 0644);
c3_w fln_w, red_w;
c3_y* pad_y;
if ( (fid_i < 0) || (fstat(fid_i, &buf_b) < 0) ) {
fprintf(stderr, "%s: %s\r\n", pas_c, strerror(errno));
return u2_cm_bail(c3__fail);
}
fln_w = buf_b.st_size;
pad_y = c3_malloc(buf_b.st_size);
red_w = read(fid_i, pad_y, fln_w);
close(fid_i);
if ( fln_w != red_w ) {
free(pad_y);
return u2_cm_bail(c3__fail);
}
else {
u2_noun pad = u2_ci_bytes(fln_w, (c3_y *)pad_y);
free(pad_y);
return pad;
}
}
#endif
/* u2_cka_add(): a + b.
*/
u2_noun
u2_cka_add(u2_noun a, u2_noun b)
{
u2_noun c = j2_mbc(Pt1, add)(a, b);
u2z(a); u2z(b);
return c;
}
/* u2_cka_sub(): a + b.
*/
u2_noun
u2_cka_sub(u2_noun a, u2_noun b)
{
u2_noun c = j2_mbc(Pt1, sub)(a, b);
u2z(a); u2z(b);
return c;
}
/* u2_cka_gth(): a + b.
*/
u2_noun
u2_cka_gth(u2_noun a, u2_noun b)
{
u2_noun c = j2_mbc(Pt1, gth)(a, b);
u2z(a); u2z(b);
return c;
}
/* u2_cka_mul(): a * b.
*/
u2_noun
u2_cka_mul(u2_noun a, u2_noun b)
{
u2_noun c = j2_mbc(Pt1, mul)(a, b);
u2z(a); u2z(b);
return c;
}
/* u2_cka_lte(): a * b.
*/
u2_noun
u2_cka_lte(u2_noun a, u2_noun b)
{
u2_noun c = j2_mbc(Pt1, lte)(a, b);
u2z(a); u2z(b);
return c;
}
/* u2_ckb_lent(): length of list `a`.
*/
u2_noun
u2_ckb_lent(u2_noun a)
{
u2_noun b = j2_mbc(Pt2, lent)(a);
u2z(a);
return b;
}
/* u2_ckb_flop(): reverse list `a`.
*/
u2_noun
u2_ckb_flop(u2_noun a)
{
u2_noun b = j2_mbc(Pt2, flop)(a);
u2z(a);
return b;
}
/* u2_ckb_weld(): concatenate lists `a` before `b`.
*/
u2_noun
u2_ckb_weld(u2_noun a, u2_noun b)
{
u2_noun c = j2_mbc(Pt2, weld)(a, b);
u2z(a); u2z(b);
return c;
}
/* u2_ckc_lsh(): left shift.
*/
u2_noun
u2_ckc_lsh(u2_noun a, u2_noun b, u2_noun c)
{
u2_noun d = j2_mbc(Pt3, lsh)(a, b, c);
u2z(a); u2z(b); u2z(c);
return d;
}
/* u2_ckc_rsh(): right shift.
*/
u2_noun
u2_ckc_rsh(u2_noun a, u2_noun b, u2_noun c)
{
u2_noun d = j2_mbc(Pt3, rsh)(a, b, c);
u2z(a); u2z(b); u2z(c);
return d;
}
/* u2_ckd_by_get(): map get for key `b` in map `a` with u2_none.
*/
u2_weak
u2_ckd_by_get(u2_noun a, u2_noun b)
{
u2_noun c = _coal_by_get(a, b);
u2z(a); u2z(b);
if ( u2_no == u2_cr_du(c) ) {
u2z(c);
return u2_none;
} else {
u2_noun pro = u2k(u2t(c));
u2z(c);
return pro;
}
}
/* u2_ckd_by_got(): map get for key `b` in map `a` with fail.
*/
u2_noun
u2_ckd_by_got(u2_noun a, u2_noun b)
{
u2_weak c = u2_ckd_by_get(a, b);
if ( u2_none == c ) {
return u2_cm_bail(c3__exit);
}
else return c;
}
/* u2_ckd_by_put(): map put for key `b`, value `c` in map `a`.
*/
u2_weak
u2_ckd_by_put(u2_noun a, u2_noun b, u2_noun c)
{
// Bizarre asymmetry in old jets.
//
// (Mysterious comment in old glue code.)
//
u2_noun pro = _coal_by_put(a, b, c);
u2z(a); u2z(b); u2z(c);
return pro;
}
/* u2_ckd_by_gas(): list to map.
*/
u2_noun
u2_ckd_by_gas(u2_noun a, u2_noun b)
{
u2_weak c = _coal_by_gas(a, b);
u2z(a); u2z(b);
if ( u2_none == c ) {
return u2_cm_bail(c3__exit);
}
else return c;
}
/* u2_ckd_in_gas(): list to map.
*/
u2_noun
u2_ckd_in_gas(u2_noun a, u2_noun b)
{
u2_weak c = _coal_in_gas(a, b);
u2z(a); u2z(b);
if ( u2_none == c ) {
return u2_cm_bail(c3__exit);
}
else return c;
}
/* u2_ckd_by_has(): test for presence.
*/
u2_bean
u2_ckd_by_has(u2_noun a, u2_noun b)
{
u2_weak c = _coal_by_has(a, b);
u2z(a); u2z(b);
if ( u2_none == c ) {
return u2_cm_bail(c3__exit);
}
else return c;
}
/* u2_ckd_in_has(): test for presence.
*/
u2_bean
u2_ckd_in_has(u2_noun a, u2_noun b)
{
u2_weak c = _coal_in_has(a, b);
u2z(a); u2z(b);
if ( u2_none == c ) {
return u2_cm_bail(c3__exit);
}
else return c;
}
/* u2_ckd_in_tap(): map/set convert to list. (solves by_tap also.)
*/
u2_noun
u2_ckd_in_tap(u2_noun a, u2_noun b)
{
u2_weak c = _coal_in_tap(a, b);
u2z(a); u2z(b);
if ( u2_none == c ) {
return u2_cm_bail(c3__exit);
}
else return c;
}
/* u2_cke_cue(): expand saved pill.
*/
u2_noun
u2_cke_cue(u2_atom a)
{
u2_noun b = _coal_cue(a);
u2z(a);
return b;
}
/* u2_cke_jam(): pack noun as atom.
*/
u2_atom
u2_cke_jam(u2_noun a)
{
u2_atom b = _coal_jam(a);
u2z(a);
return b;
}
/* u2_cke_trip(): atom to tape.
*/
u2_atom
u2_cke_trip(u2_noun a)
{
u2_atom b = _coal_trip(a);
u2z(a);
return b;
}
// Wordy.
#if 0
static int
_bits_word(c3_w w)
{
return w ? (32 - __builtin_clz(w)) : 0;
}
static void
_test_words(void)
{
c3_w i_w = 0;
while ( 1 ) {
if ( _bits_word(i_w) != c3_bits_word(i_w) ) {
printf("i_w %x, bw %d, cbw %d\n",
i_w, _bits_word(i_w), c3_bits_word(i_w));
c3_assert(0);
}
if ( 0xffffffff == i_w ) {
break;
}
i_w++;
}
}
#endif
static void
_test_jam(void)
{
_road_dump();
{
u2_noun pil = u2_walk_load("urb/urbit.pill");
u2_noun cue, jam;
printf("cueing pill - %d bytes\n", u2_cr_met(3, pil));
cue = u2_cke_cue(pil);
printf("cued - mug %x\n", u2_cr_mug(cue));
#if 1
jam = u2_cke_jam(cue);
printf("jammed - %d bytes\n", u2_cr_met(3, jam));
cue = u2_cke_cue(jam);
printf("cued - mug %x\n", u2_cr_mug(cue));
#endif
u2z(cue);
}
_road_dump();
}
static void
_test_hash(void)
{
_road_dump();
{
u2_ch_root* har_u = u2_ch_new();
c3_w i_w;
c3_w max_w = (1 << 20);
for ( i_w = 0; i_w < max_w; i_w++ ) {
u2_noun key = u2nc(0, i_w);
u2_ch_put(har_u, key, (i_w + 1));
u2z(key);
}
for ( i_w = 0; i_w < max_w; i_w++ ) {
u2_noun key = u2nc(0, i_w);
u2_noun val = u2_ch_get(har_u, key);
if ( val != (i_w + 1) ) {
if ( u2_none == val ) {
printf("at %d, nothing\n", i_w);
}
else printf("at %d, oddly, is %d\n", i_w, val);
c3_assert(0);
}
u2z(key);
}
u2_ch_free(har_u);
}
_road_dump();
}
// A simple memory tester.
//
int c3_cooked() { u2_cm_bail(c3__oops); return 0; }
int
main(int argc, char *argv[])
{
printf("hello, world: len %dMB\n", (1 << U2_OS_LoomBits) >> 18);
// _test_words();
u2_cm_boot(U2_OS_LoomBase, (1 << U2_OS_LoomBits));
printf("booted.\n");
_test_hash();
}
#if 0
/* Finalization mix for better avalanching.
*/
static c3_w
_mur_fmix(c3_w h_w)
{
h_w ^= h_w >> 16;
h_w *= 0x85ebca6b;
h_w ^= h_w >> 13;
h_w *= 0xc2b2ae35;
h_w ^= h_w >> 16;
return h_w;
}
/* u2_mur_words(): MurmurHash3 on raw words.
*/
static c3_w
_mur_words(c3_w syd_w, const c3_w* key_w, c3_w len_w)
{
c3_w goc_w = syd_w;
c3_w lig_w = 0xcc9e2d51;
c3_w duf_w = 0x1b873593;
c3_w i_w;
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w kop_w = key_w[i_w];
kop_w *= lig_w;
kop_w = c3_rotw(15, kop_w);
kop_w *= duf_w;
goc_w ^= kop_w;
goc_w = c3_rotw(13, goc_w);
goc_w = (goc_w * 5) + 0xe6546b64;
}
goc_w ^= len_w;
goc_w = _mur_fmix(goc_w);
return goc_w;
}
/* u2_mug_words(): 31-bit nonzero MurmurHash3 on raw words.
*/
c3_w
u2_mug_words(const c3_w* key_w, c3_w len_w)
{
c3_w syd_w = 0xcafebabe;
while ( 1 ) {
c3_w haz_w = _mur_words(syd_w, key_w, len_w);
c3_w ham_w = (haz_w >> 31) ^ (haz_w & 0x7fffffff);
if ( 0 != ham_w ) return ham_w;
else syd_w++;
}
}
/* u2_mug_both():
**
** Join two mugs.
*/
c3_w
u2_mug_both(c3_w lef_w, c3_w rit_w)
{
c3_w ham_w = lef_w ^ (0x7fffffff ^ rit_w);
return u2_mug_words(&ham_w, (0 == ham_w) ? 0 : 1);
}
/* u2_mug(): MurmurHash3 on a noun.
*/
c3_w
u2_mug(u2_noun veb)
{
if ( u2_fly_is_cat(veb) ) {
return u2_mug_words(&veb, (0 == veb) ? 0 : 1);
}
else {
c3_w mug_w;
if ( (mug_w=*u2_at_dog_mug(veb)) ) {
return mug_w;
}
if ( u2_dog_is_pom(veb) ) {
mug_w = u2_mug_both(u2_mug(u2h(veb)), u2_mug(u2t(veb)));
}
else {
c3_w len_w = u2_met(5, veb);
c3_w* buf_w = malloc(4 * len_w);
u2_words(0, len_w, buf_w, veb);
mug_w = u2_mug_words(buf_w, len_w);
free(buf_w);
}
*u2_at_dog_mug(veb) = mug_w;
return mug_w;
}
}
/* u2_mug_string():
**
** Compute the mug of `a`, LSB first.
*/
c3_w
u2_mug_string(const c3_c *a_c)
{
c3_w len_w = strlen(a_c);
c3_w wor_w = ((len_w + 3) >> 2);
c3_w* buf_w = alloca(4 * wor_w);
c3_w i_w;
for ( i_w = 0; i_w < wor_w; i_w++ ) { buf_w[i_w] = 0; }
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w inx_w = (i_w >> 2);
c3_w byt_w = (i_w & 3);
buf_w[inx_w] |= (a_c[i_w] << (8 * byt_w));
}
return u2_mug_words(buf_w, wor_w);
}
/* u2_mug_cell():
**
** Compute the mug of the cell `[hed tel]`.
*/
c3_w
u2_mug_cell(u2_noun hed,
u2_noun tel)
{
c3_w lus_w = u2_mug(hed);
c3_w biq_w = u2_mug(tel);
return u2_mug_both(lus_w, biq_w);
}
/* u2_mug_trel():
**
** Compute the mug of `[a b c]`.
*/
c3_w
u2_mug_trel(u2_noun a,
u2_noun b,
u2_noun c)
{
return u2_mug_both(u2_mug(a), u2_mug_both(u2_mug(b), u2_mug(c)));
}
/* u2_mug_qual():
**
** Compute the mug of `[a b c d]`.
*/
c3_w
u2_mug_qual(u2_noun a,
u2_noun b,
u2_noun c,
u2_noun d)
{
return u2_mug_both(u2_mug(a),
u2_mug_both(u2_mug(b),
u2_mug_both(u2_mug(c), u2_mug(d))));
}
#endif

417
f/test.c Normal file
View File

@ -0,0 +1,417 @@
/* f/test.c
**
** This file is in the public domain.
*/
#define C3_GLOBAL
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <setjmp.h>
#include <gmp.h>
#include <dirent.h>
#include <stdint.h>
#include <uv.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include <errno.h>
#include "f/meme.h"
#if 1
/* u2_walk_load(): load file or bail.
*/
static u2_noun
u2_walk_load(c3_c* pas_c)
{
struct stat buf_b;
c3_i fid_i = open(pas_c, O_RDONLY, 0644);
c3_w fln_w, red_w;
c3_y* pad_y;
if ( (fid_i < 0) || (fstat(fid_i, &buf_b) < 0) ) {
fprintf(stderr, "%s: %s\r\n", pas_c, strerror(errno));
return u2_cm_bail(c3__fail);
}
fln_w = buf_b.st_size;
pad_y = c3_malloc(buf_b.st_size);
red_w = read(fid_i, pad_y, fln_w);
close(fid_i);
if ( fln_w != red_w ) {
free(pad_y);
return u2_cm_bail(c3__fail);
}
else {
u2_noun pad = u2_ci_bytes(fln_w, (c3_y *)pad_y);
free(pad_y);
return pad;
}
}
#endif
static void
_road_dump(void)
{
c3_w hat_w;
c3_w fre_w = 0;
c3_w i_w;
hat_w = u2_so(u2_co_is_north) ? u2R->hat_w - u2R->rut_w
: u2R->hat_w - u2R->rut_w;
for ( i_w = 0; i_w < u2_cc_fbox_no; i_w++ ) {
u2_cs_fbox* fre_u = u2R->all.fre_u[i_w];
while ( fre_u ) {
fre_w += fre_u->box_u.siz_w;
fre_u = fre_u->nex_u;
}
}
printf("dump: hat_w %x, fre_w %x, allocated %x\n",
hat_w, fre_w, (hat_w - fre_w));
if ( 0 != (hat_w - fre_w) ) {
c3_w* box_w = u2R->rut_w;
c3_w mem_w = 0;
while ( box_w < u2R->hat_w ) {
u2_cs_box* box_u = (void *)box_w;
if ( 0 != box_u->use_w ) {
mem_w += box_u->siz_w;
}
box_w += box_u->siz_w;
}
printf("second count: %x\n", mem_w);
}
}
static void
_test_jam(void)
{
_road_dump();
{
u2_noun pil = u2_walk_load("urb/urbit.pill");
u2_noun cue, jam;
printf("cueing pill - %d bytes\n", u2_cr_met(3, pil));
cue = u2_cke_cue(pil);
printf("cued - mug %x\n", u2_cr_mug(cue));
#if 1
jam = u2_cke_jam(cue);
printf("jammed - %d bytes\n", u2_cr_met(3, jam));
cue = u2_cke_cue(jam);
printf("cued - mug %x\n", u2_cr_mug(cue));
#endif
u2z(cue);
}
_road_dump();
}
#if 0
static c3_w*
_test_walloc(c3_w siz_w)
{
c3_w *ptr_w = u2_ca_walloc(siz_w);
c3_w i_w;
c3_assert(siz_w >= 1);
*ptr_w = siz_w;
for ( i_w = 1; i_w < siz_w; i_w++ ) {
ptr_w[i_w] = u2_cr_mug((0xffff & (c3_p)(ptr_w)) + i_w);
}
return ptr_w;
}
static void
_test_free(c3_w* ptr_w)
{
c3_w i_w, siz_w = *ptr_w;
for ( i_w = 1; i_w < siz_w; i_w++ ) {
c3_assert(ptr_w[i_w] == u2_cr_mug((0xffff & (c3_p)(ptr_w)) + i_w));
}
u2_ca_free(ptr_w);
}
#define NUM 16384
// Simple allocation test.
//
void
test(void)
{
c3_w* one_w[NUM];
c3_w* two_w[NUM];
c3_w i_w;
for ( i_w = 0; i_w < NUM; i_w++ ) {
c3_w siz_w = c3_max(1, u2_cr_mug(i_w) & 0xff);
one_w[i_w] = _test_walloc(siz_w);
two_w[i_w] = _test_walloc(siz_w);
}
_road_sane();
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(two_w[NUM - (i_w + 1)]);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
c3_w siz_w = c3_max(1, u2_cr_mug(i_w + 1) & 0xff);
two_w[i_w] = _test_walloc(siz_w);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(one_w[NUM - (i_w + 1)]);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
c3_w siz_w = c3_max(1, u2_cr_mug(i_w + 2) & 0xff);
one_w[i_w] = _test_walloc(siz_w);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(one_w[NUM - (i_w + 1)]);
_road_sane();
}
for ( i_w = 0; i_w < NUM; i_w++ ) {
_test_free(two_w[NUM - (i_w + 1)]);
_road_sane();
}
printf("allocations %d, iterations %d\n", ALL_w, ITE_w);
}
#endif
static void
_test_hash(void)
{
_road_dump();
{
u2_ch_root* har_u = u2_ch_new();
c3_w i_w;
c3_w max_w = (1 << 20);
for ( i_w = 0; i_w < max_w; i_w++ ) {
u2_noun key = u2nc(0, i_w);
u2_ch_put(har_u, key, (i_w + 1));
u2z(key);
}
for ( i_w = 0; i_w < max_w; i_w++ ) {
u2_noun key = u2nc(0, i_w);
u2_noun val = u2_ch_get(har_u, key);
if ( val != (i_w + 1) ) {
if ( u2_none == val ) {
printf("at %d, nothing\n", i_w);
}
else printf("at %d, oddly, is %d\n", i_w, val);
c3_assert(0);
}
u2z(key);
}
u2_ch_free(har_u);
}
_road_dump();
}
// A simple memory tester.
//
int c3_cooked() { u2_cm_bail(c3__oops); return 0; }
int
main(int argc, char *argv[])
{
printf("hello, world: len %dMB\n", (1 << U2_OS_LoomBits) >> 18);
// _test_words();
u2_cm_boot(U2_OS_LoomBase, (1 << U2_OS_LoomBits));
printf("booted.\n");
_test_hash();
}
#if 0
/* Finalization mix for better avalanching.
*/
static c3_w
_mur_fmix(c3_w h_w)
{
h_w ^= h_w >> 16;
h_w *= 0x85ebca6b;
h_w ^= h_w >> 13;
h_w *= 0xc2b2ae35;
h_w ^= h_w >> 16;
return h_w;
}
/* _mur_words(): raw MurmurHash3 on raw words.
*/
static c3_w
_mur_words(c3_w syd_w, const c3_w* key_w, c3_w len_w)
{
c3_w goc_w = syd_w;
c3_w lig_w = 0xcc9e2d51;
c3_w duf_w = 0x1b873593;
c3_w i_w;
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w kop_w = key_w[i_w];
kop_w *= lig_w;
kop_w = c3_rotw(15, kop_w);
kop_w *= duf_w;
goc_w ^= kop_w;
goc_w = c3_rotw(13, goc_w);
goc_w = (goc_w * 5) + 0xe6546b64;
}
goc_w ^= len_w;
goc_w = _mur_fmix(goc_w);
return goc_w;
}
/* u2_mur_words(): 31-bit nonzero MurmurHash3 on raw words.
*/
c3_w
u2_mur_words(const c3_w* key_w, c3_w len_w)
{
c3_w syd_w = 0xcafebabe;
while ( 1 ) {
c3_w haz_w = _mur_words(syd_w, key_w, len_w);
c3_w ham_w = (haz_w >> 31) ^ (haz_w & 0x7fffffff);
if ( 0 != ham_w ) return ham_w;
else syd_w++;
}
}
/* u2_mur_both():
**
** Join two murs.
*/
c3_w
u2_mur_both(c3_w lef_w, c3_w rit_w)
{
c3_w ham_w = lef_w ^ (0x7fffffff ^ rit_w);
return u2_mur_words(&ham_w, (0 == ham_w) ? 0 : 1);
}
/* u2_mur(): MurmurHash3 on a noun.
*/
c3_w
u2_mur(u2_noun veb)
{
if ( u2_fly_is_cat(veb) ) {
return u2_mur_words(&veb, (0 == veb) ? 0 : 1);
}
else {
c3_w mur_w;
if ( (mur_w=*u2_at_dog_mur(veb)) ) {
return mur_w;
}
if ( u2_dog_is_pom(veb) ) {
mur_w = u2_mur_both(u2_mur(u2h(veb)), u2_mur(u2t(veb)));
}
else {
c3_w len_w = u2_met(5, veb);
c3_w* buf_w = malloc(4 * len_w);
u2_words(0, len_w, buf_w, veb);
mur_w = u2_mur_words(buf_w, len_w);
free(buf_w);
}
*u2_at_dog_mur(veb) = mur_w;
return mur_w;
}
}
/* u2_mur_string():
**
** Compute the mur of `a`, LSB first.
*/
c3_w
u2_mur_string(const c3_c *a_c)
{
c3_w len_w = strlen(a_c);
c3_w wor_w = ((len_w + 3) >> 2);
c3_w* buf_w = alloca(4 * wor_w);
c3_w i_w;
for ( i_w = 0; i_w < wor_w; i_w++ ) { buf_w[i_w] = 0; }
for ( i_w = 0; i_w < len_w; i_w++ ) {
c3_w inx_w = (i_w >> 2);
c3_w byt_w = (i_w & 3);
buf_w[inx_w] |= (a_c[i_w] << (8 * byt_w));
}
return u2_mur_words(buf_w, wor_w);
}
/* u2_mur_cell():
**
** Compute the mur of the cell `[hed tel]`.
*/
c3_w
u2_mur_cell(u2_noun hed,
u2_noun tel)
{
c3_w lus_w = u2_mur(hed);
c3_w biq_w = u2_mur(tel);
return u2_mur_both(lus_w, biq_w);
}
/* u2_mur_trel():
**
** Compute the mur of `[a b c]`.
*/
c3_w
u2_mur_trel(u2_noun a,
u2_noun b,
u2_noun c)
{
return u2_mur_both(u2_mur(a), u2_mur_both(u2_mur(b), u2_mur(c)));
}
/* u2_mur_qual():
**
** Compute the mur of `[a b c d]`.
*/
c3_w
u2_mur_qual(u2_noun a,
u2_noun b,
u2_noun c,
u2_noun d)
{
return u2_mur_both(u2_mur(a),
u2_mur_both(u2_mur(b),
u2_mur_both(u2_mur(c), u2_mur(d))));
}
#endif

View File

@ -1446,7 +1446,7 @@
u2_weak
u2_ckd_by_get(u2_noun a, u2_noun b);
/* u2_ckd_by_got(): map get for key `b` in map `a` with fail.
/* u2_ckd_by_got(): map get for key `b` in map `a` with bail.
*/
u2_noun
u2_ckd_by_got(u2_noun a, u2_noun b);