Skip to content

Add an fN::mul_add variant that might round twice #712

@scottmcm

Description

@scottmcm

Proposal

I could have sworn there was conversation about this before, but couldn't find it anywhere, so opening this. If I totally missed it, sorry, and please close this as dup.

Problem statement

fN::mul_add is specifically defined to be fusedMultiplyAdd and *+ rounds twice, which is good for determinism.

However, sometimes people want to give up that determinism for speed, and we should give them that option, letting the codegen backend pick (based on target or whatever) the fastest approach.

Motivating examples or use cases

Easing functions are often defined as polynomials, but when used only for (say) UI smoothing (as opposed to replicated physics) there's no need to calculate them perfectly accurately. It would be nice if they could just be written using Horner's method and let the compiler do it the fastest way, rather than needing to think about it.

Solution sketch

impl f16/f32/f64/f128 {
    /// Either `Self::mul_add` or `*+`, nondeterministically.
    fn mul_add_relaxed(self, multiplicand: Self, addend: Self) -> Self;
}

The intrinsic for this was added in rust-lang/rust#124874 but has never been exposed in even an unstable non-intrinsic method.

Alternatives

  • Use a different name
  • Offer different versions of */+ instead so people write, say, x.contracting_mul(y).contracting_add(z) instead.
  • Say we don't care about the only-contract version and tell people that if they want nondet they can use algebraic_* instead.

Links and related work

https://doc.rust-lang.org/nightly/core/arch/wasm/fn.f32x4_relaxed_min.html uses the "relaxed" name.

This issue prompted by #t-compiler/llvm > float::min/max vs SNaN @ 💬.

algebraic_*: rust-lang/rust#136469

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions