urbit/j/5/rub.c

83 lines
1.5 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/3/rub.c
**
** This file is in the public domain.
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
2013-09-29 00:21:18 +04:00
/* functions
*/
2014-09-06 00:13:24 +04:00
u3_noun
u3_cqe_rub(
u3_atom a,
u3_atom b)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_atom c, d, e;
u3_atom w, x, y, z;
u3_atom p, q;
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3_atom m = u3_cqa_add(a, u3_cr_met(0, b));
2013-09-29 00:21:18 +04:00
// Compute c and d.
{
2014-09-06 00:13:24 +04:00
x = u3k(a);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
while ( 0 == u3_cqc_cut(0, x, 1, b) ) {
u3_atom y = u3_cqa_inc(x);
2013-09-29 00:21:18 +04:00
// Sanity check: crash if decoding more bits than available
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_cqa_gth(x, m)) {
// fprintf(stderr, "[%%rub-hard %d %d %d]\r\n", a, x, m);
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__exit);
}
2014-09-06 00:13:24 +04:00
u3z(x);
2013-09-29 00:21:18 +04:00
x = y;
}
2014-09-06 00:13:24 +04:00
if ( u3_yes == u3_cr_sing(x, a) ) {
u3z(x);
return u3nc(1, 0);
2013-09-29 00:21:18 +04:00
}
2014-09-06 00:13:24 +04:00
c = u3_cqa_sub(x, a);
d = u3_cqa_inc(x);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(x);
2013-09-29 00:21:18 +04:00
}
2013-09-29 00:21:18 +04:00
// Compute e, p, q.
{
2014-09-06 00:13:24 +04:00
x = u3_cqa_dec(c);
y = u3_cqc_bex(x);
z = u3_cqc_cut(0, d, x, b);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
e = u3_cqa_add(y, z);
u3z(y); u3z(z);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
w = u3_cqa_add(c, c);
y = u3_cqa_add(w, e);
z = u3_cqa_add(d, x);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
p = u3_cqa_add(w, e);
q = u3_cqc_cut(0, z, e, b);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
u3z(w); u3z(x); u3z(y); u3z(z);
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
return u3nc(p, q);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwe_rub(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun a, b;
2013-09-29 00:21:18 +04:00
2014-09-06 00:13:24 +04:00
if ( (u3_no == u3_cr_mean(cor, u3_cv_sam_2, &a, u3_cv_sam_3, &b, 0)) ||
(u3_no == u3ud(a)) ||
(u3_no == u3ud(b)) )
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-09-06 00:13:24 +04:00
return u3_cqe_rub(a, b);
2013-09-29 00:21:18 +04:00
}
}