Skip to content

Commit

Permalink
ARROW-10875 [Rust] simplify simd cfg check with cfg_aliases
Browse files Browse the repository at this point in the history
Closes #8891 from houqp/qp_cfg

Authored-by: Qingping Hou <dave2008713@gmail.com>
Signed-off-by: Neville Dipale <nevilledips@gmail.com>
  • Loading branch information
houqp authored and nevi-me committed Dec 12, 2020
1 parent 0309dab commit 73b7576
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 156 deletions.
3 changes: 3 additions & 0 deletions rust/arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ criterion = "0.3"
flate2 = "1"
tempfile = "3"

[build-dependencies]
cfg_aliases = "0.1"

[[bench]]
name = "aggregate_kernels"
harness = false
Expand Down
25 changes: 25 additions & 0 deletions rust/arrow/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

use cfg_aliases::cfg_aliases;

fn main() {
// Setup cfg aliases
cfg_aliases! {
simd_x86: { all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd") },
}
}
10 changes: 5 additions & 5 deletions rust/arrow/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<T: AsRef<[u8]>> From<T> for Buffer {
/// and the `scalar_op` gets applied to remaining bytes.
/// Contrary to the non-simd version `bitwise_bin_op_helper`, the offset and length is specified in bytes
/// and this version does not support operations starting at arbitrary bit offsets.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
fn bitwise_bin_op_simd_helper<F_SIMD, F_SCALAR>(
left: &Buffer,
left_offset: usize,
Expand Down Expand Up @@ -274,7 +274,7 @@ where
/// and the `scalar_op` gets applied to remaining bytes.
/// Contrary to the non-simd version `bitwise_unary_op_helper`, the offset and length is specified in bytes
/// and this version does not support operations starting at arbitrary bit offsets.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
fn bitwise_unary_op_simd_helper<F_SIMD, F_SCALAR>(
left: &Buffer,
left_offset: usize,
Expand Down Expand Up @@ -437,7 +437,7 @@ pub(super) fn buffer_bin_and(
}
}

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
pub(super) fn buffer_bin_and(
left: &Buffer,
left_offset_in_bits: usize,
Expand Down Expand Up @@ -546,7 +546,7 @@ pub(super) fn buffer_bin_or(
}
}

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
pub(super) fn buffer_bin_or(
left: &Buffer,
left_offset_in_bits: usize,
Expand Down Expand Up @@ -603,7 +603,7 @@ pub(super) fn buffer_unary_not(
len_in_bits: usize,
) -> Buffer {
// SIMD implementation if available and byte-aligned
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
if offset_in_bits % 8 == 0 && len_in_bits % 8 == 0 {
return bitwise_unary_op_simd_helper(
&left,
Expand Down
14 changes: 7 additions & 7 deletions rust/arrow/src/compute/kernels/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn min_max_string<T: StringOffsetSizeTrait, F: Fn(&str, &str) -> bool>(

/// Returns the minimum value in the array, according to the natural order.
/// For floating point arrays any NaN values are considered to be greater than any other non-null value
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd")))]
#[cfg(not(simd_x86))]
pub fn min<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
where
T: ArrowNumericType,
Expand All @@ -77,7 +77,7 @@ where

/// Returns the maximum value in the array, according to the natural order.
/// For floating point arrays any NaN values are considered to be greater than any other non-null value
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd")))]
#[cfg(not(simd_x86))]
pub fn max<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
where
T: ArrowNumericType,
Expand Down Expand Up @@ -138,7 +138,7 @@ where
/// Returns the sum of values in the array.
///
/// Returns `None` if the array is empty or only contains null values.
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd")))]
#[cfg(not(simd_x86))]
pub fn sum<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
where
T: ArrowNumericType,
Expand Down Expand Up @@ -192,7 +192,7 @@ where
}
}

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
mod simd {
use super::is_nan;
use crate::array::{Array, PrimitiveArray};
Expand Down Expand Up @@ -537,7 +537,7 @@ mod simd {
/// Returns the sum of values in the array.
///
/// Returns `None` if the array is empty or only contains null values.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
pub fn sum<T: ArrowNumericType>(array: &PrimitiveArray<T>) -> Option<T::Native>
where
T::Native: Add<Output = T::Native>,
Expand All @@ -547,7 +547,7 @@ where
simd::simd_aggregation::<T, SumAggregate<T>>(&array)
}

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
/// Returns the minimum value in the array, according to the natural order.
/// For floating point arrays any NaN values are considered to be greater than any other non-null value
pub fn min<T: ArrowNumericType>(array: &PrimitiveArray<T>) -> Option<T::Native>
Expand All @@ -559,7 +559,7 @@ where
simd::simd_aggregation::<T, MinAggregate<T>>(&array)
}

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
/// Returns the maximum value in the array, according to the natural order.
/// For floating point arrays any NaN values are considered to be greater than any other non-null value
pub fn max<T: ArrowNumericType>(array: &PrimitiveArray<T>) -> Option<T::Native>
Expand Down
44 changes: 18 additions & 26 deletions rust/arrow/src/compute/kernels/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::buffer::Buffer;
#[cfg(feature = "simd")]
use crate::buffer::MutableBuffer;
use crate::compute::util::combine_option_bitmap;
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
use crate::compute::util::simd_load_set_invalid;
use crate::datatypes;
use crate::datatypes::ToByteSlice;
Expand Down Expand Up @@ -72,7 +72,7 @@ where
}

/// SIMD vectorized version of `signed_unary_math_op` above.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
fn simd_signed_unary_math_op<T, F>(
array: &PrimitiveArray<T>,
op: F,
Expand Down Expand Up @@ -217,7 +217,7 @@ where
}

/// SIMD vectorized version of `math_op` above.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
fn simd_math_op<T, F>(
left: &PrimitiveArray<T>,
right: &PrimitiveArray<T>,
Expand Down Expand Up @@ -269,7 +269,7 @@ where
/// SIMD vectorized version of `divide`, the divide kernel needs it's own implementation as there
/// is a need to handle situations where a divide by `0` occurs. This is complicated by `NULL`
/// slots and padding.
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
fn simd_divide<T>(
left: &PrimitiveArray<T>,
right: &PrimitiveArray<T>,
Expand Down Expand Up @@ -338,11 +338,10 @@ where
+ Div<Output = T::Native>
+ Zero,
{
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
return simd_math_op(&left, &right, |a, b| a + b);

#[allow(unreachable_code)]
math_op(left, right, |a, b| a + b)
#[cfg(not(simd_x86))]
return math_op(left, right, |a, b| a + b);
}

/// Perform `left - right` operation on two arrays. If either left or right value is null
Expand All @@ -359,11 +358,10 @@ where
+ Div<Output = T::Native>
+ Zero,
{
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
return simd_math_op(&left, &right, |a, b| a - b);

#[allow(unreachable_code)]
math_op(left, right, |a, b| a - b)
#[cfg(not(simd_x86))]
return math_op(left, right, |a, b| a - b);
}

/// Perform `-` operation on an array. If value is null then the result is also null.
Expand All @@ -372,13 +370,9 @@ where
T: datatypes::ArrowSignedNumericType,
T::Native: Neg<Output = T::Native>,
{
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
return simd_signed_unary_math_op(array, |x| -x);

#[cfg(any(
not(any(target_arch = "x86", target_arch = "x86_64")),
not(feature = "simd")
))]
#[cfg(not(simd_x86))]
return signed_unary_math_op(array, |x| -x);
}

Expand All @@ -396,11 +390,10 @@ where
+ Div<Output = T::Native>
+ Zero,
{
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
return simd_math_op(&left, &right, |a, b| a * b);

#[allow(unreachable_code)]
math_op(left, right, |a, b| a * b)
#[cfg(not(simd_x86))]
return math_op(left, right, |a, b| a * b);
}

/// Perform `left / right` operation on two arrays. If either left or right value is null
Expand All @@ -419,11 +412,10 @@ where
+ Zero
+ One,
{
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
#[cfg(simd_x86)]
return simd_divide(&left, &right);

#[allow(unreachable_code)]
math_divide(&left, &right)
#[cfg(not(simd_x86))]
return math_divide(&left, &right);
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 73b7576

Please sign in to comment.