Skip to content

Commit b839939

Browse files
authored
feat: add ZERO and ONE consts, and Default impl to simd types (#61)
1 parent 6dfb60b commit b839939

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
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>`.
1010
- Replace the `SimdValue::lanes()` function by an associated constant `SimdValue::LANES`.
11+
- Add `ZERO` and `ONE` associated consts to all the simd newtypes.
12+
- Implement `Default` for all simd newtypes.
1113

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

src/simd/auto_simd_impl.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ macro_rules! ident_to_value (
4040
///
4141
/// This is needed to overcome the orphan rules.
4242
#[repr(align(16))]
43-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
43+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
4444
#[cfg_attr(
4545
feature = "rkyv",
4646
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize),
@@ -64,6 +64,15 @@ macro_rules! impl_bool_simd (
6464
($($t: ty, $lanes: expr, $($i: ident),*;)*) => {$(
6565
impl_simd_value!($t, bool, $lanes, AutoSimd<$t> $(, $i)*;);
6666

67+
impl AutoSimd<$t> {
68+
pub const ZERO: Self = AutoSimd([false; $lanes]);
69+
pub const ONE: Self = AutoSimd([true; $lanes]);
70+
71+
pub fn new($($i: bool),*) -> Self {
72+
AutoSimd([$($i),*])
73+
}
74+
}
75+
6776
impl From<[bool; $lanes]> for AutoSimd<$t> {
6877
#[inline(always)]
6978
fn from(vals: [bool; $lanes]) -> Self {
@@ -280,12 +289,6 @@ macro_rules! impl_simd_value (
280289
}
281290
}
282291

283-
impl AutoSimd<$t> {
284-
pub fn new($($i: $elt),*) -> Self {
285-
AutoSimd([$($i),*])
286-
}
287-
}
288-
289292
impl PrimitiveSimdValue for AutoSimd<$t> {}
290293

291294
impl SimdValue for AutoSimd<$t> {
@@ -334,6 +337,15 @@ macro_rules! impl_uint_simd (
334337
($($t: ty, $elt: ty, $lanes: expr, $bool: ty, $($i: ident),*;)*) => ($(
335338
impl_simd_value!($t, $elt, $lanes, $bool $(, $i)*;);
336339

340+
impl AutoSimd<$t> {
341+
pub const ZERO: Self = AutoSimd([0 as $elt; $lanes]);
342+
pub const ONE: Self = AutoSimd([1 as $elt; $lanes]);
343+
344+
pub fn new($($i: $elt),*) -> Self {
345+
AutoSimd([$($i),*])
346+
}
347+
}
348+
337349
impl From<[$elt; $lanes]> for AutoSimd<$t> {
338350
#[inline(always)]
339351
fn from(vals: [$elt; $lanes]) -> Self {
@@ -1534,7 +1546,7 @@ impl_bool_simd!(
15341546
[bool; 8], 8, _0, _1, _2, _3, _4, _5, _6, _7;
15351547
[bool; 16], 16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
15361548
[bool; 32], 32, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
1537-
// [bool; 64], 64, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63;
1549+
// [bool; 64], 64, 0, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63;
15381550
);
15391551

15401552
//

src/simd/portable_simd_impl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ macro_rules! ident_to_value (
4545
///
4646
/// This is needed to overcome the orphan rules.
4747
#[repr(transparent)]
48-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
48+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
4949
pub struct Simd<N>(pub N);
5050

5151
macro_rules! impl_bool_simd (
@@ -288,6 +288,9 @@ macro_rules! impl_simd_value (
288288
}
289289

290290
impl Simd<$t> {
291+
pub const ZERO: Self = Simd(<$t>::from_array([0 as $elt; <$t>::LEN]));
292+
pub const ONE: Self = Simd(<$t>::from_array([1 as $elt; <$t>::LEN]));
293+
291294
#[inline]
292295
pub fn new($($i: $elt),*) -> Self {
293296
Simd(<$t>::from_array([$($i),*]))

src/simd/wide_simd_impl.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ macro_rules! impl_rkyv {
5353
///
5454
/// This is needed to overcome the orphan rules.
5555
#[repr(transparent)]
56-
#[derive(Copy, Clone, Debug)]
56+
#[derive(Copy, Clone, Debug, Default)]
5757
pub struct WideF32x4(pub wide::f32x4);
5858

5959
#[cfg(feature = "rkyv")]
@@ -63,7 +63,7 @@ impl_rkyv!(WideF32x4, [f32; 4]);
6363
///
6464
/// This is needed to overcome the orphan rules.
6565
#[repr(transparent)]
66-
#[derive(Copy, Clone, Debug)]
66+
#[derive(Copy, Clone, Debug, Default)]
6767
pub struct WideBoolF32x4(pub wide::f32x4);
6868

6969
#[cfg(feature = "rkyv")]
@@ -73,7 +73,7 @@ impl_rkyv!(WideBoolF32x4, [f32; 4]);
7373
///
7474
/// This is needed to overcome the orphan rules.
7575
#[repr(transparent)]
76-
#[derive(Copy, Clone, Debug)]
76+
#[derive(Copy, Clone, Debug, Default)]
7777
pub struct WideF32x8(pub wide::f32x8);
7878

7979
#[cfg(feature = "rkyv")]
@@ -83,7 +83,7 @@ impl_rkyv!(WideF32x8, [f32; 8]);
8383
///
8484
/// This is needed to overcome the orphan rules.
8585
#[repr(transparent)]
86-
#[derive(Copy, Clone, Debug)]
86+
#[derive(Copy, Clone, Debug, Default)]
8787
pub struct WideBoolF32x8(pub wide::f32x8);
8888

8989
#[cfg(feature = "rkyv")]
@@ -93,7 +93,7 @@ impl_rkyv!(WideBoolF32x8, [f32; 8]);
9393
///
9494
/// This is needed to overcome the orphan rules.
9595
#[repr(transparent)]
96-
#[derive(Copy, Clone, Debug)]
96+
#[derive(Copy, Clone, Debug, Default)]
9797
pub struct WideF64x4(pub wide::f64x4);
9898

9999
#[cfg(feature = "rkyv")]
@@ -103,7 +103,7 @@ impl_rkyv!(WideF64x4, [f64; 4]);
103103
///
104104
/// This is needed to overcome the orphan rules.
105105
#[repr(transparent)]
106-
#[derive(Copy, Clone, Debug)]
106+
#[derive(Copy, Clone, Debug, Default)]
107107
pub struct WideBoolF64x4(pub wide::f64x4);
108108

109109
#[cfg(feature = "rkyv")]
@@ -115,6 +115,9 @@ macro_rules! impl_wide_f32 (
115115
impl PrimitiveSimdValue for $WideBoolF32xX {}
116116

117117
impl $WideF32xX {
118+
pub const ZERO: Self = $WideF32xX(<wide::$f32xX>::ZERO);
119+
pub const ONE: Self = $WideF32xX(<wide::$f32xX>::ONE);
120+
118121
#[inline(always)]
119122
fn into_arr(self) -> [$f32; $lanes] {
120123
self.0.into()

0 commit comments

Comments
 (0)