cyb70289 commented on code in PR #39403:
URL: https://github.com/apache/arrow/pull/39403#discussion_r1439173153


##########
cpp/src/arrow/util/bit_util.h:
##########
@@ -167,10 +167,8 @@ constexpr int64_t CoveringBytes(int64_t offset, int64_t 
length) {
 
 // Returns the 'num_bits' least-significant bits of 'v'.
 static inline uint64_t TrailingBits(uint64_t v, int num_bits) {
-  if (ARROW_PREDICT_FALSE(num_bits == 0)) return 0;
   if (ARROW_PREDICT_FALSE(num_bits >= 64)) return v;
-  int n = 64 - num_bits;
-  return (v << n) >> n;
+  return ((v >> num_bits) << num_bits) ^ v;

Review Comment:
   What about `return v & ~(-1ULL << num_bits);` ?
   It enables gcc to optimize the code with bmi2 `bzhi`.
   https://godbolt.org/z/oq9zx4nhf
   And looks it's slightly faster even without bmi2.
   https://quick-bench.com/q/rgBQzUFls9IP48xm3JiRPtJoI_M



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to