@@ -234,22 +234,29 @@ const OPERATIONS: Record<string, Intr> = Object.setPrototypeOf({
234234 ...tpl_ops ( "nat_" , "add:$1 double:$0" , "nat_chk(e, $0 + $o)" ,
235235 "nat_chk($0 + $o)" ) ,
236236 nat_mul : { C : "nat_mul(e, $0, $1)" , JS : "nat_chk($0 * $1)" } ,
237+ nat_pow : { JS : "nat_pow($0, $1)" } ,
237238 nat_cmp : { C : "(($0 > $1) + ($0 >= $1))" , JS : "cmp_new($0, $1)" } ,
238239 ...tpl_ops ( "nat_" , "sub" , "($0 < $1 ? 0 : $0 - $1)" ) ,
239- ...tpl_ops ( "nat_" , "is_lt:<" , "($0 $o $1)" ) ,
240+ ...tpl_ops ( "nat_" , CMPS , "($0 $o $1)" ) ,
240241 ...tpl_ops ( "nat_" , "min:< max:>" , "($0 $o $1 ? $0 : $1)" ) ,
241242 nat_divmod : {
242243 C : [ "($1 == 0 ? 0 : $0 / $1)" , "($1 == 0 ? $0 : $0 % $1)" ] ,
243244 JS : "nat_divmod($0, $1)" ,
244245 } ,
245246 ...tpl_ops ( "bool_" , "or:|:|| xor:^:!==" , "(($0) $o ($1))" , "($0 $o $1)" ) ,
246247 u32_show : { JS : "String($0)" } ,
248+ map_bit : { JS : "map_bit($0, $1)" } ,
249+ map_msb_u : { JS : "Math.min($0, 32 - Math.clz32($1))" } ,
247250 nat_show : { JS : "nat_show($0)" } ,
248251 u32_read : { JS : "decimal_read($0, 4294967295)" } ,
249252 nat_read : { JS : "decimal_read($0, 281474976710655)" } ,
250253 string_append : { JS : "($0 + $1)" } ,
251254 string_contains : { JS : "string_contains($0, $1)" } ,
252255 string_order : { JS : "cmp_new(string_compare($0, $1), 0)" } ,
256+ string_cmp : {
257+ JS : "{$: \"Tuple\", fst: {$: \"Tuple\", fst: $0, snd: $1},"
258+ + " snd: cmp_new(string_compare($0, $1), 0)}" ,
259+ } ,
253260 string_eq : { JS : "(string_compare($0, $1) === 0)" } ,
254261 string_get : { JS : "string_get($0, $1)" } ,
255262 string_length : { JS : "[...str_out($0)].length" } ,
@@ -508,6 +515,24 @@ function nat_chk(n) {
508515 return n;
509516}
510517
518+ function nat_pow(a, n) {
519+ let r = 1;
520+ while (n > 0) {
521+ if (n % 2) r = nat_chk(r * a);
522+ n = Math.floor(n / 2);
523+ if (n > 0) a = nat_chk(a * a);
524+ }
525+ return r;
526+ }
527+
528+ function map_bit(s, n) {
529+ let i = 0, k = Math.floor(n / 33);
530+ const off = n % 33;
531+ while (i < s.length && k-- > 0) i += char_len(s, i);
532+ return {$: "Tuple", fst: s, snd: i < s.length
533+ && (off === 0 || ((char_code(s, i) >>> (32 - off)) & 1) !== 0)};
534+ }
535+
511536function nat_in(n) {
512537 return Number.isInteger(n) && n >= 0 && n <= 2 ** 53 ? n : nat_chk(1 / 0);
513538}
0 commit comments