AntoinePrv commented on code in PR #47515: URL: https://github.com/apache/arrow/pull/47515#discussion_r2333349221
########## 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: Arf, yes. It is not so simple to write a dumb implementation with alignments, bit shifts... I thought on top of that we could add some unpacking tests against known values (manually packed) but the data may not be very readable. ########## 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: Yes, one utility functions throws `std::invalid_argument`. -- 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