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-04 01:33:18 +04:00
u2_noun
2014-09-03 07:36:29 +04:00
u2_cqe_rub(
2014-09-04 01:33:18 +04:00
u2_atom a,
u2_atom b)
2013-09-29 00:21:18 +04:00
{
u2_atom c, d, e;
u2_atom w, x, y, z;
u2_atom p, q;
2014-09-03 07:36:29 +04:00
u2_atom m = u2_cqa_add(a, u2_cr_met(0, b));
2013-09-29 00:21:18 +04:00
// Compute c and d.
{
2014-08-20 10:22:41 +04:00
x = u2k(a);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
while ( 0 == u2_cqc_cut(0, x, 1, b) ) {
u2_atom y = u2_cqa_inc(x);
2013-09-29 00:21:18 +04:00
// Sanity check: crash if decoding more bits than available
2014-09-03 07:36:29 +04:00
if ( u2_yes == u2_cqa_gth(x, m)) {
// fprintf(stderr, "[%%rub-hard %d %d %d]\r\n", a, x, m);
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__exit);
}
2014-08-20 10:22:41 +04:00
u2z(x);
2013-09-29 00:21:18 +04:00
x = y;
}
2014-08-20 10:22:41 +04:00
if ( u2_yes == u2_cr_sing(x, a) ) {
u2z(x);
return u2nc(1, 0);
2013-09-29 00:21:18 +04:00
}
2014-09-03 07:36:29 +04:00
c = u2_cqa_sub(x, a);
d = u2_cqa_inc(x);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(x);
2013-09-29 00:21:18 +04:00
}
2013-09-29 00:21:18 +04:00
// Compute e, p, q.
{
2014-09-03 07:36:29 +04:00
x = u2_cqa_dec(c);
y = u2_cqc_bex(x);
z = u2_cqc_cut(0, d, x, b);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
e = u2_cqa_add(y, z);
2014-08-20 10:22:41 +04:00
u2z(y); u2z(z);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
w = u2_cqa_add(c, c);
y = u2_cqa_add(w, e);
z = u2_cqa_add(d, x);
2013-09-29 00:21:18 +04:00
2014-09-03 07:36:29 +04:00
p = u2_cqa_add(w, e);
q = u2_cqc_cut(0, z, e, b);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
u2z(w); u2z(x); u2z(y); u2z(z);
2013-09-29 00:21:18 +04:00
2014-08-20 10:22:41 +04:00
return u2nc(p, q);
2013-09-29 00:21:18 +04:00
}
}
2014-09-04 01:33:18 +04:00
u2_noun
2014-09-03 09:46:55 +04:00
u2_cwe_rub(
2014-09-04 01:33:18 +04:00
u2_noun cor)
2013-09-29 00:21:18 +04:00
{
u2_noun a, b;
2014-08-20 10:22:41 +04:00
if ( (u2_no == u2_cr_mean(cor, u2_cv_sam_2, &a, u2_cv_sam_3, &b, 0)) ||
(u2_no == u2ud(a)) ||
(u2_no == u2ud(b)) )
2013-09-29 00:21:18 +04:00
{
2014-08-20 10:22:41 +04:00
return u2_cm_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
2014-09-03 07:36:29 +04:00
return u2_cqe_rub(a, b);
2013-09-29 00:21:18 +04:00
}
}