Skip to content

AVX: vandnpd, vandnps, vblendvpd and vblendvps #91

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

Closed
wants to merge 2 commits into from
Closed
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
86 changes: 86 additions & 0 deletions src/x86/avx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ pub unsafe fn _mm256_and_ps(a: f32x8, b: f32x8) -> f32x8 {
mem::transmute(a & b)
}

/// Compute the bitwise NOT of a packed double-precision (64-bit) floating-point elements in `a`
/// and then AND with `b`.
#[inline(always)]
#[target_feature = "+avx"]
// Should be 'vandnpd' instuction.
// See https://github.com/rust-lang-nursery/stdsimd/issues/71
#[cfg_attr(test, assert_instr(vandnps))]
pub unsafe fn _mm256_andnot_pd(a: f64x4, b: f64x4) -> f64x4 {
let a: u64x4 = mem::transmute(a);
let b: u64x4 = mem::transmute(b);
mem::transmute(!a & b)
}

/// Compute the bitwise NOT of a packed single-precision (32-bit) floating-point elements in `a`
/// and then AND with `b`.
#[inline(always)]
#[target_feature = "+avx"]
#[cfg_attr(test, assert_instr(vandnps))]
pub unsafe fn _mm256_andnot_ps(a: f32x8, b: f32x8) -> f32x8 {
let a: u32x8 = mem::transmute(a);
let b: u32x8 = mem::transmute(b);
mem::transmute(!a & b)
}

/// Compute the bitwise OR packed double-precision (64-bit) floating-point elements
/// in `a` and `b`.
#[inline(always)]
Expand All @@ -68,6 +92,24 @@ pub unsafe fn _mm256_or_ps(a: f32x8, b: f32x8) -> f32x8 {
mem::transmute(a | b)
}

/// Blend packed double-precision (64-bit) floating-point elements
/// from `a` and `b` using `mask`
#[inline(always)]
#[target_feature = "+avx"]
#[cfg_attr(test, assert_instr(vblendvpd))]
pub unsafe fn _mm256_blendv_pd(a: f64x4, b: f64x4, mask: f64x4) -> f64x4 {
blendvpd256(a, b, mask)
}

/// Blend packed single-precision (32-bit) floating-point elements
/// from `a` and `b` using `mask`
#[inline(always)]
#[target_feature = "+avx"]
#[cfg_attr(test, assert_instr(vblendvps))]
pub unsafe fn _mm256_blendv_ps(a: f32x8, b: f32x8, mask: f32x8) -> f32x8 {
blendvps256(a, b, mask)
}

/// Compare packed double-precision (64-bit) floating-point elements
/// in `a` and `b`, and return packed maximum values
#[inline(always)]
Expand Down Expand Up @@ -272,6 +314,10 @@ pub unsafe fn _mm256_sqrt_pd(a: f64x4) -> f64x4 {
/// LLVM intrinsics used in the above functions
#[allow(improper_ctypes)]
extern "C" {
#[link_name = "llvm.x86.avx.blendv.pd.256"]
fn blendvpd256(a: f64x4, b: f64x4, mask: f64x4) -> f64x4;
#[link_name = "llvm.x86.avx.blendv.ps.256"]
fn blendvps256(a: f32x8, b: f32x8, mask: f32x8) -> f32x8;
#[link_name = "llvm.x86.avx.addsub.pd.256"]
fn addsubpd256(a: f64x4, b: f64x4) -> f64x4;
#[link_name = "llvm.x86.avx.addsub.ps.256"]
Expand Down Expand Up @@ -337,6 +383,24 @@ mod tests {
assert_eq!(r, e);
}

#[simd_test = "avx"]
unsafe fn _mm256_andnot_pd() {
let a = f64x4::splat(1.0);
let b = f64x4::splat(1.0);
let r = avx::_mm256_andnot_pd(a, b);
let e = f64x4::splat(0.0);
assert_eq!(r, e);
}

#[simd_test = "avx"]
unsafe fn _mm256_andnot_ps() {
let a = f32x8::splat(1.0);
let b = f32x8::splat(1.0);
let r = avx::_mm256_andnot_ps(a, b);
let e = f32x8::splat(0.0);
assert_eq!(r, e);
}

#[simd_test = "avx"]
unsafe fn _mm256_or_pd() {
let a = f64x4::splat(1.0);
Expand All @@ -355,6 +419,28 @@ mod tests {
assert_eq!(r, e);
}

#[simd_test = "avx"]
unsafe fn _mm256_blendv_pd() {
use std::mem::transmute;
let a = f64x4::splat(1.0);
let b = f64x4::splat(2.0);
let mask = transmute(i64x4::splat(0).replace(2, -1));
let r = avx::_mm256_blendv_pd(a, b, mask);
let e = f64x4::splat(1.0).replace(2, 2.0);
assert_eq!(r, e);
}

#[simd_test = "avx"]
unsafe fn _mm256_blendv_ps() {
use std::mem::transmute;
let a = f32x8::splat(1.0);
let b = f32x8::splat(2.0);
let mask = transmute(i32x8::splat(0).replace(4, -1));
let r = avx::_mm256_blendv_ps(a, b, mask);
let e = f32x8::splat(1.0).replace(4, 2.0);
assert_eq!(r, e);
}

#[simd_test = "avx"]
unsafe fn _mm256_max_pd() {
let a = f64x4::new(1.0, 4.0, 5.0, 8.0);
Expand Down