RFR: 8234331: Add robust and optimized utility for rounding up to next power of two+

Claes Redestad claes.redestad at oracle.com
Thu Dec 5 01:32:55 UTC 2019


Hi again,

On 2019-12-04 18:37, Claes Redestad wrote:
>>
>>
>> http://cr.openjdk.java.net/~redestad/8234331/open.03/src/hotspot/share/utilities/count_leading_zeros.hpp.frames.html 
>>
>>
>> template <typename T> inline uint32_t count_leading_zeros(T x) {
>>
>> Instead of all the "if sizeof(T) == 4|8", could we specialize the 
>> functions for different operand sizes? Example:
>>
>> template <typename T, size_t n> struct Impl;
>> template <typename T> struct Impl<T, 8> { static int doit(T v) { 
>> return __builtin_clzll(v); } };
>> template <typename T> struct Impl<T, 4> { static int doit(T v) { 
>> return __builtin_clz(v); } };
>> template <typename T> int count_leading_zero(T v) { return Impl<T, 
>> sizeof(T)>::doit(v); }
>>
>> Would make it easier later to plug in implementations for different 
>> sizes, and we'd still get a compile time error for invalid input types.
> 
> I guess we could, but I'm not so sure it'd help readability.

after some considerations, I took this idea for a spin, along with
implementations and tests for 8 and 16-bit for completeness up
front:

http://cr.openjdk.java.net/~redestad/8234331/open.04

I also turned tests into template functions etc. I think it turned
out an improvement.

Testing: tier1-3 (some ongoing)

Thanks!

/Claes


More information about the hotspot-compiler-dev mailing list