Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checked_* elementwise arithmetic operations for {I,U}Vec{2,3,4} #596

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,74 @@ impl {{ self_t }} {
{% endif %}

{% if not is_float %}
/// Returns a vector containing the wrapping addition of `self` and `rhs`.
///
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_add(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// Returns a vector containing the wrapping subtraction of `self` and `rhs`.
///
/// In other words this computes `Some([self.x - rhs.x, self.y - rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_sub(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// Returns a vector containing the wrapping multiplication of `self` and `rhs`.
///
/// In other words this computes `Some([self.x * rhs.x, self.y * rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_mul(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// Returns a vector containing the wrapping division of `self` and `rhs`.
///
/// In other words this computes `Some([self.x / rhs.x, self.y / rhs.y, ..])` but returns `None` on any division by zero.
#[inline]
#[must_use]
pub const fn checked_div(self, rhs: Self) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_div(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// Returns a vector containing the wrapping addition of `self` and `rhs`.
///
/// In other words this computes `[self.x.wrapping_add(rhs.x), self.y.wrapping_add(rhs.y), ..]`.
Expand Down Expand Up @@ -2686,6 +2754,40 @@ impl {{ self_t }} {
}
}
{% if is_signed %}
/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_add_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_add_unsigned(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `Some([self.x - rhs.x, self.y - rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_sub_unsigned(self, rhs: {{ opposite_signedness_t }}) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_sub_unsigned(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// 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), ..]`.
Expand Down Expand Up @@ -2738,6 +2840,23 @@ impl {{ self_t }} {
}
}
{% else %}
/// Returns a vector containing the wrapping addition of `self` and signed vector `rhs`.
///
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_add_signed(self, rhs: {{ opposite_signedness_t }}) -> Option<Self> {
{% for c in components %}
let {{ c }} = match self.{{ c }}.checked_add_signed(rhs.{{ c }}) { Some(v) => v, None => {return None} };
{%- endfor %}

Some(Self {
{% for c in components %}
{{ c }},
{%- endfor %}
})
}

/// 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), ..]`.
Expand Down
108 changes: 108 additions & 0 deletions src/i16/i16vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,78 @@ impl I16Vec2 {
crate::U64Vec2::new(self.x as u64, self.y as u64)
}

/// Returns a vector containing the wrapping addition of `self` and `rhs`.
///
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
let x = match self.x.checked_add(rhs.x) {
Some(v) => v,
None => return None,
};
let y = match self.y.checked_add(rhs.y) {
Some(v) => v,
None => return None,
};

Some(Self { x, y })
}

/// Returns a vector containing the wrapping subtraction of `self` and `rhs`.
///
/// In other words this computes `Some([self.x - rhs.x, self.y - rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
let x = match self.x.checked_sub(rhs.x) {
Some(v) => v,
None => return None,
};
let y = match self.y.checked_sub(rhs.y) {
Some(v) => v,
None => return None,
};

Some(Self { x, y })
}

/// Returns a vector containing the wrapping multiplication of `self` and `rhs`.
///
/// In other words this computes `Some([self.x * rhs.x, self.y * rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
let x = match self.x.checked_mul(rhs.x) {
Some(v) => v,
None => return None,
};
let y = match self.y.checked_mul(rhs.y) {
Some(v) => v,
None => return None,
};

Some(Self { x, y })
}

/// Returns a vector containing the wrapping division of `self` and `rhs`.
///
/// In other words this computes `Some([self.x / rhs.x, self.y / rhs.y, ..])` but returns `None` on any division by zero.
#[inline]
#[must_use]
pub const fn checked_div(self, rhs: Self) -> Option<Self> {
let x = match self.x.checked_div(rhs.x) {
Some(v) => v,
None => return None,
};
let y = match self.y.checked_div(rhs.y) {
Some(v) => v,
None => return None,
};

Some(Self { x, y })
}

/// Returns a vector containing the wrapping addition of `self` and `rhs`.
///
/// In other words this computes `[self.x.wrapping_add(rhs.x), self.y.wrapping_add(rhs.y), ..]`.
Expand Down Expand Up @@ -608,6 +680,42 @@ impl I16Vec2 {
}
}

/// Returns a vector containing the wrapping addition of `self` and unsigned vector `rhs`.
///
/// In other words this computes `Some([self.x + rhs.x, self.y + rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_add_unsigned(self, rhs: U16Vec2) -> Option<Self> {
let x = match self.x.checked_add_unsigned(rhs.x) {
Some(v) => v,
None => return None,
};
let y = match self.y.checked_add_unsigned(rhs.y) {
Some(v) => v,
None => return None,
};

Some(Self { x, y })
}

/// Returns a vector containing the wrapping subtraction of `self` and unsigned vector `rhs`.
///
/// In other words this computes `Some([self.x - rhs.x, self.y - rhs.y, ..])` but returns `None` on any overflow.
#[inline]
#[must_use]
pub const fn checked_sub_unsigned(self, rhs: U16Vec2) -> Option<Self> {
let x = match self.x.checked_sub_unsigned(rhs.x) {
Some(v) => v,
None => return None,
};
let y = match self.y.checked_sub_unsigned(rhs.y) {
Some(v) => v,
None => return None,
};

Some(Self { x, 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), ..]`.
Expand Down
Loading
Loading