rust-lang / rust-lang/rustc_apfloat

Consider further speeding up division by `const`-generic specialization.

Open
#4 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
23
Forks
16
Avg merge
6h 52m
Merged PRs (30d)
1

Description

The main implementation of the ieee::sig::div "arbitrary-precision division" function could be made into a separate function with a const SPECIALIZE_FOR_KNOWN_DIVISOR: u128 const-generic parameter, which it would use as such:

// The parameter being `0` is like `None` - could use `Option<NonZeroU128>` in the future.
if SPECIALIZE_FOR_KNOWN_DIVISOR != 0 {
    assert_eq!(divisor[0], SPECIALIZE_FOR_KNOWN_DIVISOR);
    assert!(is_all_zeros(&divisor[1..]));
}

(Hopefully this is enough for the rest of the body to be specialized by LLVM, but it can be further forced if necessary)

Then ieee::sig::div would become a "dispatch" fn, which invokes N+1 different instantiations of the const-generic implementation, for N "commonly used divisors" (10 comes to mind, tho there may be a whole sequence of powers of 5 for the conversion from decimal strings IIRC), and one 0 instantiation (which isn't specialized at all), and because the code still does the same division, we're only relying on the optimizer to actually turn the divisions into multiplications.


Whatever we do to the division algorithm, we shouldn't forget to add benchmarks first (unless the "from decimal" benchmark would cover enough interesting cases).

Contributor guide

No contributing guide indexed for this repository

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 at the ieee::sig::div arbitrary-precision division implementation and inspect whether the existing "from decimal" benchmark covers the relevant cases. Add benchmarks before changing the implementation, then evaluate const-generic specializations for commonly used divisors. Done means the division behavior is preserved and benchmarks demonstrate a meaningful speedup.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.