Skip to content

Commit 7596d73

Browse files
committed
Shortcut f32/f64/i64 conversions to bool
1 parent c30c62e commit 7596d73

6 files changed

+1194
-175
lines changed

src/compiler.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,7 +2560,10 @@ export class Compiler extends DiagnosticEmitter {
25602560

25612561
// f32 to int
25622562
if (fromType.kind == TypeKind.F32) {
2563-
if (toType.is(TypeFlags.SIGNED)) {
2563+
if (toType == Type.bool) {
2564+
expr = module.createBinary(BinaryOp.NeF32, expr, module.createF32(0));
2565+
wrapMode = WrapMode.NONE;
2566+
} else if (toType.is(TypeFlags.SIGNED)) {
25642567
if (toType.is(TypeFlags.LONG)) {
25652568
expr = module.createUnary(UnaryOp.TruncF32ToI64, expr);
25662569
} else {
@@ -2576,7 +2579,10 @@ export class Compiler extends DiagnosticEmitter {
25762579

25772580
// f64 to int
25782581
} else {
2579-
if (toType.is(TypeFlags.SIGNED)) {
2582+
if (toType == Type.bool) {
2583+
expr = module.createBinary(BinaryOp.NeF64, expr, module.createF64(0));
2584+
wrapMode = WrapMode.NONE;
2585+
} else if (toType.is(TypeFlags.SIGNED)) {
25802586
if (toType.is(TypeFlags.LONG)) {
25812587
expr = module.createUnary(UnaryOp.TruncF64ToI64, expr);
25822588
} else {
@@ -2643,7 +2649,10 @@ export class Compiler extends DiagnosticEmitter {
26432649
if (fromType.is(TypeFlags.LONG)) {
26442650

26452651
// i64 to i32 or smaller
2646-
if (!toType.is(TypeFlags.LONG)) {
2652+
if (toType == Type.bool) {
2653+
expr = module.createBinary(BinaryOp.NeI64, expr, module.createI64(0));
2654+
wrapMode = WrapMode.NONE;
2655+
} else if (!toType.is(TypeFlags.LONG)) {
26472656
expr = module.createUnary(UnaryOp.WrapI64, expr); // discards upper bits
26482657
}
26492658

tests/compiler/bool.optimized.wat

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
unreachable
3232
end
3333
get_global $bool/I
34-
i32.wrap/i64
35-
i32.const 0
36-
i32.ne
34+
i64.const 0
35+
i64.ne
3736
i32.const 1
3837
i32.ne
3938
if
@@ -58,9 +57,8 @@
5857
unreachable
5958
end
6059
get_global $bool/U
61-
i32.wrap/i64
62-
i32.const 0
63-
i32.ne
60+
i64.const 0
61+
i64.ne
6462
i32.const 1
6563
i32.ne
6664
if
@@ -72,9 +70,8 @@
7270
unreachable
7371
end
7472
get_global $bool/f
75-
i32.trunc_u/f32
76-
i32.const 0
77-
i32.ne
73+
f32.const 0
74+
f32.ne
7875
i32.const 1
7976
i32.ne
8077
if
@@ -86,9 +83,8 @@
8683
unreachable
8784
end
8885
get_global $bool/F
89-
i32.trunc_u/f64
90-
i32.const 0
91-
i32.ne
86+
f64.const 0
87+
f64.ne
9288
i32.const 1
9389
i32.ne
9490
if

tests/compiler/bool.untouched.wat

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
unreachable
3434
end
3535
get_global $bool/I
36-
i32.wrap/i64
37-
i32.const 0
38-
i32.ne
36+
i64.const 0
37+
i64.ne
3938
i32.const 1
4039
i32.eq
4140
i32.eqz
@@ -62,9 +61,8 @@
6261
unreachable
6362
end
6463
get_global $bool/U
65-
i32.wrap/i64
66-
i32.const 0
67-
i32.ne
64+
i64.const 0
65+
i64.ne
6866
i32.const 1
6967
i32.eq
7068
i32.eqz
@@ -77,9 +75,8 @@
7775
unreachable
7876
end
7977
get_global $bool/f
80-
i32.trunc_u/f32
81-
i32.const 0
82-
i32.ne
78+
f32.const 0
79+
f32.ne
8380
i32.const 1
8481
i32.eq
8582
i32.eqz
@@ -92,9 +89,8 @@
9289
unreachable
9390
end
9491
get_global $bool/F
95-
i32.trunc_u/f64
96-
i32.const 0
97-
i32.ne
92+
f64.const 0
93+
f64.ne
9894
i32.const 1
9995
i32.eq
10096
i32.eqz

0 commit comments

Comments
 (0)