Skip to content

Add optimized for size float to bool conversion #2445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 64 additions & 28 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9883,38 +9883,74 @@ export class Compiler extends DiagnosticEmitter {
: expr;
}
case TypeKind.F32: {
// 0 < abs(bitCast(x)) <= bitCast(Infinity) or
// (reinterpret<u32>(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1
//
// and finally:
// (reinterpret<u32>(x) << 1) - (1 << 1) <= ((0x7F800000 - 1) << 1)
return module.binary(BinaryOp.LeU32,
module.binary(BinaryOp.SubI32,
module.binary(BinaryOp.ShlI32,
module.unary(UnaryOp.ReinterpretF32ToI32, expr),
module.i32(1)
let options = this.options;
if (
options.shrinkLevelHint > 1 &&
options.hasFeature(Feature.NONTRAPPING_F2I)
) {
// Use more compact but slower 5-byte (3 bytes in best case) approach
// !!(i32.trunc_sat_f32_u(f32.ceil(f32.abs(x))))
return module.unary(UnaryOp.EqzI32,
module.unary(UnaryOp.EqzI32,
module.unary(UnaryOp.TruncSatF32ToU32,
module.unary(UnaryOp.CeilF32,
module.unary(UnaryOp.AbsF32, expr)
)
)
)
);
} else {
// 0 < abs(bitCast(x)) <= bitCast(Infinity) or
// (reinterpret<u32>(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1
//
// and finally:
// (reinterpret<u32>(x) << 1) - (1 << 1) <= ((0x7F800000 - 1) << 1)
return module.binary(BinaryOp.LeU32,
module.binary(BinaryOp.SubI32,
module.binary(BinaryOp.ShlI32,
module.unary(UnaryOp.ReinterpretF32ToI32, expr),
module.i32(1)
),
module.i32(2) // 1 << 1
),
module.i32(2) // 1 << 1
),
module.i32(0xFEFFFFFE) // (0x7F800000 - 1) << 1
);
module.i32(0xFEFFFFFE) // (0x7F800000 - 1) << 1
);
}
}
case TypeKind.F64: {
// 0 < abs(bitCast(x)) <= bitCast(Infinity) or
// (reinterpret<u64>(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1
//
// and finally:
// (reinterpret<u64>(x) << 1) - (1 << 1) <= ((0x7FF0000000000000 - 1) << 1)
return module.binary(BinaryOp.LeU64,
module.binary(BinaryOp.SubI64,
module.binary(BinaryOp.ShlI64,
module.unary(UnaryOp.ReinterpretF64ToI64, expr),
module.i64(1)
let options = this.options;
if (
options.shrinkLevelHint > 1 &&
options.hasFeature(Feature.NONTRAPPING_F2I)
) {
// Use more compact but slower 5-byte (3 bytes in best case) approach
// !!(i32.trunc_sat_f64_u(f64.ceil(f64.abs(x))))
return module.unary(UnaryOp.EqzI32,
module.unary(UnaryOp.EqzI32,
module.unary(UnaryOp.TruncSatF64ToU32,
module.unary(UnaryOp.CeilF64,
module.unary(UnaryOp.AbsF64, expr)
)
)
)
);
} else {
// 0 < abs(bitCast(x)) <= bitCast(Infinity) or
// (reinterpret<u64>(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1
//
// and finally:
// (reinterpret<u64>(x) << 1) - (1 << 1) <= ((0x7FF0000000000000 - 1) << 1)
return module.binary(BinaryOp.LeU64,
module.binary(BinaryOp.SubI64,
module.binary(BinaryOp.ShlI64,
module.unary(UnaryOp.ReinterpretF64ToI64, expr),
module.i64(1)
),
module.i64(2) // 1 << 1
),
module.i64(2) // 1 << 1
),
module.i64(0xFFFFFFFE, 0xFFDFFFFF) // (0x7FF0000000000000 - 1) << 1
);
module.i64(0xFFFFFFFE, 0xFFDFFFFF) // (0x7FF0000000000000 - 1) << 1
);
}
}
case TypeKind.V128: {
return module.unary(UnaryOp.AnyTrueV128, expr);
Expand Down
6 changes: 6 additions & 0 deletions tests/compiler/bool-Oz.debug.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(module
(memory $0 1)
(data (i32.const 12) ",")
(data (i32.const 24) "\01\00\00\00\14\00\00\00b\00o\00o\00l\00-\00O\00z\00.\00t\00s")
(export "memory" (memory $0))
)
5 changes: 5 additions & 0 deletions tests/compiler/bool-Oz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"asc_flags": [
"-Oz"
]
}
6 changes: 6 additions & 0 deletions tests/compiler/bool-Oz.release.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(module
(memory $0 1)
(data (i32.const 12) ",")
(data (i32.const 24) "\01\00\00\00\14\00\00\00b\00o\00o\00l\00-\00O\00z\00.\00t\00s")
(export "memory" (memory $0))
)
61 changes: 61 additions & 0 deletions tests/compiler/bool-Oz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var f = <f32>2;
assert(<bool>f == true);
var f0 = <f32>+0.0;
assert(<bool>f0 == false);
var f1 = <f32>-0.0;
assert(<bool>f1 == false);
var f2 = <f32>+NaN;
assert(<bool>f2 == false);
var f3 = <f32>-NaN;
assert(<bool>f3 == false);
var f4 = +f32.MAX_VALUE;
assert(<bool>f4 == true);
var f5 = -f32.MAX_VALUE;
assert(<bool>f5 == true);
var f6 = <f32>+Infinity;
assert(<bool>f6 == true);
var f7 = <f32>-Infinity;
assert(<bool>f7 == true);
var f8 = +f32.MIN_VALUE;
assert(<bool>f8 == true);
var f9 = -f32.MIN_VALUE;
assert(<bool>f9 == true);
var f10 = reinterpret<f32>(1);
assert(<bool>f10 == true);
var f11 = reinterpret<f32>(0x7F800000 - 1);
assert(<bool>f11 == true);
var f12 = reinterpret<f32>(0x7F800000 + 1);
assert(<bool>f12 == false);
var f13 = reinterpret<f32>(0xFF800000 + 1);
assert(<bool>f13 == false);

var F = <f64>2;
assert(<bool>F == true);
var F0 = <f64>+0.0;
assert(<bool>F0 == false);
var F1 = <f64>-0.0;
assert(<bool>F1 == false);
var F2 = <f64>+NaN;
assert(<bool>F2 == false);
var F3 = <f64>-NaN;
assert(<bool>F3 == false);
var F4 = +f64.MAX_VALUE;
assert(<bool>F4 == true);
var F5 = -f64.MAX_VALUE;
assert(<bool>F5 == true);
var F6 = +Infinity;
assert(<bool>F6 == true);
var F7 = -Infinity;
assert(<bool>F7 == true);
var F8 = +f64.MIN_VALUE;
assert(<bool>F8 == true);
var F9 = -f64.MIN_VALUE;
assert(<bool>F9 == true);
var F10 = reinterpret<f64>(1);
assert(<bool>F10 == true);
var F11 = reinterpret<f64>(0x7FF0000000000000 - 1);
assert(<bool>F11 == true);
var F12 = reinterpret<f64>(0x7FF0000000000000 + 1);
assert(<bool>F12 == false);
var F13 = reinterpret<f64>(0xFFF0000000000000 + 1);
assert(<bool>F13 == false);