Avoid needless gather in fast_integer_divide lowering#6441
Merged
Conversation
fast_integer_divide did two lookups, one for a multiplier, and one for a shift. It turns out you can just use count leading zeros to compute a workable shift instead of having to do a lookup. This PR speeds up use of fast_integer_divide in cases where the denominator varies across vector lanes by ~70% or so by avoiding one of the two expensive gathers.
| std::lock_guard<std::mutex> lock_guard(initialize_lock); | ||
| { | ||
| static Buffer<uint8_t> im(256, 2); | ||
| static Buffer<uint8_t> im(256); |
Contributor
There was a problem hiding this comment.
Unrelated: since C++11 onwards default to promising thread-safe initialization of statics, we could rewrite this to be a bit terser like so:
Buffer<uint8_t> integer_divide_table_u8() {
static Buffer<uint8_t> im(256) = []() {
Buffer<uint8_t> im(256);
for (uint32_t i = 0; i < 256; i++) {
im(i) = table_runtime_u8[i][2];
if (i > 1) {
internal_assert(table_runtime_u8[i][3] == shift_for_denominator(i));
}
}
return im;
}();
return im;
}
steven-johnson
approved these changes
Nov 23, 2021
shoaibkamil
approved these changes
Nov 23, 2021
Member
Author
|
No, that's compile-time code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fast_integer_divide did two lookups, one for a multiplier, and one for a
shift. It turns out you can just use count leading zeros to compute a
workable shift instead of having to do a lookup. This PR speeds up use
of fast_integer_divide in cases where the denominator varies across
vector lanes by ~70% or so by avoiding one of the two expensive gathers.