1 /* j/3/rub.c
2 **
3 */
4 #include "all.h"
5 
6 
7 /* functions
8 */
9   u3_noun
u3qe_rub(u3_atom a,u3_atom b)10   u3qe_rub(u3_atom a,
11            u3_atom b)
12   {
13     u3_atom c, d, e;
14     u3_atom w, x, y, z;
15     u3_atom p, q;
16 
17     u3_atom m = u3qa_add(a, u3r_met(0, b));
18 
19     //  Compute c and d.
20     {
21       x = u3k(a);
22 
23       while ( 0 == u3qc_cut(0, x, 1, b) ) {
24         u3_atom y = u3qa_inc(x);
25 
26         //  Sanity check: crash if decoding more bits than available
27         if ( c3y == u3qa_gth(x, m)) {
28           //  fprintf(stderr, "[%%rub-hard %d %d %d]\r\n", a, x, m);
29           return u3m_bail(c3__exit);
30         }
31 
32         u3z(x);
33         x = y;
34       }
35       if ( c3y == u3r_sing(x, a) ) {
36         u3z(x);
37         return u3nc(1, 0);
38       }
39       c = u3qa_sub(x, a);
40       d = u3qa_inc(x);
41 
42       u3z(x);
43     }
44 
45     //  Compute e, p, q.
46     {
47       x = u3qa_dec(c);
48       y = u3qc_bex(x);
49       z = u3qc_cut(0, d, x, b);
50 
51       e = u3qa_add(y, z);
52       u3z(y); u3z(z);
53 
54       w = u3qa_add(c, c);
55       y = u3qa_add(w, e);
56       z = u3qa_add(d, x);
57 
58       p = u3qa_add(w, e);
59       q = u3qc_cut(0, z, e, b);
60 
61       u3z(w); u3z(x); u3z(y); u3z(z);
62 
63       return u3nc(p, q);
64     }
65   }
66   u3_noun
u3we_rub(u3_noun cor)67   u3we_rub(u3_noun cor)
68   {
69     u3_noun a, b;
70 
71     if ( (c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0)) ||
72          (c3n == u3ud(a)) ||
73          (c3n == u3ud(b)) )
74     {
75       return u3m_bail(c3__fail);
76     } else {
77       return u3qe_rub(a, b);
78     }
79   }
80