summaryrefslogtreecommitdiff
path: root/vere/pkg/noun/jets/e/rub.c
diff options
context:
space:
mode:
Diffstat (limited to 'vere/pkg/noun/jets/e/rub.c')
-rw-r--r--vere/pkg/noun/jets/e/rub.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/vere/pkg/noun/jets/e/rub.c b/vere/pkg/noun/jets/e/rub.c
new file mode 100644
index 0000000..81739be
--- /dev/null
+++ b/vere/pkg/noun/jets/e/rub.c
@@ -0,0 +1,85 @@
+/// @file
+
+#include "jets/q.h"
+#include "jets/w.h"
+
+#include "noun.h"
+
+
+ u3_noun
+ u3qe_rub(u3_atom a,
+ u3_atom b)
+ {
+ u3_atom c, d, e;
+ u3_atom w, x, y, z;
+ u3_atom p, q;
+
+ u3_atom m;
+ {
+ c3_w bit_w = u3r_met(0, b);
+ u3_noun bit = u3i_words(1, &bit_w);
+ m = u3qa_add(a, bit);
+ u3z(bit);
+ }
+
+ // Compute c and d.
+ {
+ x = u3k(a);
+
+ while ( 0 == u3qc_cut(0, x, 1, b) ) {
+ u3_atom y = u3qa_inc(x);
+
+ // Sanity check: crash if decoding more bits than available
+ if ( c3y == u3qa_gth(x, m)) {
+ // u3l_log("[%%rub-hard %d %d %d]", a, x, m);
+ return u3m_bail(c3__exit);
+ }
+
+ u3z(x);
+ x = y;
+ }
+ if ( c3y == u3r_sing(x, a) ) {
+ u3z(x);
+ return u3nc(1, 0);
+ }
+ c = u3qa_sub(x, a);
+ d = u3qa_inc(x);
+
+ u3z(x);
+ }
+
+ // Compute e, p, q.
+ {
+ x = u3qa_dec(c);
+ y = u3qc_bex(x);
+ z = u3qc_cut(0, d, x, b);
+
+ e = u3qa_add(y, z);
+ u3z(y); u3z(z);
+
+ w = u3qa_add(c, c);
+ y = u3qa_add(w, e);
+ z = u3qa_add(d, x);
+
+ p = u3qa_add(w, e);
+ q = u3qc_cut(0, z, e, b);
+
+ u3z(w); u3z(x); u3z(y); u3z(z);
+
+ return u3nc(p, q);
+ }
+ }
+ u3_noun
+ u3we_rub(u3_noun cor)
+ {
+ u3_noun a, b;
+
+ if ( (c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0)) ||
+ (c3n == u3ud(a)) ||
+ (c3n == u3ud(b)) )
+ {
+ return u3m_bail(c3__fail);
+ } else {
+ return u3qe_rub(a, b);
+ }
+ }