urbit/jets/c/rip.c

93 lines
2.0 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/3/rip.c
**
*/
#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
2014-11-06 22:13:57 +03:00
u3qc_rip(u3_atom a,
u3_atom b)
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
if ( !_(u3a_is_cat(a)) || (a >= 32) ) {
return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
}
else {
2014-09-06 00:13:24 +04:00
u3_noun pir = u3_nul;
2013-09-29 00:21:18 +04:00
c3_g a_g = a;
c3_w i_w;
if ( a_g < 5 ) {
2014-11-06 03:20:01 +03:00
c3_w met_w = u3r_met(a_g, b);
2013-09-29 00:21:18 +04:00
c3_w mek_w = ((1 << (1 << a_g)) - 1);
for ( i_w = 0; i_w < met_w; i_w++ ) {
c3_w pat_w = (met_w - (i_w + 1));
c3_w bit_w = (pat_w << a_g);
c3_w wor_w = (bit_w >> 5);
c3_w sif_w = (bit_w & 31);
2014-11-06 03:20:01 +03:00
c3_w src_w = u3r_word(wor_w, b);
2013-09-29 00:21:18 +04:00
c3_w rip_w = ((src_w >> sif_w) & mek_w);
2014-09-06 00:13:24 +04:00
pir = u3nc(rip_w, pir);
2013-09-29 00:21:18 +04:00
}
return pir;
}
else {
2014-11-06 03:20:01 +03:00
c3_w met_w = u3r_met(a_g, b);
c3_w len_w = u3r_met(5, b);
c3_g san_g = (a_g - 5);
2013-09-29 00:21:18 +04:00
c3_w san_w = 1 << san_g;
c3_w dif_w = (met_w << san_g) - len_w;
c3_w tub_w = ((dif_w == 0) ? san_w : (san_w - dif_w));
for ( i_w = 0; i_w < met_w; i_w++ ) {
2014-08-20 05:39:48 +04:00
c3_w pat_w = (met_w - (i_w + 1));
c3_w wut_w = (pat_w << san_g);
c3_w sap_w = ((0 == i_w) ? tub_w : san_w);
2014-11-06 03:20:01 +03:00
c3_w* sal_w = u3a_slab(sap_w);
2013-09-29 00:21:18 +04:00
2014-08-20 05:39:48 +04:00
if ( 0 == sal_w ) {
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
} else {
c3_w j_w;
2014-09-06 00:13:24 +04:00
u3_atom rip;
2013-09-29 00:21:18 +04:00
for ( j_w = 0; j_w < sap_w; j_w++ ) {
2014-11-06 03:20:01 +03:00
sal_w[j_w] = u3r_word(wut_w + j_w, b);
2013-09-29 00:21:18 +04:00
}
2014-11-06 03:20:01 +03:00
rip = u3a_malt(sal_w);
2014-09-06 00:13:24 +04:00
pir = u3nc(rip, pir);
2013-09-29 00:21:18 +04:00
}
len_w -= san_w;
}
}
return pir;
}
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3wc_rip(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-11-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam_2, &a, u3x_sam_3, &b, 0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3ud(a)) ||
(c3n == u3ud(b)) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
2013-09-29 00:21:18 +04:00
} else {
2014-11-06 22:13:57 +03:00
return u3qc_rip(a, b);
2013-09-29 00:21:18 +04:00
}
}
u3_noun
u3kc_rip(u3_atom a,
u3_atom b)
{
u3_noun res = u3qc_rip(a, b);
u3z(a); u3z(b);
return res;
}