summaryrefslogtreecommitdiff
path: root/vere/pkg/noun/jets/c/clz.c
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-05 21:56:51 +0700
committerpolwex <polwex@sortug.com>2025-10-05 21:56:51 +0700
commitfcedfddf00b3f994e4f4e40332ac7fc192c63244 (patch)
tree51d38e62c7bdfcc5f9a5e9435fe820c93cfc9a3d /vere/pkg/noun/jets/c/clz.c
claude is gud
Diffstat (limited to 'vere/pkg/noun/jets/c/clz.c')
-rw-r--r--vere/pkg/noun/jets/c/clz.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/vere/pkg/noun/jets/c/clz.c b/vere/pkg/noun/jets/c/clz.c
new file mode 100644
index 0000000..f8da45a
--- /dev/null
+++ b/vere/pkg/noun/jets/c/clz.c
@@ -0,0 +1,75 @@
+/// @file
+
+#include "jets/q.h"
+#include "jets/w.h"
+
+#include "noun.h"
+
+u3_atom
+u3qc_clz(u3_atom boq, u3_atom sep, u3_atom a)
+{
+ if ( !_(u3a_is_cat(boq)) || (boq >= 32) ) {
+ return u3m_bail(c3__fail);
+ }
+
+ if ( !_(u3a_is_cat(sep)) ) {
+ return u3m_bail(c3__fail);
+ }
+
+ c3_g boq_g = boq;
+ c3_w sep_w = sep;
+ c3_w tot_w = sep_w << boq_g;
+
+ if ( (tot_w >> boq_g) != sep_w ) {
+ return u3m_bail(c3__fail);
+ }
+
+ c3_w met_w = u3r_met(0, a);
+
+ if ( met_w <= tot_w ) {
+ tot_w -= met_w;
+ return u3i_word(tot_w);
+ }
+ else {
+ c3_w wid_w = tot_w >> 5;
+ c3_w bit_w = tot_w & 31;
+ c3_w wor_w;
+
+ if ( bit_w ) {
+ wor_w = u3r_word(wid_w, a);
+ wor_w &= (1 << bit_w) - 1;
+
+ if ( wor_w ) {
+ return bit_w - (32 - c3_lz_w(wor_w));
+ }
+ }
+
+ while ( wid_w-- ) {
+ wor_w = u3r_word(wid_w, a);
+
+ if ( wor_w ) {
+ bit_w += c3_lz_w(wor_w);
+ break;
+ }
+
+ bit_w += 32;
+ }
+
+ return u3i_word(bit_w);
+ }
+}
+
+u3_noun
+u3wc_clz(u3_noun cor)
+{
+ u3_atom boq, sep, vat;
+ {
+ u3_noun sam = u3h(u3t(cor));
+ u3_noun bit = u3h(sam);
+
+ u3x_bite(bit, &boq, &sep);
+ vat = u3x_atom(u3t(sam));
+ }
+
+ return u3qc_clz(boq, sep, vat);
+}