Skip to content

Commit

Permalink
Fix mul_upper_s_s
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw committed Dec 31, 2024
1 parent c62bd4a commit 93bd87a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
23 changes: 14 additions & 9 deletions assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ export function mulUpperUnsigned(a: u64, b: u64): u64 {
* Same as [mulUpperUnsigned] but treat the arguments as signed (two-complement) 64-bit numbers and the result alike.
*/
export function mulUpperSigned(a: i64, b: i64): u64 {
const aSign = a < 0 ? 1 : -1;
const bSign = b < 0 ? 1 : -1;
const sign = aSign * bSign;
let isResultNegative = false;
let aAbs = a;
let bAbs = b;
if (a < 0) {
isResultNegative = !isResultNegative;
aAbs = ~a + 1;
}
if (b < 0) {
isResultNegative = !isResultNegative;
bAbs = ~b + 1;
}

if (sign < 0) {
const aAbs: u64 = a < 0 ? ~a + 1 : a;
const bAbs: u64 = b < 0 ? ~b + 1 : b;
if (isResultNegative) {
const upper = mulUpperUnsigned(aAbs, bAbs);
const lower = aAbs * bAbs;
return ~upper + (lower === 0 ? 1 : 0);
}

return mulUpperUnsigned(a, b);
return mulUpperUnsigned(aAbs, bAbs);
}

export function mulUpperSignedUnsigned(a: i64, b: u64): u64 {
const aSign = a < 0 ? 1 : -1;
if (aSign === 1) {
if (a < 0) {
const aAbs: u64 = ~a + 1;
const upper = mulUpperUnsigned(aAbs, b);
const lower = aAbs * b;
Expand Down
32 changes: 18 additions & 14 deletions bin/fuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ export function fuzz(data) {
assert(pvm.getProgramCounter(), output.pc, 'pc');
});

writeTestCase(
program,
{
pc,
gas,
registers,
},
{
status: pvm.getStatus(),
gasLeft: pvm.getGasLeft(),
pc: pvm.getProgramCounter(),
registers: pvm.getRegisters()
},
);
try {
writeTestCase(
program,
{
pc,
gas,
registers,
},
{
status: pvm.getStatus(),
gasLeft: pvm.getGasLeft(),
pc: pvm.getProgramCounter(),
registers: pvm.getRegisters()
},
);
} catch (e) {
console.warn('Unable to write file', e);
}
} catch (e) {
const hex = programHex(program);
console.log(program);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"prestart": "npm run build",
"preweb": "npm run build",
"web": "npx live-server ./web",
"start": "node ./bin/index.js"
"start": "node ./bin/index.js",
"fuzz": "npx jazzer --sync ./bin/fuzz"
},
"keywords": [],
"author": "Fluffy Labs",
Expand Down

0 comments on commit 93bd87a

Please sign in to comment.