Skip to content

[std] Optimize integer powers with 2 ** y case #1610

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 3 commits into from
Jan 30, 2021
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
6 changes: 6 additions & 0 deletions std/assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>(1 << e, 0, <u32>e < 32);
}
if (e <= 0) {
if (x == -1) return select<i32>(-1, 1, e & 1);
return i32(e == 0) | i32(x == 1);
Expand Down Expand Up @@ -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<i64>(1 << e, 0, <u64>e < 64);
}
if (e <= 0) {
if (x == -1) return select<i64>(-1, 1, e & 1);
return i64(e == 0) | i64(x == 1);
Expand Down
14 changes: 14 additions & 0 deletions tests/compiler/binary.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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
Expand Down
14 changes: 14 additions & 0 deletions tests/compiler/resolve-binary.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6429,6 +6429,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
Expand Down
92 changes: 59 additions & 33 deletions tests/compiler/std/math.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -49372,6 +49372,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
Expand All @@ -49380,7 +49406,7 @@
if
i32.const 0
i32.const 1056
i32.const 4082
i32.const 4084
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49393,7 +49419,7 @@
if
i32.const 0
i32.const 1056
i32.const 4083
i32.const 4085
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49406,7 +49432,7 @@
if
i32.const 0
i32.const 1056
i32.const 4084
i32.const 4086
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49419,7 +49445,7 @@
if
i32.const 0
i32.const 1056
i32.const 4085
i32.const 4087
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49432,7 +49458,7 @@
if
i32.const 0
i32.const 1056
i32.const 4086
i32.const 4088
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49445,7 +49471,7 @@
if
i32.const 0
i32.const 1056
i32.const 4087
i32.const 4089
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49456,7 +49482,7 @@
if
i32.const 0
i32.const 1056
i32.const 4089
i32.const 4091
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49467,7 +49493,7 @@
if
i32.const 0
i32.const 1056
i32.const 4090
i32.const 4092
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49478,7 +49504,7 @@
if
i32.const 0
i32.const 1056
i32.const 4093
i32.const 4095
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49491,7 +49517,7 @@
if
i32.const 0
i32.const 1056
i32.const 4095
i32.const 4097
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49504,7 +49530,7 @@
if
i32.const 0
i32.const 1056
i32.const 4096
i32.const 4098
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49517,7 +49543,7 @@
if
i32.const 0
i32.const 1056
i32.const 4099
i32.const 4101
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49532,7 +49558,7 @@
if
i32.const 0
i32.const 1056
i32.const 4101
i32.const 4103
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49547,7 +49573,7 @@
if
i32.const 0
i32.const 1056
i32.const 4102
i32.const 4104
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49562,7 +49588,7 @@
if
i32.const 0
i32.const 1056
i32.const 4103
i32.const 4105
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49575,7 +49601,7 @@
if
i32.const 0
i32.const 1056
i32.const 4104
i32.const 4106
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49590,7 +49616,7 @@
if
i32.const 0
i32.const 1056
i32.const 4105
i32.const 4107
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49603,7 +49629,7 @@
if
i32.const 0
i32.const 1056
i32.const 4107
i32.const 4109
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49616,7 +49642,7 @@
if
i32.const 0
i32.const 1056
i32.const 4108
i32.const 4110
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49629,7 +49655,7 @@
if
i32.const 0
i32.const 1056
i32.const 4109
i32.const 4111
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49642,7 +49668,7 @@
if
i32.const 0
i32.const 1056
i32.const 4110
i32.const 4112
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49655,7 +49681,7 @@
if
i32.const 0
i32.const 1056
i32.const 4111
i32.const 4113
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49668,7 +49694,7 @@
if
i32.const 0
i32.const 1056
i32.const 4112
i32.const 4114
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49681,7 +49707,7 @@
if
i32.const 0
i32.const 1056
i32.const 4113
i32.const 4115
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49694,7 +49720,7 @@
if
i32.const 0
i32.const 1056
i32.const 4114
i32.const 4116
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49707,7 +49733,7 @@
if
i32.const 0
i32.const 1056
i32.const 4115
i32.const 4117
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49726,7 +49752,7 @@
if
i32.const 0
i32.const 1056
i32.const 4117
i32.const 4119
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49743,7 +49769,7 @@
if
i32.const 0
i32.const 1056
i32.const 4118
i32.const 4120
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49756,7 +49782,7 @@
if
i32.const 0
i32.const 1056
i32.const 4120
i32.const 4122
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49769,7 +49795,7 @@
if
i32.const 0
i32.const 1056
i32.const 4121
i32.const 4123
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49782,7 +49808,7 @@
if
i32.const 0
i32.const 1056
i32.const 4122
i32.const 4124
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49795,7 +49821,7 @@
if
i32.const 0
i32.const 1056
i32.const 4123
i32.const 4125
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -49808,7 +49834,7 @@
if
i32.const 0
i32.const 1056
i32.const 4124
i32.const 4126
i32.const 1
call $~lib/builtins/abort
unreachable
Expand Down
Loading