Fix Math.imul and add tests (#432)

This commit is contained in:
Max Graey
2019-01-24 02:23:53 +02:00
committed by Daniel Wirtz
parent 54b02c287c
commit d3715688fc
8 changed files with 745 additions and 258 deletions

View File

@ -550,7 +550,18 @@ export namespace NativeMath {
}
export function imul(x: f64, y: f64): f64 {
return <f64>(<i32>x * <i32>y);
/*
* Wasm (MVP) and JS have different approachas for double->int conversions.
*
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
* our float-point arguments before actual convertion to integers.
*/
if (!isFinite(x + y)) return 0;
const inv32 = 1.0 / 4294967296;
return (
<i32><i64>(x - 4294967296 * builtin_floor(x * inv32)) *
<i32><i64>(y - 4294967296 * builtin_floor(y * inv32))
);
}
export function log(x: f64): f64 { // see: musl/src/math/log.c and SUN COPYRIGHT NOTICE above