diff --git a/cpp/src/arrow/util/CMakeLists.txt b/cpp/src/arrow/util/CMakeLists.txt index cb516a4d84b..48675e28aff 100644 --- a/cpp/src/arrow/util/CMakeLists.txt +++ b/cpp/src/arrow/util/CMakeLists.txt @@ -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 bit_util_test.cc bitmap_test.cc bpacking_test.cc diff --git a/cpp/src/arrow/util/bit_util.h b/cpp/src/arrow/util/bit_util.h index 9d5b715a000..7163c00adae 100644 --- a/cpp/src/arrow/util/bit_util.h +++ b/cpp/src/arrow/util/bit_util.h @@ -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 constexpr int BitWidth(T x) noexcept { static_assert(std::is_unsigned_v, "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(std::log2p1(x)); #else return static_cast(std::bit_width(x)); @@ -160,7 +163,8 @@ constexpr int BitWidth(T x) noexcept { template constexpr bool HasSingleBit(T x) noexcept { static_assert(std::is_unsigned_v, "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); diff --git a/cpp/src/arrow/util/bit_util_feature_test.cc b/cpp/src/arrow/util/bit_util_feature_test.cc new file mode 100644 index 00000000000..0eb8a300984 --- /dev/null +++ b/cpp/src/arrow/util/bit_util_feature_test.cc @@ -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 + +#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 diff --git a/cpp/src/arrow/util/meson.build b/cpp/src/arrow/util/meson.build index 96cbcfa2f5e..a4e9a9181c8 100644 --- a/cpp/src/arrow/util/meson.build +++ b/cpp/src/arrow/util/meson.build @@ -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',