Skip to content

Commit 448fb69

Browse files
committed
add float math: sin, cos
1 parent 909e278 commit 448fb69

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

coresimd/ppsv/api/float_math.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ macro_rules! impl_float_math {
3939
use coresimd::ppsv::codegen::fma::FloatFma;
4040
FloatFma::fma(self, y, z)
4141
}
42+
43+
/// Sin
44+
#[inline(always)]
45+
pub fn sin(self) -> Self {
46+
use coresimd::ppsv::codegen::sin::FloatSin;
47+
FloatSin::sin(self)
48+
}
49+
50+
/// Cos
51+
#[inline]
52+
pub fn cos(self) -> Self {
53+
use coresimd::ppsv::codegen::cos::FloatCos;
54+
FloatCos::cos(self)
55+
}
4256
}
4357
};
4458
}
@@ -54,6 +68,14 @@ macro_rules! test_float_math {
5468
}
5569
}
5670

71+
fn pi() -> $elem_ty {
72+
match ::mem::size_of::<$elem_ty>() {
73+
4 => ::std::f32::consts::PI as $elem_ty,
74+
8 => ::std::f64::consts::PI as $elem_ty,
75+
_ => unreachable!(),
76+
}
77+
}
78+
5779
#[test]
5880
fn abs() {
5981
use coresimd::simd::*;
@@ -126,7 +148,36 @@ macro_rules! test_float_math {
126148

127149
assert_eq!(f, t.fma(t, z));
128150
assert_eq!(f, t.fma(o, t));
129-
assert_eq!(t3, t.fma(t, o));
151+
assert_eq!(t3, t.fma(o, o));
152+
}
153+
154+
#[test]
155+
fn sin() {
156+
use coresimd::simd::*;
157+
let z = $id::splat(0 as $elem_ty);
158+
let p = $id::splat(pi() as $elem_ty);
159+
let ph = $id::splat(pi() as $elem_ty / 2.);
160+
let o_r = $id::splat((pi() as $elem_ty / 2.).sin());
161+
let z_r = $id::splat((pi() as $elem_ty).sin());
162+
163+
assert_eq!(z, z.sin());
164+
assert_eq!(o_r, ph.sin());
165+
assert_eq!(z_r, p.sin());
166+
}
167+
168+
#[test]
169+
fn cos() {
170+
use coresimd::simd::*;
171+
let z = $id::splat(0 as $elem_ty);
172+
let o = $id::splat(1 as $elem_ty);
173+
let p = $id::splat(pi() as $elem_ty);
174+
let ph = $id::splat(pi() as $elem_ty / 2.);
175+
let z_r = $id::splat((pi() as $elem_ty / 2.).cos());
176+
let o_r = $id::splat((pi() as $elem_ty).cos());
177+
178+
assert_eq!(o, z.cos());
179+
assert_eq!(z_r, ph.cos());
180+
assert_eq!(o_r, p.cos());
130181
}
131182
};
132183
}

coresimd/ppsv/codegen/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ pub mod masks_reductions;
88
pub mod sqrt;
99
pub mod abs;
1010
pub mod fma;
11+
pub mod sin;
12+
pub mod cos;

0 commit comments

Comments
 (0)