emkornfield commented on a change in pull request #7029:
URL: https://github.com/apache/arrow/pull/7029#discussion_r415231122



##########
File path: cpp/src/arrow/util/spaced.h
##########
@@ -0,0 +1,266 @@
+// 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 "arrow/util/bit_util.h"
+
+#ifdef ARROW_HAVE_AVX512
+#include <immintrin.h>
+#endif  // ARROW_HAVE_AVX512
+
+namespace arrow {
+namespace util {
+namespace internal {
+
+template <typename T>
+int PutSpacedScalar(const T* values, int num_values, const uint8_t* valid_bits,
+                    int64_t valid_bits_offset, T* output) {
+  int num_valid_values = 0;
+  arrow::internal::BitmapReader valid_bits_reader(valid_bits, 
valid_bits_offset,
+                                                  num_values);
+  for (int32_t i = 0; i < num_values; i++) {
+    if (valid_bits_reader.IsSet()) {
+      output[num_valid_values++] = values[i];
+    }
+    valid_bits_reader.Next();
+  }
+  return num_valid_values;
+}
+
+template <typename T>
+int DecodeSpacedScalar(T* buffer, int num_values, int null_count,
+                       const uint8_t* valid_bits, int64_t valid_bits_offset) {
+  const int values_read = num_values - null_count;
+
+  // Depending on the number of nulls, some of the value slots in buffer may
+  // be uninitialized, and this will cause valgrind warnings / potentially UB
+  memset(static_cast<void*>(buffer + values_read), 0,
+         (num_values - values_read) * sizeof(T));
+
+  // Add spacing for null entries. As we have filled the buffer from the front,
+  // we need to add the spacing from the back.
+  int values_to_move = values_read - 1;
+  // We stop early on one of two conditions:
+  // 1. There are no more null values that need spacing.  Note we infer this
+  //     backwards, when 'i' is equal to 'values_to_move' it indicates
+  //    all nulls have been consumed.
+  // 2. There are no more non-null values that need to move which indicates
+  //    all remaining slots are null, so their exact value doesn't matter.
+  for (int i = num_values - 1; (i > values_to_move) && (values_to_move >= 0); 
i--) {
+    if (BitUtil::GetBit(valid_bits, valid_bits_offset + i)) {
+      buffer[i] = buffer[values_to_move];
+      values_to_move--;
+    }
+  }
+  return num_values;
+}
+
+#if defined(ARROW_HAVE_AVX512)
+template <typename T>
+int PutSpacedAvx512Compress(const T* values, int num_values, const uint8_t* 
valid_bits,
+                            int64_t valid_bits_offset, T* output) {
+  assert(sizeof(T) == 4 || sizeof(T) == 8);  // Only support epi32 and epi64
+  constexpr int kBatchSize = sizeof(__m512i) / sizeof(T);
+  constexpr int kBatchValidBytes = kBatchSize / 8;
+  int num_valid_values = 0;
+  int idx_values = 0;
+  int64_t idx_valid_bits = valid_bits_offset;
+
+  // First handle the front suffix
+  const int64_t offset_suffix_front = 8 - (valid_bits_offset % 8);
+  for (int64_t i = 0; (i < offset_suffix_front) && (idx_values < num_values); 
i++) {
+    if (BitUtil::GetBit(valid_bits, idx_valid_bits)) {
+      output[num_valid_values] = values[idx_values];
+      num_valid_values++;
+    }
+    idx_values++;
+    idx_valid_bits++;
+  }
+
+  // The parts can fill into batches
+  uint8_t valid_count;
+  int64_t idx_valid_bytes = BitUtil::BytesForBits(idx_valid_bits + 1) - 1;
+  static const __m512i zero = _mm512_set_epi64(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
0x0, 0x0);
+  while (num_values - idx_values >= kBatchSize) {
+    // count the valid numbers of one batch.
+    valid_count = BitUtil::kBytePopcount[valid_bits[idx_valid_bytes]];
+    if (kBatchValidBytes > 1) {
+      valid_count += BitUtil::kBytePopcount[valid_bits[idx_valid_bytes + 1]];
+    }
+
+    // pack the data
+    if (valid_count > 0) {
+      __m512i src = _mm512_loadu_si512(values + idx_values);
+      __m512i result;
+      if (sizeof(T) == 4) {
+        // 16 float for one m512i block, two bytes in valid_bits
+        __mmask16 k = *(reinterpret_cast<const __mmask16*>(valid_bits + 
idx_valid_bytes));
+        result = _mm512_mask_compress_epi32(zero, k, src);
+      } else {
+        // 8 double for one m512i block, one byte in valid_bits
+        __mmask8 k = *(valid_bits + idx_valid_bytes);
+        result = _mm512_mask_compress_epi64(zero, k, src);
+      }
+
+      memcpy(output + num_valid_values, &result, valid_count * sizeof(T));
+      num_valid_values += valid_count;
+    }
+
+    // Step the index
+    idx_values += kBatchSize;
+    idx_valid_bits += kBatchSize;
+    idx_valid_bytes += kBatchValidBytes;
+  }
+
+  // The remainging back suffix
+  while (idx_values < num_values) {

Review comment:
       same comment above about potentially using Scalar function 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.

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


Reply via email to