spec: bitwise operations (bitwise-and/or/xor/not, arithmetic-shift, bit-count, integer-length)

OCaml: land/lor/lxor/lnot/lsl/asr in sx_primitives.ml
JS: & | ^ ~ << >> with Kernighan popcount and Math.clz32 for integer-length
spec/primitives.sx: stdlib.bitwise module with 7 entries
26 tests, 158 assertions, all pass OCaml+JS

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 19:06:09 +00:00
parent 1ad9d63f1b
commit a8a79dc902
5 changed files with 289 additions and 2 deletions

View File

@@ -1309,6 +1309,27 @@ PRIMITIVES_JS_MODULES: dict[str, str] = {
return NIL;
};
''',
"stdlib.bitwise": '''
// stdlib.bitwise
PRIMITIVES["bitwise-and"] = function(a, b) { return (a & b) | 0; };
PRIMITIVES["bitwise-or"] = function(a, b) { return (a | b) | 0; };
PRIMITIVES["bitwise-xor"] = function(a, b) { return (a ^ b) | 0; };
PRIMITIVES["bitwise-not"] = function(a) { return ~a; };
PRIMITIVES["arithmetic-shift"] = function(a, count) {
return count >= 0 ? (a << count) | 0 : a >> (-count);
};
PRIMITIVES["bit-count"] = function(a) {
var n = Math.abs(a) >>> 0;
n = n - ((n >> 1) & 0x55555555);
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
return (((n + (n >> 4)) & 0x0f0f0f0f) * 0x01010101) >>> 24;
};
PRIMITIVES["integer-length"] = function(a) {
if (a === 0) return 0;
return 32 - Math.clz32(Math.abs(a));
};
''',
}
# Modules to include by default (all)
_ALL_JS_MODULES = list(PRIMITIVES_JS_MODULES.keys())