Skip to content

Commit

Permalink
builtins: rewrite lgc32 prng impl
Browse files Browse the repository at this point in the history
reduce into one impl based on glibc/musl
  • Loading branch information
CanadaHonk committed Dec 9, 2024
1 parent 44e8404 commit 2f8e639
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
35 changes: 4 additions & 31 deletions compiler/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,13 @@ export const BuiltinFuncs = function() {
const prngSeed0 = (Math.random() * (2 ** 30)) | 0, prngSeed1 = (Math.random() * (2 ** 30)) | 0;

const prng = ({
'lcg32_glibc': {
'lcg32': {
globals: [ Valtype.i32 ],
locals: [],
returns: Valtype.i32,
wasm: [
// use glibc/musl's constants
// seed = (MULTIPLIER * seed + INCREMENT) % MODULUS
// MULTIPLIER * state0
[ Opcodes.global_get, 0 ],
number(1103515245, Valtype.i32),
[ Opcodes.i32_mul ],
Expand All @@ -380,30 +380,8 @@ export const BuiltinFuncs = function() {
[ Opcodes.i32_add ],

// % MODULUS
number(2 ** 31, Valtype.i32),
[ Opcodes.i32_rem_s ],

// state0 =
[ Opcodes.global_set, 0 ],

// state0
[ Opcodes.global_get, 0 ],
],
},
'lcg32_minstd': {
globals: [ Valtype.i32 ],
locals: [],
returns: Valtype.i32,
wasm: [
// seed = (MULTIPLIER * seed + INCREMENT) % MODULUS
// MULTIPLIER * state0
[ Opcodes.global_get, 0 ],
number(48271, Valtype.i32),
[ Opcodes.i32_mul ],

// % MODULUS
number((2 ** 31) - 1, Valtype.i32),
[ Opcodes.i32_rem_s ],
number(0x7fffffff, Valtype.i32),
[ Opcodes.i32_and ],

// state0 =
[ Opcodes.global_set, 0 ],
Expand Down Expand Up @@ -484,11 +462,6 @@ export const BuiltinFuncs = function() {
// state0 = s1
[ Opcodes.global_set, 0 ],

// // s1 * 0x2545F4914F6CDD1D
// [ Opcodes.local_get, 0 ],
// [ Opcodes.i64_const, 0x9d, 0xba, 0xb3, 0xfb, 0x94, 0x92, 0xfd, 0xa2, 0x25 ],
// [ Opcodes.i64_mul ]

// s1
[ Opcodes.local_get, 0 ],
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "porffor",
"description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
"version": "0.50.27",
"version": "0.50.28",
"author": "CanadaHonk",
"license": "MIT",
"scripts": {},
Expand Down
4 changes: 2 additions & 2 deletions runner/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import fs from 'node:fs';
globalThis.version = '0.50.27';
globalThis.version = '0.50.28';

// deno compat
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
Expand Down Expand Up @@ -63,7 +63,7 @@ if (process.argv.includes('--help')) {
pgo: 'Enable PGO (profile-guided optimization)',
'profile-compiler': 'Log general compiler performance (on by default when compiling to a file)',
valtype: 'Valtype to use (i32|i64|\x1B[1mf64\x1B[0m)',
prng: 'PRNG algorithm to use (lcg32_glibc|lcg32_minstd|xorshift32+|xorshift64+|\x1B[1mxorshift128+\x1B[0m|xoroshiro128+|xoshiro128+)',
prng: 'PRNG algorithm to use (lcg32|xorshift32+|xorshift64+|\x1B[1mxorshift128+\x1B[0m|xoroshiro128+|xoshiro128+)',
allocator: 'Allocator to use (oneshot|\x1B[1mchunk\x1B[0m)',
'exception-mode': 'Exception mode to use (lut|\x1B[1mstack\x1B[0m)',
fastLength: 'Spec non-compliant optimization to make .length faster',
Expand Down

0 comments on commit 2f8e639

Please sign in to comment.