From 99dfc2892e0950120ab19296769975f0783784ac Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 5 Jan 2021 20:38:21 +0200 Subject: [PATCH 1/2] speedup 2 ** y cases --- std/assembly/math.ts | 6 + tests/compiler/binary.untouched.wat | 14 ++ tests/compiler/resolve-binary.untouched.wat | 14 ++ tests/compiler/std/math.optimized.wat | 92 ++++++++----- tests/compiler/std/math.ts | 2 + tests/compiler/std/math.untouched.wat | 122 +++++++++++++----- .../std/operator-overloading.untouched.wat | 14 ++ tests/compiler/std/string.untouched.wat | 14 ++ 8 files changed, 212 insertions(+), 66 deletions(-) diff --git a/std/assembly/math.ts b/std/assembly/math.ts index 0b4313a61d..f43842b022 100644 --- a/std/assembly/math.ts +++ b/std/assembly/math.ts @@ -3080,6 +3080,9 @@ export namespace NativeMathf { export function ipow32(x: i32, e: i32): i32 { var out = 1; if (ASC_SHRINK_LEVEL < 1) { + if (x == 2) { + return select(1 << e, 0, e < 32); + } if (e <= 0) { if (x == -1) return select(-1, 1, e & 1); return i32(e == 0) | i32(x == 1); @@ -3129,6 +3132,9 @@ export function ipow32(x: i32, e: i32): i32 { export function ipow64(x: i64, e: i64): i64 { var out: i64 = 1; if (ASC_SHRINK_LEVEL < 1) { + if (x == 2) { + return select(1 << e, 0, e < 64); + } if (e <= 0) { if (x == -1) return select(-1, 1, e & 1); return i64(e == 0) | i64(x == 1); diff --git a/tests/compiler/binary.untouched.wat b/tests/compiler/binary.untouched.wat index fe63b928b4..7693163b3b 100644 --- a/tests/compiler/binary.untouched.wat +++ b/tests/compiler/binary.untouched.wat @@ -28,6 +28,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s diff --git a/tests/compiler/resolve-binary.untouched.wat b/tests/compiler/resolve-binary.untouched.wat index b2947861df..de2aba5fb8 100644 --- a/tests/compiler/resolve-binary.untouched.wat +++ b/tests/compiler/resolve-binary.untouched.wat @@ -4543,6 +4543,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s diff --git a/tests/compiler/std/math.optimized.wat b/tests/compiler/std/math.optimized.wat index 2469d09a7e..954de88bad 100644 --- a/tests/compiler/std/math.optimized.wat +++ b/tests/compiler/std/math.optimized.wat @@ -49370,6 +49370,32 @@ call $~lib/builtins/abort unreachable end + i64.const 2 + i64.const 64 + call $~lib/math/ipow64 + i64.eqz + i32.eqz + if + i32.const 0 + i32.const 1056 + i32.const 4077 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 2 + i64.const 128 + call $~lib/math/ipow64 + i64.eqz + i32.eqz + if + i32.const 0 + i32.const 1056 + i32.const 4078 + i32.const 1 + call $~lib/builtins/abort + unreachable + end i32.const 1 i32.const 3 call $~lib/math/ipow32 @@ -49378,7 +49404,7 @@ if i32.const 0 i32.const 1056 - i32.const 4082 + i32.const 4084 i32.const 1 call $~lib/builtins/abort unreachable @@ -49391,7 +49417,7 @@ if i32.const 0 i32.const 1056 - i32.const 4083 + i32.const 4085 i32.const 1 call $~lib/builtins/abort unreachable @@ -49404,7 +49430,7 @@ if i32.const 0 i32.const 1056 - i32.const 4084 + i32.const 4086 i32.const 1 call $~lib/builtins/abort unreachable @@ -49417,7 +49443,7 @@ if i32.const 0 i32.const 1056 - i32.const 4085 + i32.const 4087 i32.const 1 call $~lib/builtins/abort unreachable @@ -49430,7 +49456,7 @@ if i32.const 0 i32.const 1056 - i32.const 4086 + i32.const 4088 i32.const 1 call $~lib/builtins/abort unreachable @@ -49443,7 +49469,7 @@ if i32.const 0 i32.const 1056 - i32.const 4087 + i32.const 4089 i32.const 1 call $~lib/builtins/abort unreachable @@ -49454,7 +49480,7 @@ if i32.const 0 i32.const 1056 - i32.const 4089 + i32.const 4091 i32.const 1 call $~lib/builtins/abort unreachable @@ -49465,7 +49491,7 @@ if i32.const 0 i32.const 1056 - i32.const 4090 + i32.const 4092 i32.const 1 call $~lib/builtins/abort unreachable @@ -49476,7 +49502,7 @@ if i32.const 0 i32.const 1056 - i32.const 4093 + i32.const 4095 i32.const 1 call $~lib/builtins/abort unreachable @@ -49489,7 +49515,7 @@ if i32.const 0 i32.const 1056 - i32.const 4095 + i32.const 4097 i32.const 1 call $~lib/builtins/abort unreachable @@ -49502,7 +49528,7 @@ if i32.const 0 i32.const 1056 - i32.const 4096 + i32.const 4098 i32.const 1 call $~lib/builtins/abort unreachable @@ -49515,7 +49541,7 @@ if i32.const 0 i32.const 1056 - i32.const 4099 + i32.const 4101 i32.const 1 call $~lib/builtins/abort unreachable @@ -49530,7 +49556,7 @@ if i32.const 0 i32.const 1056 - i32.const 4101 + i32.const 4103 i32.const 1 call $~lib/builtins/abort unreachable @@ -49545,7 +49571,7 @@ if i32.const 0 i32.const 1056 - i32.const 4102 + i32.const 4104 i32.const 1 call $~lib/builtins/abort unreachable @@ -49560,7 +49586,7 @@ if i32.const 0 i32.const 1056 - i32.const 4103 + i32.const 4105 i32.const 1 call $~lib/builtins/abort unreachable @@ -49573,7 +49599,7 @@ if i32.const 0 i32.const 1056 - i32.const 4104 + i32.const 4106 i32.const 1 call $~lib/builtins/abort unreachable @@ -49588,7 +49614,7 @@ if i32.const 0 i32.const 1056 - i32.const 4105 + i32.const 4107 i32.const 1 call $~lib/builtins/abort unreachable @@ -49601,7 +49627,7 @@ if i32.const 0 i32.const 1056 - i32.const 4107 + i32.const 4109 i32.const 1 call $~lib/builtins/abort unreachable @@ -49614,7 +49640,7 @@ if i32.const 0 i32.const 1056 - i32.const 4108 + i32.const 4110 i32.const 1 call $~lib/builtins/abort unreachable @@ -49627,7 +49653,7 @@ if i32.const 0 i32.const 1056 - i32.const 4109 + i32.const 4111 i32.const 1 call $~lib/builtins/abort unreachable @@ -49640,7 +49666,7 @@ if i32.const 0 i32.const 1056 - i32.const 4110 + i32.const 4112 i32.const 1 call $~lib/builtins/abort unreachable @@ -49653,7 +49679,7 @@ if i32.const 0 i32.const 1056 - i32.const 4111 + i32.const 4113 i32.const 1 call $~lib/builtins/abort unreachable @@ -49666,7 +49692,7 @@ if i32.const 0 i32.const 1056 - i32.const 4112 + i32.const 4114 i32.const 1 call $~lib/builtins/abort unreachable @@ -49679,7 +49705,7 @@ if i32.const 0 i32.const 1056 - i32.const 4113 + i32.const 4115 i32.const 1 call $~lib/builtins/abort unreachable @@ -49692,7 +49718,7 @@ if i32.const 0 i32.const 1056 - i32.const 4114 + i32.const 4116 i32.const 1 call $~lib/builtins/abort unreachable @@ -49705,7 +49731,7 @@ if i32.const 0 i32.const 1056 - i32.const 4115 + i32.const 4117 i32.const 1 call $~lib/builtins/abort unreachable @@ -49724,7 +49750,7 @@ if i32.const 0 i32.const 1056 - i32.const 4117 + i32.const 4119 i32.const 1 call $~lib/builtins/abort unreachable @@ -49741,7 +49767,7 @@ if i32.const 0 i32.const 1056 - i32.const 4118 + i32.const 4120 i32.const 1 call $~lib/builtins/abort unreachable @@ -49754,7 +49780,7 @@ if i32.const 0 i32.const 1056 - i32.const 4120 + i32.const 4122 i32.const 1 call $~lib/builtins/abort unreachable @@ -49767,7 +49793,7 @@ if i32.const 0 i32.const 1056 - i32.const 4121 + i32.const 4123 i32.const 1 call $~lib/builtins/abort unreachable @@ -49780,7 +49806,7 @@ if i32.const 0 i32.const 1056 - i32.const 4122 + i32.const 4124 i32.const 1 call $~lib/builtins/abort unreachable @@ -49793,7 +49819,7 @@ if i32.const 0 i32.const 1056 - i32.const 4123 + i32.const 4125 i32.const 1 call $~lib/builtins/abort unreachable @@ -49806,7 +49832,7 @@ if i32.const 0 i32.const 1056 - i32.const 4124 + i32.const 4126 i32.const 1 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/std/math.ts b/tests/compiler/std/math.ts index cae8f4c655..b2f6dbba38 100644 --- a/tests/compiler/std/math.ts +++ b/tests/compiler/std/math.ts @@ -4074,6 +4074,8 @@ assert(ipow64(3, 128) == -9204772141784466943); // should overflow assert(ipow64(1, -1) == 1); assert(ipow64(2, -1) == 0); +assert(ipow64(2, 64) == 0); +assert(ipow64(2, 128) == 0); // integer pow operators diff --git a/tests/compiler/std/math.untouched.wat b/tests/compiler/std/math.untouched.wat index 5d042f41b7..86acb73941 100644 --- a/tests/compiler/std/math.untouched.wat +++ b/tests/compiler/std/math.untouched.wat @@ -15974,6 +15974,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i64.const 2 + i64.eq + if + i64.const 1 + local.get $1 + i64.shl + i64.const 0 + local.get $1 + i64.const 64 + i64.lt_u + select + return + end local.get $1 i64.const 0 i64.le_s @@ -16223,6 +16237,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s @@ -58765,6 +58793,34 @@ call $~lib/builtins/abort unreachable end + i64.const 2 + i64.const 64 + call $~lib/math/ipow64 + i64.const 0 + i64.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 4077 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 2 + i64.const 128 + call $~lib/math/ipow64 + i64.const 0 + i64.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 4078 + i32.const 1 + call $~lib/builtins/abort + unreachable + end i32.const 1 i32.const 0 i32.const 0 @@ -58792,7 +58848,7 @@ if i32.const 0 i32.const 32 - i32.const 4082 + i32.const 4084 i32.const 1 call $~lib/builtins/abort unreachable @@ -58806,7 +58862,7 @@ if i32.const 0 i32.const 32 - i32.const 4083 + i32.const 4085 i32.const 1 call $~lib/builtins/abort unreachable @@ -58820,7 +58876,7 @@ if i32.const 0 i32.const 32 - i32.const 4084 + i32.const 4086 i32.const 1 call $~lib/builtins/abort unreachable @@ -58834,7 +58890,7 @@ if i32.const 0 i32.const 32 - i32.const 4085 + i32.const 4087 i32.const 1 call $~lib/builtins/abort unreachable @@ -58848,7 +58904,7 @@ if i32.const 0 i32.const 32 - i32.const 4086 + i32.const 4088 i32.const 1 call $~lib/builtins/abort unreachable @@ -58862,7 +58918,7 @@ if i32.const 0 i32.const 32 - i32.const 4087 + i32.const 4089 i32.const 1 call $~lib/builtins/abort unreachable @@ -58876,7 +58932,7 @@ if i32.const 0 i32.const 32 - i32.const 4089 + i32.const 4091 i32.const 1 call $~lib/builtins/abort unreachable @@ -58890,7 +58946,7 @@ if i32.const 0 i32.const 32 - i32.const 4090 + i32.const 4092 i32.const 1 call $~lib/builtins/abort unreachable @@ -58922,7 +58978,7 @@ if i32.const 0 i32.const 32 - i32.const 4093 + i32.const 4095 i32.const 1 call $~lib/builtins/abort unreachable @@ -58936,7 +58992,7 @@ if i32.const 0 i32.const 32 - i32.const 4095 + i32.const 4097 i32.const 1 call $~lib/builtins/abort unreachable @@ -58950,7 +59006,7 @@ if i32.const 0 i32.const 32 - i32.const 4096 + i32.const 4098 i32.const 1 call $~lib/builtins/abort unreachable @@ -58982,7 +59038,7 @@ if i32.const 0 i32.const 32 - i32.const 4099 + i32.const 4101 i32.const 1 call $~lib/builtins/abort unreachable @@ -59000,7 +59056,7 @@ if i32.const 0 i32.const 32 - i32.const 4101 + i32.const 4103 i32.const 1 call $~lib/builtins/abort unreachable @@ -59018,7 +59074,7 @@ if i32.const 0 i32.const 32 - i32.const 4102 + i32.const 4104 i32.const 1 call $~lib/builtins/abort unreachable @@ -59034,7 +59090,7 @@ if i32.const 0 i32.const 32 - i32.const 4103 + i32.const 4105 i32.const 1 call $~lib/builtins/abort unreachable @@ -59050,7 +59106,7 @@ if i32.const 0 i32.const 32 - i32.const 4104 + i32.const 4106 i32.const 1 call $~lib/builtins/abort unreachable @@ -59066,7 +59122,7 @@ if i32.const 0 i32.const 32 - i32.const 4105 + i32.const 4107 i32.const 1 call $~lib/builtins/abort unreachable @@ -59080,7 +59136,7 @@ if i32.const 0 i32.const 32 - i32.const 4107 + i32.const 4109 i32.const 1 call $~lib/builtins/abort unreachable @@ -59094,7 +59150,7 @@ if i32.const 0 i32.const 32 - i32.const 4108 + i32.const 4110 i32.const 1 call $~lib/builtins/abort unreachable @@ -59108,7 +59164,7 @@ if i32.const 0 i32.const 32 - i32.const 4109 + i32.const 4111 i32.const 1 call $~lib/builtins/abort unreachable @@ -59122,7 +59178,7 @@ if i32.const 0 i32.const 32 - i32.const 4110 + i32.const 4112 i32.const 1 call $~lib/builtins/abort unreachable @@ -59136,7 +59192,7 @@ if i32.const 0 i32.const 32 - i32.const 4111 + i32.const 4113 i32.const 1 call $~lib/builtins/abort unreachable @@ -59150,7 +59206,7 @@ if i32.const 0 i32.const 32 - i32.const 4112 + i32.const 4114 i32.const 1 call $~lib/builtins/abort unreachable @@ -59164,7 +59220,7 @@ if i32.const 0 i32.const 32 - i32.const 4113 + i32.const 4115 i32.const 1 call $~lib/builtins/abort unreachable @@ -59178,7 +59234,7 @@ if i32.const 0 i32.const 32 - i32.const 4114 + i32.const 4116 i32.const 1 call $~lib/builtins/abort unreachable @@ -59192,7 +59248,7 @@ if i32.const 0 i32.const 32 - i32.const 4115 + i32.const 4117 i32.const 1 call $~lib/builtins/abort unreachable @@ -59212,7 +59268,7 @@ if i32.const 0 i32.const 32 - i32.const 4117 + i32.const 4119 i32.const 1 call $~lib/builtins/abort unreachable @@ -59230,7 +59286,7 @@ if i32.const 0 i32.const 32 - i32.const 4118 + i32.const 4120 i32.const 1 call $~lib/builtins/abort unreachable @@ -59245,7 +59301,7 @@ if i32.const 0 i32.const 32 - i32.const 4120 + i32.const 4122 i32.const 1 call $~lib/builtins/abort unreachable @@ -59260,7 +59316,7 @@ if i32.const 0 i32.const 32 - i32.const 4121 + i32.const 4123 i32.const 1 call $~lib/builtins/abort unreachable @@ -59275,7 +59331,7 @@ if i32.const 0 i32.const 32 - i32.const 4122 + i32.const 4124 i32.const 1 call $~lib/builtins/abort unreachable @@ -59289,7 +59345,7 @@ if i32.const 0 i32.const 32 - i32.const 4123 + i32.const 4125 i32.const 1 call $~lib/builtins/abort unreachable @@ -59303,7 +59359,7 @@ if i32.const 0 i32.const 32 - i32.const 4124 + i32.const 4126 i32.const 1 call $~lib/builtins/abort unreachable diff --git a/tests/compiler/std/operator-overloading.untouched.wat b/tests/compiler/std/operator-overloading.untouched.wat index bed7f6962a..7c7f8c8c42 100644 --- a/tests/compiler/std/operator-overloading.untouched.wat +++ b/tests/compiler/std/operator-overloading.untouched.wat @@ -386,6 +386,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index f4b1fef49b..988608830b 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -5586,6 +5586,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s From a1cadf0722683585872928983c8534fc86a11da3 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 29 Jan 2021 03:08:14 +0200 Subject: [PATCH 2/2] rebuild --- .../std/operator-overloading.untouched.wat | 34 +++++++--------- tests/compiler/std/string.untouched.wat | 40 +++++++------------ 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/tests/compiler/std/operator-overloading.untouched.wat b/tests/compiler/std/operator-overloading.untouched.wat index 0953138cd4..8a4203495e 100644 --- a/tests/compiler/std/operator-overloading.untouched.wat +++ b/tests/compiler/std/operator-overloading.untouched.wat @@ -460,25 +460,6 @@ local.set $3 i32.const 1 drop -<<<<<<< HEAD - local.get $0 - i32.const 2 - i32.eq - if - i32.const 1 - local.get $1 - i32.shl - i32.const 0 - local.get $1 - i32.const 32 - i32.lt_u - select - return - end - local.get $1 - i32.const 0 - i32.le_s -======= local.get $3 i32.const 12 i32.ge_u @@ -490,7 +471,6 @@ i32.const 0 end i32.eqz ->>>>>>> master if i32.const 0 i32.const 368 @@ -2505,6 +2485,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index c9e221ad0b..ca0d54db61 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -4310,6 +4310,20 @@ i32.const 1 i32.lt_s drop + local.get $0 + i32.const 2 + i32.eq + if + i32.const 1 + local.get $1 + i32.shl + i32.const 0 + local.get $1 + i32.const 32 + i32.lt_u + select + return + end local.get $1 i32.const 0 i32.le_s @@ -5959,31 +5973,6 @@ (func $~lib/util/number/prettify (param $0 i32) (param $1 i32) (param $2 i32) (result i32) (local $3 i32) (local $4 i32) -<<<<<<< HEAD - i32.const 1 - local.set $2 - i32.const 0 - i32.const 1 - i32.lt_s - drop - local.get $0 - i32.const 2 - i32.eq - if - i32.const 1 - local.get $1 - i32.shl - i32.const 0 - local.get $1 - i32.const 32 - i32.lt_u - select - return - end - local.get $1 - i32.const 0 - i32.le_s -======= (local $5 i32) (local $6 i32) (local $7 i32) @@ -5993,7 +5982,6 @@ (local $11 i32) local.get $2 i32.eqz ->>>>>>> master if local.get $0 local.get $1