Skip to content

Commit 3483935

Browse files
committed
Inline isNaN and isFinite
Looking at our existing tests it seems that inlining these can be performed by reusing locals most of the time, and sometimes enables erasing quite a bit of unnecessary code when optimizing.
1 parent cf8e3df commit 3483935

File tree

9 files changed

+570
-625
lines changed

9 files changed

+570
-625
lines changed

std/assembly/builtins.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
@builtin export declare function isArray<T>(value?: T): bool;
1313
@builtin export declare function isDefined(expression: void): bool;
1414
@builtin export declare function isConstant(expression: void): bool;
15-
export function isNaN<T>(value: T): bool { return value != value; }
16-
export function isFinite<T>(value: T): bool { return value - value == 0; }
15+
@inline export function isNaN<T>(value: T): bool { return value != value; }
16+
@inline export function isFinite<T>(value: T): bool { return value - value == 0; }
1717

1818
@builtin export declare function clz<T>(value: T): T;
1919
@builtin export declare function ctz<T>(value: T): T;

tests/compiler/binary.optimized.wat

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
(type $FFF (func (param f64 f64) (result f64)))
33
(type $FiF (func (param f64 i32) (result f64)))
44
(type $fff (func (param f32 f32) (result f32)))
5-
(type $fi (func (param f32) (result i32)))
65
(type $fif (func (param f32 i32) (result f32)))
7-
(type $Fi (func (param f64) (result i32)))
86
(type $v (func))
97
(global $binary/b (mut i32) (i32.const 0))
108
(global $binary/i (mut i32) (i32.const 0))
@@ -1555,13 +1553,7 @@
15551553
(f64.const 1.e+300)
15561554
)
15571555
)
1558-
(func $~lib/builtins/isNaN<f32> (; 2 ;) (type $fi) (param $0 f32) (result i32)
1559-
(f32.ne
1560-
(get_local $0)
1561-
(get_local $0)
1562-
)
1563-
)
1564-
(func $~lib/math/NativeMathf.mod (; 3 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
1556+
(func $~lib/math/NativeMathf.mod (; 2 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
15651557
(local $2 i32)
15661558
(local $3 i32)
15671559
(local $4 i32)
@@ -1613,14 +1605,18 @@
16131605
)
16141606
)
16151607
(set_local $3
1616-
(call $~lib/builtins/isNaN<f32>
1608+
(f32.ne
1609+
(get_local $1)
16171610
(get_local $1)
16181611
)
16191612
)
16201613
)
16211614
(if
16221615
(i32.eqz
1623-
(get_local $3)
1616+
(i32.and
1617+
(get_local $3)
1618+
(i32.const 1)
1619+
)
16241620
)
16251621
(set_local $3
16261622
(i32.eq
@@ -1630,7 +1626,10 @@
16301626
)
16311627
)
16321628
(if
1633-
(get_local $3)
1629+
(i32.and
1630+
(get_local $3)
1631+
(i32.const 1)
1632+
)
16341633
(return
16351634
(f32.div
16361635
(tee_local $0
@@ -1902,7 +1901,7 @@
19021901
(get_local $0)
19031902
)
19041903
)
1905-
(func $~lib/math/NativeMathf.scalbn (; 4 ;) (type $fif) (param $0 f32) (param $1 i32) (result f32)
1904+
(func $~lib/math/NativeMathf.scalbn (; 3 ;) (type $fif) (param $0 f32) (param $1 i32) (result f32)
19061905
(local $2 f32)
19071906
(set_local $2
19081907
(get_local $0)
@@ -2014,7 +2013,7 @@
20142013
)
20152014
)
20162015
)
2017-
(func $~lib/math/NativeMathf.pow (; 5 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
2016+
(func $~lib/math/NativeMathf.pow (; 4 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
20182017
(local $2 f32)
20192018
(local $3 f32)
20202019
(local $4 i32)
@@ -3201,13 +3200,7 @@
32013200
(f32.const 1.0000000031710769e-30)
32023201
)
32033202
)
3204-
(func $~lib/builtins/isNaN<f64> (; 6 ;) (type $Fi) (param $0 f64) (result i32)
3205-
(f64.ne
3206-
(get_local $0)
3207-
(get_local $0)
3208-
)
3209-
)
3210-
(func $~lib/math/NativeMath.mod (; 7 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
3203+
(func $~lib/math/NativeMath.mod (; 5 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
32113204
(local $2 i64)
32123205
(local $3 i32)
32133206
(local $4 i64)
@@ -3267,7 +3260,8 @@
32673260
)
32683261
)
32693262
(set_local $7
3270-
(call $~lib/builtins/isNaN<f64>
3263+
(f64.ne
3264+
(get_local $1)
32713265
(get_local $1)
32723266
)
32733267
)
@@ -3581,7 +3575,7 @@
35813575
(get_local $0)
35823576
)
35833577
)
3584-
(func $start (; 8 ;) (type $v)
3578+
(func $start (; 6 ;) (type $v)
35853579
(drop
35863580
(i32.rem_s
35873581
(get_global $binary/i)

tests/compiler/binary.untouched.wat

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
(type $FFF (func (param f64 f64) (result f64)))
33
(type $FiF (func (param f64 i32) (result f64)))
44
(type $fff (func (param f32 f32) (result f32)))
5-
(type $fi (func (param f32) (result i32)))
65
(type $fif (func (param f32 i32) (result f32)))
7-
(type $Fi (func (param f64) (result i32)))
86
(type $v (func))
97
(global $binary/b (mut i32) (i32.const 0))
108
(global $binary/i (mut i32) (i32.const 0))
@@ -1692,13 +1690,7 @@
16921690
(get_local $13)
16931691
)
16941692
)
1695-
(func $~lib/builtins/isNaN<f32> (; 2 ;) (type $fi) (param $0 f32) (result i32)
1696-
(f32.ne
1697-
(get_local $0)
1698-
(get_local $0)
1699-
)
1700-
)
1701-
(func $~lib/math/NativeMathf.mod (; 3 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
1693+
(func $~lib/math/NativeMathf.mod (; 2 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
17021694
(local $2 i32)
17031695
(local $3 i32)
17041696
(local $4 i32)
@@ -1741,29 +1733,38 @@
17411733
)
17421734
)
17431735
(if
1744-
(if (result i32)
1745-
(tee_local $7
1746-
(if (result i32)
1736+
(i32.and
1737+
(if (result i32)
1738+
(i32.and
17471739
(tee_local $7
1748-
(i32.eq
1749-
(i32.shl
1750-
(get_local $3)
1751-
(i32.const 1)
1740+
(if (result i32)
1741+
(tee_local $7
1742+
(i32.eq
1743+
(i32.shl
1744+
(get_local $3)
1745+
(i32.const 1)
1746+
)
1747+
(i32.const 0)
1748+
)
1749+
)
1750+
(get_local $7)
1751+
(block $~lib/builtins/isNaN<f32>|inlined.1 (result i32)
1752+
(f32.ne
1753+
(get_local $1)
1754+
(get_local $1)
1755+
)
17521756
)
1753-
(i32.const 0)
17541757
)
17551758
)
1756-
(get_local $7)
1757-
(call $~lib/builtins/isNaN<f32>
1758-
(get_local $1)
1759-
)
1759+
(i32.const 1)
1760+
)
1761+
(get_local $7)
1762+
(i32.eq
1763+
(get_local $4)
1764+
(i32.const 255)
17601765
)
17611766
)
1762-
(get_local $7)
1763-
(i32.eq
1764-
(get_local $4)
1765-
(i32.const 255)
1766-
)
1767+
(i32.const 1)
17671768
)
17681769
(return
17691770
(f32.div
@@ -2119,7 +2120,7 @@
21192120
(get_local $2)
21202121
)
21212122
)
2122-
(func $~lib/math/NativeMathf.scalbn (; 4 ;) (type $fif) (param $0 f32) (param $1 i32) (result f32)
2123+
(func $~lib/math/NativeMathf.scalbn (; 3 ;) (type $fif) (param $0 f32) (param $1 i32) (result f32)
21232124
(local $2 f32)
21242125
(set_local $2
21252126
(get_local $0)
@@ -2235,7 +2236,7 @@
22352236
)
22362237
)
22372238
)
2238-
(func $~lib/math/NativeMathf.pow (; 5 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
2239+
(func $~lib/math/NativeMathf.pow (; 4 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
22392240
(local $2 i32)
22402241
(local $3 i32)
22412242
(local $4 i32)
@@ -3550,13 +3551,7 @@
35503551
(get_local $11)
35513552
)
35523553
)
3553-
(func $~lib/builtins/isNaN<f64> (; 6 ;) (type $Fi) (param $0 f64) (result i32)
3554-
(f64.ne
3555-
(get_local $0)
3556-
(get_local $0)
3557-
)
3558-
)
3559-
(func $~lib/math/NativeMath.mod (; 7 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
3554+
(func $~lib/math/NativeMath.mod (; 5 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
35603555
(local $2 i64)
35613556
(local $3 i64)
35623557
(local $4 i32)
@@ -3605,29 +3600,38 @@
36053600
)
36063601
)
36073602
(if
3608-
(if (result i32)
3609-
(tee_local $7
3610-
(if (result i32)
3603+
(i32.and
3604+
(if (result i32)
3605+
(i32.and
36113606
(tee_local $7
3612-
(i64.eq
3613-
(i64.shl
3614-
(get_local $3)
3615-
(i64.const 1)
3607+
(if (result i32)
3608+
(tee_local $7
3609+
(i64.eq
3610+
(i64.shl
3611+
(get_local $3)
3612+
(i64.const 1)
3613+
)
3614+
(i64.const 0)
3615+
)
3616+
)
3617+
(get_local $7)
3618+
(block $~lib/builtins/isNaN<f64>|inlined.1 (result i32)
3619+
(f64.ne
3620+
(get_local $1)
3621+
(get_local $1)
3622+
)
36163623
)
3617-
(i64.const 0)
36183624
)
36193625
)
3620-
(get_local $7)
3621-
(call $~lib/builtins/isNaN<f64>
3622-
(get_local $1)
3623-
)
3626+
(i32.const 1)
3627+
)
3628+
(get_local $7)
3629+
(i32.eq
3630+
(get_local $4)
3631+
(i32.const 2047)
36243632
)
36253633
)
3626-
(get_local $7)
3627-
(i32.eq
3628-
(get_local $4)
3629-
(i32.const 2047)
3630-
)
3634+
(i32.const 1)
36313635
)
36323636
(return
36333637
(f64.div
@@ -3996,7 +4000,7 @@
39964000
(get_local $2)
39974001
)
39984002
)
3999-
(func $start (; 8 ;) (type $v)
4003+
(func $start (; 6 ;) (type $v)
40004004
(drop
40014005
(i32.lt_s
40024006
(get_global $binary/i)

0 commit comments

Comments
 (0)