From 2b6148d6232ca956e514870d86609bf672172dd7 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 28 May 2018 19:49:49 +0200 Subject: [PATCH] Use cfg_if for the nbody example Avoid the combinatorial explosion once we add support for additional architectures and simd flavours. --- library/stdarch/examples/nbody.rs | 42 ++++++++++++++++++------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/library/stdarch/examples/nbody.rs b/library/stdarch/examples/nbody.rs index 8df5aa290cb4..8f12ec436594 100644 --- a/library/stdarch/examples/nbody.rs +++ b/library/stdarch/examples/nbody.rs @@ -10,8 +10,13 @@ shadow_reuse, print_stdout))] extern crate stdsimd; +#[macro_use] +extern crate cfg_if; + use stdsimd::simd::*; + + const PI: f64 = std::f64::consts::PI; const SOLAR_MASS: f64 = 4.0 * PI * PI; const DAYS_PER_YEAR: f64 = 365.24; @@ -20,16 +25,15 @@ pub trait Frsqrt { fn frsqrt(&self) -> Self; } -impl Frsqrt for f64x2 { - fn frsqrt(&self) -> Self { - #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), - target_feature = "sse"))] - { +cfg_if! { + if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), + target_feature = "sse"))] { + fn frsqrt(s: &f64x2) -> f64x2 { #[cfg(target_arch = "x86")] use stdsimd::arch::x86::*; #[cfg(target_arch = "x86_64")] use stdsimd::arch::x86_64::*; - let t: f32x2 = (*self).into(); + let t: f32x2 = (*s).into(); let u: f64x4 = unsafe { let res = _mm_rsqrt_ps(_mm_setr_ps( @@ -40,32 +44,34 @@ impl Frsqrt for f64x2 { )); f32x4::from_bits(res).into() }; - Self::new(u.extract(0), u.extract(1)) + f64x2::new(u.extract(0), u.extract(1)) } - #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] - { + } else if #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] { + fn frsqrt(s: &f64x2) -> f64x2 { #[cfg(target_arch = "aarch64")] use stdsimd::arch::aarch64::*; #[cfg(target_arch = "arm")] use stdsimd::arch::arm::*; - let t: f32x2 = (*self).into(); + let t: f32x2 = (*s).into(); let t: f32x2 = unsafe { vrsqrte_f32(t.into_bits()).into_bits() }; t.into() } - #[cfg(not(any(all(any(target_arch = "x86", - target_arch = "x86_64"), - target_feature = "sse"), - all(target_arch = "aarch64", - target_feature = "neon"))))] - { - let r = self.replace(0, 1. / self.extract(0).sqrt()); - let r = r.replace(1, 1. / self.extract(1).sqrt()); + } else { + fn frsqrt(s: &f64x2) -> f64x2 { + let r = s.replace(0, 1. / s.extract(0).sqrt()); + let r = r.replace(1, 1. / s.extract(1).sqrt()); r } } } +impl Frsqrt for f64x2 { + fn frsqrt(&self) -> Self { + frsqrt(self) + } +} + struct Body { x: [f64; 3], _fill: f64,