Skip to content
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

Implement powi API for floating-point vectors #234

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "packed_simd"
version = "0.3.3"
version = "0.3.4"
authors = ["Gonzalo Brito Gadeschi <[email protected]>"]
description = "Portable Packed SIMD vectors"
documentation = "https://docs.rs/crate/packed_simd/"
Expand Down
4 changes: 2 additions & 2 deletions examples/aobench/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub mod scalar {

pub fn thread_rng() -> RngH {
RngH {
rng: THREAD_RNG_KEY.with(|t| t.clone()),
rng: THREAD_RNG_KEY.with(Clone::clone),
}
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ pub mod vector {

pub fn thread_rng() -> RngH {
RngH {
rng: THREAD_RNG_KEY.with(|t| t.clone()),
rng: THREAD_RNG_KEY.with(Clone::clone),
}
}
}
2 changes: 1 addition & 1 deletion examples/slice_sum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn sum_hor(x: &[f32]) -> f32 {

x.chunks_exact(f32s::lanes())
.map(f32s::from_slice_unaligned)
.map(|vec| vec.sum())
.map(f32s::sum)
.sum()
}

Expand Down
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ macro_rules! impl_f {
impl_math_float_mul_add!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
impl_math_float_mul_adde!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
impl_math_float_powf!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
impl_math_float_powi!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
impl_math_float_recpre!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
impl_math_float_rsqrte!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
impl_math_float_sin!([$elem_ty; $elem_n]: $tuple_id | $test_tt);
Expand Down
3 changes: 3 additions & 0 deletions src/api/math/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ mod exp;
#[macro_use]
mod powf;

#[macro_use]
mod powi;

#[macro_use]
mod ln;

Expand Down
5 changes: 3 additions & 2 deletions src/api/math/float/powf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ macro_rules! impl_math_float_powf {
}
}

test_if!{
test_if! {
$test_tt:
paste::item! {
pub mod [<$id _math_powf>] {
use super::*;
#[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn powf() {
let z = $id::splat(0 as $elem_ty);
let o = $id::splat(1 as $elem_ty);
Expand Down
42 changes: 42 additions & 0 deletions src/api/math/float/powi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Implements vertical (lane-wise) floating-point `powi`.

macro_rules! impl_math_float_powi {
([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
impl $id {
/// Raises `self` number to the integer power of `x`.
#[inline]
pub fn powi(self, x: i32) -> Self {
use crate::codegen::math::float::powi::Powi;
Powi::powi(self, x)
}
}

test_if! {
$test_tt:
paste::item! {
pub mod [<$id _math_powi>] {
use super::*;
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn powf() {
let z = $id::splat(0 as $elem_ty);
let o = $id::splat(1 as $elem_ty);
let t = $id::splat(2 as $elem_ty);
let f = $id::splat(4 as $elem_ty);
let s = $id::splat(7 as $elem_ty);
let e = $id::splat(8 as $elem_ty);
assert_eq!(z, z.powi(1));
assert_eq!(o, z.powi(0));
assert_eq!(o, o.powi(0));
assert_eq!(o, t.powi(0));
assert_eq!(o, o.powi(1));
assert_eq!(t, t.powi(1));
assert_eq!(f, t.powi(2));
assert_eq!(e, t.powi(3));
assert_eq!($id::splat(16807 as $elem_ty), s.powi(5));
}
}
}
}
};
}
4 changes: 2 additions & 2 deletions src/api/slice/write_to_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ macro_rules! impl_slice_write_to_slice {
0
);

#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::cast_ptr_alignment)]
*(target_ptr as *mut Self) = self;
Expand Down
1 change: 1 addition & 0 deletions src/codegen/math/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate mod ln;
crate mod mul_add;
crate mod mul_adde;
crate mod powf;
crate mod powi;
crate mod sin;
crate mod sin_cos_pi;
crate mod sin_pi;
Expand Down
54 changes: 54 additions & 0 deletions src/codegen/math/float/powi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Vertical floating-point `powf`
#![allow(unused)]

use crate::*;

crate trait Powi {
fn powi(self, x: i32) -> Self;
}

#[allow(improper_ctypes)]
extern "C" {
#[link_name = "llvm.powi.v2f32"]
fn powi_v2f32(x: f32x2, y: i32) -> f32x2;
#[link_name = "llvm.powi.v4f32"]
fn powi_v4f32(x: f32x4, y: i32) -> f32x4;
#[link_name = "llvm.powi.v8f32"]
fn powi_v8f32(x: f32x8, y: i32) -> f32x8;
#[link_name = "llvm.powi.v16f32"]
fn powi_v16f32(x: f32x16, y: i32) -> f32x16;
/* FIXME 64-bit powigle elem vectors
#[link_name = "llvm.powi.v1f64"]
fn powi_v1f64(x: f64x1, y: i32) -> f64x1;
*/
#[link_name = "llvm.powi.v2f64"]
fn powi_v2f64(x: f64x2, y: i32) -> f64x2;
#[link_name = "llvm.powi.v4f64"]
fn powi_v4f64(x: f64x4, y: i32) -> f64x4;
#[link_name = "llvm.powi.v8f64"]
fn powi_v8f64(x: f64x8, y: i32) -> f64x8;

#[link_name = "llvm.powi.f32"]
fn powi_f32(x: f32, y: i32) -> f32;
#[link_name = "llvm.powi.f64"]
fn powi_f64(x: f64, y: i32) -> f64;
}

macro_rules! impl_ {
($id:ident, $fn_id:ident) => {
impl Powi for $id {
fn powi(self, x: i32) -> Self {
use mem::transmute;
unsafe { transmute($fn_id(transmute(self), x)) }
}
}
};
}

impl_!(f32x2, powi_v2f32);
impl_!(f32x4, powi_v4f32);
impl_!(f32x8, powi_v8f32);
impl_!(f32x16, powi_v16f32);
impl_!(f64x2, powi_v2f64);
impl_!(f64x4, powi_v4f64);
impl_!(f64x8, powi_v8f64);
6 changes: 3 additions & 3 deletions src/codegen/math/float/tanh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ crate trait Tanh {
}

macro_rules! define_tanh {

($name:ident, $basetype:ty, $simdtype:ty, $lanes:expr, $trait:path) => {
fn $name(x: $simdtype) -> $simdtype {
use core::intrinsics::transmute;
Expand All @@ -31,8 +30,9 @@ macro_rules! define_tanh {
};
}

// llvm does not seem to expose the hyperbolic versions of trigonometric functions;
// we thus call the classical rust versions on all of them (which stem from cmath).
// llvm does not seem to expose the hyperbolic versions of trigonometric
// functions; we thus call the classical rust versions on all of them (which
// stem from cmath).
define_tanh!(f32 => tanh_v2f32, f32x2, 2);
define_tanh!(f32 => tanh_v4f32, f32x4, 4);
define_tanh!(f32 => tanh_v8f32, f32x8, 8);
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,12 @@
clippy::cast_lossless,
clippy::cast_possible_wrap,
clippy::cast_precision_loss,
// This lint is currently broken for generic code
// FIXME: This lint is currently broken for generic code
// See https://github.com/rust-lang/rust-clippy/issues/3410
clippy::use_self
clippy::use_self,
// FIXME: This lint is currently broken for macros
// See https://github.com/rust-lang/rust-clippy/issues/3981
clippy::unnecessary_cast,
)]
#![cfg_attr(test, feature(hashmap_internals))]
#![deny(warnings, rust_2018_idioms, clippy::missing_inline_in_public_items)]
Expand Down