Skip to content

Commit 6dfb60b

Browse files
authored
feat: replace the SimdValue::lanes() function by an associated const SimdValue::LANES (#60)
1 parent 32543cd commit 6dfb60b

File tree

6 files changed

+31
-60
lines changed

6 files changed

+31
-60
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
traits named `ClosedAddAssign, ClosedMulAssign, etc.`
88
- The `ComplexField` and `SimdComplexField` (and, by extension, `RealField` and `SimdRealField`) traits now depend on the
99
`SupersetOf<f32>` trait in addition to `SupersetOf<f64>`.
10+
- Replace the `SimdValue::lanes()` function by an associated constant `SimdValue::LANES`.
1011

1112
## Release v0.8.1 (04 Apr. 2023)
1213
- Add implementation of `rkyv` serialization/deserialization to the `Wide*` wrapper types.

src/scalar/fixed_impl.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,10 @@ macro_rules! impl_fixed_type (
9595

9696
impl<Fract: $LeEqDim> PrimitiveSimdValue for $FixedI<Fract> {}
9797
impl<Fract: $LeEqDim> SimdValue for $FixedI<Fract> {
98+
const LANES: usize = 1;
9899
type Element = Self;
99100
type SimdBool = bool;
100101

101-
#[inline(always)]
102-
fn lanes() -> usize {
103-
1
104-
}
105-
106102
#[inline(always)]
107103
fn splat(val: Self::Element) -> Self {
108104
val

src/simd/auto_simd_impl.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ macro_rules! impl_scalar_subset_of_simd (
219219
fn is_in_subset(c: &AutoSimd<N2>) -> bool {
220220
let elt0 = c.extract(0);
221221
elt0.is_in_subset() &&
222-
(1..AutoSimd::<N2>::lanes()).all(|i| c.extract(i) == elt0)
222+
(1..AutoSimd::<N2>::LANES).all(|i| c.extract(i) == elt0)
223223
}
224224
}
225225
)*}
@@ -265,13 +265,14 @@ macro_rules! impl_simd_value (
265265

266266
impl fmt::Display for AutoSimd<$t> {
267267
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
268-
if Self::lanes() == 1 {
268+
if Self::LANES == 1 {
269269
return self.extract(0).fmt(f);
270270
}
271271

272272
write!(f, "({}", self.extract(0))?;
273273

274-
for i in 1..Self::lanes() {
274+
#[allow(clippy::reversed_empty_ranges)] // Needed for LANES == 1 that’s in a different code path.
275+
for i in 1..Self::LANES {
275276
write!(f, ", {}", self.extract(i))?;
276277
}
277278

@@ -288,13 +289,10 @@ macro_rules! impl_simd_value (
288289
impl PrimitiveSimdValue for AutoSimd<$t> {}
289290

290291
impl SimdValue for AutoSimd<$t> {
292+
const LANES: usize = $lanes;
291293
type Element = $elt;
292294
type SimdBool = $bool;
293295

294-
#[inline(always)]
295-
fn lanes() -> usize {
296-
$lanes
297-
}
298296

299297
#[inline(always)]
300298
fn splat(val: Self::Element) -> Self {

src/simd/portable_simd_impl.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ macro_rules! impl_bool_simd (
5252
($($t: ty, $lanes: literal, $($i: ident),*;)*) => {$(
5353
impl fmt::Display for Simd<$t> {
5454
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55-
if Self::lanes() == 1 {
55+
if Self::LANES == 1 {
5656
return self.extract(0).fmt(f);
5757
}
5858

5959
write!(f, "({}", self.extract(0))?;
6060

61-
for i in 1..Self::lanes() {
61+
for i in 1..Self::LANES {
6262
write!(f, ", {}", self.extract(i))?;
6363
}
6464

@@ -76,14 +76,10 @@ macro_rules! impl_bool_simd (
7676
impl PrimitiveSimdValue for Simd<$t> {}
7777

7878
impl SimdValue for Simd<$t> {
79+
const LANES: usize = $lanes;
7980
type Element = bool;
8081
type SimdBool = Simd<$t>;
8182

82-
#[inline(always)]
83-
fn lanes() -> usize {
84-
$lanes
85-
}
86-
8783
#[inline(always)]
8884
fn splat(val: Self::Element) -> Self {
8985
Simd(<$t>::splat(val))
@@ -263,7 +259,7 @@ macro_rules! impl_scalar_subset_of_simd (
263259
fn is_in_subset(c: &Simd<N2>) -> bool {
264260
let elt0 = c.extract(0);
265261
elt0.is_in_subset() &&
266-
(1..Simd::<N2>::lanes()).all(|i| c.extract(i) == elt0)
262+
(1..Simd::<N2>::LANES).all(|i| c.extract(i) == elt0)
267263
}
268264
}
269265
)*}
@@ -277,13 +273,13 @@ macro_rules! impl_simd_value (
277273
($($t: ty, $elt: ty, $bool: ty, $($i: ident),*;)*) => ($(
278274
impl fmt::Display for Simd<$t> {
279275
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280-
if Self::lanes() == 1 {
276+
if Self::LANES == 1 {
281277
return self.extract(0).fmt(f);
282278
}
283279

284280
write!(f, "({}", self.extract(0))?;
285281

286-
for i in 1..Self::lanes() {
282+
for i in 1..Self::LANES {
287283
write!(f, ", {}", self.extract(i))?;
288284
}
289285

@@ -301,14 +297,10 @@ macro_rules! impl_simd_value (
301297
impl PrimitiveSimdValue for Simd<$t> {}
302298

303299
impl SimdValue for Simd<$t> {
300+
const LANES: usize = <$t>::LEN;
304301
type Element = $elt;
305302
type SimdBool = $bool;
306303

307-
#[inline(always)]
308-
fn lanes() -> usize {
309-
<$t>::LEN
310-
}
311-
312304
#[inline(always)]
313305
fn splat(val: Self::Element) -> Self {
314306
Simd(<$t>::splat(val))
@@ -351,7 +343,7 @@ macro_rules! impl_uint_simd (
351343
///
352344
/// # Panics
353345
///
354-
/// If `slice.len() < Self::lanes()`.
346+
/// If `slice.len() < Self::LANES`.
355347
#[inline]
356348
pub fn from_slice_unaligned(slice: &[$elt]) -> Self {
357349
Simd(<$t>::from_slice(slice))
@@ -1075,7 +1067,7 @@ macro_rules! impl_float_simd (
10751067
#[inline(always)]
10761068
fn simd_horizontal_product(self) -> Self::Element {
10771069
let mut prod = self.extract(0);
1078-
for ii in 1..Self::lanes() {
1070+
for ii in 1..Self::LANES {
10791071
prod *= self.extract(ii)
10801072
}
10811073
prod
@@ -1589,9 +1581,9 @@ impl_bool_simd!(
15891581
//
15901582
//macro_rules! impl_simd_complex_from(
15911583
// ($($t: ty, $elt: ty $(, $i: expr)*;)*) => ($(
1592-
// impl From<[num_complex::Complex<$elt>; <$t>::lanes()]> for num_complex::Complex<Simd<$t>> {
1584+
// impl From<[num_complex::Complex<$elt>; <$t>::LANES]> for num_complex::Complex<Simd<$t>> {
15931585
// #[inline(always)]
1594-
// fn from(vals: [num_complex::Complex<$elt>; <$t>::lanes()]) -> Self {
1586+
// fn from(vals: [num_complex::Complex<$elt>; <$t>::LANES]) -> Self {
15951587
// num_complex::Complex {
15961588
// re: <$t>::from([$(vals[$i].re),*]),
15971589
// im: <$t>::from([$(vals[$i].im),*]),

src/simd/simd_value.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@ use crate::simd::SimdBool;
22

33
/// Base trait for every SIMD types.
44
pub trait SimdValue: Sized {
5+
/// The number of lanes of this SIMD value.
6+
const LANES: usize;
57
/// The type of the elements of each lane of this SIMD value.
68
type Element: SimdValue<Element = Self::Element, SimdBool = bool>;
79
/// Type of the result of comparing two SIMD values like `self`.
810
type SimdBool: SimdBool;
911

10-
/// The number of lanes of this SIMD value.
11-
fn lanes() -> usize;
1212
/// Initializes an SIMD value with each lanes set to `val`.
1313
fn splat(val: Self::Element) -> Self;
1414
/// Extracts the i-th lane of `self`.
1515
///
16-
/// Panics if `i >= Self::lanes()`.
16+
/// Panics if `i >= Self::LANES`.
1717
fn extract(&self, i: usize) -> Self::Element;
1818
/// Extracts the i-th lane of `self` without bound-checking.
1919
///
2020
/// # Safety
21-
/// Undefined behavior if `i >= Self::lanes()`.
21+
/// Undefined behavior if `i >= Self::LANES`.
2222
unsafe fn extract_unchecked(&self, i: usize) -> Self::Element;
2323
/// Replaces the i-th lane of `self` by `val`.
2424
///
25-
/// Panics if `i >= Self::lanes()`.
25+
/// Panics if `i >= Self::LANES`.
2626
fn replace(&mut self, i: usize, val: Self::Element);
2727
/// Replaces the i-th lane of `self` by `val` without bound-checking.
2828
///
2929
/// # Safety
30-
/// Undefined behavior if `i >= Self::lanes()`.
30+
/// Undefined behavior if `i >= Self::LANES`.
3131
unsafe fn replace_unchecked(&mut self, i: usize, val: Self::Element);
3232

3333
/// Merges `self` and `other` depending on the lanes of `cond`.
@@ -48,7 +48,7 @@ pub trait SimdValue: Sized {
4848
{
4949
let mut result = self.clone();
5050

51-
for i in 0..Self::lanes() {
51+
for i in 0..Self::LANES {
5252
unsafe { result.replace_unchecked(i, f(self.extract_unchecked(i))) }
5353
}
5454

@@ -71,7 +71,7 @@ pub trait SimdValue: Sized {
7171
{
7272
let mut result = self.clone();
7373

74-
for i in 0..Self::lanes() {
74+
for i in 0..Self::LANES {
7575
unsafe {
7676
let a = self.extract_unchecked(i);
7777
let b = b.extract_unchecked(i);
@@ -92,14 +92,10 @@ pub trait SimdValue: Sized {
9292
pub trait PrimitiveSimdValue: Copy + SimdValue {}
9393

9494
impl<N: SimdValue> SimdValue for num_complex::Complex<N> {
95+
const LANES: usize = N::LANES;
9596
type Element = num_complex::Complex<N::Element>;
9697
type SimdBool = N::SimdBool;
9798

98-
#[inline(always)]
99-
fn lanes() -> usize {
100-
N::lanes()
101-
}
102-
10399
#[inline(always)]
104100
fn splat(val: Self::Element) -> Self {
105101
num_complex::Complex {
@@ -151,14 +147,10 @@ macro_rules! impl_primitive_simd_value_for_scalar (
151147
($($t: ty),*) => {$(
152148
impl PrimitiveSimdValue for $t {}
153149
impl SimdValue for $t {
150+
const LANES: usize = 1;
154151
type Element = $t;
155152
type SimdBool = bool;
156153

157-
#[inline(always)]
158-
fn lanes() -> usize {
159-
1
160-
}
161-
162154
#[inline(always)]
163155
fn splat(val: Self::Element) -> Self {
164156
val

src/simd/wide_simd_impl.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,10 @@ macro_rules! impl_wide_f32 (
153153
}
154154

155155
impl SimdValue for $WideF32xX {
156+
const LANES: usize = $lanes;
156157
type Element = $f32;
157158
type SimdBool = $WideBoolF32xX;
158159

159-
#[inline(always)]
160-
fn lanes() -> usize {
161-
$lanes
162-
}
163-
164160
#[inline(always)]
165161
fn splat(val: Self::Element) -> Self {
166162
$WideF32xX(wide::$f32xX::from(val))
@@ -197,14 +193,10 @@ macro_rules! impl_wide_f32 (
197193
}
198194

199195
impl SimdValue for $WideBoolF32xX {
196+
const LANES: usize = $lanes;
200197
type Element = bool;
201198
type SimdBool = Self;
202199

203-
#[inline(always)]
204-
fn lanes() -> usize {
205-
$lanes
206-
}
207-
208200
#[inline(always)]
209201
fn splat(val: bool) -> Self {
210202
let results = [
@@ -1090,7 +1082,7 @@ macro_rules! impl_wide_f32 (
10901082
#[inline(always)]
10911083
fn simd_horizontal_product(self) -> Self::Element {
10921084
let mut prod = self.extract(0);
1093-
for ii in 1..Self::lanes() {
1085+
for ii in 1..Self::LANES {
10941086
prod *= self.extract(ii)
10951087
}
10961088
prod

0 commit comments

Comments
 (0)