From f2f4055608ac83507da60a7cd1b3575b9f487a36 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 13:58:19 +0300 Subject: [PATCH 01/11] fix --- src/compiler.ts | 24 +++++++++++++++++-- tests/compiler/bool.untouched.wat | 12 ++++++++++ .../portable-conversions.untouched.wat | 12 ++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 99de950cd2..014433fc44 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3682,7 +3682,17 @@ export class Compiler extends DiagnosticEmitter { // f32 to int if (fromType.kind == TypeKind.F32) { if (toType.isBooleanValue) { - expr = module.binary(BinaryOp.NeF32, expr, module.f32(0)); + // (x != 0.0) & (x == x) + let flow = this.currentFlow; + let temp = flow.getTempLocal(Type.f32); + expr = module.binary(BinaryOp.AndI32, + module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), + module.binary(BinaryOp.EqF32, + module.local_get(temp.index, NativeType.F32), + module.local_get(temp.index, NativeType.F32) + ) + ); + flow.freeTempLocal(temp); wrap = false; } else if (toType.isSignedIntegerValue) { if (toType.isLongIntegerValue) { @@ -3701,7 +3711,17 @@ export class Compiler extends DiagnosticEmitter { // f64 to int } else { if (toType.isBooleanValue) { - expr = module.binary(BinaryOp.NeF64, expr, module.f64(0)); + // (x != 0.0) & (x == x) + let flow = this.currentFlow; + let temp = flow.getTempLocal(Type.f64); + expr = module.binary(BinaryOp.AndI32, + module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), + module.binary(BinaryOp.EqF64, + module.local_get(temp.index, NativeType.F64), + module.local_get(temp.index, NativeType.F64) + ) + ); + flow.freeTempLocal(temp); wrap = false; } else if (toType.isSignedIntegerValue) { if (toType.isLongIntegerValue) { diff --git a/tests/compiler/bool.untouched.wat b/tests/compiler/bool.untouched.wat index 8889b9859d..34d828ff7a 100644 --- a/tests/compiler/bool.untouched.wat +++ b/tests/compiler/bool.untouched.wat @@ -15,6 +15,8 @@ (export "memory" (memory $0)) (start $~start) (func $start:bool + (local $0 f32) + (local $1 f64) global.get $bool/i i32.const 0 i32.ne @@ -72,8 +74,13 @@ unreachable end global.get $bool/f + local.tee $0 f32.const 0 f32.ne + local.get $0 + local.get $0 + f32.eq + i32.and i32.const 1 i32.eq i32.eqz @@ -86,8 +93,13 @@ unreachable end global.get $bool/F + local.tee $1 f64.const 0 f64.ne + local.get $1 + local.get $1 + f64.eq + i32.and i32.const 1 i32.eq i32.eqz diff --git a/tests/compiler/portable-conversions.untouched.wat b/tests/compiler/portable-conversions.untouched.wat index c719567b1c..d255359077 100644 --- a/tests/compiler/portable-conversions.untouched.wat +++ b/tests/compiler/portable-conversions.untouched.wat @@ -12,6 +12,8 @@ (export "memory" (memory $0)) (start $~start) (func $start:portable-conversions + (local $0 f32) + (local $1 f64) global.get $portable-conversions/i i32.const 24 i32.shl @@ -515,8 +517,13 @@ unreachable end global.get $portable-conversions/f + local.tee $0 f32.const 0 f32.ne + local.get $0 + local.get $0 + f32.eq + i32.and i32.eqz if i32.const 0 @@ -527,8 +534,13 @@ unreachable end global.get $portable-conversions/F + local.tee $1 f64.const 0 f64.ne + local.get $1 + local.get $1 + f64.eq + i32.and i32.eqz if i32.const 0 From d23ccf9e0a9b4dd92e639a9aa61db4a9a62515e3 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 19:43:15 +0300 Subject: [PATCH 02/11] extract routines to methods --- src/compiler.ts | 84 ++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 014433fc44..3dc0638963 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3682,17 +3682,7 @@ export class Compiler extends DiagnosticEmitter { // f32 to int if (fromType.kind == TypeKind.F32) { if (toType.isBooleanValue) { - // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f32); - expr = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), - module.binary(BinaryOp.EqF32, - module.local_get(temp.index, NativeType.F32), - module.local_get(temp.index, NativeType.F32) - ) - ); - flow.freeTempLocal(temp); + expr = this.convertFloat32ToBoolExpression(expr); wrap = false; } else if (toType.isSignedIntegerValue) { if (toType.isLongIntegerValue) { @@ -3711,17 +3701,7 @@ export class Compiler extends DiagnosticEmitter { // f64 to int } else { if (toType.isBooleanValue) { - // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f64); - expr = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), - module.binary(BinaryOp.EqF64, - module.local_get(temp.index, NativeType.F64), - module.local_get(temp.index, NativeType.F64) - ) - ); - flow.freeTempLocal(temp); + expr = this.convertFloat64ToBoolExpression(expr); wrap = false; } else if (toType.isSignedIntegerValue) { if (toType.isLongIntegerValue) { @@ -3832,6 +3812,40 @@ export class Compiler extends DiagnosticEmitter { : expr; } + private convertFloat32ToBoolExpression(expr: ExpressionRef): ExpressionRef { + let module = this.module; + + // (x != 0.0) & (x == x) + let flow = this.currentFlow; + let temp = flow.getTempLocal(Type.f32); + expr = module.binary(BinaryOp.AndI32, + module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), + module.binary(BinaryOp.EqF32, + module.local_get(temp.index, NativeType.F32), + module.local_get(temp.index, NativeType.F32) + ) + ); + flow.freeTempLocal(temp); + return expr; + } + + private convertFloat64ToBoolExpression(expr: ExpressionRef): ExpressionRef { + let module = this.module; + + // (x != 0.0) & (x == x) + let flow = this.currentFlow; + let temp = flow.getTempLocal(Type.f64); + expr = module.binary(BinaryOp.AndI32, + module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), + module.binary(BinaryOp.EqF64, + module.local_get(temp.index, NativeType.F64), + module.local_get(temp.index, NativeType.F64) + ) + ); + flow.freeTempLocal(temp); + return expr; + } + private compileAssertionExpression( expression: AssertionExpression, contextualType: Type, @@ -10810,32 +10824,10 @@ export class Compiler extends DiagnosticEmitter { : expr; } case TypeKind.F32: { - // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f32); - let ret = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), - module.binary(BinaryOp.EqF32, - module.local_get(temp.index, NativeType.F32), - module.local_get(temp.index, NativeType.F32) - ) - ); - flow.freeTempLocal(temp); - return ret; + return this.convertFloat32ToBoolExpression(expr); } case TypeKind.F64: { - // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f64); - let ret = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), - module.binary(BinaryOp.EqF64, - module.local_get(temp.index, NativeType.F64), - module.local_get(temp.index, NativeType.F64) - ) - ); - flow.freeTempLocal(temp); - return ret; + return this.convertFloat64ToBoolExpression(expr); } case TypeKind.EXTERNREF: { // TODO: non-null object might still be considered falseish From 06c0a5e2cd8f46b55f234455fb2c2f885843f9a7 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 19:45:25 +0300 Subject: [PATCH 03/11] let -> var --- src/compiler.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 3dc0638963..58e989bedc 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3813,11 +3813,11 @@ export class Compiler extends DiagnosticEmitter { } private convertFloat32ToBoolExpression(expr: ExpressionRef): ExpressionRef { - let module = this.module; + var module = this.module; // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f32); + var flow = this.currentFlow; + var temp = flow.getTempLocal(Type.f32); expr = module.binary(BinaryOp.AndI32, module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), module.binary(BinaryOp.EqF32, @@ -3830,11 +3830,11 @@ export class Compiler extends DiagnosticEmitter { } private convertFloat64ToBoolExpression(expr: ExpressionRef): ExpressionRef { - let module = this.module; + var module = this.module; // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f64); + var flow = this.currentFlow; + var temp = flow.getTempLocal(Type.f64); expr = module.binary(BinaryOp.AndI32, module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), module.binary(BinaryOp.EqF64, From fb0ec7c5ebf7ee0613c0acfc4faa88d73a5d520b Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 20:14:59 +0300 Subject: [PATCH 04/11] gen faster version for shrinklevel == 0 --- src/compiler.ts | 74 ++++-- tests/compiler/bool.untouched.wat | 28 +-- tests/compiler/logical.untouched.wat | 232 +++++++++--------- .../portable-conversions.untouched.wat | 28 +-- tests/compiler/std/string.untouched.wat | 14 +- tests/compiler/unary.untouched.wat | 134 +++++----- 6 files changed, 268 insertions(+), 242 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 58e989bedc..1f791435ed 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3815,34 +3815,64 @@ export class Compiler extends DiagnosticEmitter { private convertFloat32ToBoolExpression(expr: ExpressionRef): ExpressionRef { var module = this.module; - // (x != 0.0) & (x == x) - var flow = this.currentFlow; - var temp = flow.getTempLocal(Type.f32); - expr = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), - module.binary(BinaryOp.EqF32, - module.local_get(temp.index, NativeType.F32), - module.local_get(temp.index, NativeType.F32) - ) - ); - flow.freeTempLocal(temp); + if (this.options.shrinkLevelHint == 0) { + // bitCast(1) <= abs(bitCast(x)) <= bitCast(Infinity) or + // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 + expr = module.binary(BinaryOp.LeU32, + module.binary(BinaryOp.SubI32, + module.binary(BinaryOp.AndI32, + module.unary(UnaryOp.ReinterpretF32, expr), + module.i32(0x7FFFFFFF) + ), + module.i32(1) + ), + module.i32(0x7F7FFFFF) + ); + } else { + // (x != 0.0) & (x == x) + let flow = this.currentFlow; + let temp = flow.getTempLocal(Type.f32); + expr = module.binary(BinaryOp.AndI32, + module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), + module.binary(BinaryOp.EqF32, + module.local_get(temp.index, NativeType.F32), + module.local_get(temp.index, NativeType.F32) + ) + ); + flow.freeTempLocal(temp); + } return expr; } private convertFloat64ToBoolExpression(expr: ExpressionRef): ExpressionRef { var module = this.module; - // (x != 0.0) & (x == x) - var flow = this.currentFlow; - var temp = flow.getTempLocal(Type.f64); - expr = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), - module.binary(BinaryOp.EqF64, - module.local_get(temp.index, NativeType.F64), - module.local_get(temp.index, NativeType.F64) - ) - ); - flow.freeTempLocal(temp); + if (this.options.shrinkLevelHint == 0) { + // bitCast(1) <= abs(bitCast(x)) <= bitCast(Infinity) or + // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 + expr = module.binary(BinaryOp.LeU64, + module.binary(BinaryOp.SubI64, + module.binary(BinaryOp.AndI64, + module.unary(UnaryOp.ReinterpretF64, expr), + module.i64(0xFFFFFFFF, 0x7FFFFFFF) // 0x7FFFFFFFFFFFFFFF + ), + module.i64(1) + ), + module.i64(0xFFFFFFFF, 0x7FEFFFFF) // 0x7FEFFFFFFFFFFFFF + ); + } else { + // (x != 0.0) & (x == x) + let flow = this.currentFlow; + let temp = flow.getTempLocal(Type.f64); + expr = module.binary(BinaryOp.AndI32, + module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), + module.binary(BinaryOp.EqF64, + module.local_get(temp.index, NativeType.F64), + module.local_get(temp.index, NativeType.F64) + ) + ); + flow.freeTempLocal(temp); + } return expr; } diff --git a/tests/compiler/bool.untouched.wat b/tests/compiler/bool.untouched.wat index 34d828ff7a..1df4dd91c8 100644 --- a/tests/compiler/bool.untouched.wat +++ b/tests/compiler/bool.untouched.wat @@ -15,8 +15,6 @@ (export "memory" (memory $0)) (start $~start) (func $start:bool - (local $0 f32) - (local $1 f64) global.get $bool/i i32.const 0 i32.ne @@ -74,14 +72,14 @@ unreachable end global.get $bool/f - local.tee $0 - f32.const 0 - f32.ne - local.get $0 - local.get $0 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u + i32.const 1 i32.eq i32.eqz if @@ -93,13 +91,13 @@ unreachable end global.get $bool/F - local.tee $1 - f64.const 0 - f64.ne - local.get $1 - local.get $1 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.const 1 i32.eq i32.eqz diff --git a/tests/compiler/logical.untouched.wat b/tests/compiler/logical.untouched.wat index c2dcfa73ee..5631701e41 100644 --- a/tests/compiler/logical.untouched.wat +++ b/tests/compiler/logical.untouched.wat @@ -1600,8 +1600,8 @@ local.get $2 ) (func $start:logical - (local $0 f64) - (local $1 f32) + (local $0 f32) + (local $1 f64) (local $2 i32) (local $3 i32) i32.const 0 @@ -1612,13 +1612,13 @@ end drop f64.const 0 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result i32) unreachable else @@ -1633,13 +1633,13 @@ end drop f64.const 1 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result i32) i32.const 1 else @@ -1659,25 +1659,25 @@ end drop f64.const 1 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const 2 else f64.const 1 end - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result i32) i32.const 1 else @@ -1765,13 +1765,13 @@ unreachable end f32.const 1 - local.tee $1 - f32.const 0 - f32.ne - local.get $1 - local.get $1 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u if (result f32) f32.const 2 else @@ -1791,13 +1791,13 @@ unreachable end f32.const 0 - local.tee $1 - f32.const 0 - f32.ne - local.get $1 - local.get $1 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u if (result f32) f32.const 0 else @@ -1817,13 +1817,13 @@ unreachable end f64.const 1 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const 2 else @@ -1843,13 +1843,13 @@ unreachable end f64.const 0 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const 0 else @@ -1869,13 +1869,13 @@ unreachable end f32.const nan:0x400000 - local.tee $1 - f32.const 0 - f32.ne - local.get $1 - local.get $1 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u if (result f32) f32.const nan:0x400000 else @@ -1895,13 +1895,13 @@ unreachable end f32.const 1 - local.tee $1 - f32.const 0 - f32.ne - local.get $1 - local.get $1 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u if (result f32) f32.const 1 else @@ -1921,13 +1921,13 @@ unreachable end f64.const nan:0x8000000000000 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const nan:0x8000000000000 else @@ -1947,13 +1947,13 @@ unreachable end f64.const 1 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const 1 else @@ -1973,13 +1973,13 @@ unreachable end f32.const 1 - local.tee $1 - f32.const 0 - f32.ne - local.get $1 - local.get $1 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u if (result f32) f32.const nan:0x400000 else @@ -1987,8 +1987,8 @@ end global.set $logical/f global.get $logical/f - local.tee $1 - local.get $1 + local.tee $0 + local.get $0 f32.ne i32.eqz if @@ -2000,13 +2000,13 @@ unreachable end f32.const nan:0x400000 - local.tee $1 - f32.const 0 - f32.ne - local.get $1 - local.get $1 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u if (result f32) f32.const 1 else @@ -2014,8 +2014,8 @@ end global.set $logical/f global.get $logical/f - local.tee $1 - local.get $1 + local.tee $0 + local.get $0 f32.ne i32.eqz if @@ -2027,13 +2027,13 @@ unreachable end f64.const 1 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const nan:0x8000000000000 else @@ -2041,8 +2041,8 @@ end global.set $logical/F global.get $logical/F - local.tee $0 - local.get $0 + local.tee $1 + local.get $1 f64.ne i32.eqz if @@ -2054,13 +2054,13 @@ unreachable end f64.const nan:0x8000000000000 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u if (result f64) f64.const 1 else @@ -2068,8 +2068,8 @@ end global.set $logical/F global.get $logical/F - local.tee $0 - local.get $0 + local.tee $1 + local.get $1 f64.ne i32.eqz if diff --git a/tests/compiler/portable-conversions.untouched.wat b/tests/compiler/portable-conversions.untouched.wat index d255359077..ab4809149c 100644 --- a/tests/compiler/portable-conversions.untouched.wat +++ b/tests/compiler/portable-conversions.untouched.wat @@ -12,8 +12,6 @@ (export "memory" (memory $0)) (start $~start) (func $start:portable-conversions - (local $0 f32) - (local $1 f64) global.get $portable-conversions/i i32.const 24 i32.shl @@ -517,13 +515,13 @@ unreachable end global.get $portable-conversions/f - local.tee $0 - f32.const 0 - f32.ne - local.get $0 - local.get $0 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u i32.eqz if i32.const 0 @@ -534,13 +532,13 @@ unreachable end global.get $portable-conversions/F - local.tee $1 - f64.const 0 - f64.ne - local.get $1 - local.get $1 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz if i32.const 0 diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index f5b19d53cf..13f854d3bc 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -4828,13 +4828,13 @@ i32.ge_u if local.get $8 - local.tee $3 - f64.const 0 - f64.ne - local.get $3 - local.get $3 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz if i32.const 1 diff --git a/tests/compiler/unary.untouched.wat b/tests/compiler/unary.untouched.wat index 3a8879bbb3..19785b0de6 100644 --- a/tests/compiler/unary.untouched.wat +++ b/tests/compiler/unary.untouched.wat @@ -9,10 +9,10 @@ (export "memory" (memory $0)) (start $~start) (func $start:unary - (local $0 f64) - (local $1 i32) - (local $2 i64) - (local $3 f32) + (local $0 i32) + (local $1 i64) + (local $2 f32) + (local $3 f64) i32.const 1 drop i32.const -1 @@ -29,13 +29,13 @@ f64.const -1.25 drop f64.const 1.25 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz drop global.get $unary/i @@ -104,18 +104,18 @@ global.get $unary/i global.set $unary/i global.get $unary/i - local.tee $1 + local.tee $0 i32.const 1 i32.add global.set $unary/i - local.get $1 + local.get $0 global.set $unary/i global.get $unary/i - local.tee $1 + local.tee $0 i32.const 1 i32.sub global.set $unary/i - local.get $1 + local.get $0 global.set $unary/i global.get $unary/I drop @@ -191,18 +191,18 @@ global.get $unary/I global.set $unary/I global.get $unary/I - local.tee $2 + local.tee $1 i64.const 1 i64.add global.set $unary/I - local.get $2 + local.get $1 global.set $unary/I global.get $unary/I - local.tee $2 + local.tee $1 i64.const 1 i64.sub global.set $unary/I - local.get $2 + local.get $1 global.set $unary/I global.get $unary/f drop @@ -210,13 +210,13 @@ f32.neg drop global.get $unary/f - local.tee $3 - f32.const 0 - f32.ne - local.get $3 - local.get $3 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u i32.eqz drop global.get $unary/f @@ -240,13 +240,13 @@ f32.const -1.25 global.set $unary/f f64.const 1.25 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz global.set $unary/i global.get $unary/f @@ -255,13 +255,13 @@ f32.neg global.set $unary/f global.get $unary/f - local.tee $3 - f32.const 0 - f32.ne - local.get $3 - local.get $3 - f32.eq + i32.reinterpret_f32 + i32.const 2147483647 i32.and + i32.const 1 + i32.sub + i32.const 2139095039 + i32.le_u i32.eqz global.set $unary/i global.get $unary/f @@ -277,18 +277,18 @@ global.get $unary/f global.set $unary/f global.get $unary/f - local.tee $3 + local.tee $2 f32.const 1 f32.add global.set $unary/f - local.get $3 + local.get $2 global.set $unary/f global.get $unary/f - local.tee $3 + local.tee $2 f32.const 1 f32.sub global.set $unary/f - local.get $3 + local.get $2 global.set $unary/f global.get $unary/F drop @@ -296,13 +296,13 @@ f64.neg drop global.get $unary/F - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz drop global.get $unary/F @@ -326,13 +326,13 @@ f64.const -1.25 global.set $unary/F f64.const 1.25 - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz i64.extend_i32_u global.set $unary/I @@ -342,13 +342,13 @@ f64.neg global.set $unary/F global.get $unary/F - local.tee $0 - f64.const 0 - f64.ne - local.get $0 - local.get $0 - f64.eq - i32.and + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.le_u i32.eqz i64.extend_i32_u global.set $unary/I @@ -365,18 +365,18 @@ global.get $unary/F global.set $unary/F global.get $unary/F - local.tee $0 + local.tee $3 f64.const 1 f64.add global.set $unary/F - local.get $0 + local.get $3 global.set $unary/F global.get $unary/F - local.tee $0 + local.tee $3 f64.const 1 f64.sub global.set $unary/F - local.get $0 + local.get $3 global.set $unary/F ) (func $~start From 59ab30a246d0df755bac918e5edf8c9ce7708056 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 20:34:15 +0300 Subject: [PATCH 05/11] tune compiler's condision --- src/compiler.ts | 10 ++++++++-- tests/compiler/std/string.optimized.wat | 26 ++++++++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 1f791435ed..997819b768 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3814,8 +3814,11 @@ export class Compiler extends DiagnosticEmitter { private convertFloat32ToBoolExpression(expr: ExpressionRef): ExpressionRef { var module = this.module; + var options = this.options; - if (this.options.shrinkLevelHint == 0) { + if (!options.willOptimize || ( + options.optimizeLevelHint >= 2 && options.shrinkLevelHint <= 1 + )) { // bitCast(1) <= abs(bitCast(x)) <= bitCast(Infinity) or // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 expr = module.binary(BinaryOp.LeU32, @@ -3846,8 +3849,11 @@ export class Compiler extends DiagnosticEmitter { private convertFloat64ToBoolExpression(expr: ExpressionRef): ExpressionRef { var module = this.module; + var options = this.options; - if (this.options.shrinkLevelHint == 0) { + if (!options.willOptimize || ( + options.optimizeLevelHint >= 2 && options.shrinkLevelHint <= 1 + )) { // bitCast(1) <= abs(bitCast(x)) <= bitCast(Infinity) or // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 expr = module.binary(BinaryOp.LeU64, diff --git a/tests/compiler/std/string.optimized.wat b/tests/compiler/std/string.optimized.wat index 16f5f05b5c..cea714090f 100644 --- a/tests/compiler/std/string.optimized.wat +++ b/tests/compiler/std/string.optimized.wat @@ -2653,7 +2653,7 @@ end end f64.const 1 - local.set $5 + local.set $6 i32.const 1 local.get $2 i32.const 43 @@ -2675,7 +2675,7 @@ i32.const 45 i32.eq select - local.set $5 + local.set $6 local.get $4 i32.const 2 i32.add @@ -2845,25 +2845,25 @@ local.get $1 i32.ge_u if - local.get $6 - local.get $6 - f64.eq - local.get $6 - f64.const 0 - f64.ne - i32.and - i32.eqz + local.get $5 + i64.reinterpret_f64 + i64.const 9223372036854775807 + i64.and + i64.const 1 + i64.sub + i64.const 9218868437227405311 + i64.gt_u br_if $folding-inner0 br $while-break|2 end - local.get $6 + local.get $5 local.get $1 f64.convert_i32_s f64.mul local.get $2 f64.convert_i32_u f64.add - local.set $6 + local.set $5 local.get $4 i32.const 2 i32.add @@ -2874,8 +2874,8 @@ end local.get $3 call $~lib/rt/pure/__release - local.get $5 local.get $6 + local.get $5 f64.mul return end From 96f55f0f69d1fec3a6970c82a63f7b8541106474 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 20:40:06 +0300 Subject: [PATCH 06/11] fix comments --- src/compiler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 997819b768..f05dd0530d 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3819,7 +3819,7 @@ export class Compiler extends DiagnosticEmitter { if (!options.willOptimize || ( options.optimizeLevelHint >= 2 && options.shrinkLevelHint <= 1 )) { - // bitCast(1) <= abs(bitCast(x)) <= bitCast(Infinity) or + // 0 < abs(bitCast(x)) <= bitCast(Infinity) or // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 expr = module.binary(BinaryOp.LeU32, module.binary(BinaryOp.SubI32, @@ -3854,7 +3854,7 @@ export class Compiler extends DiagnosticEmitter { if (!options.willOptimize || ( options.optimizeLevelHint >= 2 && options.shrinkLevelHint <= 1 )) { - // bitCast(1) <= abs(bitCast(x)) <= bitCast(Infinity) or + // 0 < abs(bitCast(x)) <= bitCast(Infinity) or // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 expr = module.binary(BinaryOp.LeU64, module.binary(BinaryOp.SubI64, From 8441c7ee2020b6fb14d4d69ec817d231945226d2 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 17 Aug 2020 23:11:58 +0300 Subject: [PATCH 07/11] signifinally reduce size of fast implementation and second impl --- src/compiler.ts | 82 +++++----------- tests/compiler/bool.untouched.wat | 12 +-- tests/compiler/logical.untouched.wat | 96 +++++++++---------- .../portable-conversions.untouched.wat | 12 +-- tests/compiler/std/string.optimized.wat | 6 +- tests/compiler/std/string.untouched.wat | 6 +- tests/compiler/unary.untouched.wat | 42 ++++---- 7 files changed, 113 insertions(+), 143 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index f05dd0530d..79ab004132 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3814,71 +3814,41 @@ export class Compiler extends DiagnosticEmitter { private convertFloat32ToBoolExpression(expr: ExpressionRef): ExpressionRef { var module = this.module; - var options = this.options; - - if (!options.willOptimize || ( - options.optimizeLevelHint >= 2 && options.shrinkLevelHint <= 1 - )) { - // 0 < abs(bitCast(x)) <= bitCast(Infinity) or - // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 - expr = module.binary(BinaryOp.LeU32, - module.binary(BinaryOp.SubI32, - module.binary(BinaryOp.AndI32, - module.unary(UnaryOp.ReinterpretF32, expr), - module.i32(0x7FFFFFFF) - ), + // 0 < abs(bitCast(x)) <= bitCast(Infinity) or + // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 + // + // and finally: + // (reinterpret(x) << 1) - (1 << 1) <= ((0x7F800000 - 1) << 1) + expr = module.binary(BinaryOp.LeU32, + module.binary(BinaryOp.SubI32, + module.binary(BinaryOp.ShlI32, + module.unary(UnaryOp.ReinterpretF32, expr), module.i32(1) ), - module.i32(0x7F7FFFFF) - ); - } else { - // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f32); - expr = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF32, module.local_tee(temp.index, expr), module.f32(0)), - module.binary(BinaryOp.EqF32, - module.local_get(temp.index, NativeType.F32), - module.local_get(temp.index, NativeType.F32) - ) - ); - flow.freeTempLocal(temp); - } + module.i32(2) // 1 << 1 + ), + module.i32(0xFEFFFFFE) // (0x7F800000 - 1) << 1 + ); return expr; } private convertFloat64ToBoolExpression(expr: ExpressionRef): ExpressionRef { var module = this.module; - var options = this.options; - - if (!options.willOptimize || ( - options.optimizeLevelHint >= 2 && options.shrinkLevelHint <= 1 - )) { - // 0 < abs(bitCast(x)) <= bitCast(Infinity) or - // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 - expr = module.binary(BinaryOp.LeU64, - module.binary(BinaryOp.SubI64, - module.binary(BinaryOp.AndI64, - module.unary(UnaryOp.ReinterpretF64, expr), - module.i64(0xFFFFFFFF, 0x7FFFFFFF) // 0x7FFFFFFFFFFFFFFF - ), + // 0 < abs(bitCast(x)) <= bitCast(Infinity) or + // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 + // + // and finally: + // (reinterpret(x) << 1) - (1 << 1) <= ((0x7FF0000000000000 - 1) << 1) + expr = module.binary(BinaryOp.LeU64, + module.binary(BinaryOp.SubI64, + module.binary(BinaryOp.ShlI64, + module.unary(UnaryOp.ReinterpretF64, expr), module.i64(1) ), - module.i64(0xFFFFFFFF, 0x7FEFFFFF) // 0x7FEFFFFFFFFFFFFF - ); - } else { - // (x != 0.0) & (x == x) - let flow = this.currentFlow; - let temp = flow.getTempLocal(Type.f64); - expr = module.binary(BinaryOp.AndI32, - module.binary(BinaryOp.NeF64, module.local_tee(temp.index, expr), module.f64(0)), - module.binary(BinaryOp.EqF64, - module.local_get(temp.index, NativeType.F64), - module.local_get(temp.index, NativeType.F64) - ) - ); - flow.freeTempLocal(temp); - } + module.i64(2) // 1 << 1 + ), + module.i64(0xFFFFFFFE, 0xFFDFFFFF) // (0x7FF0000000000000 - 1) << 1 + ); return expr; } diff --git a/tests/compiler/bool.untouched.wat b/tests/compiler/bool.untouched.wat index 1df4dd91c8..e0270c5274 100644 --- a/tests/compiler/bool.untouched.wat +++ b/tests/compiler/bool.untouched.wat @@ -73,11 +73,11 @@ end global.get $bool/f i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u i32.const 1 i32.eq @@ -92,11 +92,11 @@ end global.get $bool/F i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.const 1 i32.eq diff --git a/tests/compiler/logical.untouched.wat b/tests/compiler/logical.untouched.wat index 5631701e41..5ce7daa94c 100644 --- a/tests/compiler/logical.untouched.wat +++ b/tests/compiler/logical.untouched.wat @@ -1613,11 +1613,11 @@ drop f64.const 0 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result i32) unreachable @@ -1634,11 +1634,11 @@ drop f64.const 1 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result i32) i32.const 1 @@ -1660,11 +1660,11 @@ drop f64.const 1 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const 2 @@ -1672,11 +1672,11 @@ f64.const 1 end i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result i32) i32.const 1 @@ -1766,11 +1766,11 @@ end f32.const 1 i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u if (result f32) f32.const 2 @@ -1792,11 +1792,11 @@ end f32.const 0 i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u if (result f32) f32.const 0 @@ -1818,11 +1818,11 @@ end f64.const 1 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const 2 @@ -1844,11 +1844,11 @@ end f64.const 0 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const 0 @@ -1870,11 +1870,11 @@ end f32.const nan:0x400000 i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u if (result f32) f32.const nan:0x400000 @@ -1896,11 +1896,11 @@ end f32.const 1 i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u if (result f32) f32.const 1 @@ -1922,11 +1922,11 @@ end f64.const nan:0x8000000000000 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const nan:0x8000000000000 @@ -1948,11 +1948,11 @@ end f64.const 1 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const 1 @@ -1974,11 +1974,11 @@ end f32.const 1 i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u if (result f32) f32.const nan:0x400000 @@ -2001,11 +2001,11 @@ end f32.const nan:0x400000 i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u if (result f32) f32.const 1 @@ -2028,11 +2028,11 @@ end f64.const 1 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const nan:0x8000000000000 @@ -2055,11 +2055,11 @@ end f64.const nan:0x8000000000000 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u if (result f64) f64.const 1 diff --git a/tests/compiler/portable-conversions.untouched.wat b/tests/compiler/portable-conversions.untouched.wat index ab4809149c..6d00acc97a 100644 --- a/tests/compiler/portable-conversions.untouched.wat +++ b/tests/compiler/portable-conversions.untouched.wat @@ -516,11 +516,11 @@ end global.get $portable-conversions/f i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u i32.eqz if @@ -533,11 +533,11 @@ end global.get $portable-conversions/F i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz if diff --git a/tests/compiler/std/string.optimized.wat b/tests/compiler/std/string.optimized.wat index cea714090f..c156b0630c 100644 --- a/tests/compiler/std/string.optimized.wat +++ b/tests/compiler/std/string.optimized.wat @@ -2847,11 +2847,11 @@ if local.get $5 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.gt_u br_if $folding-inner0 br $while-break|2 diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index 13f854d3bc..2859ec44de 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -4829,11 +4829,11 @@ if local.get $8 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz if diff --git a/tests/compiler/unary.untouched.wat b/tests/compiler/unary.untouched.wat index 19785b0de6..30b1e390ab 100644 --- a/tests/compiler/unary.untouched.wat +++ b/tests/compiler/unary.untouched.wat @@ -30,11 +30,11 @@ drop f64.const 1.25 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz drop @@ -211,11 +211,11 @@ drop global.get $unary/f i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u i32.eqz drop @@ -241,11 +241,11 @@ global.set $unary/f f64.const 1.25 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz global.set $unary/i @@ -256,11 +256,11 @@ global.set $unary/f global.get $unary/f i32.reinterpret_f32 - i32.const 2147483647 - i32.and i32.const 1 + i32.shl + i32.const 2 i32.sub - i32.const 2139095039 + i32.const -16777218 i32.le_u i32.eqz global.set $unary/i @@ -297,11 +297,11 @@ drop global.get $unary/F i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz drop @@ -327,11 +327,11 @@ global.set $unary/F f64.const 1.25 i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz i64.extend_i32_u @@ -343,11 +343,11 @@ global.set $unary/F global.get $unary/F i64.reinterpret_f64 - i64.const 9223372036854775807 - i64.and i64.const 1 + i64.shl + i64.const 2 i64.sub - i64.const 9218868437227405311 + i64.const -9007199254740994 i64.le_u i32.eqz i64.extend_i32_u From 78aa27ce533301d7163ebc36cb37e4d36f9dc31d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 14:05:00 +0300 Subject: [PATCH 08/11] refactor according review --- src/compiler.ts | 76 +++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 79ab004132..1a112611c1 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3682,7 +3682,7 @@ export class Compiler extends DiagnosticEmitter { // f32 to int if (fromType.kind == TypeKind.F32) { if (toType.isBooleanValue) { - expr = this.convertFloat32ToBoolExpression(expr); + expr = this.makeIsTrueish(expr, Type.f32, reportNode); wrap = false; } else if (toType.isSignedIntegerValue) { if (toType.isLongIntegerValue) { @@ -3701,7 +3701,7 @@ export class Compiler extends DiagnosticEmitter { // f64 to int } else { if (toType.isBooleanValue) { - expr = this.convertFloat64ToBoolExpression(expr); + expr = this.makeIsTrueish(expr, Type.f64, reportNode); wrap = false; } else if (toType.isSignedIntegerValue) { if (toType.isLongIntegerValue) { @@ -3812,46 +3812,6 @@ export class Compiler extends DiagnosticEmitter { : expr; } - private convertFloat32ToBoolExpression(expr: ExpressionRef): ExpressionRef { - var module = this.module; - // 0 < abs(bitCast(x)) <= bitCast(Infinity) or - // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 - // - // and finally: - // (reinterpret(x) << 1) - (1 << 1) <= ((0x7F800000 - 1) << 1) - expr = module.binary(BinaryOp.LeU32, - module.binary(BinaryOp.SubI32, - module.binary(BinaryOp.ShlI32, - module.unary(UnaryOp.ReinterpretF32, expr), - module.i32(1) - ), - module.i32(2) // 1 << 1 - ), - module.i32(0xFEFFFFFE) // (0x7F800000 - 1) << 1 - ); - return expr; - } - - private convertFloat64ToBoolExpression(expr: ExpressionRef): ExpressionRef { - var module = this.module; - // 0 < abs(bitCast(x)) <= bitCast(Infinity) or - // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 - // - // and finally: - // (reinterpret(x) << 1) - (1 << 1) <= ((0x7FF0000000000000 - 1) << 1) - expr = module.binary(BinaryOp.LeU64, - module.binary(BinaryOp.SubI64, - module.binary(BinaryOp.ShlI64, - module.unary(UnaryOp.ReinterpretF64, expr), - module.i64(1) - ), - module.i64(2) // 1 << 1 - ), - module.i64(0xFFFFFFFE, 0xFFDFFFFF) // (0x7FF0000000000000 - 1) << 1 - ); - return expr; - } - private compileAssertionExpression( expression: AssertionExpression, contextualType: Type, @@ -10830,10 +10790,38 @@ export class Compiler extends DiagnosticEmitter { : expr; } case TypeKind.F32: { - return this.convertFloat32ToBoolExpression(expr); + // 0 < abs(bitCast(x)) <= bitCast(Infinity) or + // (reinterpret(x) & 0x7FFFFFFF) - 1 <= 0x7F800000 - 1 + // + // and finally: + // (reinterpret(x) << 1) - (1 << 1) <= ((0x7F800000 - 1) << 1) + return module.binary(BinaryOp.LeU32, + module.binary(BinaryOp.SubI32, + module.binary(BinaryOp.ShlI32, + module.unary(UnaryOp.ReinterpretF32, expr), + module.i32(1) + ), + module.i32(2) // 1 << 1 + ), + module.i32(0xFEFFFFFE) // (0x7F800000 - 1) << 1 + ); } case TypeKind.F64: { - return this.convertFloat64ToBoolExpression(expr); + // 0 < abs(bitCast(x)) <= bitCast(Infinity) or + // (reinterpret(x) & 0x7FFFFFFFFFFFFFFF) - 1 <= 0x7FF0000000000000 - 1 + // + // and finally: + // (reinterpret(x) << 1) - (1 << 1) <= ((0x7FF0000000000000 - 1) << 1) + return module.binary(BinaryOp.LeU64, + module.binary(BinaryOp.SubI64, + module.binary(BinaryOp.ShlI64, + module.unary(UnaryOp.ReinterpretF64, expr), + module.i64(1) + ), + module.i64(2) // 1 << 1 + ), + module.i64(0xFFFFFFFE, 0xFFDFFFFF) // (0x7FF0000000000000 - 1) << 1 + ); } case TypeKind.EXTERNREF: { // TODO: non-null object might still be considered falseish From cc39f399fed7555df81cfa33c90c8c55b7bdb5a9 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 15:49:54 +0300 Subject: [PATCH 09/11] add tests --- tests/compiler/bool.ts | 47 ++++ tests/compiler/bool.untouched.wat | 442 +++++++++++++++++++++++++++++- 2 files changed, 486 insertions(+), 3 deletions(-) diff --git a/tests/compiler/bool.ts b/tests/compiler/bool.ts index e1052f6a5c..0f825b533e 100644 --- a/tests/compiler/bool.ts +++ b/tests/compiler/bool.ts @@ -6,9 +6,56 @@ var u = 2; assert(u == true); var U = 2; assert(U == true); + var f = 2; assert(f == true); +var f0 = +0.0; +assert(f0 == false); +var f1 = -0.0; +assert(f1 == false); +var f2 = +NaN; +assert(f2 == false); +var f3 = -NaN; +assert(f3 == false); +var f4 = +f32.MAX_VALUE; +assert(f4 == true); +var f5 = -f32.MAX_VALUE; +assert(f5 == true); +var f6 = +Infinity; +assert(f6 == true); +var f7 = -Infinity; +assert(f7 == true); +// @ts-ignore +var f8 = +f32.MIN_NORMAL_VALUE; +assert(f8 == true); +// @ts-ignore +var f9 = -f32.MIN_NORMAL_VALUE; +assert(f9 == true); + var F = 2; assert(F == true); +var F0 = +0.0; +assert(F0 == false); +var F1 = -0.0; +assert(F1 == false); +var F2 = +NaN; +assert(F2 == false); +var F3 = -NaN; +assert(F3 == false); +var F4 = +f64.MAX_VALUE; +assert(F4 == true); +var F5 = -f64.MAX_VALUE; +assert(F5 == true); +var F6 = +Infinity; +assert(F6 == true); +var F7 = -Infinity; +assert(F7 == true); +// @ts-ignore +var F8 = +f64.MIN_NORMAL_VALUE; +assert(F8 == true); +// @ts-ignore +var F9 = -f64.MIN_NORMAL_VALUE; +assert(F9 == true); + var uu = 2; assert(uu == true); diff --git a/tests/compiler/bool.untouched.wat b/tests/compiler/bool.untouched.wat index e0270c5274..9fead1704f 100644 --- a/tests/compiler/bool.untouched.wat +++ b/tests/compiler/bool.untouched.wat @@ -10,7 +10,31 @@ (global $bool/u (mut i32) (i32.const 2)) (global $bool/U (mut i64) (i64.const 2)) (global $bool/f (mut f32) (f32.const 2)) + (global $bool/f0 (mut f32) (f32.const 0)) + (global $bool/f1 (mut f32) (f32.const -0)) + (global $bool/f2 (mut f32) (f32.const nan:0x400000)) + (global $bool/f3 (mut f32) (f32.const 0)) + (global $~lib/builtins/f32.MAX_VALUE f32 (f32.const 3402823466385288598117041e14)) + (global $bool/f4 (mut f32) (f32.const 0)) + (global $bool/f5 (mut f32) (f32.const 0)) + (global $bool/f6 (mut f32) (f32.const inf)) + (global $bool/f7 (mut f32) (f32.const 0)) + (global $~lib/builtins/f32.MIN_NORMAL_VALUE f32 (f32.const 1.1754943508222875e-38)) + (global $bool/f8 (mut f32) (f32.const 0)) + (global $bool/f9 (mut f32) (f32.const 0)) (global $bool/F (mut f64) (f64.const 2)) + (global $bool/F0 (mut f64) (f64.const 0)) + (global $bool/F1 (mut f64) (f64.const -0)) + (global $bool/F2 (mut f64) (f64.const nan:0x8000000000000)) + (global $bool/F3 (mut f64) (f64.const 0)) + (global $~lib/builtins/f64.MAX_VALUE f64 (f64.const 1797693134862315708145274e284)) + (global $bool/F4 (mut f64) (f64.const 0)) + (global $bool/F5 (mut f64) (f64.const 0)) + (global $bool/F6 (mut f64) (f64.const inf)) + (global $bool/F7 (mut f64) (f64.const 0)) + (global $~lib/builtins/f64.MIN_NORMAL_VALUE f64 (f64.const 2.2250738585072014e-308)) + (global $bool/F8 (mut f64) (f64.const 0)) + (global $bool/F9 (mut f64) (f64.const 0)) (global $bool/uu (mut i32) (i32.const 2)) (export "memory" (memory $0)) (start $~start) @@ -85,7 +109,213 @@ if i32.const 0 i32.const 32 - i32.const 10 + i32.const 11 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/f0 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 13 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/f1 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 15 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/f2 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 17 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + f32.const nan:0x400000 + f32.neg + global.set $bool/f3 + global.get $bool/f3 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 19 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f32.MAX_VALUE + global.set $bool/f4 + global.get $bool/f4 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 21 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f32.MAX_VALUE + f32.neg + global.set $bool/f5 + global.get $bool/f5 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 23 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/f6 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 25 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + f32.const inf + f32.neg + global.set $bool/f7 + global.get $bool/f7 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 27 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f32.MIN_NORMAL_VALUE + global.set $bool/f8 + global.get $bool/f8 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 30 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f32.MIN_NORMAL_VALUE + f32.neg + global.set $bool/f9 + global.get $bool/f9 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 33 i32.const 1 call $~lib/builtins/abort unreachable @@ -104,7 +334,213 @@ if i32.const 0 i32.const 32 - i32.const 12 + i32.const 36 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/F0 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 38 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/F1 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 40 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/F2 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 42 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + f64.const nan:0x8000000000000 + f64.neg + global.set $bool/F3 + global.get $bool/F3 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 44 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f64.MAX_VALUE + global.set $bool/F4 + global.get $bool/F4 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 46 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f64.MAX_VALUE + f64.neg + global.set $bool/F5 + global.get $bool/F5 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 48 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $bool/F6 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 50 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + f64.const inf + f64.neg + global.set $bool/F7 + global.get $bool/F7 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 52 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f64.MIN_NORMAL_VALUE + global.set $bool/F8 + global.get $bool/F8 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 55 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/builtins/f64.MIN_NORMAL_VALUE + f64.neg + global.set $bool/F9 + global.get $bool/F9 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 58 i32.const 1 call $~lib/builtins/abort unreachable @@ -118,7 +554,7 @@ if i32.const 0 i32.const 32 - i32.const 14 + i32.const 61 i32.const 1 call $~lib/builtins/abort unreachable From b0307e138c64381569b6c29363b577757f470726 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 15:56:45 +0300 Subject: [PATCH 10/11] use MIN_VALUE instead MIN_NORMAL_VALUE --- tests/compiler/bool.ts | 12 ++++------ tests/compiler/bool.untouched.wat | 40 +++++++++++++++---------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/tests/compiler/bool.ts b/tests/compiler/bool.ts index 0f825b533e..f1975489b2 100644 --- a/tests/compiler/bool.ts +++ b/tests/compiler/bool.ts @@ -25,11 +25,9 @@ var f6 = +Infinity; assert(f6 == true); var f7 = -Infinity; assert(f7 == true); -// @ts-ignore -var f8 = +f32.MIN_NORMAL_VALUE; +var f8 = +f32.MIN_VALUE; assert(f8 == true); -// @ts-ignore -var f9 = -f32.MIN_NORMAL_VALUE; +var f9 = -f32.MIN_VALUE; assert(f9 == true); var F = 2; @@ -50,11 +48,9 @@ var F6 = +Infinity; assert(F6 == true); var F7 = -Infinity; assert(F7 == true); -// @ts-ignore -var F8 = +f64.MIN_NORMAL_VALUE; +var F8 = +f64.MIN_VALUE; assert(F8 == true); -// @ts-ignore -var F9 = -f64.MIN_NORMAL_VALUE; +var F9 = -f64.MIN_VALUE; assert(F9 == true); var uu = 2; diff --git a/tests/compiler/bool.untouched.wat b/tests/compiler/bool.untouched.wat index 9fead1704f..711673cf83 100644 --- a/tests/compiler/bool.untouched.wat +++ b/tests/compiler/bool.untouched.wat @@ -19,7 +19,7 @@ (global $bool/f5 (mut f32) (f32.const 0)) (global $bool/f6 (mut f32) (f32.const inf)) (global $bool/f7 (mut f32) (f32.const 0)) - (global $~lib/builtins/f32.MIN_NORMAL_VALUE f32 (f32.const 1.1754943508222875e-38)) + (global $~lib/builtins/f32.MIN_VALUE f32 (f32.const 1.401298464324817e-45)) (global $bool/f8 (mut f32) (f32.const 0)) (global $bool/f9 (mut f32) (f32.const 0)) (global $bool/F (mut f64) (f64.const 2)) @@ -32,7 +32,7 @@ (global $bool/F5 (mut f64) (f64.const 0)) (global $bool/F6 (mut f64) (f64.const inf)) (global $bool/F7 (mut f64) (f64.const 0)) - (global $~lib/builtins/f64.MIN_NORMAL_VALUE f64 (f64.const 2.2250738585072014e-308)) + (global $~lib/builtins/f64.MIN_VALUE f64 (f64.const 5e-324)) (global $bool/F8 (mut f64) (f64.const 0)) (global $bool/F9 (mut f64) (f64.const 0)) (global $bool/uu (mut i32) (i32.const 2)) @@ -277,7 +277,7 @@ call $~lib/builtins/abort unreachable end - global.get $~lib/builtins/f32.MIN_NORMAL_VALUE + global.get $~lib/builtins/f32.MIN_VALUE global.set $bool/f8 global.get $bool/f8 i32.reinterpret_f32 @@ -293,12 +293,12 @@ if i32.const 0 i32.const 32 - i32.const 30 + i32.const 29 i32.const 1 call $~lib/builtins/abort unreachable end - global.get $~lib/builtins/f32.MIN_NORMAL_VALUE + global.get $~lib/builtins/f32.MIN_VALUE f32.neg global.set $bool/f9 global.get $bool/f9 @@ -315,7 +315,7 @@ if i32.const 0 i32.const 32 - i32.const 33 + i32.const 31 i32.const 1 call $~lib/builtins/abort unreachable @@ -334,7 +334,7 @@ if i32.const 0 i32.const 32 - i32.const 36 + i32.const 34 i32.const 1 call $~lib/builtins/abort unreachable @@ -353,7 +353,7 @@ if i32.const 0 i32.const 32 - i32.const 38 + i32.const 36 i32.const 1 call $~lib/builtins/abort unreachable @@ -372,7 +372,7 @@ if i32.const 0 i32.const 32 - i32.const 40 + i32.const 38 i32.const 1 call $~lib/builtins/abort unreachable @@ -391,7 +391,7 @@ if i32.const 0 i32.const 32 - i32.const 42 + i32.const 40 i32.const 1 call $~lib/builtins/abort unreachable @@ -413,7 +413,7 @@ if i32.const 0 i32.const 32 - i32.const 44 + i32.const 42 i32.const 1 call $~lib/builtins/abort unreachable @@ -434,7 +434,7 @@ if i32.const 0 i32.const 32 - i32.const 46 + i32.const 44 i32.const 1 call $~lib/builtins/abort unreachable @@ -456,7 +456,7 @@ if i32.const 0 i32.const 32 - i32.const 48 + i32.const 46 i32.const 1 call $~lib/builtins/abort unreachable @@ -475,7 +475,7 @@ if i32.const 0 i32.const 32 - i32.const 50 + i32.const 48 i32.const 1 call $~lib/builtins/abort unreachable @@ -497,12 +497,12 @@ if i32.const 0 i32.const 32 - i32.const 52 + i32.const 50 i32.const 1 call $~lib/builtins/abort unreachable end - global.get $~lib/builtins/f64.MIN_NORMAL_VALUE + global.get $~lib/builtins/f64.MIN_VALUE global.set $bool/F8 global.get $bool/F8 i64.reinterpret_f64 @@ -518,12 +518,12 @@ if i32.const 0 i32.const 32 - i32.const 55 + i32.const 52 i32.const 1 call $~lib/builtins/abort unreachable end - global.get $~lib/builtins/f64.MIN_NORMAL_VALUE + global.get $~lib/builtins/f64.MIN_VALUE f64.neg global.set $bool/F9 global.get $bool/F9 @@ -540,7 +540,7 @@ if i32.const 0 i32.const 32 - i32.const 58 + i32.const 54 i32.const 1 call $~lib/builtins/abort unreachable @@ -554,7 +554,7 @@ if i32.const 0 i32.const 32 - i32.const 61 + i32.const 57 i32.const 1 call $~lib/builtins/abort unreachable From f94e873c5df26f66c0e40a7eef7334462c7662a4 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 20 Aug 2020 17:51:30 +0300 Subject: [PATCH 11/11] add more tests --- tests/compiler/bool.ts | 16 +++ tests/compiler/bool.untouched.wat | 220 ++++++++++++++++++++++++++++-- 2 files changed, 224 insertions(+), 12 deletions(-) diff --git a/tests/compiler/bool.ts b/tests/compiler/bool.ts index f1975489b2..626f58076c 100644 --- a/tests/compiler/bool.ts +++ b/tests/compiler/bool.ts @@ -29,6 +29,14 @@ var f8 = +f32.MIN_VALUE; assert(f8 == true); var f9 = -f32.MIN_VALUE; assert(f9 == true); +var f10 = reinterpret(1); +assert(f10 == true); +var f11 = reinterpret(0x7F800000 - 1); +assert(f11 == true); +var f12 = reinterpret(0x7F800000 + 1); +assert(f12 == false); +var f13 = reinterpret(0xFF800000 + 1); +assert(f13 == false); var F = 2; assert(F == true); @@ -52,6 +60,14 @@ var F8 = +f64.MIN_VALUE; assert(F8 == true); var F9 = -f64.MIN_VALUE; assert(F9 == true); +var F10 = reinterpret(1); +assert(F10 == true); +var F11 = reinterpret(0x7FF0000000000000 - 1); +assert(F11 == true); +var F12 = reinterpret(0x7FF0000000000000 + 1); +assert(F12 == false); +var F13 = reinterpret(0xFFF0000000000000 + 1); +assert(F13 == false); var uu = 2; assert(uu == true); diff --git a/tests/compiler/bool.untouched.wat b/tests/compiler/bool.untouched.wat index 711673cf83..0e8c20c19e 100644 --- a/tests/compiler/bool.untouched.wat +++ b/tests/compiler/bool.untouched.wat @@ -22,6 +22,10 @@ (global $~lib/builtins/f32.MIN_VALUE f32 (f32.const 1.401298464324817e-45)) (global $bool/f8 (mut f32) (f32.const 0)) (global $bool/f9 (mut f32) (f32.const 0)) + (global $bool/f10 (mut f32) (f32.const 0)) + (global $bool/f11 (mut f32) (f32.const 0)) + (global $bool/f12 (mut f32) (f32.const 0)) + (global $bool/f13 (mut f32) (f32.const 0)) (global $bool/F (mut f64) (f64.const 2)) (global $bool/F0 (mut f64) (f64.const 0)) (global $bool/F1 (mut f64) (f64.const -0)) @@ -35,6 +39,10 @@ (global $~lib/builtins/f64.MIN_VALUE f64 (f64.const 5e-324)) (global $bool/F8 (mut f64) (f64.const 0)) (global $bool/F9 (mut f64) (f64.const 0)) + (global $bool/F10 (mut f64) (f64.const 0)) + (global $bool/F11 (mut f64) (f64.const 0)) + (global $bool/F12 (mut f64) (f64.const 0)) + (global $bool/F13 (mut f64) (f64.const 0)) (global $bool/uu (mut i32) (i32.const 2)) (export "memory" (memory $0)) (start $~start) @@ -320,6 +328,100 @@ call $~lib/builtins/abort unreachable end + i32.const 1 + f32.reinterpret_i32 + global.set $bool/f10 + global.get $bool/f10 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 33 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2139095040 + i32.const 1 + i32.sub + f32.reinterpret_i32 + global.set $bool/f11 + global.get $bool/f11 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 35 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2139095040 + i32.const 1 + i32.add + f32.reinterpret_i32 + global.set $bool/f12 + global.get $bool/f12 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 37 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -8388608 + i32.const 1 + i32.add + f32.reinterpret_i32 + global.set $bool/f13 + global.get $bool/f13 + i32.reinterpret_f32 + i32.const 1 + i32.shl + i32.const 2 + i32.sub + i32.const -16777218 + i32.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 39 + i32.const 1 + call $~lib/builtins/abort + unreachable + end global.get $bool/F i64.reinterpret_f64 i64.const 1 @@ -334,7 +436,7 @@ if i32.const 0 i32.const 32 - i32.const 34 + i32.const 42 i32.const 1 call $~lib/builtins/abort unreachable @@ -353,7 +455,7 @@ if i32.const 0 i32.const 32 - i32.const 36 + i32.const 44 i32.const 1 call $~lib/builtins/abort unreachable @@ -372,7 +474,7 @@ if i32.const 0 i32.const 32 - i32.const 38 + i32.const 46 i32.const 1 call $~lib/builtins/abort unreachable @@ -391,7 +493,7 @@ if i32.const 0 i32.const 32 - i32.const 40 + i32.const 48 i32.const 1 call $~lib/builtins/abort unreachable @@ -413,7 +515,7 @@ if i32.const 0 i32.const 32 - i32.const 42 + i32.const 50 i32.const 1 call $~lib/builtins/abort unreachable @@ -434,7 +536,7 @@ if i32.const 0 i32.const 32 - i32.const 44 + i32.const 52 i32.const 1 call $~lib/builtins/abort unreachable @@ -456,7 +558,7 @@ if i32.const 0 i32.const 32 - i32.const 46 + i32.const 54 i32.const 1 call $~lib/builtins/abort unreachable @@ -475,7 +577,7 @@ if i32.const 0 i32.const 32 - i32.const 48 + i32.const 56 i32.const 1 call $~lib/builtins/abort unreachable @@ -497,7 +599,7 @@ if i32.const 0 i32.const 32 - i32.const 50 + i32.const 58 i32.const 1 call $~lib/builtins/abort unreachable @@ -518,7 +620,7 @@ if i32.const 0 i32.const 32 - i32.const 52 + i32.const 60 i32.const 1 call $~lib/builtins/abort unreachable @@ -540,7 +642,101 @@ if i32.const 0 i32.const 32 - i32.const 54 + i32.const 62 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 1 + f64.reinterpret_i64 + global.set $bool/F10 + global.get $bool/F10 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 64 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 9218868437227405312 + i64.const 1 + i64.sub + f64.reinterpret_i64 + global.set $bool/F11 + global.get $bool/F11 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 1 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 66 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 9218868437227405312 + i64.const 1 + i64.add + f64.reinterpret_i64 + global.set $bool/F12 + global.get $bool/F12 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 68 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const -4503599627370496 + i64.const 1 + i64.add + f64.reinterpret_i64 + global.set $bool/F13 + global.get $bool/F13 + i64.reinterpret_f64 + i64.const 1 + i64.shl + i64.const 2 + i64.sub + i64.const -9007199254740994 + i64.le_u + i32.const 0 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 70 i32.const 1 call $~lib/builtins/abort unreachable @@ -554,7 +750,7 @@ if i32.const 0 i32.const 32 - i32.const 57 + i32.const 73 i32.const 1 call $~lib/builtins/abort unreachable