Skip to content

Add round - #409

Merged
Shnatsel merged 3 commits into
linebender:mainfrom
RunDevelopment:float-round
Sep 30, 2026
Merged

Shnatsel merged 3 commits into
linebender:mainfrom
RunDevelopment:float-round

Conversation

@RunDevelopment

@RunDevelopment RunDevelopment commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

See #355

This implements round for all floating-point types to match std::simd.

On ARM, it gets lowered to the vrndaq family of instructions. On x86 and WASM it's emulated using trunc. The fallback uses scalar f<N>::round as usual. Because of the fallback, we now also depend on libm's round{,f} functions.

Notes on the emulation:

  • It's the same trick LLVM uses, so it naturally gives bit-exact results.
  • My use of copysign(a, b) might not be perfectly optimal. On x86 and WASM, it's implemented using bitwise masking. Part of that is setting the sign bit of a to 0. The emulation uses a positive constant so that part is unnecessary. I'm not sure if LLVM can optimize it away.
    Update: LLVM successfully optimizes copysign to use VPTERNLOG with AVX512. So emulation is optimal with AVX512.

Comment thread fearless_simd/src/generated/simd_trait.rs Outdated
@Shnatsel

Copy link
Copy Markdown
Contributor

Looks good. Thank you!

@Shnatsel
Shnatsel added this pull request to the merge queue Sep 30, 2026
Merged via the queue into linebender:main with commit b619aa5 Sep 30, 2026
23 checks passed
@RunDevelopment
RunDevelopment deleted the float-round branch October 1, 2026 11:14
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.

2 participants