From d0eb576d3149f3a1ce1cd1b740fd91a6b03c09b2 Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Tue, 25 Aug 2026 13:00:45 -0400 Subject: [PATCH 1/4] GH-49841: [C++] Fix macOS 12.3 SDK build by using __cpp_lib_int_pow2 The macOS 12.3 SDK build failed with "no member named 'log2p1' in namespace 'std'". The Apple clang workaround selects std::log2p1 when __cpp_lib_bitops is undefined, but that macro covers popcount and friends, not std::bit_width. Use __cpp_lib_int_pow2, which covers std::bit_width and std::has_single_bit and is undefined on 11.3, defined on 12.3. Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/arrow/util/align_util.h | 3 ++- cpp/src/arrow/util/bit_util.h | 3 ++- cpp/src/arrow/util/rle_encoding_test.cc | 3 ++- cpp/src/parquet/chunker_internal.cc | 3 ++- cpp/src/parquet/encoder.cc | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/util/align_util.h b/cpp/src/arrow/util/align_util.h index 0b23c6d77bd9..e8ed44467b4b 100644 --- a/cpp/src/arrow/util/align_util.h +++ b/cpp/src/arrow/util/align_util.h @@ -47,7 +47,8 @@ inline BitmapWordAlignParams BitmapWordAlign(const uint8_t* data, int64_t bit_of // 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 -#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops) + // std::has_single_bit is guarded by __cpp_lib_int_pow2, not __cpp_lib_bitops +#if defined(__apple_build_version__) && !defined(__cpp_lib_int_pow2) static_assert((ALIGN_IN_BYTES != 0) && ((ALIGN_IN_BYTES & (ALIGN_IN_BYTES - 1)) == 0), "ALIGN_IN_BYTES should be a positive power of two"); #else diff --git a/cpp/src/arrow/util/bit_util.h b/cpp/src/arrow/util/bit_util.h index 971b77d69798..93d38b49dcf6 100644 --- a/cpp/src/arrow/util/bit_util.h +++ b/cpp/src/arrow/util/bit_util.h @@ -145,7 +145,8 @@ static inline int Log2(uint64_t x) { // 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 -#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops) +// std::bit_width is guarded by __cpp_lib_int_pow2, not __cpp_lib_bitops +#if defined(__apple_build_version__) && !defined(__cpp_lib_int_pow2) return std::log2p1(x - 1); #else return std::bit_width(x - 1); diff --git a/cpp/src/arrow/util/rle_encoding_test.cc b/cpp/src/arrow/util/rle_encoding_test.cc index 16b702c23d49..35a03cfd128f 100644 --- a/cpp/src/arrow/util/rle_encoding_test.cc +++ b/cpp/src/arrow/util/rle_encoding_test.cc @@ -945,7 +945,8 @@ TEST(BitRle, Random) { // 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 -#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops) + // std::bit_width is guarded by __cpp_lib_int_pow2, not __cpp_lib_bitops +#if defined(__apple_build_version__) && !defined(__cpp_lib_int_pow2) if (!CheckRoundTrip(values, std::log2p1(values.size()))) { #else if (!CheckRoundTrip(values, std::bit_width(values.size()))) { diff --git a/cpp/src/parquet/chunker_internal.cc b/cpp/src/parquet/chunker_internal.cc index 794075b73379..c94a0fc4be89 100644 --- a/cpp/src/parquet/chunker_internal.cc +++ b/cpp/src/parquet/chunker_internal.cc @@ -89,7 +89,8 @@ uint64_t CalculateMask(int64_t min_chunk_size, int64_t max_chunk_size, int norm_ // 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 -#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops) + // std::bit_width is guarded by __cpp_lib_int_pow2, not __cpp_lib_bitops +#if defined(__apple_build_version__) && !defined(__cpp_lib_int_pow2) auto target_bits = std::log2p1(static_cast(target_size)); #else auto target_bits = std::bit_width(static_cast(target_size)); diff --git a/cpp/src/parquet/encoder.cc b/cpp/src/parquet/encoder.cc index 3e469df277b8..ae3664885949 100644 --- a/cpp/src/parquet/encoder.cc +++ b/cpp/src/parquet/encoder.cc @@ -1172,7 +1172,8 @@ void DeltaBitPackEncoder::FlushBlock() { // 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 -#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops) + // std::bit_width is guarded by __cpp_lib_int_pow2, not __cpp_lib_bitops +#if defined(__apple_build_version__) && !defined(__cpp_lib_int_pow2) const auto bit_width = bit_width_data[i] = std::log2p1(static_cast(max_delta) - static_cast(min_delta)); #else From 492c867259a0f1326e186b8a3b8eef3916dde9d4 Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Wed, 25 Nov 2026 09:17:45 -0500 Subject: [PATCH 2/4] GH-49841: [C++] Test macOS 12 feature macro Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/arrow/util/CMakeLists.txt | 1 + cpp/src/arrow/util/bit_util.h | 14 ++++++---- cpp/src/arrow/util/bit_util_feature_test.cc | 31 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 cpp/src/arrow/util/bit_util_feature_test.cc diff --git a/cpp/src/arrow/util/CMakeLists.txt b/cpp/src/arrow/util/CMakeLists.txt index cb516a4d84b4..48675e28aff7 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 6e003e21d2d8..7163c00adaef 100644 --- a/cpp/src/arrow/util/bit_util.h +++ b/cpp/src/arrow/util/bit_util.h @@ -141,15 +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. The macOS 12 SDKs have std::bit_width but leave __cpp_lib_bitops -// undefined, so the check uses __cpp_lib_int_pow2, the feature-test macro for -// std::bit_width and std::has_single_bit. +// 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_int_pow2) +#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)); @@ -162,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_int_pow2) +#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 000000000000..f2acfdf9ba55 --- /dev/null +++ b/cpp/src/arrow/util/bit_util_feature_test.cc @@ -0,0 +1,31 @@ +// 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 From 6b8ea6e549ef93c1153bc2cc5b5788bf239a6673 Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Wed, 25 Nov 2026 09:23:39 -0500 Subject: [PATCH 3/4] GH-49841: [C++] Register feature test with Meson Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/arrow/util/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/src/arrow/util/meson.build b/cpp/src/arrow/util/meson.build index 96cbcfa2f5ef..a4e9a9181c87 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', From 3fb3b8b4a9c5f6aee275942fce1da037bf3a14bc Mon Sep 17 00:00:00 2001 From: 1fanwang <1fannnw@gmail.com> Date: Wed, 25 Nov 2026 09:30:05 -0500 Subject: [PATCH 4/4] GH-49841: [C++] Cover legacy Apple SDK helpers Signed-off-by: 1fanwang <1fannnw@gmail.com> --- cpp/src/arrow/util/bit_util_feature_test.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cpp/src/arrow/util/bit_util_feature_test.cc b/cpp/src/arrow/util/bit_util_feature_test.cc index f2acfdf9ba55..0eb8a3009841 100644 --- a/cpp/src/arrow/util/bit_util_feature_test.cc +++ b/cpp/src/arrow/util/bit_util_feature_test.cc @@ -29,3 +29,10 @@ 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