Skip to content

Commit

Permalink
Signed and unsigned wrapping and saturating add and sub for integer v…
Browse files Browse the repository at this point in the history
…ectors. (#460) (#461)
  • Loading branch information
Ababwa authored Jan 11, 2024
1 parent df80aae commit 76f6a19
Show file tree
Hide file tree
Showing 22 changed files with 1,187 additions and 0 deletions.
85 changes: 85 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
{% set is_signed = true %}
{% set is_float = false %}
{% set self_t = "I16Vec" ~ dim %}
{% set opposite_signedness_t = "U16Vec" ~ dim %}
{% set vec2_t = "I16Vec2" %}
{% set vec3_t = "I16Vec3" %}
{% set vec4_t = "I16Vec4" %}
Expand All @@ -51,6 +52,7 @@
{% set is_signed = false %}
{% set is_float = false %}
{% set self_t = "U16Vec" ~ dim %}
{% set opposite_signedness_t = "I16Vec" ~ dim %}
{% set vec2_t = "U16Vec2" %}
{% set vec3_t = "U16Vec3" %}
{% set vec4_t = "U16Vec4" %}
Expand All @@ -59,6 +61,7 @@
{% set is_signed = true %}
{% set is_float = false %}
{% set self_t = "IVec" ~ dim %}
{% set opposite_signedness_t = "UVec" ~ dim %}
{% set vec2_t = "IVec2" %}
{% set vec3_t = "IVec3" %}
{% set vec4_t = "IVec4" %}
Expand All @@ -68,6 +71,7 @@
{% set is_signed = false %}
{% set is_float = false %}
{% set self_t = "UVec" ~ dim %}
{% set opposite_signedness_t = "IVec" ~ dim %}
{% set vec2_t = "UVec2" %}
{% set vec3_t = "UVec3" %}
{% set vec4_t = "UVec4" %}
Expand All @@ -77,6 +81,7 @@
{% set is_signed = true %}
{% set is_float = false %}
{% set self_t = "I64Vec" ~ dim %}
{% set opposite_signedness_t = "U64Vec" ~ dim %}
{% set vec2_t = "I64Vec2" %}
{% set vec3_t = "I64Vec3" %}
{% set vec4_t = "I64Vec4" %}
Expand All @@ -86,6 +91,7 @@
{% set is_signed = false %}
{% set is_float = false %}
{% set self_t = "U64Vec" ~ dim %}
{% set opposite_signedness_t = "I64Vec" ~ dim %}
{% set vec2_t = "U64Vec2" %}
{% set vec3_t = "U64Vec3" %}
{% set vec4_t = "U64Vec4" %}
Expand Down Expand Up @@ -2019,6 +2025,85 @@ impl {{ self_t }} {
{%- endfor %}
}
}
{% if is_signed %}
/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_add_unsigned(rhs.x), self.y.wrapping_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_add_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self {
Self {
{% for c in components %}
{{ c }}: self.{{ c }}.wrapping_add_unsigned(rhs.{{ c }}),
{%- endfor %}
}
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_sub_unsigned(rhs.x), self.y.wrapping_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_sub_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self {
Self {
{% for c in components %}
{{ c }}: self.{{ c }}.wrapping_sub_unsigned(rhs.{{ c }}),
{%- endfor %}
}
}

// Returns a vector containing the saturating addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_add_unsigned(rhs.x), self.y.saturating_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_add_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self {
Self {
{% for c in components %}
{{ c }}: self.{{ c }}.saturating_add_unsigned(rhs.{{ c }}),
{%- endfor %}
}
}

/// Returns a vector containing the saturating subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_sub_unsigned(rhs.x), self.y.saturating_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_sub_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Self {
Self {
{% for c in components %}
{{ c }}: self.{{ c }}.saturating_sub_unsigned(rhs.{{ c }}),
{%- endfor %}
}
}
{% else %}
/// Returns a vector containing the wrapping addition of `self` and signed vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_add_signed(rhs.x), self.y.wrapping_add_signed(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_add_signed(self, rhs: {{ opposite_signedness_t }}) -> Self {
Self {
{% for c in components %}
{{ c }}: self.{{ c }}.wrapping_add_signed(rhs.{{ c }}),
{%- endfor %}
}
}

/// Returns a vector containing the saturating addition of `self` and signed vector `rhs`.
///
/// In other words this computes `[self.x.saturating_add_signed(rhs.x), self.y.saturating_add_signed(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_add_signed(self, rhs: {{ opposite_signedness_t }}) -> Self {
Self {
{% for c in components %}
{{ c }}: self.{{ c }}.saturating_add_signed(rhs.{{ c }}),
{%- endfor %}
}
}
{% endif %}
{% endif %}
}

Expand Down
48 changes: 48 additions & 0 deletions src/i16/i16vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,54 @@ impl I16Vec2 {
y: self.y.saturating_div(rhs.y),
}
}

/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_add_unsigned(rhs.x), self.y.wrapping_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_add_unsigned(self, rhs: U16Vec2) -> Self {
Self {
x: self.x.wrapping_add_unsigned(rhs.x),
y: self.y.wrapping_add_unsigned(rhs.y),
}
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_sub_unsigned(rhs.x), self.y.wrapping_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_sub_unsigned(self, rhs: U16Vec2) -> Self {
Self {
x: self.x.wrapping_sub_unsigned(rhs.x),
y: self.y.wrapping_sub_unsigned(rhs.y),
}
}

// Returns a vector containing the saturating addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_add_unsigned(rhs.x), self.y.saturating_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_add_unsigned(self, rhs: U16Vec2) -> Self {
Self {
x: self.x.saturating_add_unsigned(rhs.x),
y: self.y.saturating_add_unsigned(rhs.y),
}
}

