Skip to content

Commit

Permalink
Added abs method to Vec2, Vec3 and Vec4.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Oct 9, 2019
1 parent d32ac49 commit c54b102
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ impl Vec2 {
pub(crate) fn neg_mul_sub(self, a: Self, b: Self) -> Self {
Self(b.0 - (self.0 * a.0), b.1 - (self.1 * a.1))
}

/// Returns a new `Vec2` containing the absolute value of each component of the original
/// `Vec2`.
#[inline]
pub fn abs(self) -> Self {
Self(self.0.abs(), self.1.abs())
}
}

impl fmt::Display for Vec2 {
Expand Down
5 changes: 5 additions & 0 deletions src/f32/vec3_f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ impl Vec3 {
b.2 - (self.2 * a.2),
)
}

#[inline]
pub fn abs(self) -> Self {
Self(self.0.abs(), self.1.abs(), self.2.abs())
}
}

impl fmt::Display for Vec3 {
Expand Down
12 changes: 12 additions & 0 deletions src/f32/vec3_sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,18 @@ impl Vec3 {
pub(crate) fn neg_mul_sub(self, a: Self, b: Self) -> Self {
unsafe { Self(_mm_sub_ps(b.0, _mm_mul_ps(self.0, a.0))) }
}

/// Returns a new `Vec3` containing the absolute value of each component of the original
/// `Vec3`.
#[inline]
pub fn abs(self) -> Self {
unsafe {
Self(_mm_and_ps(
self.0,
_mm_castsi128_ps(_mm_set1_epi32(0x7f_ff_ff_ff)),
))
}
}
}

impl fmt::Display for Vec3 {
Expand Down
5 changes: 5 additions & 0 deletions src/f32/vec4_f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ impl Vec4 {
b.3 - (self.3 * a.3),
)
}

#[inline]
pub fn abs(self) -> Self {
Self(self.0.abs(), self.1.abs(), self.2.abs(), self.3.abs())
}
}

impl fmt::Display for Vec4 {
Expand Down
12 changes: 12 additions & 0 deletions src/f32/vec4_sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ impl Vec4 {
pub(crate) fn neg_mul_sub(self, a: Self, b: Self) -> Self {
unsafe { Self(_mm_sub_ps(b.0, _mm_mul_ps(self.0, a.0))) }
}

/// Returns a new `Vec4` containing the absolute value of each component of the original
/// `Vec4`.
#[inline]
pub fn abs(self) -> Self {
unsafe {
Self(_mm_and_ps(
self.0,
_mm_castsi128_ps(_mm_set1_epi32(0x7f_ff_ff_ff)),
))
}
}
}

impl fmt::Display for Vec4 {
Expand Down
14 changes: 14 additions & 0 deletions tests/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ fn test_vec2_rand() {
assert_eq!(a, b.into());
}

#[test]
fn test_vec2_sign() {
assert_eq!(Vec2::zero().sign(), Vec2::one());
assert_eq!(Vec2::one().sign(), Vec2::one());
assert_eq!((-Vec2::one()).sign(), -Vec2::one());
}

#[test]
fn test_vec2_abs() {
assert_eq!(Vec2::zero().abs(), Vec2::zero());
assert_eq!(Vec2::one().abs(), Vec2::one());
assert_eq!((-Vec2::one()).abs(), Vec2::one());
}

#[cfg(feature = "serde")]
#[test]
fn test_vec2_serde() {
Expand Down
14 changes: 14 additions & 0 deletions tests/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,20 @@ fn test_vec3_rand() {
assert_eq!(a, b.into());
}

#[test]
fn test_vec3_sign() {
assert_eq!(Vec3::zero().sign(), Vec3::one());
assert_eq!(Vec3::one().sign(), Vec3::one());
assert_eq!((-Vec3::one()).sign(), -Vec3::one());
}

#[test]
fn test_vec3_abs() {
assert_eq!(Vec3::zero().abs(), Vec3::zero());
assert_eq!(Vec3::one().abs(), Vec3::one());
assert_eq!((-Vec3::one()).abs(), Vec3::one());
}

#[cfg(feature = "serde")]
#[test]
fn test_vec3_serde() {
Expand Down
7 changes: 7 additions & 0 deletions tests/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ fn test_vec4_sign() {
assert_eq!(Vec4::splat(core::f32::NEG_INFINITY).sign(), -Vec4::one());
}

#[test]
fn test_vec4_abs() {
assert_eq!(Vec4::zero().abs(), Vec4::zero());
assert_eq!(Vec4::one().abs(), Vec4::one());
assert_eq!((-Vec4::one()).abs(), Vec4::one());
}

// #[test]
// fn dup_element() {
// let a = vec4(1.0, 2.0, 3.0, 4.0);
Expand Down

0 comments on commit c54b102

Please sign in to comment.