Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/src/arrow/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ add_arrow_test(async-utility-test
add_arrow_test(bit-utility-test
SOURCES
bit_block_counter_test.cc
bit_util_feature_test.cc
Comment thread
1fanwang marked this conversation as resolved.
Resolved
bit_util_test.cc
bitmap_test.cc
bpacking_test.cc
Expand Down
12 changes: 8 additions & 4 deletions cpp/src/arrow/util/bit_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ static inline uint64_t TrailingBits(uint64_t v, int num_bits) {
// 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.
// names std::log2p1 and std::ispow2. __apple_build_version__ should be defined
// only on Apple clang. The macOS 12 SDKs have std::bit_width and
// std::has_single_bit but leave __cpp_lib_bitops undefined, so use
// __cpp_lib_int_pow2, their feature-test macro.
// 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)
#if defined(__apple_build_version__) && \
(!defined(__cpp_lib_int_pow2) || __cpp_lib_int_pow2 < 202002L)
return static_cast<int>(std::log2p1(x));
#else
return static_cast<int>(std::bit_width(x));
Expand All @@ -160,7 +163,8 @@ constexpr int BitWidth(T x) noexcept {
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)
#if defined(__apple_build_version__) && \
(!defined(__cpp_lib_int_pow2) || __cpp_lib_int_pow2 < 202002L)
return std::ispow2(x);
#else
return std::has_single_bit(x);
Expand Down
38 changes: 38 additions & 0 deletions cpp/src/arrow/util/bit_util_feature_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <bit>

#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
// Simulate the macOS 12 SDK, which defines int_pow2 but not bitops.
# undef __apple_build_version__
# define __apple_build_version__ 1
# undef __cpp_lib_bitops
#endif

#include "arrow/util/bit_util.h"

#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
static_assert(arrow::bit_util::BitWidth(5U) == 3);
static_assert(arrow::bit_util::HasSingleBit(8U));
#endif

#if defined(__apple_build_version__) && \
(!defined(__cpp_lib_int_pow2) || __cpp_lib_int_pow2 < 202002L)
[[maybe_unused]] bool LegacyAppleHelpersCompile() {
return arrow::bit_util::BitWidth(1U) == 1 && arrow::bit_util::HasSingleBit(1U);
}
#endif
1 change: 1 addition & 0 deletions cpp/src/arrow/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ util_tests = {
'arrow-bit-utility-test': {
'sources': [
'bit_block_counter_test.cc',
'bit_util_feature_test.cc',
'bit_util_test.cc',
'bitmap_test.cc',
'bpacking_test.cc',
Expand Down
Loading