Skip to content

Commit d371568

Browse files
MaxGraeydcodeIO
authored andcommitted
Fix Math.imul and add tests (AssemblyScript#432)
1 parent 54b02c2 commit d371568

File tree

8 files changed

+609
-122
lines changed

8 files changed

+609
-122
lines changed

std/assembly/math.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,18 @@ export namespace NativeMath {
550550
}
551551

552552
export function imul(x: f64, y: f64): f64 {
553-
return <f64>(<i32>x * <i32>y);
553+
/*
554+
* Wasm (MVP) and JS have different approachas for double->int conversions.
555+
*
556+
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
557+
* our float-point arguments before actual convertion to integers.
558+
*/
559+
if (!isFinite(x + y)) return 0;
560+
const inv32 = 1.0 / 4294967296;
561+
return (
562+
<i32><i64>(x - 4294967296 * builtin_floor(x * inv32)) *
563+
<i32><i64>(y - 4294967296 * builtin_floor(y * inv32))
564+
);
554565
}
555566

556567
export function log(x: f64): f64 { // see: musl/src/math/log.c and SUN COPYRIGHT NOTICE above

tests/compiler/std/array.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,7 +3813,7 @@
38133813
if
38143814
i32.const 0
38153815
i32.const 2816
3816-
i32.const 959
3816+
i32.const 970
38173817
i32.const 4
38183818
call $~lib/env/abort
38193819
unreachable
@@ -5479,7 +5479,7 @@
54795479
if
54805480
i32.const 0
54815481
i32.const 2816
5482-
i32.const 968
5482+
i32.const 979
54835483
i32.const 24
54845484
call $~lib/env/abort
54855485
unreachable

tests/compiler/std/array.untouched.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5225,7 +5225,7 @@
52255225
if
52265226
i32.const 0
52275227
i32.const 2816
5228-
i32.const 959
5228+
i32.const 970
52295229
i32.const 4
52305230
call $~lib/env/abort
52315231
unreachable
@@ -8241,7 +8241,7 @@
82418241
if
82428242
i32.const 0
82438243
i32.const 2816
8244-
i32.const 968
8244+
i32.const 979
82458245
i32.const 24
82468246
call $~lib/env/abort
82478247
unreachable

tests/compiler/std/libm.optimized.wat

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,19 +2347,53 @@
23472347
get_local $1
23482348
call $~lib/math/NativeMath.hypot
23492349
)
2350-
(func $std/libm/imul (; 34 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
2350+
(func $~lib/math/NativeMath.imul (; 34 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
2351+
(local $2 f64)
23512352
get_local $0
2352-
i32.trunc_s/f64
23532353
get_local $1
2354-
i32.trunc_s/f64
2354+
f64.add
2355+
tee_local $2
2356+
get_local $2
2357+
f64.sub
2358+
f64.const 0
2359+
f64.ne
2360+
if
2361+
f64.const 0
2362+
return
2363+
end
2364+
get_local $0
2365+
f64.const 4294967296
2366+
get_local $0
2367+
f64.const 2.3283064365386963e-10
2368+
f64.mul
2369+
f64.floor
2370+
f64.mul
2371+
f64.sub
2372+
i64.trunc_s/f64
2373+
i32.wrap/i64
2374+
get_local $1
2375+
f64.const 4294967296
2376+
get_local $1
2377+
f64.const 2.3283064365386963e-10
2378+
f64.mul
2379+
f64.floor
2380+
f64.mul
2381+
f64.sub
2382+
i64.trunc_s/f64
2383+
i32.wrap/i64
23552384
i32.mul
23562385
f64.convert_s/i32
23572386
)
2358-
(func $std/libm/log (; 35 ;) (type $FF) (param $0 f64) (result f64)
2387+
(func $std/libm/imul (; 35 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
2388+
get_local $0
2389+
get_local $1
2390+
call $~lib/math/NativeMath.imul
2391+
)
2392+
(func $std/libm/log (; 36 ;) (type $FF) (param $0 f64) (result f64)
23592393
get_local $0
23602394
call $~lib/math/NativeMath.log
23612395
)
2362-
(func $~lib/math/NativeMath.log10 (; 36 ;) (type $FF) (param $0 f64) (result f64)
2396+
(func $~lib/math/NativeMath.log10 (; 37 ;) (type $FF) (param $0 f64) (result f64)
23632397
(local $1 f64)
23642398
(local $2 i32)
23652399
(local $3 i32)
@@ -2571,15 +2605,15 @@
25712605
get_local $0
25722606
f64.add
25732607
)
2574-
(func $std/libm/log10 (; 37 ;) (type $FF) (param $0 f64) (result f64)
2608+
(func $std/libm/log10 (; 38 ;) (type $FF) (param $0 f64) (result f64)
25752609
get_local $0
25762610
call $~lib/math/NativeMath.log10
25772611
)
2578-
(func $std/libm/log1p (; 38 ;) (type $FF) (param $0 f64) (result f64)
2612+
(func $std/libm/log1p (; 39 ;) (type $FF) (param $0 f64) (result f64)
25792613
get_local $0
25802614
call $~lib/math/NativeMath.log1p
25812615
)
2582-
(func $~lib/math/NativeMath.log2 (; 39 ;) (type $FF) (param $0 f64) (result f64)
2616+
(func $~lib/math/NativeMath.log2 (; 40 ;) (type $FF) (param $0 f64) (result f64)
25832617
(local $1 f64)
25842618
(local $2 i32)
25852619
(local $3 i32)
@@ -2785,21 +2819,21 @@
27852819
get_local $0
27862820
f64.add
27872821
)
2788-
(func $std/libm/log2 (; 40 ;) (type $FF) (param $0 f64) (result f64)
2822+
(func $std/libm/log2 (; 41 ;) (type $FF) (param $0 f64) (result f64)
27892823
get_local $0
27902824
call $~lib/math/NativeMath.log2
27912825
)
2792-
(func $std/libm/max (; 41 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
2826+
(func $std/libm/max (; 42 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
27932827
get_local $0
27942828
get_local $1
27952829
f64.max
27962830
)
2797-
(func $std/libm/min (; 42 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
2831+
(func $std/libm/min (; 43 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
27982832
get_local $0
27992833
get_local $1
28002834
f64.min
28012835
)
2802-
(func $~lib/math/NativeMath.pow (; 43 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
2836+
(func $~lib/math/NativeMath.pow (; 44 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
28032837
(local $2 f64)
28042838
(local $3 f64)
28052839
(local $4 i32)
@@ -3749,20 +3783,20 @@
37493783
f64.const 1e-300
37503784
f64.mul
37513785
)
3752-
(func $std/libm/pow (; 44 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
3786+
(func $std/libm/pow (; 45 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
37533787
get_local $0
37543788
get_local $1
37553789
call $~lib/math/NativeMath.pow
37563790
)
3757-
(func $std/libm/round (; 45 ;) (type $FF) (param $0 f64) (result f64)
3791+
(func $std/libm/round (; 46 ;) (type $FF) (param $0 f64) (result f64)
37583792
get_local $0
37593793
f64.const 0.5
37603794
f64.add
37613795
f64.floor
37623796
get_local $0
37633797
f64.copysign
37643798
)
3765-
(func $std/libm/sign (; 46 ;) (type $FF) (param $0 f64) (result f64)
3799+
(func $std/libm/sign (; 47 ;) (type $FF) (param $0 f64) (result f64)
37663800
get_local $0
37673801
f64.const 0
37683802
f64.gt
@@ -3780,7 +3814,7 @@
37803814
end
37813815
get_local $0
37823816
)
3783-
(func $~lib/math/NativeMath.sinh (; 47 ;) (type $FF) (param $0 f64) (result f64)
3817+
(func $~lib/math/NativeMath.sinh (; 48 ;) (type $FF) (param $0 f64) (result f64)
37843818
(local $1 f64)
37853819
(local $2 f64)
37863820
(local $3 i32)
@@ -3857,15 +3891,15 @@
38573891
f64.mul
38583892
f64.mul
38593893
)
3860-
(func $std/libm/sinh (; 48 ;) (type $FF) (param $0 f64) (result f64)
3894+
(func $std/libm/sinh (; 49 ;) (type $FF) (param $0 f64) (result f64)
38613895
get_local $0
38623896
call $~lib/math/NativeMath.sinh
38633897
)
3864-
(func $std/libm/sqrt (; 49 ;) (type $FF) (param $0 f64) (result f64)
3898+
(func $std/libm/sqrt (; 50 ;) (type $FF) (param $0 f64) (result f64)
38653899
get_local $0
38663900
f64.sqrt
38673901
)
3868-
(func $~lib/math/NativeMath.tanh (; 50 ;) (type $FF) (param $0 f64) (result f64)
3902+
(func $~lib/math/NativeMath.tanh (; 51 ;) (type $FF) (param $0 f64) (result f64)
38693903
(local $1 f64)
38703904
(local $2 i32)
38713905
(local $3 i64)
@@ -3944,15 +3978,15 @@
39443978
get_local $0
39453979
f64.copysign
39463980
)
3947-
(func $std/libm/tanh (; 51 ;) (type $FF) (param $0 f64) (result f64)
3981+
(func $std/libm/tanh (; 52 ;) (type $FF) (param $0 f64) (result f64)
39483982
get_local $0
39493983
call $~lib/math/NativeMath.tanh
39503984
)
3951-
(func $std/libm/trunc (; 52 ;) (type $FF) (param $0 f64) (result f64)
3985+
(func $std/libm/trunc (; 53 ;) (type $FF) (param $0 f64) (result f64)
39523986
get_local $0
39533987
f64.trunc
39543988
)
3955-
(func $null (; 53 ;) (type $v)
3989+
(func $null (; 54 ;) (type $v)
39563990
nop
39573991
)
39583992
)

0 commit comments

Comments
 (0)