Skip to content

Commit 1cea3c6

Browse files
MaxGraeydcodeIO
authored andcommitted
Improve Math#sign (#72)
1 parent 2002e32 commit 1cea3c6

File tree

5 files changed

+40
-50
lines changed

5 files changed

+40
-50
lines changed

std/assembly/math.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,8 @@ export namespace NativeMath {
10421042

10431043
@inline
10441044
export function sign(x: f64): f64 {
1045-
return x > 0 ? 1 : x < 0 ? -1 : x;
1045+
// return x > 0 ? 1 : x < 0 ? -1 : x;
1046+
return builtin_abs<f64>(x) > 0 ? builtin_copysign<f64>(1, x) : x;
10461047
}
10471048

10481049
export function sin(x: f64): f64 { // TODO
@@ -2096,7 +2097,8 @@ export namespace NativeMathf {
20962097

20972098
@inline
20982099
export function sign(x: f32): f32 {
2099-
return x > 0 ? 1 : x < 0 ? -1 : x;
2100+
// return x > 0 ? 1 : x < 0 ? -1 : x;
2101+
return builtin_abs<f32>(x) > 0 ? builtin_copysign<f32>(1, x) : x;
21002102
}
21012103

21022104
export function sin(x: f32): f32 { // TODO

tests/compiler/std/libm.optimized.wat

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5719,19 +5719,17 @@
57195719
(func $std/libm/sign (; 50 ;) (type $FF) (param $0 f64) (result f64)
57205720
(local $1 f64)
57215721
(select
5722-
(f64.const 1)
5723-
(select
5724-
(f64.const -1)
5722+
(f64.copysign
5723+
(f64.const 1)
57255724
(tee_local $1
57265725
(get_local $0)
57275726
)
5728-
(f64.lt
5729-
(get_local $1)
5730-
(f64.const 0)
5731-
)
57325727
)
5728+
(get_local $1)
57335729
(f64.gt
5734-
(get_local $1)
5730+
(f64.abs
5731+
(get_local $1)
5732+
)
57355733
(f64.const 0)
57365734
)
57375735
)

tests/compiler/std/libm.untouched.wat

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6839,18 +6839,16 @@
68396839
(br $~lib/math/NativeMath.sign|inlined.0
68406840
(if (result f64)
68416841
(f64.gt
6842-
(get_local $1)
6843-
(f64.const 0)
6844-
)
6845-
(f64.const 1)
6846-
(if (result f64)
6847-
(f64.lt
6842+
(f64.abs
68486843
(get_local $1)
6849-
(f64.const 0)
68506844
)
6851-
(f64.const -1)
6845+
(f64.const 0)
6846+
)
6847+
(f64.copysign
6848+
(f64.const 1)
68526849
(get_local $1)
68536850
)
6851+
(get_local $1)
68546852
)
68556853
)
68566854
)

tests/compiler/std/math.optimized.wat

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12474,19 +12474,17 @@
1247412474
(tee_local $4
1247512475
(call $std/math/check<f64>
1247612476
(select
12477-
(f64.const 1)
12478-
(select
12479-
(f64.const -1)
12477+
(f64.copysign
12478+
(f64.const 1)
1248012479
(tee_local $5
1248112480
(get_local $0)
1248212481
)
12483-
(f64.lt
12484-
(get_local $5)
12485-
(f64.const 0)
12486-
)
1248712482
)
12483+
(get_local $5)
1248812484
(f64.gt
12489-
(get_local $5)
12485+
(f64.abs
12486+
(get_local $5)
12487+
)
1249012488
(f64.const 0)
1249112489
)
1249212490
)
@@ -12518,19 +12516,17 @@
1251812516
(local $4 f32)
1251912517
(call $std/math/check<f32>
1252012518
(select
12521-
(f32.const 1)
12522-
(select
12523-
(f32.const -1)
12519+
(f32.copysign
12520+
(f32.const 1)
1252412521
(tee_local $4
1252512522
(get_local $0)
1252612523
)
12527-
(f32.lt
12528-
(get_local $4)
12529-
(f32.const 0)
12530-
)
1253112524
)
12525+
(get_local $4)
1253212526
(f32.gt
12533-
(get_local $4)
12527+
(f32.abs
12528+
(get_local $4)
12529+
)
1253412530
(f32.const 0)
1253512531
)
1253612532
)

tests/compiler/std/math.untouched.wat

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14859,18 +14859,16 @@
1485914859
(br $~lib/math/NativeMath.sign|inlined.0
1486014860
(if (result f64)
1486114861
(f64.gt
14862-
(get_local $4)
14863-
(f64.const 0)
14864-
)
14865-
(f64.const 1)
14866-
(if (result f64)
14867-
(f64.lt
14862+
(f64.abs
1486814863
(get_local $4)
14869-
(f64.const 0)
1487014864
)
14871-
(f64.const -1)
14865+
(f64.const 0)
14866+
)
14867+
(f64.copysign
14868+
(f64.const 1)
1487214869
(get_local $4)
1487314870
)
14871+
(get_local $4)
1487414872
)
1487514873
)
1487614874
)
@@ -14912,18 +14910,16 @@
1491214910
(br $~lib/math/NativeMathf.sign|inlined.0
1491314911
(if (result f32)
1491414912
(f32.gt
14915-
(get_local $4)
14916-
(f32.const 0)
14917-
)
14918-
(f32.const 1)
14919-
(if (result f32)
14920-
(f32.lt
14913+
(f32.abs
1492114914
(get_local $4)
14922-
(f32.const 0)
1492314915
)
14924-
(f32.const -1)
14916+
(f32.const 0)
14917+
)
14918+
(f32.copysign
14919+
(f32.const 1)
1492514920
(get_local $4)
1492614921
)
14922+
(get_local $4)
1492714923
)
1492814924
)
1492914925
)

0 commit comments

Comments
 (0)