pitrou commented on code in PR #47515:
URL: https://github.com/apache/arrow/pull/47515#discussion_r2330432955


##########
cpp/src/arrow/util/CMakeLists.txt:
##########
@@ -116,7 +117,8 @@ add_arrow_test(crc32-test
                Boost::headers)
 
 add_arrow_benchmark(bit_block_counter_benchmark)
-add_arrow_benchmark(bit_util_benchmark)
+add_arrow_benchmark(bit_util_benchmark SOURCES bit_util_benchmark.cc
+                    bpacking_benchmark.cc)

Review Comment:
   I think it's ok to have a separate benchmark target for bit-packing.



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+namespace {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {

Review Comment:
   You should use `CamelCase`.



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)

Review Comment:
   It would be better to use `ARROW_HAVE_RUNTIME_AVX2`, since that's the macro 
used in `bpacking.cc`.
   
   `ARROW_HAVE_AVX2` means AVX2 is always enabled at compile-time.
   `ARROW_HAVE_RUNTIME_AVX2` means AVX2 can be enabled at runtime based on CPU 
characteristics and environment variables. 
   
   If using `ARROW_HAVE_RUNTIME_AVX2`, you will have to skip the AVX2 benchmark 
based on `CpuInfo::IsSupported(CpuInfo::AVX2)`.



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+namespace {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+template <typename Int>
+void BM_Unpack(benchmark::State& state, bool aligned, UnpackFunc<Int> unpack) {
+  auto const bit_width = static_cast<int32_t>(state.range(0));
+  auto const num_values = static_cast<int32_t>(state.range(1));
+  constexpr int32_t kExtraValues = sizeof(Int) * 8;

Review Comment:
   Can you add a comment explaining what this is for?



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)

Review Comment:
   Same remarks as for the benchmarks: we probably want this to be enabled 
dynamically.



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+namespace {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+template <typename Int>
+void BM_Unpack(benchmark::State& state, bool aligned, UnpackFunc<Int> unpack) {
+  auto const bit_width = static_cast<int32_t>(state.range(0));
+  auto const num_values = static_cast<int32_t>(state.range(1));
+  constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+  auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+  const uint8_t* packed_ptr =
+      getNextAlignedByte(packed.data(), sizeof(Int)) + (aligned ? 0 : 1);
+
+  std::vector<Int> unpacked(num_values, 0);
+
+  for (auto _ : state) {
+    unpack(packed_ptr, unpacked.data(), num_values, bit_width);
+    benchmark::ClobberMemory();
+  }
+  state.SetItemsProcessed(num_values);

Review Comment:
   I get figures like `items_per_second=454.756/s` so this is certainly wrong. 
Perhaps:
   ```suggestion
     state.SetItemsProcessed(num_values * state.iterations());
   ```



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+namespace {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+template <typename Int>
+void BM_Unpack(benchmark::State& state, bool aligned, UnpackFunc<Int> unpack) {
+  auto const bit_width = static_cast<int32_t>(state.range(0));
+  auto const num_values = static_cast<int32_t>(state.range(1));
+  constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+  auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+  const uint8_t* packed_ptr =
+      getNextAlignedByte(packed.data(), sizeof(Int)) + (aligned ? 0 : 1);
+
+  std::vector<Int> unpacked(num_values, 0);
+
+  for (auto _ : state) {
+    unpack(packed_ptr, unpacked.data(), num_values, bit_width);
+    benchmark::ClobberMemory();
+  }
+  state.SetItemsProcessed(num_values);
+}
+
+constexpr int32_t MIN_RANGE = 64;
+constexpr int32_t MAX_RANGE = 32768;
+constexpr std::initializer_list<int64_t> kBitWidths32 = {1, 8, 20};
+constexpr std::initializer_list<int64_t> kBitWidths64 = {1, 8, 20, 47};
+static const std::vector<std::vector<int64_t>> bitWidthsNumValues32 = {
+    kBitWidths32,
+    benchmark::CreateRange(MIN_RANGE, MAX_RANGE, /*multi=*/8),
+};
+static const std::vector<std::vector<int64_t>> bitWidthsNumValues64 = {
+    kBitWidths64,
+    benchmark::CreateRange(MIN_RANGE, MAX_RANGE, /*multi=*/8),
+};

Review Comment:
   I think we can test less sizes and use multi=32.
   



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+/// Convenience wrapper to unpack into a vector
+template <typename Int>
+std::vector<Int> unpackValues(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  std::vector<Int> out(num_values);
+  int values_read = unpack(packed, out.data(), num_values, bit_width);
+  ARROW_DCHECK_GE(values_read, 0);
+  out.resize(values_read);
+  return out;
+}
+
+/// Use BitWriter to pack values into a vector.
+template <typename Int>
+std::vector<uint8_t> packValues(std::vector<Int> const& values, int32_t 
num_values,
+                                int32_t bit_width) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(static_cast<std::size_t>(num_bytes));
+  bit_util::BitWriter writer(out.data(), num_bytes);
+  for (auto const& v : values) {
+    bool written = writer.PutValue(v, bit_width);
+    if (!written) {
+      throw std::runtime_error("Cannot write move values");
+    }
+  }
+
+  return out;
+}
+
+template <typename Int>
+void checkUnpackPackRoundtrip(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  auto const unpacked = unpackValues(packed, num_values, bit_width, unpack);
+  EXPECT_EQ(unpacked.size(), num_values);
+  auto const roundtrip = packValues(unpacked, num_values, bit_width);
+  EXPECT_EQ(num_bytes, roundtrip.size());
+  for (int i = 0; i < num_bytes; ++i) {
+    EXPECT_EQ(packed[i], roundtrip[i]) << "differ in position " << i;
+  }
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+struct UnpackingData {
+  int32_t num_values;
+  int32_t bit_width;
+};
+
+class UnpackingRandomRoundTrip : public 
::testing::TestWithParam<UnpackingData> {
+ protected:
+  template <typename Int>
+  void testRoundtripAlignment(UnpackFunc<Int> unpack, std::size_t 
alignment_offset) {
+    auto [num_values, bit_width] = GetParam();
+    constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+    auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+    const uint8_t* packed_unaligned =
+        getNextAlignedByte(packed.data(), sizeof(Int)) + alignment_offset;
+
+    checkUnpackPackRoundtrip(packed_unaligned, num_values, bit_width, unpack);
+  }
+
+  template <typename Int>
+  void testRoundtrip(UnpackFunc<Int> unpack) {
+    // Aligned test
+    testRoundtripAlignment(unpack, 0);
+    // Unaligned test
+    testRoundtripAlignment(unpack, 1);
+  }
+};
+
+INSTANTIATE_TEST_SUITE_P(
+    MutpliesOf64Values, UnpackingRandomRoundTrip,
+    ::testing::Values(UnpackingData{64, 1}, UnpackingData{128, 1}, 
UnpackingData{2048, 1},
+                      UnpackingData{64, 31}, UnpackingData{128, 31},
+                      UnpackingData{2048, 31}, UnpackingData{64000, 7},
+                      UnpackingData{64000, 8}, UnpackingData{64000, 13},
+                      UnpackingData{64000, 16}, UnpackingData{64000, 31},
+                      UnpackingData{64000, 32}));
+
+TEST_P(UnpackingRandomRoundTrip, unpack32Default) {
+  this->testRoundtrip(&unpack32_default);
+}
+TEST_P(UnpackingRandomRoundTrip, unpack64Default) {
+  this->testRoundtrip(&unpack64_default);
+}
+
+#if defined(ARROW_HAVE_AVX2)
+TEST_P(UnpackingRandomRoundTrip, unpack32Avx2) { 
this->testRoundtrip(&unpack32_avx2); }
+#endif

Review Comment:
   If we enable this test dynamically, this would become something like:
   ```suggestion
   #if defined(ARROW_HAVE_RUNTIME_AVX2)
   TEST_P(UnpackingRandomRoundTrip, unpack32Avx2) {
     if (!CpuInfo::GetInstance()->IsSupported(CpuInfo::AVX2)) {
       GTEST_SKIP() << "Test requires AVX2";
     }
     this->testRoundtrip(&unpack32_avx2);
   }
   #endif
   ```



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {

Review Comment:
   Please let's stick to `CamelCase` for functions.



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+namespace {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+template <typename Int>
+void BM_Unpack(benchmark::State& state, bool aligned, UnpackFunc<Int> unpack) {
+  auto const bit_width = static_cast<int32_t>(state.range(0));
+  auto const num_values = static_cast<int32_t>(state.range(1));
+  constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+  auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+  const uint8_t* packed_ptr =
+      getNextAlignedByte(packed.data(), sizeof(Int)) + (aligned ? 0 : 1);
+
+  std::vector<Int> unpacked(num_values, 0);
+
+  for (auto _ : state) {
+    unpack(packed_ptr, unpacked.data(), num_values, bit_width);
+    benchmark::ClobberMemory();
+  }
+  state.SetItemsProcessed(num_values);
+}
+
+constexpr int32_t MIN_RANGE = 64;
+constexpr int32_t MAX_RANGE = 32768;
+constexpr std::initializer_list<int64_t> kBitWidths32 = {1, 8, 20};
+constexpr std::initializer_list<int64_t> kBitWidths64 = {1, 8, 20, 47};

Review Comment:
   We would probably like a very small non-1 width as well, I think.
   ```suggestion
   constexpr std::initializer_list<int64_t> kBitWidths32 = {1, 2, 8, 20};
   constexpr std::initializer_list<int64_t> kBitWidths64 = {1, 2, 8, 20, 47};
   ```



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+/// Convenience wrapper to unpack into a vector
+template <typename Int>
+std::vector<Int> unpackValues(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  std::vector<Int> out(num_values);
+  int values_read = unpack(packed, out.data(), num_values, bit_width);
+  ARROW_DCHECK_GE(values_read, 0);
+  out.resize(values_read);
+  return out;
+}
+
+/// Use BitWriter to pack values into a vector.

Review Comment:
   So `BitWriter` is used as a reference bit-packing implementation? 



##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,147 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)

Review Comment:
   Same here (`ARROW_HAVE_RUNTIME_AVX512` + conditional skipping in the 
benchmark body)



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);

Review Comment:
   For testing we don't care about performance and can just use `std::function`.



##########
cpp/src/arrow/util/bpacking_avx2.cc:
##########
@@ -19,13 +19,11 @@
 #include "arrow/util/bpacking_simd256_generated_internal.h"
 #include "arrow/util/bpacking_simd_internal.h"
 
-namespace arrow {
-namespace internal {
+namespace arrow::internal {
 
-int unpack32_avx2(const uint32_t* in, uint32_t* out, int batch_size, int 
num_bits) {
-  return unpack32_specialized<UnpackBits256<DispatchLevel::AVX2>>(in, out, 
batch_size,
-                                                                  num_bits);
+int unpack32_avx2(const uint8_t* in, uint32_t* out, int batch_size, int 
num_bits) {
+  return unpack32_specialized<UnpackBits256<DispatchLevel::AVX2>>(
+      reinterpret_cast<const uint32_t*>(in), out, batch_size, num_bits);

Review Comment:
   I think we can also change the signature of the generated functions, what do 
you think? The current signature assumes the SIMD functions will load the input 
in 32-bit chunks, but they might make different choices.



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+/// Convenience wrapper to unpack into a vector
+template <typename Int>
+std::vector<Int> unpackValues(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  std::vector<Int> out(num_values);
+  int values_read = unpack(packed, out.data(), num_values, bit_width);
+  ARROW_DCHECK_GE(values_read, 0);
+  out.resize(values_read);
+  return out;
+}
+
+/// Use BitWriter to pack values into a vector.
+template <typename Int>
+std::vector<uint8_t> packValues(std::vector<Int> const& values, int32_t 
num_values,
+                                int32_t bit_width) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(static_cast<std::size_t>(num_bytes));
+  bit_util::BitWriter writer(out.data(), num_bytes);
+  for (auto const& v : values) {
+    bool written = writer.PutValue(v, bit_width);
+    if (!written) {
+      throw std::runtime_error("Cannot write move values");
+    }
+  }
+
+  return out;
+}
+
+template <typename Int>
+void checkUnpackPackRoundtrip(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  auto const unpacked = unpackValues(packed, num_values, bit_width, unpack);
+  EXPECT_EQ(unpacked.size(), num_values);
+  auto const roundtrip = packValues(unpacked, num_values, bit_width);
+  EXPECT_EQ(num_bytes, roundtrip.size());
+  for (int i = 0; i < num_bytes; ++i) {
+    EXPECT_EQ(packed[i], roundtrip[i]) << "differ in position " << i;
+  }
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+struct UnpackingData {
+  int32_t num_values;
+  int32_t bit_width;
+};
+
+class UnpackingRandomRoundTrip : public 
::testing::TestWithParam<UnpackingData> {
+ protected:
+  template <typename Int>
+  void testRoundtripAlignment(UnpackFunc<Int> unpack, std::size_t 
alignment_offset) {
+    auto [num_values, bit_width] = GetParam();
+    constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+    auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);

Review Comment:
   The random data doesn't need to be generated again in each call to 
`TestRoundtripAlignement`. You can just generate it in the fixture's `SetUp` 
method.



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+/// Convenience wrapper to unpack into a vector
+template <typename Int>
+std::vector<Int> unpackValues(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  std::vector<Int> out(num_values);
+  int values_read = unpack(packed, out.data(), num_values, bit_width);
+  ARROW_DCHECK_GE(values_read, 0);
+  out.resize(values_read);
+  return out;
+}
+
+/// Use BitWriter to pack values into a vector.
+template <typename Int>
+std::vector<uint8_t> packValues(std::vector<Int> const& values, int32_t 
num_values,
+                                int32_t bit_width) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(static_cast<std::size_t>(num_bytes));
+  bit_util::BitWriter writer(out.data(), num_bytes);
+  for (auto const& v : values) {
+    bool written = writer.PutValue(v, bit_width);
+    if (!written) {
+      throw std::runtime_error("Cannot write move values");
+    }
+  }
+
+  return out;
+}
+
+template <typename Int>
+void checkUnpackPackRoundtrip(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  auto const unpacked = unpackValues(packed, num_values, bit_width, unpack);
+  EXPECT_EQ(unpacked.size(), num_values);
+  auto const roundtrip = packValues(unpacked, num_values, bit_width);
+  EXPECT_EQ(num_bytes, roundtrip.size());
+  for (int i = 0; i < num_bytes; ++i) {
+    EXPECT_EQ(packed[i], roundtrip[i]) << "differ in position " << i;
+  }
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+struct UnpackingData {
+  int32_t num_values;
+  int32_t bit_width;
+};
+
+class UnpackingRandomRoundTrip : public 
::testing::TestWithParam<UnpackingData> {
+ protected:
+  template <typename Int>
+  void testRoundtripAlignment(UnpackFunc<Int> unpack, std::size_t 
alignment_offset) {
+    auto [num_values, bit_width] = GetParam();
+    constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+    auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+    const uint8_t* packed_unaligned =
+        getNextAlignedByte(packed.data(), sizeof(Int)) + alignment_offset;
+
+    checkUnpackPackRoundtrip(packed_unaligned, num_values, bit_width, unpack);
+  }
+
+  template <typename Int>
+  void testRoundtrip(UnpackFunc<Int> unpack) {
+    // Aligned test
+    testRoundtripAlignment(unpack, 0);
+    // Unaligned test
+    testRoundtripAlignment(unpack, 1);
+  }
+};
+
+INSTANTIATE_TEST_SUITE_P(
+    MutpliesOf64Values, UnpackingRandomRoundTrip,
+    ::testing::Values(UnpackingData{64, 1}, UnpackingData{128, 1}, 
UnpackingData{2048, 1},
+                      UnpackingData{64, 31}, UnpackingData{128, 31},
+                      UnpackingData{2048, 31}, UnpackingData{64000, 7},
+                      UnpackingData{64000, 8}, UnpackingData{64000, 13},
+                      UnpackingData{64000, 16}, UnpackingData{64000, 31},
+                      UnpackingData{64000, 32}));

Review Comment:
   I don't going up to 64000 is useful, and 2048 is already a lot given that 
we're never going to have SIMD chunks as large as that (perhaps 512 is a good 
top length here?). On the other hand, we want to test non power of two sizes, 
and potentially more bit widths.



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+
+#if defined(ARROW_HAVE_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#endif
+#if defined(ARROW_HAVE_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t getNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> generateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+/// Convenience wrapper to unpack into a vector
+template <typename Int>
+std::vector<Int> unpackValues(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  std::vector<Int> out(num_values);
+  int values_read = unpack(packed, out.data(), num_values, bit_width);
+  ARROW_DCHECK_GE(values_read, 0);
+  out.resize(values_read);
+  return out;
+}
+
+/// Use BitWriter to pack values into a vector.
+template <typename Int>
+std::vector<uint8_t> packValues(std::vector<Int> const& values, int32_t 
num_values,
+                                int32_t bit_width) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(static_cast<std::size_t>(num_bytes));
+  bit_util::BitWriter writer(out.data(), num_bytes);
+  for (auto const& v : values) {
+    bool written = writer.PutValue(v, bit_width);
+    if (!written) {
+      throw std::runtime_error("Cannot write move values");
+    }
+  }
+
+  return out;
+}
+
+template <typename Int>
+void checkUnpackPackRoundtrip(const uint8_t* packed, int32_t num_values,
+                              int32_t bit_width, UnpackFunc<Int> unpack) {
+  auto const num_bytes = getNumBytes(num_values, bit_width);
+
+  auto const unpacked = unpackValues(packed, num_values, bit_width, unpack);
+  EXPECT_EQ(unpacked.size(), num_values);
+  auto const roundtrip = packValues(unpacked, num_values, bit_width);
+  EXPECT_EQ(num_bytes, roundtrip.size());
+  for (int i = 0; i < num_bytes; ++i) {
+    EXPECT_EQ(packed[i], roundtrip[i]) << "differ in position " << i;
+  }
+}
+
+const uint8_t* getNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+struct UnpackingData {
+  int32_t num_values;
+  int32_t bit_width;
+};
+
+class UnpackingRandomRoundTrip : public 
::testing::TestWithParam<UnpackingData> {
+ protected:
+  template <typename Int>
+  void testRoundtripAlignment(UnpackFunc<Int> unpack, std::size_t 
alignment_offset) {
+    auto [num_values, bit_width] = GetParam();
+    constexpr int32_t kExtraValues = sizeof(Int) * 8;
+
+    auto const packed = generateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+    const uint8_t* packed_unaligned =
+        getNextAlignedByte(packed.data(), sizeof(Int)) + alignment_offset;
+
+    checkUnpackPackRoundtrip(packed_unaligned, num_values, bit_width, unpack);
+  }
+
+  template <typename Int>
+  void testRoundtrip(UnpackFunc<Int> unpack) {
+    // Aligned test
+    testRoundtripAlignment(unpack, 0);
+    // Unaligned test
+    testRoundtripAlignment(unpack, 1);
+  }
+};
+
+INSTANTIATE_TEST_SUITE_P(
+    MutpliesOf64Values, UnpackingRandomRoundTrip,

Review Comment:
   Why "MutpliesOf64Values"?



##########
cpp/src/arrow/util/bpacking_test.cc:
##########
@@ -0,0 +1,180 @@
+// 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 <stdexcept>

Review Comment:
   Is this include actually used 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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to