Skip to content

Commit f018aae

Browse files
committed
Pack wide primitive array comparisons with NEON
Use the existing fill-and-pack collector for i64, u64, and f64 array pairs on AArch64 with NEON. Keep this dispatch and its bounds proof inside the private primitive comparison kernel. Extend word-boundary coverage to wide types with sliced inputs and exercise floating-point total ordering through full words and a tail. Signed-off-by: Connor Tsui <[email protected]>
1 parent a59294e commit f018aae

2 files changed

Lines changed: 55 additions & 14 deletions

File tree

‎vortex-array/src/scalar_fn/fns/binary/compare/primitive.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ fn collect_zip_bits_dispatch<T: NativePType>(
168168
f: impl Fn(T, T) -> bool,
169169
allocator: &BufferAllocatorRef,
170170
) -> BitBuffer {
171+
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
172+
if matches!(T::PTYPE, PType::I64 | PType::U64 | PType::F64) {
173+
// Fill-and-pack improves wide array pairs on NEON. Constants keep their existing loop.
174+
return collect_zip_bits_wide(lhs, rhs, f, allocator);
175+
}
176+
171177
if matches!(T::PTYPE, PType::I8 | PType::U8 | PType::F32) {
172178
collect_zip_bits_narrow(lhs, rhs, f, allocator)
173179
} else {
@@ -211,3 +217,23 @@ fn collect_zip_bits_narrow<T: Copy>(
211217
}
212218
bit_buffer_from_words(words, lhs.len())
213219
}
220+
221+
/// Keep the predicate free of bounds checks so NEON can fuse comparison and bitmap packing.
222+
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
223+
fn collect_zip_bits_wide<T: Copy>(
224+
lhs: &[T],
225+
rhs: &[T],
226+
f: impl Fn(T, T) -> bool,
227+
allocator: &BufferAllocatorRef,
228+
) -> BitBuffer {
229+
assert_eq!(lhs.len(), rhs.len());
230+
BitBuffer::collect_bool_multiversioned_in(
231+
lhs.len(),
232+
|index| {
233+
// SAFETY: the collector calls with `index < lhs.len()`, and the slice lengths match.
234+
let (lhs, rhs) = unsafe { (*lhs.get_unchecked(index), *rhs.get_unchecked(index)) };
235+
f(lhs, rhs)
236+
},
237+
allocator.clone(),
238+
)
239+
}

‎vortex-array/src/scalar_fn/fns/binary/compare/tests.rs‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -449,23 +449,35 @@ fn int_constant_lhs_and_rhs(#[case] op: Operator, #[case] expected: [bool; 4]) {
449449

450450
/// Floats compare with Vortex's total ordering: NaN is the largest value, equality is bitwise,
451451
/// and -0.0 < +0.0. This matches `Scalar` comparison semantics.
452-
#[test]
453-
fn float_total_order() {
452+
#[rstest]
453+
fn float_total_order(#[values(4, 129)] len: usize) {
454454
let mut ctx = SESSION.create_execution_ctx();
455-
let lhs = buffer![f64::NAN, f64::NAN, -0.0f64, 1.0].into_array();
456-
let rhs = buffer![f64::NAN, f64::INFINITY, 0.0f64, f64::NAN].into_array();
455+
let lhs = PrimitiveArray::from_iter(
456+
[f64::NAN, f64::NAN, -0.0, 1.0]
457+
.into_iter()
458+
.cycle()
459+
.take(len),
460+
)
461+
.into_array();
462+
let rhs = PrimitiveArray::from_iter(
463+
[f64::NAN, f64::INFINITY, 0.0, f64::NAN]
464+
.into_iter()
465+
.cycle()
466+
.take(len),
467+
)
468+
.into_array();
457469

458470
let result = execute_compare_test(lhs.clone(), rhs.clone(), Operator::Eq);
459471
assert_arrays_eq!(
460472
result,
461-
BoolArray::from_iter([true, false, false, false]),
473+
BoolArray::from_iter([true, false, false, false].into_iter().cycle().take(len)),
462474
&mut ctx
463475
);
464476

465477
let result = execute_compare_test(lhs, rhs, Operator::Lt);
466478
assert_arrays_eq!(
467479
result,
468-
BoolArray::from_iter([false, false, true, true]),
480+
BoolArray::from_iter([false, false, true, true].into_iter().cycle().take(len)),
469481
&mut ctx
470482
);
471483
}
@@ -906,8 +918,8 @@ fn struct_of_map_compare() -> VortexResult<()> {
906918
}
907919

908920
#[rstest]
909-
fn byte_comparisons_across_bitmap_words(
910-
#[values(PType::I8, PType::U8)] ptype: PType,
921+
fn primitive_comparisons_across_bitmap_words(
922+
#[values(PType::I8, PType::U8, PType::I64, PType::U64, PType::F64)] ptype: PType,
911923
#[values(0, 1, 63, 64, 65, 129)] len: usize,
912924
#[values(
913925
CompareOperator::Eq,
@@ -950,16 +962,19 @@ fn byte_comparisons_across_bitmap_words(
950962
.map(value_for_type)
951963
.collect();
952964
let dtype = DType::Primitive(ptype, Nullability::Nullable);
953-
let lhs = PrimitiveArray::from_option_iter(left.iter().copied())
965+
let lhs = PrimitiveArray::from_option_iter([None].into_iter().chain(left.iter().copied()))
954966
.into_array()
955967
.cast(dtype.clone())?
956968
.execute::<PrimitiveArray>(&mut ctx)?
957-
.into_array();
958-
let rhs = PrimitiveArray::from_option_iter(right.iter().copied())
959969
.into_array()
960-
.cast(dtype.clone())?
961-
.execute::<PrimitiveArray>(&mut ctx)?
962-
.into_array();
970+
.slice(1..len + 1)?;
971+
let rhs =
972+
PrimitiveArray::from_option_iter([None, None].into_iter().chain(right.iter().copied()))
973+
.into_array()
974+
.cast(dtype.clone())?
975+
.execute::<PrimitiveArray>(&mut ctx)?
976+
.into_array()
977+
.slice(2..len + 2)?;
963978
let predicate = |a: i16, b: i16| match op {
964979
CompareOperator::Eq => a == b,
965980
CompareOperator::NotEq => a != b,

0 commit comments

Comments
 (0)