RFR: 8257985: count_trailing_zeros doesn't handle 64-bit values on 32-bit JVM [v4]

Kim Barrett kbarrett at openjdk.java.net
Fri Dec 11 14:03:10 UTC 2020


On Fri, 11 Dec 2020 13:44:16 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:

>> Claes Redestad has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Explicit cast to unsigned
>
> This can be much simpler:
> 
> #if defined(TARGET_COMPILER_gcc)
> 
> inline unsigned count_trailing_zeros_32(uint32_t x) {
>   return __builtin_ctz(x);
> }
> 
> inline unsigned count_trailing_zeros_64(uint64_t x) {
>   return __builtin_ctzll(x);
> }
> 
> #elif ...
> 
> ...
> 
> #endif
> 
> template<typename T,
>          ENABLE_IF(std::is_integral<T>::value),
>          ENABLE_IF(sizeof(T) <= sizeof(uint64_t))>
> inline unsigned count_trailing_zeros(T x) {
>   assert(x != 0, "precondition");
>   return (sizeof(x) <= sizeof(uint32_t)) ?
>          count_trailing_zeros_32(x) :
>          count_trailing_zeros_64(x);
>   }
> }

> This can be much simpler:
> 
> ```
> [...]
> template<typename T,
>          ENABLE_IF(std::is_integral<T>::value),
>          ENABLE_IF(sizeof(T) <= sizeof(uint64_t))>
> inline unsigned count_trailing_zeros(T x) {
>   assert(x != 0, "precondition");
>   return (sizeof(x) <= sizeof(uint32_t)) ?
>          count_trailing_zeros_32(x) :
>          count_trailing_zeros_64(x);
>   }
> }
> ```

It's possible that some compiler will whine about the implicit narrowing
conversion when sizeof(x) > sizeof(uint32_t). If so, a cast should silence
it, i.e. `count_trailing_zeros_32(static_cast<uint32_t>(x))`.

-------------

PR: https://git.openjdk.java.net/jdk/pull/1747


More information about the hotspot-dev mailing list