Skip to content

Implement vertical min/max ops #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions coresimd/ppsv/api/minmax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//! Lane-wise arithmetic operations.
#![allow(unused)]

macro_rules! impl_int_minmax_ops {
($id:ident) => {
impl $id {
// Note:
//
// * if two elements are equal min returns
// always the second element
// * if two elements are equal max returns
// always the second element
//
// Since we are dealing with integers here, and `min` and `max`
// construct a new integer vector, whether the first or the
// second element is returned when two elements compare equal
// does not matter.

/// Minimum of two vectors.
///
/// Returns a new vector containing the minimum value of each of
/// the input vector lanes.
#[inline]
pub fn min(self, x: Self) -> Self {
self.lt(x).select(self, x)
}

/// Maximum of two vectors.
///
/// Returns a new vector containing the minimum value of each of
/// the input vector lanes.
#[inline]
pub fn max(self, x: Self) -> Self {
self.gt(x).select(self, x)
}
}
};
}

#[cfg(test)]
macro_rules! test_int_minmax_ops {
($id:ident, $elem_ty:ident) => {
#[test]
fn minmax() {
use coresimd::simd::$id;
let o = $id::splat(1 as $elem_ty);
let t = $id::splat(2 as $elem_ty);

let mut m = o;
for i in 0..$id::lanes() {
if i % 2 == 0 {
m = m.replace(i, 2 as $elem_ty);
}
}

assert_eq!(o.min(t), o);
assert_eq!(t.min(o), o);
assert_eq!(m.min(o), o);
assert_eq!(o.min(m), o);
assert_eq!(m.min(t), m);
assert_eq!(t.min(m), m);

assert_eq!(o.max(t), t);
assert_eq!(t.max(o), t);
assert_eq!(m.max(o), m);
assert_eq!(o.max(m), m);
assert_eq!(m.max(t), t);
assert_eq!(t.max(m), t);
}
};
}

macro_rules! impl_float_minmax_ops {
($id:ident) => {
impl $id {
/// Minimum of two vectors.
///
/// Returns a new vector containing the minimum value of each of the
/// input vector lanes. The lane-wise semantics are the same as that
/// of `min` for the primitive floating-point types.
#[inline]
pub fn min(self, x: Self) -> Self {
use coresimd::simd_llvm::simd_fmin;
unsafe { simd_fmin(self, x) }
}

/// Maximum of two vectors.
///
/// Returns a new vector containing the minimum value of each of the
/// input vector lanes. The lane-wise semantics are the same as that
/// of `max` for the primitive floating-point types.
#[inline]
pub fn max(self, x: Self) -> Self {
// FIXME: https://github.com/rust-lang-nursery/stdsimd/issues/416
// use coresimd::simd_llvm::simd_fmax;
// unsafe { simd_fmax(self, x) }
use num::Float;
let mut r = self;
for i in 0..$id::lanes() {
let a = self.extract(i);
let b = x.extract(i);
r = r.replace(i, a.max(b))
}
r
}
}
}
}

#[cfg(test)]
macro_rules! test_float_minmax_ops {
($id:ident, $elem_ty:ident) => {
#[test]
fn minmax() {
use coresimd::simd::$id;
let n = ::std::$elem_ty::NAN;
let o = $id::splat(1. as $elem_ty);
let t = $id::splat(2. as $elem_ty);

let mut m = o;
let mut on = o;
for i in 0..$id::lanes() {
if i % 2 == 0 {
m = m.replace(i, 2. as $elem_ty);
on = on.replace(i, n);
}
}

assert_eq!(o.min(t), o);
assert_eq!(t.min(o), o);
assert_eq!(m.min(o), o);
assert_eq!(o.min(m), o);
assert_eq!(m.min(t), m);
assert_eq!(t.min(m), m);

assert_eq!(o.max(t), t);
assert_eq!(t.max(o), t);
assert_eq!(m.max(o), m);
assert_eq!(o.max(m), m);
assert_eq!(m.max(t), t);
assert_eq!(t.max(m), t);

assert_eq!(on.min(o), o);
assert_eq!(o.min(on), o);
assert_eq!(on.max(o), o);
assert_eq!(o.max(on), o);
}
};
}
14 changes: 11 additions & 3 deletions coresimd/ppsv/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ mod masks_reductions;
#[macro_use]
mod minimal;
#[macro_use]
mod minmax;
#[macro_use]
mod minmax_reductions;
#[macro_use]
mod neg;
Expand Down Expand Up @@ -139,7 +141,8 @@ macro_rules! simd_f_ty {
[impl_minmax_reductions, $id, $elem_ty],
[impl_neg_op, $id, $elem_ty],
[impl_partial_eq, $id],
[impl_default, $id, $elem_ty]
[impl_default, $id, $elem_ty],
[impl_float_minmax_ops, $id]
);

$test_macro!(
Expand All @@ -156,6 +159,7 @@ macro_rules! simd_f_ty {
test_partial_eq!($id, 1. as $elem_ty, 0. as $elem_ty);
test_default!($id, $elem_ty);
test_mask_select!($mask_ty, $id, $elem_ty);
test_float_minmax_ops!($id, $elem_ty);
}
);
}
Expand Down Expand Up @@ -184,7 +188,8 @@ macro_rules! simd_i_ty {
[impl_hex_fmt, $id, $elem_ty],
[impl_eq, $id],
[impl_partial_eq, $id],
[impl_default, $id, $elem_ty]
[impl_default, $id, $elem_ty],
[impl_int_minmax_ops, $id]
);

$test_macro!(
Expand All @@ -208,6 +213,7 @@ macro_rules! simd_i_ty {
test_partial_eq!($id, 1 as $elem_ty, 0 as $elem_ty);
test_default!($id, $elem_ty);
test_mask_select!($mask_ty, $id, $elem_ty);
test_int_minmax_ops!($id, $elem_ty);
}
);
}
Expand Down Expand Up @@ -235,7 +241,8 @@ macro_rules! simd_u_ty {
[impl_hex_fmt, $id, $elem_ty],
[impl_eq, $id],
[impl_partial_eq, $id],
[impl_default, $id, $elem_ty]
[impl_default, $id, $elem_ty],
[impl_int_minmax_ops, $id]
);

$test_macro!(
Expand All @@ -258,6 +265,7 @@ macro_rules! simd_u_ty {
test_partial_eq!($id, 1 as $elem_ty, 0 as $elem_ty);
test_default!($id, $elem_ty);
test_mask_select!($mask_ty, $id, $elem_ty);
test_int_minmax_ops!($id, $elem_ty);
}
);
}
Expand Down
4 changes: 4 additions & 0 deletions coresimd/simd_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ extern "platform-intrinsic" {
pub fn simd_reduce_any<T>(x: T) -> bool;

pub fn simd_select<M, T>(m: M, a: T, b: T) -> T;

pub fn simd_fmin<T>(a: T, b: T) -> T;
// FIXME: https://github.com/rust-lang-nursery/stdsimd/issues/416
// pub fn simd_fmax<T>(a: T, b: T) -> T;
}