RFR: 8257815: Replace global log2 functions with efficient implementations
Thomas Stuefe
stuefe at openjdk.java.net
Tue Dec 8 14:23:15 UTC 2020
On Tue, 8 Dec 2020 01:15:46 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:
>> This patch replaces the log2 functions in globalDefinitions.hpp with more efficient counterparts in utilities/powerOfTwo.hpp
>>
>> Naming is hard, but I think the following scheme is reasonable:
>>
>> - log2i: any integral type. 0-hostile
>> - log2i_allow_zero: any integral type. gracefully handles zero (adds a branch)
>> - exact_log2i: any integral type. value must be a power of two
>>
>> I chose log2i rather than log2 to stand out from the log2 functions defined in various standard libraries.
>>
>> Going through all usage, quite a few uses of log2_long et.c. could be replaced by exact_log2i since they take something that has been checked to be a power of two. Most of the remaining usage seem to be able to use the 0-hostile variant, which avoids a branch.
>>
>> To sanity check that calculating log2 using count_leading_zeros gives better performance I added a couple of trivial and short-running microbenchmarks to test_powerOfTwo. For small values (<= 1025) the new impl is ~5x faster, with a larger speed-up for larger integer values:
>>
>> [ RUN ] power_of_2.log2_long_micro
>> [ OK ] power_of_2.log2_long_micro (3581 ms)
>> [ RUN ] power_of_2.log2_long_small_micro
>> [ OK ] power_of_2.log2_long_small_micro (549 ms)
>> [ RUN ] power_of_2.log2i_micro
>> [ OK ] power_of_2.log2i_micro (259 ms)
>> [ RUN ] power_of_2.log2i_small_micro
>> [ OK ] power_of_2.log2i_small_micro (113 ms)
>>
>> I'm not sure if this naive microbenchmark carries its own weight, but it just adds a few seconds and can be useful for quickly checking this performance assumption on other H/W
>>
>> (Intending this for 17)
>
>> Naming is hard, but I think the following scheme is reasonable:
>>
>> * log2i: any integral type. 0-hostile
>
> Not yet a review, but the "usual" name is "ilog2". Do a web search for that and you'll find lots of relevant hits. I like the short name having the non-zero precondition. Whether the longer version that tests for zero carry's it's weight is a question.
Do you guys test on big endian? To be sure I'll run this through our landscape.
-------------
PR: https://git.openjdk.java.net/jdk/pull/1663
More information about the hotspot-dev
mailing list