From 6e65329b229b3cf32942bc8c59ffe418dde50101 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 29 May 2025 14:50:26 -0700 Subject: [PATCH 1/3] Add conversions for Simd from/to arrays of types convertible from/to Simd --- crates/core_simd/src/vector.rs | 102 +++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index d76a6cd52bf..0b02f9de4c2 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -1090,6 +1090,108 @@ where } } +macro_rules! impl_simd_array_conversions { + ($A:literal, $B:literal) => { + impl From> for [U; $B] + where + T: SimdElement, + Simd: Into, + { + fn from(v: Simd) -> Self { + // Safety: these have identical layout + let intermediate: [Simd; $B] = unsafe { core::mem::transmute_copy(&v) }; + intermediate.map(Into::into) + } + } + impl From<[U; $B]> for Simd + where + T: SimdElement, + U: Into>, + { + fn from(v: [U; $B]) -> Self { + let intermediate: [Simd; $B] = v.map(Into::into); + // Safety: these have identical layout + unsafe { core::mem::transmute_copy(&intermediate) } + } + } + }; + ($($A:literal, [$($B:literal),+];)+) => { + $($(impl_simd_array_conversions!($A, $B);)+)+ + }; +} + +impl_simd_array_conversions! { + 1, [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, 64]; + 2, [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]; + 3, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21]; + 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + 6, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + 7, [1, 2, 3, 4, 5, 6, 7, 8, 9]; + 8, [1, 2, 3, 4, 5, 6, 7, 8]; + 9, [1, 2, 3, 4, 5, 6, 7]; + 10, [1, 2, 3, 4, 5, 6]; + 11, [1, 2, 3, 4, 5]; + 12, [1, 2, 3, 4, 5]; + 13, [1, 2, 3, 4]; + 14, [1, 2, 3, 4]; + 15, [1, 2, 3, 4]; + 16, [1, 2, 3, 4]; + 17, [1, 2, 3]; + 18, [1, 2, 3]; + 19, [1, 2, 3]; + 20, [1, 2, 3]; + 21, [1, 2, 3]; + 22, [1, 2]; + 23, [1, 2]; + 24, [1, 2]; + 25, [1, 2]; + 26, [1, 2]; + 27, [1, 2]; + 28, [1, 2]; + 29, [1, 2]; + 30, [1, 2]; + 31, [1, 2]; + 32, [1, 2]; + 33, [1]; + 34, [1]; + 35, [1]; + 36, [1]; + 37, [1]; + 38, [1]; + 39, [1]; + 40, [1]; + 41, [1]; + 42, [1]; + 43, [1]; + 44, [1]; + 45, [1]; + 46, [1]; + 47, [1]; + 48, [1]; + 49, [1]; + 50, [1]; + 51, [1]; + 52, [1]; + 53, [1]; + 54, [1]; + 55, [1]; + 56, [1]; + 57, [1]; + 58, [1]; + 59, [1]; + 60, [1]; + 61, [1]; + 62, [1]; + 63, [1]; + 64, [1]; +} + mod sealed { pub trait Sealed {} } From 4b06b434e6958b671103538411b9c788641aad87 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 29 May 2025 15:04:46 -0700 Subject: [PATCH 2/3] conversions from/to arrays of 1 element conflict, so limit to >= 2 --- crates/core_simd/src/vector.rs | 98 +++++++++++----------------------- 1 file changed, 31 insertions(+), 67 deletions(-) diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index 0b02f9de4c2..2c4319300ce 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -1121,75 +1121,39 @@ macro_rules! impl_simd_array_conversions { } impl_simd_array_conversions! { - 1, [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, 64]; - 2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 2, [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]; - 3, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 3, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]; - 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; - 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; - 6, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - 7, [1, 2, 3, 4, 5, 6, 7, 8, 9]; - 8, [1, 2, 3, 4, 5, 6, 7, 8]; - 9, [1, 2, 3, 4, 5, 6, 7]; - 10, [1, 2, 3, 4, 5, 6]; - 11, [1, 2, 3, 4, 5]; - 12, [1, 2, 3, 4, 5]; - 13, [1, 2, 3, 4]; - 14, [1, 2, 3, 4]; - 15, [1, 2, 3, 4]; - 16, [1, 2, 3, 4]; - 17, [1, 2, 3]; - 18, [1, 2, 3]; - 19, [1, 2, 3]; - 20, [1, 2, 3]; - 21, [1, 2, 3]; - 22, [1, 2]; - 23, [1, 2]; - 24, [1, 2]; - 25, [1, 2]; - 26, [1, 2]; - 27, [1, 2]; - 28, [1, 2]; - 29, [1, 2]; - 30, [1, 2]; - 31, [1, 2]; - 32, [1, 2]; - 33, [1]; - 34, [1]; - 35, [1]; - 36, [1]; - 37, [1]; - 38, [1]; - 39, [1]; - 40, [1]; - 41, [1]; - 42, [1]; - 43, [1]; - 44, [1]; - 45, [1]; - 46, [1]; - 47, [1]; - 48, [1]; - 49, [1]; - 50, [1]; - 51, [1]; - 52, [1]; - 53, [1]; - 54, [1]; - 55, [1]; - 56, [1]; - 57, [1]; - 58, [1]; - 59, [1]; - 60, [1]; - 61, [1]; - 62, [1]; - 63, [1]; - 64, [1]; + 4, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + 5, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + 6, [2, 3, 4, 5, 6, 7, 8, 9, 10]; + 7, [2, 3, 4, 5, 6, 7, 8, 9]; + 8, [2, 3, 4, 5, 6, 7, 8]; + 9, [2, 3, 4, 5, 6, 7]; + 10, [2, 3, 4, 5, 6]; + 11, [2, 3, 4, 5]; + 12, [2, 3, 4, 5]; + 13, [2, 3, 4]; + 14, [2, 3, 4]; + 15, [2, 3, 4]; + 16, [2, 3, 4]; + 17, [2, 3]; + 18, [2, 3]; + 19, [2, 3]; + 20, [2, 3]; + 21, [2, 3]; + 22, [2]; + 23, [2]; + 24, [2]; + 25, [2]; + 26, [2]; + 27, [2]; + 28, [2]; + 29, [2]; + 30, [2]; + 31, [2]; + 32, [2]; } mod sealed { From cfc7516bb7df78c1d789e73fad357ef778946dd0 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 29 May 2025 20:11:39 -0700 Subject: [PATCH 3/3] simd/array-of-simd conversion: describe how size pairs are chosen --- crates/core_simd/src/vector.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index 2c4319300ce..fed0d5a81a4 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -1120,6 +1120,7 @@ macro_rules! impl_simd_array_conversions { }; } +// this covers all `A`, `B` pairs where `A >= 2 && B >= 2 && A * B <= 64` impl_simd_array_conversions! { 2, [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];