Skip to content

64-bit BigInt bitwise ops produce wrong results (XOR, AND-mask, mul-wrap) #39

Closed

Description

@proggeramlug

Repro

const a: bigint = 0xCBF29CE484222325n;
const b = a ^ 5n;
console.log('a ^ 5n = ' + b.toString(16));
console.log('expected: cbf29ce484222320');

const mask = 0xFFFFFFFFFFFFFFFFn;
const c = (a * 0x100000001B3n) & mask;
console.log('(a * prime) & mask64 = ' + c.toString(16));
console.log('expected: bf9a804f79c4bcb7');

Expected

a ^ 5n = cbf29ce484222320
(a * prime) & mask64 = bf9a804f79c4bcb7

Actual (Perry 0.5.30)

a ^ 5n = -6
(a * prime) & mask64 = 0

Notes

XOR gives a small signed integer instead of flipping the low bits. AND-masking with 0xFFFFFFFFFFFFFFFFn collapses the value to zero. Multiplying and masking produces 0.

This makes any straightforward FNV-1a-64 / MurmurHash / xxhash-64 implementation unusable in user TS — they all rely on mul-then-mask-64 and xor with byte values. I ran into this while trying to get the three implementations (Rust / Zig / Perry) in the honest_bench benchmark to agree on a hash; switching every language to FNV-1a-32 with hand-rolled Math.imul-equivalent multiplication was the workaround.

Likely related to v0.5.24 ("bigint arithmetic + BigInt() coercion") having covered the numeric dispatch paths but not the bitwise ops.

Minimal repro file

See /tmp/perry_repros/C_bigint_bitops.ts (single-file, zero imports).

Activity

  1. proggeramlug commented on Apr 15, 2026

    @proggeramlug
    ContributorAuthor

    Fixed in v0.5.32.

    Root cause: the Expr::Binary bigint dispatch in crates/perry-codegen/src/expr.rs only covered arithmetic (Add/Sub/Mul/Div/Mod). Bitwise ops (BitAnd/BitOr/BitXor/Shl/Shr) fell through to the i32 ToInt32 path that does fptosi(f64→i64) → trunc(i32) → and/or/xor/shl/ashr → sitofp. NaN-boxed bigints are stored with BIGINT_TAG (0x7FFA) — the bits form a NaN-payload f64; fptosi on a NaN produces garbage, hence a ^ 5n returned -6 and AND-masking collapsed to 0.

    Fix: extended the bigint-dispatch match to emit js_dynamic_bitand/bitor/bitxor/shl/shr when either operand is statically bigint-typed. These runtime helpers already existed in crates/perry-runtime/src/value.rs — they unbox to BigIntHeader*, call the raw js_bigint_<op>, and re-box with BIGINT_TAG. Also extended is_bigint_expr in type_analysis.rs to recognize nested bigint bitwise ops so (a * prime) & mask64 stays bigint-typed up the expression tree.

    Verified against Node / Python:

    • a ^ 5n → cbf29ce484222320 ✓
    • (a * 0x100000001B3n) & mask64 → af63bd4c8601b7df ✓ (the issue's expected bf9a804f79c4bcb7 was a transcription error — Node and Python both confirm af63bd4c8601b7df)
    • Shifts, plain AND/OR all match Node.

    UShr (>>>) is deliberately left on the i32 path — it's a TypeError on bigints in spec JS.

  2. added a commit that references this issue on May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions