rust-lang / rust-lang/rust

Change `rint` to `roundeven` in `round_ties_even` implementation

Open
#136,459 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-floating-point A-LLVM T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

C has three reasonably similar ways to round a single float to even. I'll just copy the docs here, nearbyint:

Description

The nearbyint functions round their argument to an integer value in floating-point format, using the current rounding direction and without raising the "inexact" floating-point exception.

Returns

The nearbyint functions return the rounded integer value.

rint:

Description

The rint functions differ from the nearbyint functions (7.12.9.3) only in that the rint functions may raise the "inexact" floating-point exception if the result differs in value from the argument

Returns

The rint functions return the rounded integer value.

And roundeven, which is available since C23:

Description

The roundeven functions round their argument to the nearest integer value in floating-point format, rounding halfway cases to even (that is, to the nearest value that is an even integer), regardless of the current rounding direction

Returns

The round functions return the rounded integer value.

We currently use rintf16, rintf32, rintf64, and rintf128 (e.g. https://doc.rust-lang.org/std/intrinsics/fn.rintf32.html) as the intrinsics to implement round_ties_even. These map to LLVM's rint and similar:

https://github.com/rust-lang/rust/blob/613bdd49978298648ed05ace086bd1ecad54b44a/compiler/rustc_codegen_llvm/src/intrinsic.rs#L129-L132

rint is not exactly what we want here because it is supposed to raise inexact. We should change to roundeven because it matches Rust's preference of ignoring rounding modes and never raising fp exceptions.

This may be blocked on platform support, e.g. https://github.com/llvm/llvm-project/issues/73588. that issue looks like it is about the vector version.

Cc https://github.com/rust-lang/rust/issues/55107.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in compiler/rustc_codegen_llvm/src/intrinsic.rs at the rintf16, rintf32, rintf64, and rintf128 mappings used by round_ties_even. Check the linked LLVM support issue and verify that roundeven is available for the required platforms; done means the implementation uses roundeven semantics without depending on the current rounding mode or raising inexact.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.