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

@@ -31,7 +31,7 @@
// =========================================================================
var NIL = Object.freeze({ _nil: true, toString: function() { return "nil"; } });
var SX_VERSION = "2026-04-26T18:15:33Z";
var SX_VERSION = "2026-04-26T19:02:22Z";
function isNil(x) { return x === NIL || x === null || x === undefined; }
function isSxTruthy(x) { return x !== false && !isNil(x); }
@@ -697,6 +697,26 @@
};
// 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));
};
function isPrimitive(name) { return name in PRIMITIVES; }
function getPrimitive(name) { return PRIMITIVES[name]; }