tadeja commented on code in PR #53946:
URL: https://github.com/apache/arrow/pull/53946#discussion_r4432737819


##########
cpp/src/arrow/util/bit_util.h:
##########
@@ -138,20 +138,41 @@ static inline uint64_t TrailingBits(uint64_t v, int 
num_bits) {
   return (v << n) >> n;
 }
 
-// Returns ceil(log2(x)).
-static inline int Log2(uint64_t x) {
-  // DCHECK_GT(x, 0);
+// Returns the number of bits needed to represent x, i.e. std::bit_width(x).
+//
+// The libc++ in the macOS 11.3 SDK used by CRAN only has the pre-standard
+// name std::log2p1. __apple_build_version__ should be defined only on Apple
+// clang.
+// TODO: We can remove this condition once CRAN upgrades its macOS SDK from 
11.3.
+template <typename T>
+constexpr int BitWidth(T x) noexcept {
+  static_assert(std::is_unsigned_v<T>, "BitWidth requires an unsigned integer 
type");
+#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops)
+  return static_cast<int>(std::log2p1(x));
+#else
+  return static_cast<int>(std::bit_width(x));
+#endif
+}
 
-// TODO: We can remove this condition once CRAN upgrades its macOS
-// SDK from 11.3.
-// __apple_build_version__ should be defined only on Apple clang
+// Returns true if x is a power of two, i.e. std::has_single_bit(x).
+// See the note on BitWidth: the macOS 11.3 SDK only has the pre-standard
+// name std::ispow2.
+template <typename T>
+constexpr bool HasSingleBit(T x) noexcept {
+  static_assert(std::is_unsigned_v<T>, "HasSingleBit requires an unsigned 
integer type");
 #if defined(__apple_build_version__) && !defined(__cpp_lib_bitops)
-  return std::log2p1(x - 1);
+  return std::ispow2(x);
 #else
-  return std::bit_width(x - 1);
+  return std::has_single_bit(x);
 #endif
 }
 
+// Returns ceil(log2(x)).
+static inline int Log2(uint64_t x) {
+  // DCHECK_GT(x, 0);

Review Comment:
   nothing here :)



-- 
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