1616// under the License.
1717
1818#include < array>
19+ #include < type_traits>
1920
2021#include " arrow/util/bpacking_internal.h"
2122#include " arrow/util/bpacking_scalar_internal.h"
2223#include " arrow/util/bpacking_simd_internal.h"
24+ #include " arrow/util/cpu_info.h"
2325#include " arrow/util/dispatch_internal.h"
2426
2527namespace arrow ::internal {
@@ -32,11 +34,13 @@ struct UnpackDynamicFunction {
3234
3335 static constexpr auto targets () {
3436 return std::array{
35- ARROW_DISPATCH_TARGET_NONE (&bpacking::unpack_scalar<Uint>) //
36- ARROW_DISPATCH_TARGET_NEON (&bpacking::unpack_neon<Uint>) //
37+ ARROW_DISPATCH_TARGET_NONE (&bpacking::unpack_scalar<Uint>) //
38+ ARROW_DISPATCH_TARGET_NEON (&bpacking::unpack_neon<Uint>) //
39+ ARROW_DISPATCH_TARGET_RVV128 (&bpacking::unpack_rvv128<Uint>) //
3740 // GH-50503: No SVE128 dispatch as it increases code size without
3841 // increasing performance vs. Neon, and can produce ODR violations.
3942 ARROW_DISPATCH_TARGET_SVE256 (&bpacking::unpack_sve256<Uint>) //
43+ ARROW_DISPATCH_TARGET_RVV256 (&bpacking::unpack_rvv256<Uint>) //
4044 ARROW_DISPATCH_TARGET_SSE4_2 (&bpacking::unpack_sse4_2<Uint>) //
4145 ARROW_DISPATCH_TARGET_AVX2 (&bpacking::unpack_avx2<Uint>) //
4246 ARROW_DISPATCH_TARGET_AVX512 (&bpacking::unpack_avx512<Uint>) //
@@ -48,6 +52,46 @@ struct UnpackDynamicFunction {
4852
4953template <typename Uint>
5054void unpack (const uint8_t * in, Uint* out, const UnpackOptions& opts) {
55+ #if defined(ARROW_HAVE_RVV128) || defined(ARROW_HAVE_RVV256) || \
56+ defined (ARROW_HAVE_RUNTIME_RVV128 ) || \
57+ defined (ARROW_HAVE_RUNTIME_RVV256 )
58+ // On RISC-V RVV, the medium SIMD kernel is slower than the scalar
59+ // implementation (which the compiler auto-vectorizes efficiently) for several
60+ // unpack combinations. Route them directly to the scalar path to avoid both
61+ // the slow kernel and the extra SIMD dispatch indirection.
62+ constexpr bool kIsUint64 = sizeof (Uint) == sizeof (uint64_t );
63+ // Whether the runtime dispatch would target the RVV256 kernel. CpuInfo::RVV256
64+ // is set only when the hardware VLEN is 256 (see cpu_info.cc), so this is
65+ // reliable on both VLEN=128 and VLEN=256 machines.
66+ static const bool kRvv256 = CpuInfo::GetInstance ()->IsSupported (CpuInfo::RVV256 );
67+ if (opts.bit_width == 1 && kIsUint64 ) {
68+ // Uint64/1-bit regresses on all RVV VLENs.
69+ return bpacking::unpack_scalar<Uint>(in, out, opts);
70+ }
71+ if (kRvv256 ) {
72+ // The following combinations only regress on VLEN=256 hardware; on VLEN=128
73+ // they are faster than scalar and must keep using the SIMD kernel.
74+ if (kIsUint64 && opts.bit_width == 2 ) {
75+ // Uint64/2-bit regresses on VLEN=256 only.
76+ return bpacking::unpack_scalar<Uint>(in, out, opts);
77+ }
78+ // Large bit widths on small batches have too much fixed medium-kernel
79+ // overhead to beat the scalar path on VLEN=256.
80+ if constexpr (std::is_same_v<Uint, uint16_t >) {
81+ if (opts.bit_width == 13 && opts.batch_size <= 32 ) {
82+ return bpacking::unpack_scalar<Uint>(in, out, opts);
83+ }
84+ } else if constexpr (std::is_same_v<Uint, uint32_t >) {
85+ if (opts.bit_width == 20 && opts.batch_size <= 32 ) {
86+ return bpacking::unpack_scalar<Uint>(in, out, opts);
87+ }
88+ } else if constexpr (std::is_same_v<Uint, uint64_t >) {
89+ if (opts.bit_width == 20 && opts.batch_size <= 64 ) {
90+ return bpacking::unpack_scalar<Uint>(in, out, opts);
91+ }
92+ }
93+ }
94+ #endif
5195 static const DynamicDispatch<UnpackDynamicFunction<Uint>> dispatch;
5296 return dispatch (in, out, opts);
5397}
0 commit comments