/// Returns a vector containing the saturating subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_sub_unsigned(rhs.x), self.y.saturating_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_sub_unsigned(self, rhs: U16Vec2) -> Self {
Self {
x: self.x.saturating_sub_unsigned(rhs.x),
y: self.y.saturating_sub_unsigned(rhs.y),
}
}
}

impl Default for I16Vec2 {
Expand Down
52 changes: 52 additions & 0 deletions src/i16/i16vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,58 @@ impl I16Vec3 {
z: self.z.saturating_div(rhs.z),
}
}

/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_add_unsigned(rhs.x), self.y.wrapping_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_add_unsigned(self, rhs: U16Vec3) -> Self {
Self {
x: self.x.wrapping_add_unsigned(rhs.x),
y: self.y.wrapping_add_unsigned(rhs.y),
z: self.z.wrapping_add_unsigned(rhs.z),
}
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_sub_unsigned(rhs.x), self.y.wrapping_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_sub_unsigned(self, rhs: U16Vec3) -> Self {
Self {
x: self.x.wrapping_sub_unsigned(rhs.x),
y: self.y.wrapping_sub_unsigned(rhs.y),
z: self.z.wrapping_sub_unsigned(rhs.z),
}
}

// Returns a vector containing the saturating addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_add_unsigned(rhs.x), self.y.saturating_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_add_unsigned(self, rhs: U16Vec3) -> Self {
Self {
x: self.x.saturating_add_unsigned(rhs.x),
y: self.y.saturating_add_unsigned(rhs.y),
z: self.z.saturating_add_unsigned(rhs.z),
}
}

/// Returns a vector containing the saturating subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_sub_unsigned(rhs.x), self.y.saturating_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_sub_unsigned(self, rhs: U16Vec3) -> Self {
Self {
x: self.x.saturating_sub_unsigned(rhs.x),
y: self.y.saturating_sub_unsigned(rhs.y),
z: self.z.saturating_sub_unsigned(rhs.z),
}
}
}

impl Default for I16Vec3 {
Expand Down
56 changes: 56 additions & 0 deletions src/i16/i16vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,62 @@ impl I16Vec4 {
w: self.w.saturating_div(rhs.w),
}
}

/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_add_unsigned(rhs.x), self.y.wrapping_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_add_unsigned(self, rhs: U16Vec4) -> Self {
Self {
x: self.x.wrapping_add_unsigned(rhs.x),
y: self.y.wrapping_add_unsigned(rhs.y),
z: self.z.wrapping_add_unsigned(rhs.z),
w: self.w.wrapping_add_unsigned(rhs.w),
}
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_sub_unsigned(rhs.x), self.y.wrapping_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_sub_unsigned(self, rhs: U16Vec4) -> Self {
Self {
x: self.x.wrapping_sub_unsigned(rhs.x),
y: self.y.wrapping_sub_unsigned(rhs.y),
z: self.z.wrapping_sub_unsigned(rhs.z),
w: self.w.wrapping_sub_unsigned(rhs.w),
}
}

// Returns a vector containing the saturating addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_add_unsigned(rhs.x), self.y.saturating_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_add_unsigned(self, rhs: U16Vec4) -> Self {
Self {
x: self.x.saturating_add_unsigned(rhs.x),
y: self.y.saturating_add_unsigned(rhs.y),
z: self.z.saturating_add_unsigned(rhs.z),
w: self.w.saturating_add_unsigned(rhs.w),
}
}

/// Returns a vector containing the saturating subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_sub_unsigned(rhs.x), self.y.saturating_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_sub_unsigned(self, rhs: U16Vec4) -> Self {
Self {
x: self.x.saturating_sub_unsigned(rhs.x),
y: self.y.saturating_sub_unsigned(rhs.y),
z: self.z.saturating_sub_unsigned(rhs.z),
w: self.w.saturating_sub_unsigned(rhs.w),
}
}
}

impl Default for I16Vec4 {
Expand Down
48 changes: 48 additions & 0 deletions src/i32/ivec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,54 @@ impl IVec2 {
y: self.y.saturating_div(rhs.y),
}
}

/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_add_unsigned(rhs.x), self.y.wrapping_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_add_unsigned(self, rhs: UVec2) -> Self {
Self {
x: self.x.wrapping_add_unsigned(rhs.x),
y: self.y.wrapping_add_unsigned(rhs.y),
}
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.wrapping_sub_unsigned(rhs.x), self.y.wrapping_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn wrapping_sub_unsigned(self, rhs: UVec2) -> Self {
Self {
x: self.x.wrapping_sub_unsigned(rhs.x),
y: self.y.wrapping_sub_unsigned(rhs.y),
}
}

// Returns a vector containing the saturating addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_add_unsigned(rhs.x), self.y.saturating_add_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_add_unsigned(self, rhs: UVec2) -> Self {
Self {
x: self.x.saturating_add_unsigned(rhs.x),
y: self.y.saturating_add_unsigned(rhs.y),
}
}

/// Returns a vector containing the saturating subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `[self.x.saturating_sub_unsigned(rhs.x), self.y.saturating_sub_unsigned(rhs.y), ..]`.
#[inline]
#[must_use]
pub const fn saturating_sub_unsigned(self, rhs: UVec2) -> Self {
Self {
x: self.x.saturating_sub_unsigned(rhs.x),
y: self.y.saturating_sub_unsigned(rhs.y),
}
}
}

impl Default for IVec2 {
Expand Down
Loading

0 comments on commit 76f6a19

Please sign in to comment.