Skip to content

Add SimdInt::saturating_{abs,neg} - #450

Open
RunDevelopment wants to merge 4 commits into
linebender:mainfrom
RunDevelopment:sat-neg-abs
Open

RunDevelopment wants to merge 4 commits into
linebender:mainfrom
RunDevelopment:sat-neg-abs

Conversation

@RunDevelopment

Copy link
Copy Markdown
Contributor

Related to #355

This PR adds saturating negation and absolute value for integer vectors.

  • Neon just has instructions for both, so the implementation is trivial.
  • WASM has neither, so I do max(x, iN::MIN + 1) before applying the wrapping operation. Both wrapping operations only wrap on iN::MIN, so I map iN::MIN to iN::MIN+1 to get rid of the wrapping.
  • x86 uses the same trick as WASM with one caveat: max doesn't have a native instruction for some combinations of platform x scalar type. E.g. AVX2 and SSE4.2 don't have a max for i64. So I fallback to a second trick: we know that the output of the wrapping operation is iN::MIN iff wrapping occurred. So let t be the output of the wrapping operation, then we return t ^ eq(t, iN::MIN). The xor negates t iff wrapping occurred, and !iN::MIN == iN::MAX so we get the correct result.

Of course, that is only for signed integer vectors. The implementation of unsigned is trivial, so I added default implementations for them in the Simd trait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant