Skip to content

Commit 8d5702e

Browse files
committed
Fix performance issues
1 parent 38b1890 commit 8d5702e

File tree

3 files changed

+90
-86
lines changed

3 files changed

+90
-86
lines changed

crates/core_simd/tests/float.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ macro_rules! impl_op_test {
66
test_helpers::test_lanes! {
77
fn $fn<const LANES: usize>() {
88
test_helpers::test_unary_elementwise(
9-
<$vector as core::ops::$trait>::$fn,
10-
<$scalar as core::ops::$trait>::$fn,
11-
|_| true,
9+
&<$vector as core::ops::$trait>::$fn,
10+
&<$scalar as core::ops::$trait>::$fn,
11+
&|_| true,
1212
);
1313
}
1414
}
@@ -20,41 +20,41 @@ macro_rules! impl_op_test {
2020
test_helpers::test_lanes! {
2121
fn normal<const LANES: usize>() {
2222
test_helpers::test_binary_elementwise(
23-
<$vector as core::ops::$trait>::$fn,
24-
<$scalar as core::ops::$trait>::$fn,
25-
|_, _| true,
23+
&<$vector as core::ops::$trait>::$fn,
24+
&<$scalar as core::ops::$trait>::$fn,
25+
&|_, _| true,
2626
);
2727
}
2828

2929
fn scalar_rhs<const LANES: usize>() {
3030
test_helpers::test_binary_scalar_rhs_elementwise(
31-
<$vector as core::ops::$trait<$scalar>>::$fn,
32-
<$scalar as core::ops::$trait>::$fn,
33-
|_, _| true,
31+
&<$vector as core::ops::$trait<$scalar>>::$fn,
32+
&<$scalar as core::ops::$trait>::$fn,
33+
&|_, _| true,
3434
);
3535
}
3636

3737
fn scalar_lhs<const LANES: usize>() {
3838
test_helpers::test_binary_scalar_lhs_elementwise(
39-
<$scalar as core::ops::$trait<$vector>>::$fn,
40-
<$scalar as core::ops::$trait>::$fn,
41-
|_, _| true,
39+
&<$scalar as core::ops::$trait<$vector>>::$fn,
40+
&<$scalar as core::ops::$trait>::$fn,
41+
&|_, _| true,
4242
);
4343
}
4444

4545
fn assign<const LANES: usize>() {
4646
test_helpers::test_binary_elementwise(
47-
|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
48-
|mut a, b| { <$scalar as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
49-
|_, _| true,
47+
&|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
48+
&|mut a, b| { <$scalar as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
49+
&|_, _| true,
5050
)
5151
}
5252

5353
fn assign_scalar_rhs<const LANES: usize>() {
5454
test_helpers::test_binary_scalar_rhs_elementwise(
55-
|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
56-
|mut a, b| { <$scalar as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
57-
|_, _| true,
55+
&|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
56+
&|mut a, b| { <$scalar as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
57+
&|_, _| true,
5858
)
5959
}
6060
}
@@ -79,33 +79,33 @@ macro_rules! impl_tests {
7979
test_helpers::test_lanes! {
8080
fn abs<const LANES: usize>() {
8181
test_helpers::test_unary_elementwise(
82-
Vector::<LANES>::abs,
83-
Scalar::abs,
84-
|_| true,
82+
&Vector::<LANES>::abs,
83+
&Scalar::abs,
84+
&|_| true,
8585
)
8686
}
8787

8888
fn ceil<const LANES: usize>() {
8989
test_helpers::test_unary_elementwise(
90-
Vector::<LANES>::ceil,
91-
Scalar::ceil,
92-
|_| true,
90+
&Vector::<LANES>::ceil,
91+
&Scalar::ceil,
92+
&|_| true,
9393
)
9494
}
9595

9696
fn floor<const LANES: usize>() {
9797
test_helpers::test_unary_elementwise(
98-
Vector::<LANES>::floor,
99-
Scalar::floor,
100-
|_| true,
98+
&Vector::<LANES>::floor,
99+
&Scalar::floor,
100+
&|_| true,
101101
)
102102
}
103103

104104
fn round_from_int<const LANES: usize>() {
105105
test_helpers::test_unary_elementwise(
106-
Vector::<LANES>::round_from_int,
107-
|x| x as Scalar,
108-
|_| true,
106+
&Vector::<LANES>::round_from_int,
107+
&|x| x as Scalar,
108+
&|_| true,
109109
)
110110
}
111111

crates/core_simd/tests/integer.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ macro_rules! impl_unary_op_test {
66
test_helpers::test_lanes! {
77
fn $fn<const LANES: usize>() {
88
test_helpers::test_unary_elementwise(
9-
<$vector as core::ops::$trait>::$fn,
10-
$scalar_fn,
11-
|_| true,
9+
&<$vector as core::ops::$trait>::$fn,
10+
&$scalar_fn,
11+
&|_| true,
1212
);
1313
}
1414
}
@@ -26,42 +26,42 @@ macro_rules! impl_binary_op_test {
2626
test_helpers::test_lanes! {
2727
fn normal<const LANES: usize>() {
2828
test_helpers::test_binary_elementwise(
29-
<$vector as core::ops::$trait>::$fn,
30-
$scalar_fn,
31-
|_, _| true,
29+
&<$vector as core::ops::$trait>::$fn,
30+
&$scalar_fn,
31+
&|_, _| true,
3232
);
3333
}
3434

3535
fn scalar_rhs<const LANES: usize>() {
3636
test_helpers::test_binary_scalar_rhs_elementwise(
37-
<$vector as core::ops::$trait<$scalar>>::$fn,
38-
$scalar_fn,
39-
|_, _| true,
37+
&<$vector as core::ops::$trait<$scalar>>::$fn,
38+
&$scalar_fn,
39+
&|_, _| true,
4040
);
4141
}
4242

4343
fn scalar_lhs<const LANES: usize>() {
4444
test_helpers::test_binary_scalar_lhs_elementwise(
45-
<$scalar as core::ops::$trait<$vector>>::$fn,
46-
$scalar_fn,
47-
|_, _| true,
45+
&<$scalar as core::ops::$trait<$vector>>::$fn,
46+
&$scalar_fn,
47+
&|_, _| true,
4848
);
4949
}
5050

5151
fn assign<const LANES: usize>() {
5252
test_helpers::test_binary_elementwise(
53-
|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
54-
$scalar_fn,
55-
|_, _| true,
56-
)
53+
&|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
54+
&$scalar_fn,
55+
&|_, _| true,
56+
);
5757
}
5858

5959
fn assign_scalar_rhs<const LANES: usize>() {
6060
test_helpers::test_binary_scalar_rhs_elementwise(
61-
|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
62-
$scalar_fn,
63-
|_, _| true,
64-
)
61+
&|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
62+
&$scalar_fn,
63+
&|_, _| true,
64+
);
6565
}
6666
}
6767
}
@@ -79,41 +79,41 @@ macro_rules! impl_binary_checked_op_test {
7979
test_helpers::test_lanes! {
8080
fn normal<const LANES: usize>() {
8181
test_helpers::test_binary_elementwise(
82-
<$vector as core::ops::$trait>::$fn,
83-
$scalar_fn,
84-
|x, y| x.iter().zip(y.iter()).all(|(x, y)| $check_fn(*x, *y)),
82+
&<$vector as core::ops::$trait>::$fn,
83+
&$scalar_fn,
84+
&|x, y| x.iter().zip(y.iter()).all(|(x, y)| $check_fn(*x, *y)),
8585
);
8686
}
8787

8888
fn scalar_rhs<const LANES: usize>() {
8989
test_helpers::test_binary_scalar_rhs_elementwise(
90-
<$vector as core::ops::$trait<$scalar>>::$fn,
91-
$scalar_fn,
92-
|x, y| x.iter().all(|x| $check_fn(*x, y)),
90+
&<$vector as core::ops::$trait<$scalar>>::$fn,
91+
&$scalar_fn,
92+
&|x, y| x.iter().all(|x| $check_fn(*x, y)),
9393
);
9494
}
9595

9696
fn scalar_lhs<const LANES: usize>() {
9797
test_helpers::test_binary_scalar_lhs_elementwise(
98-
<$scalar as core::ops::$trait<$vector>>::$fn,
99-
$scalar_fn,
100-
|x, y| y.iter().all(|y| $check_fn(x, *y)),
98+
&<$scalar as core::ops::$trait<$vector>>::$fn,
99+
&$scalar_fn,
100+
&|x, y| y.iter().all(|y| $check_fn(x, *y)),
101101
);
102102
}
103103

104104
fn assign<const LANES: usize>() {
105105
test_helpers::test_binary_elementwise(
106-
|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
107-
$scalar_fn,
108-
|x, y| x.iter().zip(y.iter()).all(|(x, y)| $check_fn(*x, *y)),
106+
&|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
107+
&$scalar_fn,
108+
&|x, y| x.iter().zip(y.iter()).all(|(x, y)| $check_fn(*x, *y)),
109109
)
110110
}
111111

112112
fn assign_scalar_rhs<const LANES: usize>() {
113113
test_helpers::test_binary_scalar_rhs_elementwise(
114-
|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
115-
$scalar_fn,
116-
|x, y| x.iter().all(|x| $check_fn(*x, y)),
114+
&|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
115+
&$scalar_fn,
116+
&|x, y| x.iter().all(|x| $check_fn(*x, y)),
117117
)
118118
}
119119
}
@@ -133,9 +133,9 @@ macro_rules! impl_signed_tests {
133133
test_helpers::test_lanes! {
134134
fn neg<const LANES: usize>() {
135135
test_helpers::test_unary_elementwise(
136-
<Vector<LANES> as core::ops::Neg>::neg,
137-
<Scalar as core::ops::Neg>::neg,
138-
|x| !x.contains(&Scalar::MIN),
136+
&<Vector<LANES> as core::ops::Neg>::neg,
137+
&<Scalar as core::ops::Neg>::neg,
138+
&|x| !x.contains(&Scalar::MIN),
139139
);
140140
}
141141
}

crates/test_helpers/src/lib.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ impl<T: core::fmt::Debug + DefaultStrategy, const LANES: usize> DefaultStrategy
4242
}
4343

4444
pub fn test_1<A: core::fmt::Debug + DefaultStrategy>(
45-
f: impl Fn(A) -> proptest::test_runner::TestCaseResult,
45+
f: &dyn Fn(A) -> proptest::test_runner::TestCaseResult,
4646
) {
4747
let mut runner = proptest::test_runner::TestRunner::default();
4848
runner.run(&A::default_strategy(), f).unwrap();
4949
}
5050

5151
pub fn test_2<A: core::fmt::Debug + DefaultStrategy, B: core::fmt::Debug + DefaultStrategy>(
52-
f: impl Fn(A, B) -> proptest::test_runner::TestCaseResult,
52+
f: &dyn Fn(A, B) -> proptest::test_runner::TestCaseResult,
5353
) {
5454
let mut runner = proptest::test_runner::TestRunner::default();
5555
runner
@@ -59,17 +59,18 @@ pub fn test_2<A: core::fmt::Debug + DefaultStrategy, B: core::fmt::Debug + Defau
5959
.unwrap();
6060
}
6161

62+
#[inline(never)]
6263
pub fn test_unary_elementwise<Scalar, ScalarResult, Vector, VectorResult, const LANES: usize>(
63-
fv: impl Fn(Vector) -> VectorResult,
64-
fs: impl Fn(Scalar) -> ScalarResult,
65-
check: impl Fn([Scalar; LANES]) -> bool,
64+
fv: &dyn Fn(Vector) -> VectorResult,
65+
fs: &dyn Fn(Scalar) -> ScalarResult,
66+
check: &dyn Fn([Scalar; LANES]) -> bool,
6667
) where
6768
Scalar: Copy + Default + core::fmt::Debug + DefaultStrategy,
6869
ScalarResult: Copy + Default + biteq::BitEq + core::fmt::Debug + DefaultStrategy,
6970
Vector: Into<[Scalar; LANES]> + From<[Scalar; LANES]> + Copy,
7071
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
7172
{
72-
test_1(|x: [Scalar; LANES]| {
73+
test_1(&|x: [Scalar; LANES]| {
7374
proptest::prop_assume!(check(x));
7475
let result_1: [ScalarResult; LANES] = fv(x.into()).into();
7576
let result_2: [ScalarResult; LANES] = {
@@ -84,6 +85,7 @@ pub fn test_unary_elementwise<Scalar, ScalarResult, Vector, VectorResult, const
8485
});
8586
}
8687

88+
#[inline(never)]
8789
pub fn test_binary_elementwise<
8890
Scalar1,
8991
Scalar2,
@@ -93,9 +95,9 @@ pub fn test_binary_elementwise<
9395
VectorResult,
9496
const LANES: usize,
9597
>(
96-
fv: impl Fn(Vector1, Vector2) -> VectorResult,
97-
fs: impl Fn(Scalar1, Scalar2) -> ScalarResult,
98-
check: impl Fn([Scalar1; LANES], [Scalar2; LANES]) -> bool,
98+
fv: &dyn Fn(Vector1, Vector2) -> VectorResult,
99+
fs: &dyn Fn(Scalar1, Scalar2) -> ScalarResult,
100+
check: &dyn Fn([Scalar1; LANES], [Scalar2; LANES]) -> bool,
99101
) where
100102
Scalar1: Copy + Default + core::fmt::Debug + DefaultStrategy,
101103
Scalar2: Copy + Default + core::fmt::Debug + DefaultStrategy,
@@ -104,7 +106,7 @@ pub fn test_binary_elementwise<
104106
Vector2: Into<[Scalar2; LANES]> + From<[Scalar2; LANES]> + Copy,
105107
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
106108
{
107-
test_2(|x: [Scalar1; LANES], y: [Scalar2; LANES]| {
109+
test_2(&|x: [Scalar1; LANES], y: [Scalar2; LANES]| {
108110
proptest::prop_assume!(check(x, y));
109111
let result_1: [ScalarResult; LANES] = fv(x.into(), y.into()).into();
110112
let result_2: [ScalarResult; LANES] = {
@@ -119,6 +121,7 @@ pub fn test_binary_elementwise<
119121
});
120122
}
121123

124+
#[inline(never)]
122125
pub fn test_binary_scalar_rhs_elementwise<
123126
Scalar1,
124127
Scalar2,
@@ -127,17 +130,17 @@ pub fn test_binary_scalar_rhs_elementwise<
127130
VectorResult,
128131
const LANES: usize,
129132
>(
130-
fv: impl Fn(Vector, Scalar2) -> VectorResult,
131-
fs: impl Fn(Scalar1, Scalar2) -> ScalarResult,
132-
check: impl Fn([Scalar1; LANES], Scalar2) -> bool,
133+
fv: &dyn Fn(Vector, Scalar2) -> VectorResult,
134+
fs: &dyn Fn(Scalar1, Scalar2) -> ScalarResult,
135+
check: &dyn Fn([Scalar1; LANES], Scalar2) -> bool,
133136
) where
134137
Scalar1: Copy + Default + core::fmt::Debug + DefaultStrategy,
135138
Scalar2: Copy + Default + core::fmt::Debug + DefaultStrategy,
136139
ScalarResult: Copy + Default + biteq::BitEq + core::fmt::Debug + DefaultStrategy,
137140
Vector: Into<[Scalar1; LANES]> + From<[Scalar1; LANES]> + Copy,
138141
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
139142
{
140-
test_2(|x: [Scalar1; LANES], y: Scalar2| {
143+
test_2(&|x: [Scalar1; LANES], y: Scalar2| {
141144
proptest::prop_assume!(check(x, y));
142145
let result_1: [ScalarResult; LANES] = fv(x.into(), y).into();
143146
let result_2: [ScalarResult; LANES] = {
@@ -152,6 +155,7 @@ pub fn test_binary_scalar_rhs_elementwise<
152155
});
153156
}
154157

158+
#[inline(never)]
155159
pub fn test_binary_scalar_lhs_elementwise<
156160
Scalar1,
157161
Scalar2,
@@ -160,17 +164,17 @@ pub fn test_binary_scalar_lhs_elementwise<
160164
VectorResult,
161165
const LANES: usize,
162166
>(
163-
fv: impl Fn(Scalar1, Vector) -> VectorResult,
164-
fs: impl Fn(Scalar1, Scalar2) -> ScalarResult,
165-
check: impl Fn(Scalar1, [Scalar2; LANES]) -> bool,
167+
fv: &dyn Fn(Scalar1, Vector) -> VectorResult,
168+
fs: &dyn Fn(Scalar1, Scalar2) -> ScalarResult,
169+
check: &dyn Fn(Scalar1, [Scalar2; LANES]) -> bool,
166170
) where
167171
Scalar1: Copy + Default + core::fmt::Debug + DefaultStrategy,
168172
Scalar2: Copy + Default + core::fmt::Debug + DefaultStrategy,
169173
ScalarResult: Copy + Default + biteq::BitEq + core::fmt::Debug + DefaultStrategy,
170174
Vector: Into<[Scalar2; LANES]> + From<[Scalar2; LANES]> + Copy,
171175
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
172176
{
173-
test_2(|x: Scalar1, y: [Scalar2; LANES]| {
177+
test_2(&|x: Scalar1, y: [Scalar2; LANES]| {
174178
proptest::prop_assume!(check(x, y));
175179
let result_1: [ScalarResult; LANES] = fv(x, y.into()).into();
176180
let result_2: [ScalarResult; LANES] = {

0 commit comments

Comments
 (0)