Skip to content

Improve Falseish / Trueish for floats #787

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

Closed
wants to merge 5 commits into from
Closed
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
52 changes: 48 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8874,10 +8874,32 @@ export class Compiler extends DiagnosticEmitter {
return module.unary(type.size == 64 ? UnaryOp.EqzI64 : UnaryOp.EqzI32, expr);
}
case TypeKind.F32: {
return module.binary(BinaryOp.EqF32, expr, module.f32(0));
// (x == 0.0) | (x != x)
let temp = this.currentFlow.getTempLocal(type);
let isEqzExpr = module.binary(BinaryOp.EqF32,
module.local_tee(temp.index, expr),
module.f32(0)
);
let isNaNExpr = module.binary(BinaryOp.NeF32,
module.local_get(temp.index, type.toNativeType()),
module.local_get(temp.index, type.toNativeType())
);
this.currentFlow.freeTempLocal(temp);
return module.binary(BinaryOp.OrI32, isEqzExpr, isNaNExpr);
}
case TypeKind.F64: {
return module.binary(BinaryOp.EqF64, expr, module.f64(0));
// (x == 0.0) | (x != x)
let temp = this.currentFlow.getTempLocal(type);
let isEqzExpr = module.binary(BinaryOp.EqF64,
module.local_tee(temp.index, expr),
module.f64(0)
);
let isNaNExpr = module.binary(BinaryOp.NeF64,
module.local_get(temp.index, type.toNativeType()),
module.local_get(temp.index, type.toNativeType())
);
this.currentFlow.freeTempLocal(temp);
return module.binary(BinaryOp.OrI32, isEqzExpr, isNaNExpr);
}
// case TypeKind.ANYREF: {
// TODO: ref.is_null
Expand Down Expand Up @@ -8916,10 +8938,32 @@ export class Compiler extends DiagnosticEmitter {
: expr;
}
case TypeKind.F32: {
return module.binary(BinaryOp.NeF32, expr, module.f32(0));
// (x != 0.0) & (x == x)
let temp = this.currentFlow.getTempLocal(type);
let isEqzExpr = module.binary(BinaryOp.NeF32,
module.local_tee(temp.index, expr),
module.f32(0)
);
let isNotNaNExpr = module.binary(BinaryOp.EqF32,
module.local_get(temp.index, type.toNativeType()),
module.local_get(temp.index, type.toNativeType())
);
this.currentFlow.freeTempLocal(temp);
return module.binary(BinaryOp.AndI32, isEqzExpr, isNotNaNExpr);
}
case TypeKind.F64: {
return module.binary(BinaryOp.NeF64, expr, module.f64(0));
// (x != 0.0) & (x == x)
let temp = this.currentFlow.getTempLocal(type);
let isEqzExpr = module.binary(BinaryOp.NeF64,
module.local_tee(temp.index, expr),
module.f64(0)
);
let isNotNaNExpr = module.binary(BinaryOp.EqF64,
module.local_get(temp.index, type.toNativeType()),
module.local_get(temp.index, type.toNativeType())
);
this.currentFlow.freeTempLocal(temp);
return module.binary(BinaryOp.AndI32, isEqzExpr, isNotNaNExpr);
}
// case TypeKind.ANYREF: {
// TODO: !ref.is_null
Expand Down
80 changes: 77 additions & 3 deletions tests/compiler/logical.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
(module
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$if (func (param f32) (result i32)))
(type $FUNCSIG$id (func (param f64) (result i32)))
(type $FUNCSIG$v (func))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 8) "\14\00\00\00\01\00\00\00\01\00\00\00\14\00\00\00l\00o\00g\00i\00c\00a\00l\00.\00t\00s")
(global $logical/i (mut i32) (i32.const 0))
Expand All @@ -8,7 +12,17 @@
(global $logical/F (mut f64) (f64.const 0))
(export "memory" (memory $0))
(start $start)
(func $start:logical (; 0 ;) (type $FUNCSIG$v)
(func $~lib/number/isNaN<f32> (; 1 ;) (type $FUNCSIG$if) (param $0 f32) (result i32)
local.get $0
local.get $0
f32.ne
)
(func $~lib/number/isNaN<f64> (; 2 ;) (type $FUNCSIG$id) (param $0 f64) (result i32)
local.get $0
local.get $0
f64.ne
)
(func $start:logical (; 3 ;) (type $FUNCSIG$v)
i32.const 2
global.set $logical/i
i32.const 1
Expand All @@ -25,11 +39,71 @@
global.set $logical/F
f64.const 1
global.set $logical/F
f32.const 1
global.set $logical/f
f32.const 1
global.set $logical/f
f64.const 1
global.set $logical/F
f64.const 1
global.set $logical/F
f32.const nan:0x400000
global.set $logical/f
f32.const nan:0x400000
call $~lib/number/isNaN<f32>
i32.eqz
if
i32.const 0
i32.const 24
i32.const 54
i32.const 0
call $~lib/builtins/abort
unreachable
end
f32.const nan:0x400000
global.set $logical/f
f32.const nan:0x400000
call $~lib/number/isNaN<f32>
i32.eqz
if
i32.const 0
i32.const 24
i32.const 57
i32.const 0
call $~lib/builtins/abort
unreachable
end
f64.const nan:0x8000000000000
global.set $logical/F
f64.const nan:0x8000000000000
call $~lib/number/isNaN<f64>
i32.eqz
if
i32.const 0
i32.const 24
i32.const 60
i32.const 0
call $~lib/builtins/abort
unreachable
end
f64.const nan:0x8000000000000
global.set $logical/F
f64.const nan:0x8000000000000
call $~lib/number/isNaN<f64>
i32.eqz
if
i32.const 0
i32.const 24
i32.const 63
i32.const 0
call $~lib/builtins/abort
unreachable
end
)
(func $start (; 1 ;) (type $FUNCSIG$v)
(func $start (; 4 ;) (type $FUNCSIG$v)
call $start:logical
)
(func $null (; 2 ;) (type $FUNCSIG$v)
(func $null (; 5 ;) (type $FUNCSIG$v)
nop
)
)
24 changes: 24 additions & 0 deletions tests/compiler/logical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,27 @@ assert(F == 2.0);

F = 0.0 || 1.0;
assert(F == 1.0);

f = NaN as f32 || 1.0 as f32;
assert(f == 1.0);

f = 1.0 as f32 || NaN as f32;
assert(f == 1.0);

F = NaN || 1.0;
assert(F == 1.0);

F = 1.0 || NaN;
assert(F == 1.0);

f = 1.0 as f32 && NaN as f32;
assert(isNaN(f));

f = NaN as f32 && 1.0 as f32;
assert(isNaN(f));

F = 1.0 && NaN;
assert(isNaN(F));

F = NaN && 1.0;
assert(isNaN(F));
Loading