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


##########
cpp/apidoc/Doxyfile:
##########
@@ -1095,6 +1095,7 @@ EXCLUDE_PATTERNS       = *-test.cc \
                          *test* \
                          *_generated.h \
                          *-benchmark.cc \
+                         *_codegen.py \

Review Comment:
   I'm surprised, does Doxygen actually look at `*.py` files?



##########
cpp/src/arrow/util/bpacking_dispatch_internal.h:
##########
@@ -0,0 +1,104 @@
+// 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.
+
+#pragma once
+
+#include <cstring>
+#include <utility>
+
+#include "arrow/util/endian.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/ubsan.h"
+
+namespace arrow::internal {
+
+/// Unpack a zero bit packed array.
+template <typename Uint>
+int unpack_null(const uint8_t* in, Uint* out, int batch_size) {
+  std::memset(out, 0, batch_size * sizeof(Uint));
+  return batch_size;
+}
+
+/// Unpack a packed array where packed and unpacked values have exactly the 
same number of
+/// bits.
+template <typename Uint>
+int unpack_full(const uint8_t* in, Uint* out, int batch_size) {
+  if constexpr (ARROW_LITTLE_ENDIAN == 1) {
+    std::memcpy(out, in, batch_size * sizeof(Uint));
+  } else {
+    using bit_util::FromLittleEndian;
+    using util::SafeLoadAs;
+
+    for (int k = 0; k < batch_size; k += 1) {
+      out[k] = FromLittleEndian(SafeLoadAs<Uint>(in + (k * sizeof(Uint))));
+    }
+  }
+  return batch_size;
+}
+
+/// Unpack a packed array, delegating to a Unpacker struct.
+///
+/// @tparam kPackedBitWidth The width in bits of the values in the packed 
array.
+/// @tparam Unpacker The struct providing information and an ``unpack`` method 
to unpack a
+///                  fixed amount of values (usually constrained by SIMD batch 
sizes and
+///                  byte alignment).
+/// @tparam UnpackedUInt The type in which we unpack the values.
+template <int kPackedBitWidth, template <typename, int> typename Unpacker,
+          typename UnpackedUInt>
+int unpack_width(const uint8_t* in, UnpackedUInt* out, int batch_size) {
+  using UnpackerForWidth = Unpacker<UnpackedUInt, kPackedBitWidth>;
+
+  constexpr auto kValuesUnpacked = UnpackerForWidth::kValuesUnpacked;

Review Comment:
   Should we `ARROW_DCHECK_EQ(batch_size % kValuesUnpacked, 0)`?



##########
cpp/src/arrow/util/bpacking_scalar_generated_internal.h:
##########
@@ -0,0 +1,5832 @@
+// 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.
+
+// This file was generated by script which is modified from its original 
version in GitHub.
+// Original source:
+// 
https://github.com/lemire/FrameOfReference/blob/master/scripts/turbopacking64.py
+// The original copyright notice follows.
+
+// This code is released under the
+// Apache License Version 2.0 http://www.apache.org/licenses/.
+// (c) Daniel Lemire 2013
+
+// WARNING: this file is generated, DO NOT EDIT.
+// Usage:
+//   python cpp/src/arrow/util/bpacking_scalar_codegen.py
+
+#pragma once
+
+#include <cstdint>
+#include <cstring>
+
+#include "arrow/util/endian.h"
+#include "arrow/util/ubsan.h"
+
+namespace arrow::internal {
+
+template <typename Int>

Review Comment:
   Perhaps try to put all this in the anonymous namespace to allow for 
potentially more optimizations?



##########
cpp/src/arrow/util/bpacking_scalar_internal.h:
##########
@@ -23,7 +23,20 @@
 
 namespace arrow::internal {
 
-ARROW_EXPORT int unpack32_neon(const uint8_t* in, uint32_t* out, int 
batch_size,
+template <typename Uint>
+ARROW_EXPORT int unpack_scalar(const uint8_t* in, Uint* out, int batch_size,
                                int num_bits);
 
+extern template ARROW_TEMPLATE_EXPORT int unpack_scalar<uint8_t>(const 
uint8_t*, uint8_t*,
+                                                                 int, int);

Review Comment:
   Same remark here wrt. parameter names.



##########
cpp/src/arrow/util/bpacking_scalar_generated_internal.h:
##########


Review Comment:
   Can you update the `.gitattributes` file so that this file is considered 
generated?



##########
cpp/src/arrow/util/bpacking_internal.h:
##########
@@ -23,17 +23,19 @@
 
 namespace arrow::internal {
 
-/// The scalar 32 bit unpacking.
-ARROW_EXPORT int unpack32_scalar(const uint8_t* in, uint32_t* out, int 
batch_size,
-                                 int num_bits);
-
-/// The scalar 64 bit unpacking.
-ARROW_EXPORT int unpack64_scalar(const uint8_t* in, uint64_t* out, int 
batch_size,
-                                 int num_bits);
-
-ARROW_EXPORT
-int unpack32(const uint8_t* in, uint32_t* out, int batch_size, int num_bits);
-ARROW_EXPORT
-int unpack64(const uint8_t* in, uint64_t* out, int batch_size, int num_bits);
+template <typename Uint>
+ARROW_EXPORT int unpack(const uint8_t* in, Uint* out, int batch_size, int 
num_bits);
+
+extern template ARROW_TEMPLATE_EXPORT int unpack<uint8_t>(const uint8_t*, 
uint8_t*, int,
+                                                          int);

Review Comment:
   Could we keep the parameter names explicit in the signature? The last two 
`int`s especially are not self-describing.



##########
cpp/src/arrow/CMakeLists.txt:
##########
@@ -490,6 +490,8 @@ set(ARROW_UTIL_SRCS
     util/bitmap_builders.cc
     util/bitmap_ops.cc
     util/bpacking.cc
+    util/bpacking_scalar.cc
+    util/bpacking_simd_min.cc

Review Comment:
   What is "min" for?



##########
cpp/src/arrow/util/bpacking.cc:
##########
@@ -15,381 +15,53 @@
 // specific language governing permissions and limitations
 // under the License.
 
-#include "arrow/util/bpacking_internal.h"
+#include <array>
 
-#include "arrow/util/bpacking64_default_internal.h"
-#include "arrow/util/bpacking_default_internal.h"
-#include "arrow/util/cpu_info.h"
+#include "arrow/util/bpacking_dispatch_internal.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/bpacking_scalar_internal.h"
+#include "arrow/util/bpacking_simd_internal.h"
 #include "arrow/util/dispatch_internal.h"
-#include "arrow/util/logging_internal.h"
-
-#if defined(ARROW_HAVE_RUNTIME_AVX2)
-#  include "arrow/util/bpacking_avx2_internal.h"
-#endif
-#if defined(ARROW_HAVE_RUNTIME_AVX512)
-#  include "arrow/util/bpacking_avx512_internal.h"
-#endif
-#if defined(ARROW_HAVE_NEON)
-#  include "arrow/util/bpacking_neon_internal.h"
-#endif
-
-namespace arrow {
-namespace internal {
-
-int unpack32_scalar(const uint8_t* in_, uint32_t* out, int batch_size, int 
num_bits) {
-  const uint32_t* in = reinterpret_cast<const uint32_t*>(in_);
 
-  batch_size = batch_size / 32 * 32;
-  int num_loops = batch_size / 32;
-
-  switch (num_bits) {
-    case 0:
-      for (int i = 0; i < num_loops; ++i) in = nullunpacker32(in, out + i * 
32);
-      break;
-    case 1:
-      for (int i = 0; i < num_loops; ++i) in = unpack1_32(in, out + i * 32);
-      break;
-    case 2:
-      for (int i = 0; i < num_loops; ++i) in = unpack2_32(in, out + i * 32);
-      break;
-    case 3:
-      for (int i = 0; i < num_loops; ++i) in = unpack3_32(in, out + i * 32);
-      break;
-    case 4:
-      for (int i = 0; i < num_loops; ++i) in = unpack4_32(in, out + i * 32);
-      break;
-    case 5:
-      for (int i = 0; i < num_loops; ++i) in = unpack5_32(in, out + i * 32);
-      break;
-    case 6:
-      for (int i = 0; i < num_loops; ++i) in = unpack6_32(in, out + i * 32);
-      break;
-    case 7:
-      for (int i = 0; i < num_loops; ++i) in = unpack7_32(in, out + i * 32);
-      break;
-    case 8:
-      for (int i = 0; i < num_loops; ++i) in = unpack8_32(in, out + i * 32);
-      break;
-    case 9:
-      for (int i = 0; i < num_loops; ++i) in = unpack9_32(in, out + i * 32);
-      break;
-    case 10:
-      for (int i = 0; i < num_loops; ++i) in = unpack10_32(in, out + i * 32);
-      break;
-    case 11:
-      for (int i = 0; i < num_loops; ++i) in = unpack11_32(in, out + i * 32);
-      break;
-    case 12:
-      for (int i = 0; i < num_loops; ++i) in = unpack12_32(in, out + i * 32);
-      break;
-    case 13:
-      for (int i = 0; i < num_loops; ++i) in = unpack13_32(in, out + i * 32);
-      break;
-    case 14:
-      for (int i = 0; i < num_loops; ++i) in = unpack14_32(in, out + i * 32);
-      break;
-    case 15:
-      for (int i = 0; i < num_loops; ++i) in = unpack15_32(in, out + i * 32);
-      break;
-    case 16:
-      for (int i = 0; i < num_loops; ++i) in = unpack16_32(in, out + i * 32);
-      break;
-    case 17:
-      for (int i = 0; i < num_loops; ++i) in = unpack17_32(in, out + i * 32);
-      break;
-    case 18:
-      for (int i = 0; i < num_loops; ++i) in = unpack18_32(in, out + i * 32);
-      break;
-    case 19:
-      for (int i = 0; i < num_loops; ++i) in = unpack19_32(in, out + i * 32);
-      break;
-    case 20:
-      for (int i = 0; i < num_loops; ++i) in = unpack20_32(in, out + i * 32);
-      break;
-    case 21:
-      for (int i = 0; i < num_loops; ++i) in = unpack21_32(in, out + i * 32);
-      break;
-    case 22:
-      for (int i = 0; i < num_loops; ++i) in = unpack22_32(in, out + i * 32);
-      break;
-    case 23:
-      for (int i = 0; i < num_loops; ++i) in = unpack23_32(in, out + i * 32);
-      break;
-    case 24:
-      for (int i = 0; i < num_loops; ++i) in = unpack24_32(in, out + i * 32);
-      break;
-    case 25:
-      for (int i = 0; i < num_loops; ++i) in = unpack25_32(in, out + i * 32);
-      break;
-    case 26:
-      for (int i = 0; i < num_loops; ++i) in = unpack26_32(in, out + i * 32);
-      break;
-    case 27:
-      for (int i = 0; i < num_loops; ++i) in = unpack27_32(in, out + i * 32);
-      break;
-    case 28:
-      for (int i = 0; i < num_loops; ++i) in = unpack28_32(in, out + i * 32);
-      break;
-    case 29:
-      for (int i = 0; i < num_loops; ++i) in = unpack29_32(in, out + i * 32);
-      break;
-    case 30:
-      for (int i = 0; i < num_loops; ++i) in = unpack30_32(in, out + i * 32);
-      break;
-    case 31:
-      for (int i = 0; i < num_loops; ++i) in = unpack31_32(in, out + i * 32);
-      break;
-    case 32:
-      for (int i = 0; i < num_loops; ++i) in = unpack32_32(in, out + i * 32);
-      break;
-    default:
-      DCHECK(false) << "Unsupported num_bits";
-  }
-
-  return batch_size;
-}
+namespace arrow::internal {
 
 namespace {
 
-struct Unpack32DynamicFunction {
-  using FunctionType = decltype(&unpack32_scalar);
+template <typename Uint>
+struct UnpackDynamicFunction {
+  using FunctionType = decltype(&unpack_scalar<Uint>);
+  using Implementation = std::pair<DispatchLevel, FunctionType>;
 
-  static std::vector<std::pair<DispatchLevel, FunctionType>> implementations() 
{
-    return {{DispatchLevel::NONE, unpack32_scalar}
+  static auto implementations() {

Review Comment:
   Nit: this might be `constexpr`?



##########
cpp/src/arrow/util/bit_stream_utils_internal.h:
##########
@@ -296,6 +296,18 @@ inline bool BitReader::GetValue(int num_bits, T* v) {
   return GetBatch(num_bits, v, 1) == 1;
 }
 
+namespace internal_bit_reader {
+template <typename T>
+struct unpack_detect {
+  using type = std::make_unsigned_t<T>;
+};
+
+template <>
+struct unpack_detect<bool> {
+  using type = uint8_t;
+};

Review Comment:
   This seems to assume that `bool` always occupies a single byte in memory, 
but the C++ standard [does not 
guarantee](https://en.cppreference.com/w/cpp/language/types.html#Boolean_type) 
anything about it:
   > The value of sizeof(bool) is implementation defined and might differ from 
1. 



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