Skip to content

Commit

Permalink
applied suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecificProtagonist committed Jul 29, 2023
1 parent e4a8d9f commit ee87442
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 338 deletions.
42 changes: 20 additions & 22 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -1076,45 +1076,43 @@ impl {{ self_t }} {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
{%- if not is_float%}
///
{% if is_float%}
{% if not is_scalar %}
/// This is not SIMD-accelerated.
{% endif %}
{% else %}
/// # Panics
/// This function will panic if any `rhs` element is 0 or the division results in overflow.
{% endif %}
{% if is_float %} #[cfg(not(feature = "libm"))] {% endif %}
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..{{ dim }} {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
{% for c in components %}
{% if is_float %}
math::div_euclid(self.{{ c }}, rhs.{{ c }}),
{% else %}
self.{{ c }}.div_euclid(rhs.{{ c }}),
{% endif %}
{%- endfor %}
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
{%- if not is_float %}
///
{% if is_float %}
{% if not is_scalar %}
/// This is not SIMD-accelerated.
{% endif %}
{% else %}
/// # Panics
/// This function will panic if any `rhs` element is 0 or the division results in overflow.
{% endif %}
///
/// [Euclidean division]: {{scalar_t}}::rem_euclid
{% if is_float %} #[cfg(not(feature = "libm"))] {% endif %}
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..{{ dim }} {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
{% for c in components %}
{% if is_float %}
math::rem_euclid(self.{{ c }}, rhs.{{ c }}),
{% else %}
self.{{ c }}.rem_euclid(rhs.{{ c }}),
{% endif %}
{%- endfor %}
)
}
{% endif %}

Expand Down
30 changes: 10 additions & 20 deletions src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,35 +380,25 @@ impl Vec3A {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

#[cfg(not(feature = "libm"))]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..3 {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
math::div_euclid(self.x, rhs.x),
math::div_euclid(self.y, rhs.y),
math::div_euclid(self.z, rhs.z),
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

///
/// [Euclidean division]: f32::rem_euclid
#[cfg(not(feature = "libm"))]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..3 {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
math::rem_euclid(self.x, rhs.x),
math::rem_euclid(self.y, rhs.y),
math::rem_euclid(self.z, rhs.z),
)
}

/// Returns `self` normalized to length 1.0.
Expand Down
32 changes: 12 additions & 20 deletions src/f32/coresimd/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,35 +353,27 @@ impl Vec4 {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

#[cfg(not(feature = "libm"))]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..4 {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
math::div_euclid(self.x, rhs.x),
math::div_euclid(self.y, rhs.y),
math::div_euclid(self.z, rhs.z),
math::div_euclid(self.w, rhs.w),
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

///
/// [Euclidean division]: f32::rem_euclid
#[cfg(not(feature = "libm"))]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..4 {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
math::rem_euclid(self.x, rhs.x),
math::rem_euclid(self.y, rhs.y),
math::rem_euclid(self.z, rhs.z),
math::rem_euclid(self.w, rhs.w),
)
}

/// Returns `self` normalized to length 1.0.
Expand Down
30 changes: 30 additions & 0 deletions src/f32/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ mod libm_math {
pub(crate) fn mul_add(a: f32, b: f32, c: f32) -> f32 {
libm::fmaf(a, b, c)
}

#[inline]
pub fn div_euclid(a: f32, b: f32) -> f32 {
// Based on https://doc.rust-lang.org/src/std/f32.rs.html#293
let q = libm::truncf(a / b);
if a % b < 0.0 {
return if b > 0.0 { q - 1.0 } else { q + 1.0 };
}
q
}

#[inline]
pub fn rem_euclid(a: f32, b: f32) -> f32 {
let r = a % b;
if r < 0.0 {
r + abs(b)
} else {
r
}
}
}

#[cfg(not(feature = "libm"))]
Expand Down Expand Up @@ -212,6 +232,16 @@ mod std_math {
pub(crate) fn mul_add(a: f32, b: f32, c: f32) -> f32 {
f32::mul_add(a, b, c)
}

#[inline]
pub fn div_euclid(a: f32, b: f32) -> f32 {
f32::div_euclid(a, b)
}

#[inline]
pub fn rem_euclid(a: f32, b: f32) -> f32 {
f32::rem_euclid(a, b)
}
}

#[cfg(feature = "libm")]
Expand Down
26 changes: 10 additions & 16 deletions src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,31 +402,25 @@ impl Vec3A {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
///

#[cfg(not(feature = "libm"))]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..3 {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
math::div_euclid(self.x, rhs.x),
math::div_euclid(self.y, rhs.y),
math::div_euclid(self.z, rhs.z),
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
///

///
/// [Euclidean division]: f32::rem_euclid
#[cfg(not(feature = "libm"))]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..3 {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
math::rem_euclid(self.x, rhs.x),
math::rem_euclid(self.y, rhs.y),
math::rem_euclid(self.z, rhs.z),
)
}

/// Returns `self` normalized to length 1.0.
Expand Down
28 changes: 12 additions & 16 deletions src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,31 +433,27 @@ impl Vec4 {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
///

#[cfg(not(feature = "libm"))]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..4 {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
math::div_euclid(self.x, rhs.x),
math::div_euclid(self.y, rhs.y),
math::div_euclid(self.z, rhs.z),
math::div_euclid(self.w, rhs.w),
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
///

///
/// [Euclidean division]: f32::rem_euclid
#[cfg(not(feature = "libm"))]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..4 {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
math::rem_euclid(self.x, rhs.x),
math::rem_euclid(self.y, rhs.y),
math::rem_euclid(self.z, rhs.z),
math::rem_euclid(self.w, rhs.w),
)
}

/// Returns `self` normalized to length 1.0.
Expand Down
30 changes: 10 additions & 20 deletions src/f32/sse2/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,35 +416,25 @@ impl Vec3A {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

#[cfg(not(feature = "libm"))]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..3 {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
math::div_euclid(self.x, rhs.x),
math::div_euclid(self.y, rhs.y),
math::div_euclid(self.z, rhs.z),
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

///
/// [Euclidean division]: f32::rem_euclid
#[cfg(not(feature = "libm"))]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..3 {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
math::rem_euclid(self.x, rhs.x),
math::rem_euclid(self.y, rhs.y),
math::rem_euclid(self.z, rhs.z),
)
}

/// Returns `self` normalized to length 1.0.
Expand Down
32 changes: 12 additions & 20 deletions src/f32/sse2/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,35 +390,27 @@ impl Vec4 {
}

/// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

#[cfg(not(feature = "libm"))]
#[inline]
pub fn div_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..4 {
out[i] = self[i].div_euclid(rhs[i]);
}
out
Self::new(
math::div_euclid(self.x, rhs.x),
math::div_euclid(self.y, rhs.y),
math::div_euclid(self.z, rhs.z),
math::div_euclid(self.w, rhs.w),
)
}

/// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
///

/// This is not SIMD-accelerated.

///
/// [Euclidean division]: f32::rem_euclid
#[cfg(not(feature = "libm"))]
#[inline]
pub fn rem_euclid(self, rhs: Self) -> Self {
let mut out = Self::default();
for i in 0..4 {
out[i] = self[i].rem_euclid(rhs[i]);
}
out
Self::new(
math::rem_euclid(self.x, rhs.x),
math::rem_euclid(self.y, rhs.y),
math::rem_euclid(self.z, rhs.z),
math::rem_euclid(self.w, rhs.w),
)
}

/// Returns `self` normalized to length 1.0.
Expand Down
Loading

0 comments on commit ee87442

Please sign in to comment.