Skip to content

Comments

Add mul_add_relaxed methods for floating-point types#151793

Open
landsharkiest wants to merge 9 commits intorust-lang:mainfrom
landsharkiest:float_mul_add_relaxed-#151770
Open

Add mul_add_relaxed methods for floating-point types#151793
landsharkiest wants to merge 9 commits intorust-lang:mainfrom
landsharkiest:float_mul_add_relaxed-#151770

Conversation

@landsharkiest
Copy link

@landsharkiest landsharkiest commented Jan 28, 2026

Implements mul_add_relaxed for f16, f32, f64, and f128, which computes (self * a) + b with relaxed precision semantics. Unlike mul_add which guarantees a fused operation, this variant allows the compiler to choose between fused or separate operations based on target performance.

This fills the gap between the precision-guaranteed mul_add and the fully-optimizable algebraic operators, providing target-specific optimization while maintaining reasonable floating-point semantics.

Tracking issue: #151770
#151770

Implements mul_add_relaxed for f16, f32, f64, and f128, which computes
(self * a) + b with relaxed precision semantics. Unlike mul_add which
guarantees a fused operation, this variant allows the compiler to choose
between fused or separate operations based on target performance.

This fills the gap between the precision-guaranteed mul_add and the
fully-optimizable algebraic operators, providing target-specific
optimization while maintaining reasonable floating-point semantics.

Tracking issue: rust-lang#151770
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 28, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

- Use core::intrinsics::fmuladd* directly in std impl
- Remove trailing whitespace from test file
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

These doc tests require platform-specific f16/f128 support and
should not be run during standard doc builds.
@rust-log-analyzer

This comment has been minimized.

@@ -0,0 +1,49 @@
//@ run-pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f16 and f128 should be covered here too. They just need to be gated behing #[cfg(target_has_reliable_f16_math)] / target_has_reliable_f128_math

- Fix test: 0.0 * INFINITY = NaN, not 1.0 (IEEE 754)
- Add #[cfg(target_has_reliable_f16_math)] gate for f16
- Add #[cfg(target_has_reliable_f128_math)] gate for f128
- Remove 'ignore' from doc tests as they now have proper cfg gates
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

/// // - (m * x) + b with separate roundings
/// let result = m.mul_add_relaxed(x, b);
///
/// // For simple values, both approaches give the same result
Copy link
Member

@scottmcm scottmcm Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please include at least one example where they don't give the same result, and the assert has to be "well it's either this or that"?

Here's one I tossed together; there are probably better ones:

let r = 0.1_f32.mul_add_relaxed(0.1_f32, -0.01_f32);
assert!(r == 9.313226e-10 || r == 5.2154064e-10);

Self::mul_add instead of f32::mul_add/f64::mul_add + added examples
@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 1, 2026
@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
REPOSITORY                                   TAG       IMAGE ID       CREATED      SIZE
ghcr.io/dependabot/dependabot-updater-core   latest    bcec0b4e062b   8 days ago   783MB
=> Removing docker images...
Deleted Images:
untagged: ghcr.io/dependabot/dependabot-updater-core:latest
untagged: ghcr.io/dependabot/dependabot-updater-core@sha256:b662be51f7b8ef7e2c8464428f14e49cb79c36aa9afb7ecb9221dfe0f507050c
deleted: sha256:bcec0b4e062b5ffe11cc1c2729558c0cd96621c0271ab5e97ff3a56e0c25045a
deleted: sha256:64e147d5e54d9be8b8aa322e511cda02296eda4b8b8d063c6a314833aca50e29
deleted: sha256:5cba409bb463f4e7fa1a19f695450170422582c1bc7c0e934d893b4e5f558bc6
deleted: sha256:cddc6ebd344b0111eaab170ead1dfda24acdfe865ed8a12599a34d338fa8e28b
deleted: sha256:2412c3f334d79134573cd45e657fb6cc0abd75bef3881458b0d498d936545c8d
---
---- library/core/src/num/f16.rs - f16::f16::mul_add_relaxed (line 1480) stdout ----
error: unexpected `cfg` condition name: `reliable_f16`
    --> library/core/src/num/f16.rs:1484:7
     |
1484 | #[cfg(reliable_f16)] {
     |       ^^^^^^^^^^^^
     |
     = help: expected names are: `docsrs`, `feature`, `llvm_enzyme`, `no_fp_fmt_parse`, `target_has_reliable_f128`, `target_has_reliable_f128_math`, `target_has_reliable_f16`, `target_has_reliable_f16_math`, and `test` and 31 more
     = help: consider using a Cargo feature instead
     = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
              [lints.rust]
              unexpected_cfgs = { level = "warn", check-cfg = ['cfg(reliable_f16)'] }
     = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(reliable_f16)");` to the top of the `build.rs`
     = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
note: the lint level is defined here
    --> library/core/src/num/f16.rs:1476:9
     |
1476 | #![deny(warnings)]
---

stderr:

thread 'main' (85511) panicked at library/core/src/num/f64.rs:22:1:
assertion failed: r == 9.313225746154785e-10 || r == 2.7755575615628914e-17
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: rust_out::main::_doctest_main_library_core_src_num_f64_rs_1655_0
   4: rust_out::main
   5: <fn() as core::ops::function::FnOnce<()>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- library/core/src/num/f64.rs - f64::f64::mul_add_relaxed (line 1655) stdout end ----

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 21, 2026

☔ The latest upstream changes (presumably #152624) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants