From e75d2e70b297e28d1c39e3ed4b734a346a5eee9a Mon Sep 17 00:00:00 2001 From: "C. Guy Yarvin" Date: Tue, 26 Aug 2014 12:11:59 -0700 Subject: [PATCH] Add MurmurHash3. --- Makefile | 3 ++- f/loom.c | 41 ++++++++++++++++++++++++++++++++++++++ gen164/3/mur.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ gen164/watt.c | 2 ++ include/c/defs.h | 4 ++++ include/f/loom.h | 5 +++++ 6 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 gen164/3/mur.c diff --git a/Makefile b/Makefile index 2d1d8a2355..5e5502c1b0 100644 --- a/Makefile +++ b/Makefile @@ -144,6 +144,7 @@ J164_3_OFILES=\ gen164/3/met.o \ gen164/3/mix.o \ gen164/3/mug.o \ + gen164/3/mur.o \ gen164/3/peg.o \ gen164/3/po.o \ gen164/3/rap.o \ @@ -314,7 +315,7 @@ vere: $(BIN)/vere all: vere $(LIBUV_MAKEFILE): - cd outside/libuv_0.11 ; sh autogen.sh ; ./configure + cd outside/libuv_0.11 ; sh autogen.sh ; ./configure --disable-dtrace $(LIBUV): $(LIBUV_MAKEFILE) $(MAKE) -C outside/libuv_0.11 all-am diff --git a/f/loom.c b/f/loom.c index f0c3839b50..d3aa0dd380 100644 --- a/f/loom.c +++ b/f/loom.c @@ -1178,6 +1178,47 @@ u2_frag(u2_atom a, } } +/* 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. +*/ +c3_w +u2_mur_words(c3_w* key_w, c3_w len_w, c3_w syd_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(): ** ** Compute and/or recall the mug (31-bit FNV1a hash) of (a). diff --git a/gen164/3/mur.c b/gen164/3/mur.c new file mode 100644 index 0000000000..02a0e62601 --- /dev/null +++ b/gen164/3/mur.c @@ -0,0 +1,51 @@ +/* j/3/mur.c +** +** This file is in the public domain. +*/ +#include "all.h" +#include "../pit.h" + +/* functions +*/ + u2_weak // transfer + j2_mbc(Pt3, mur)(u2_wire wir_r, + u2_atom key, // retain + u2_atom syd) // retain + { + c3_w len_w = u2_cr_met(5, key); + c3_w syd_w = u2_cr_word(0, syd); + { + c3_w* key_w = alloca(4 * len_w); + c3_w goc_w; + + u2_cr_words(0, len_w, key_w, key); + goc_w = u2_mur_words(key_w, len_w, syd_w); + + fprintf(stderr, "goc_w %x\r\n", goc_w); + return u2_ci_words(1, &goc_w); + } + } + u2_weak // transfer + j2_mb(Pt3, mur)(u2_wire wir_r, + u2_noun cor) // retain + { + u2_noun a, b; + + if ( (u2_no == u2_mean(cor, u2_cv_sam_2, &a, u2_cv_sam_3, &b, 0)) || + (u2_no == u2_stud(a)) || + (u2_no == u2_stud(b)) ) + { + return u2_bl_bail(wir_r, c3__exit); + } else { + return j2_mbc(Pt3, mur)(wir_r, a, b); + } + } + +/* structures +*/ + u2_ho_jet + j2_mbj(Pt3, mur)[] = { + { ".2", c3__lite, j2_mb(Pt3, mur), + u2_jet_test | u2_jet_live, u2_none, u2_none }, + { } + }; diff --git a/gen164/watt.c b/gen164/watt.c index 40ff5ce257..7ee06ca8d5 100644 --- a/gen164/watt.c +++ b/gen164/watt.c @@ -63,6 +63,7 @@ extern u2_ho_jet j2_mbj(Pt3, met)[]; extern u2_ho_jet j2_mbj(Pt3, mix)[]; extern u2_ho_jet j2_mbj(Pt3, mug)[]; + extern u2_ho_jet j2_mbj(Pt3, mur)[]; extern u2_ho_jet j2_mbj(Pt3, peg)[]; extern u2_ho_jet j2_mbj(Pt3, rap)[]; extern u2_ho_jet j2_mbj(Pt3, rip)[]; @@ -211,6 +212,7 @@ { j2_sb(Pt3, met), j2_mbj(Pt3, met), 0, 0, u2_none }, { j2_sb(Pt3, mix), j2_mbj(Pt3, mix), 0, 0, u2_none }, { j2_sb(Pt3, mug), j2_mbj(Pt3, mug), 0, 0, u2_none }, + { j2_sb(Pt3, mur), j2_mbj(Pt3, mur), 0, 0, u2_none }, { j2_sb(Pt3, peg), j2_mbj(Pt3, peg), 0, 0, u2_none }, { j2_sb(Pt3, rap), j2_mbj(Pt3, rap), 0, 0, u2_none }, { j2_sb(Pt3, rip), j2_mbj(Pt3, rip), 0, 0, u2_none }, diff --git a/include/c/defs.h b/include/c/defs.h index f5e6cfc601..6c43239eb7 100644 --- a/include/c/defs.h +++ b/include/c/defs.h @@ -60,6 +60,10 @@ # define c3_max(x, y) ( ((x) > (y)) ? (x) : (y) ) # define c3_min(x, y) ( ((x) < (y)) ? (x) : (y) ) + /* Rotate. + */ +# define c3_rotw(r, x) ( ((x) << (r)) | ((x) >> (32 - (r))) ) + /* Emergency stdio fix. */ int diff --git a/include/f/loom.h b/include/f/loom.h index 03f6191639..7cc2e74511 100644 --- a/include/f/loom.h +++ b/include/f/loom.h @@ -555,6 +555,11 @@ c3_w u2_mug(u2_noun a); + /* u2_mur_words(): MurmurHash3 on raw words. + */ + c3_w + u2_mur_words(c3_w* key_w, c3_w len_w, c3_w syd_w); + /* u2_mug_string(): ** ** Compute the mug of `a`, LSB first.