Skip to content

Commit

Permalink
Better to/from slice operations
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed Oct 20, 2024
1 parent 2a3f6d4 commit 02d0a3b
Show file tree
Hide file tree
Showing 34 changed files with 66 additions and 98 deletions.
5 changes: 2 additions & 3 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl {{ self_t }} {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[{{ scalar_t }}]) -> Self {
assert!(slice.len() >= {{ dim }});
Self::new(
{% for c in components %}
slice[{{ loop.index0 }}],
Expand All @@ -504,9 +505,7 @@ impl {{ self_t }} {
assert!(slice.len() >= 4);
unsafe { vst1q_f32(slice.as_mut_ptr(), self.0); }
{% else %}
{% for c in components %}
slice[{{ loop.index0 }}] = self.{{ c }};
{%- endfor %}
slice.copy_from_slice(&self.to_array());
{% endif %}
}

Expand Down
5 changes: 2 additions & 3 deletions src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl Vec3A {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -142,9 +143,7 @@ impl Vec3A {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
Expand Down
6 changes: 2 additions & 4 deletions src/f32/coresimd/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl Vec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand All @@ -144,10 +145,7 @@ impl Vec4 {
/// Panics if `slice` is less than 4 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
Expand Down
5 changes: 2 additions & 3 deletions src/f32/neon/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl Vec3A {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -147,9 +148,7 @@ impl Vec3A {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
Expand Down
1 change: 1 addition & 0 deletions src/f32/neon/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl Vec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand Down
5 changes: 2 additions & 3 deletions src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl Vec3A {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -149,9 +150,7 @@ impl Vec3A {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
Expand Down
6 changes: 2 additions & 4 deletions src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl Vec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand All @@ -168,10 +169,7 @@ impl Vec4 {
/// Panics if `slice` is less than 4 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
Expand Down
5 changes: 2 additions & 3 deletions src/f32/sse2/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl Vec3A {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -155,9 +156,7 @@ impl Vec3A {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
Expand Down
1 change: 1 addition & 0 deletions src/f32/sse2/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl Vec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand Down
4 changes: 2 additions & 2 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl Vec2 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 2);
Self::new(slice[0], slice[1])
}

Expand All @@ -133,8 +134,7 @@ impl Vec2 {
/// Panics if `slice` is less than 2 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from `self` and the given `z` value.
Expand Down
5 changes: 2 additions & 3 deletions src/f32/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Vec3 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -140,9 +141,7 @@ impl Vec3 {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
Expand Down
5 changes: 2 additions & 3 deletions src/f32/wasm32/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl Vec3A {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -141,9 +142,7 @@ impl Vec3A {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
Expand Down
6 changes: 2 additions & 4 deletions src/f32/wasm32/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl Vec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f32]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand All @@ -143,10 +144,7 @@ impl Vec4 {
/// Panics if `slice` is less than 4 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
Expand Down
4 changes: 2 additions & 2 deletions src/f64/dvec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl DVec2 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f64]) -> Self {
assert!(slice.len() >= 2);
Self::new(slice[0], slice[1])
}

Expand All @@ -133,8 +134,7 @@ impl DVec2 {
/// Panics if `slice` is less than 2 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f64]) {
slice[0] = self.x;
slice[1] = self.y;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from `self` and the given `z` value.
Expand Down
5 changes: 2 additions & 3 deletions src/f64/dvec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl DVec3 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f64]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -140,9 +141,7 @@ impl DVec3 {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f64]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
Expand Down
6 changes: 2 additions & 4 deletions src/f64/dvec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl DVec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[f64]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand All @@ -159,10 +160,7 @@ impl DVec4 {
/// Panics if `slice` is less than 4 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [f64]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
Expand Down
4 changes: 2 additions & 2 deletions src/i16/i16vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl I16Vec2 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[i16]) -> Self {
assert!(slice.len() >= 2);
Self::new(slice[0], slice[1])
}

Expand All @@ -125,8 +126,7 @@ impl I16Vec2 {
/// Panics if `slice` is less than 2 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [i16]) {
slice[0] = self.x;
slice[1] = self.y;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from `self` and the given `z` value.
Expand Down
5 changes: 2 additions & 3 deletions src/i16/i16vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl I16Vec3 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[i16]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -132,9 +133,7 @@ impl I16Vec3 {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [i16]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
Expand Down
6 changes: 2 additions & 4 deletions src/i16/i16vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl I16Vec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[i16]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand All @@ -151,10 +152,7 @@ impl I16Vec4 {
/// Panics if `slice` is less than 4 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [i16]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
Expand Down
4 changes: 2 additions & 2 deletions src/i32/ivec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl IVec2 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[i32]) -> Self {
assert!(slice.len() >= 2);
Self::new(slice[0], slice[1])
}

Expand All @@ -125,8 +126,7 @@ impl IVec2 {
/// Panics if `slice` is less than 2 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [i32]) {
slice[0] = self.x;
slice[1] = self.y;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from `self` and the given `z` value.
Expand Down
5 changes: 2 additions & 3 deletions src/i32/ivec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl IVec3 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[i32]) -> Self {
assert!(slice.len() >= 3);
Self::new(slice[0], slice[1], slice[2])
}

Expand All @@ -132,9 +133,7 @@ impl IVec3 {
/// Panics if `slice` is less than 3 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [i32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice.copy_from_slice(&self.to_array());
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
Expand Down
6 changes: 2 additions & 4 deletions src/i32/ivec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl IVec4 {
#[inline]
#[must_use]
pub const fn from_slice(slice: &[i32]) -> Self {
assert!(slice.len() >= 4);
Self::new(slice[0], slice[1], slice[2], slice[3])
}

Expand All @@ -151,10 +152,7 @@ impl IVec4 {
/// Panics if `slice` is less than 4 elements long.
#[inline]
pub fn write_to_slice(self, slice: &mut [i32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
slice.copy_from_slice(&self.to_array());
}

/// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
Expand Down
Loading

0 comments on commit 02d0a3b

Please sign in to comment.