* 32-bit addition with overflow handling (JavaScript numbers are 53-bit). * * Example: add32(0xFFFFFFFF, 0x00000001) = 0x00000000 (wraps around)
(x: number, y: number)
| 536 | * Example: add32(0xFFFFFFFF, 0x00000001) = 0x00000000 (wraps around) |
| 537 | */ |
| 538 | function add32(x: number, y: number): number { |
| 539 | // Add lower 16 bits separately to handle carry |
| 540 | // Example: x=0x12345678, y=0xABCDEF01 |
| 541 | // lsb = (0x5678 + 0xEF01) = 0x14579 |
| 542 | const lsb = (x & 0xffff) + (y & 0xffff) |
| 543 | |
| 544 | // Add upper 16 bits + carry from lower addition |
| 545 | // msb = (0x1234 + 0xABCD + (0x14579 >>> 16)) = (0x1234 + 0xABCD + 0x1) = 0xBE02 |
| 546 | const msb = (x >>> 16) + (y >>> 16) + (lsb >>> 16) |
| 547 | |
| 548 | // Combine: upper 16 bits | lower 16 bits (masked to prevent double carry) |
| 549 | // return (0xBE02 << 16) | (0x14579 & 0xffff) = 0xBE024579 |
| 550 | return (msb << 16) | (lsb & 0xffff) |
| 551 | } |
| 552 | |
| 553 | const seedEncoder = new TextEncoder() |
| 554 |