Skip to content

add Div and Mul for Complex<{float}> - #162832

Open
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:complex-mul-div
Open

folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:complex-mul-div

Conversation

@folkertdev

@folkertdev folkertdev commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

View all comments

tracking issue: #154023

There are no fallbacks for the libcalls, so Miri will currently fail. The fallback is blocked on rust-lang/libs-team#876.

@folkertdev folkertdev added the F-complex_numbers `#![feature(complex_numbers)]` label Sep 15, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 15, 2026
@folkertdev

Copy link
Copy Markdown
Contributor Author

@bors try jobs=test-various,test-aarch64-apple-,-gnu-nopt-,test-x86_64-mingw-,test-aarch64-msvc-*,test-arm-android

rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
add `Div` and `Mul` for `Complex<{float}>`


try-job: test-various
try-job: test-aarch64-apple-*
try-job: *-gnu-nopt-*
try-job: test-x86_64-mingw-*
try-job: test-aarch64-msvc-*
try-job: test-arm-android
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 3b59d93 (3b59d930efb08e53dc535d0e705b2564a402ce9b)
Base parent: 28e8a8c (28e8a8c81bf3b37909edac6c2a76e56f30cd492f)

@folkertdev

Copy link
Copy Markdown
Contributor Author

@RalfJung I'm a bit unsure on how to get Miri to support this. Is this a case where cfg(miri) is OK?

We can add a fn complex_mul<F: Float>(mut a: F, mut b: F, mut c: F, mut d: F) -> Complex<F> implementation based on crate::num::imp::Float and only call it when cfg(miri).

if unlikely(z.re.is_nan() && z.im.is_nan()) {
    cfg_select! {
        miri => complex_mul(a, b, c, d), // use an implementation in core
        _ => $mul(a, b, c, d), // call the libcall
    }
} else {
    z
}

I know you dislike using cfg(miri) but I don't see other good options here.

@RalfJung

Copy link
Copy Markdown
Member

Typically we just implement the libcalls in Miri.

@folkertdev

Copy link
Copy Markdown
Contributor Author

Ok, that should work. We can merge this then and I'll work on a Miri implementation in parallel.

r? tgross35 or @beetrees

@rustbot

rustbot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

tgross35 is currently at their maximum review capacity.
They may take a while to respond.

@folkertdev
folkertdev marked this pull request as ready for review September 16, 2026 15:01
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 16, 2026
@RalfJung

Copy link
Copy Markdown
Member

@bors try jobs=test-x86_64-gnu-aux

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 16, 2026
add `Div` and `Mul` for `Complex<{float}>`


try-job: test-x86_64-gnu-aux
@rust-bors

rust-bors Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: f9b288f (f9b288f412d8bf91cb68eaee520f73e5bdb9410e)
Base parent: e15cecc (e15ceccfc6209c15b6c4bc6352f6ec6bfe579eaa)

Comment on lines +68 to +82
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let Complex { re: a, im: b } = self;
let Complex { re: c, im: d } = rhs;

let ac = a * c;
let bd = b * d;
let ad = a * d;
let bc = b * c;

let z = Complex::new(ac - bd, ad + bc);

// Only call the libcall when both components are NaN.
if unlikely(z.re.is_nan() && z.im.is_nan()) { $mul(a, b, c, d) } else { z }
}

@tgross35 tgross35 Sep 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comment says pretty much the same thing as the code, it would be helpful to explain why. Also mild preference for cold_path over unlikely since it's stable.

I think the optimization may pay off but it's unfortunate it means repeated work. Not too bad for hard floats but this means e.g. 12 additional function calls if you hit a NaN with f16. I wonder if there's a case to be made that these (and possibly other) libcalls should be preserve_most or preserve_all since stashing the registers is likely the most expensive part of a call.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wrote something, and now usecold_path. I'm not sure how much it helps given that the branch is small, but it also serves as documentation that this is an unlikely branch.

And, this ABI etc was designed way before f16 entered the picture, I don't think this will matter much in practice.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I did mean more of a comment about the optimization going on here, i.e. why are we doing some things inline rather than going directly to the libcall

@tgross35

Copy link
Copy Markdown
Member

The fallback is blocked on rust-lang/libs-team#876.

Also I don't think this is blocked on anything, the same mul/div algorithms could be implemented in apfloat for CTFE/miri.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 17, 2026
Complex conjugate, negation and default

tracking issue: rust-lang#154023

Add several more straightforward (I hope) impls and methods to `Complex`.

- `Default` when `T: Default`. For numbers this means `Complex::default` is the unit element with respect to complex addition
- `Neg` when `T: Neg`. Maps `a + bi` to `-a - bi`, which can underflow on integers (but this is no different from addition being able to overflow/underflow too
- `conjugate` maps `a + bi` to `a - bi`
- make `Complex::new` a `const fn`, because why not

Complex division and multiplication is in progress in rust-lang#162832.

r? @nia-e for continuity (or `r? libs` if you're busy)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 17, 2026
Complex conjugate, negation and default

tracking issue: rust-lang#154023

Add several more straightforward (I hope) impls and methods to `Complex`.

- `Default` when `T: Default`. For numbers this means `Complex::default` is the unit element with respect to complex addition
- `Neg` when `T: Neg`. Maps `a + bi` to `-a - bi`, which can underflow on integers (but this is no different from addition being able to overflow/underflow too
- `conjugate` maps `a + bi` to `a - bi`
- make `Complex::new` a `const fn`, because why not

Complex division and multiplication is in progress in rust-lang#162832.

r? @nia-e for continuity (or `r? libs` if you're busy)

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have a Miri PR ready to go, but to test it I need the instances to work first.

We can move it into CMSE later, right now there isn't much point to it I think because many other things do not support const.

View changes since this review

rust-bors Bot pushed a commit that referenced this pull request Sep 17, 2026
Rollup merge of #162865 - folkertdev:complex-conjugate, r=nia-e

Complex conjugate, negation and default

tracking issue: #154023

Add several more straightforward (I hope) impls and methods to `Complex`.

- `Default` when `T: Default`. For numbers this means `Complex::default` is the unit element with respect to complex addition
- `Neg` when `T: Neg`. Maps `a + bi` to `-a - bi`, which can underflow on integers (but this is no different from addition being able to overflow/underflow too
- `conjugate` maps `a + bi` to `a - bi`
- make `Complex::new` a `const fn`, because why not

Complex division and multiplication is in progress in #162832.

r? @nia-e for continuity (or `r? libs` if you're busy)
@rust-bors

This comment has been minimized.

@tgross35 tgross35 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

r=me with a rebase and optional comment update. Make sure to update the tracking issue API summary, probably going to get easy to lose track

View changes since this review

Comment on lines +68 to +82
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let Complex { re: a, im: b } = self;
let Complex { re: c, im: d } = rhs;

let ac = a * c;
let bd = b * d;
let ad = a * d;
let bc = b * c;

let z = Complex::new(ac - bd, ad + bc);

// Only call the libcall when both components are NaN.
if unlikely(z.re.is_nan() && z.im.is_nan()) { $mul(a, b, c, d) } else { z }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I did mean more of a comment about the optimization going on here, i.e. why are we doing some things inline rather than going directly to the libcall

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@folkertdev

Copy link
Copy Markdown
Contributor Author

@bors r=tgross35

@rust-bors

rust-bors Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ffaef7f has been approved by tgross35

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 18, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…ss35

add `Div` and `Mul` for `Complex<{float}>`

tracking issue: rust-lang#154023

There are no fallbacks for the libcalls, so Miri will currently fail. The fallback is blocked on rust-lang/libs-team#876.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…ss35

add `Div` and `Mul` for `Complex<{float}>`

tracking issue: rust-lang#154023

There are no fallbacks for the libcalls, so Miri will currently fail. The fallback is blocked on rust-lang/libs-team#876.
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #161777 (Add Natvis visualiser and debuginfo tests for `f128`)
 - #162423 (Rename the src install build step to rust-src.)
 - #162740 (stdarch subtree update)
 - #162824 (link Enzyme and the offload with in-tree lld if possible)
 - #162832 (add `Div` and `Mul` for `Complex<{float}>`)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
@JonathanBrouwer

Copy link
Copy Markdown
Member

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #162951 (comment)

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 18, 2026
@rust-bors

rust-bors Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#162951), which was unapproved.

View changes since this unapproval

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

Labels

F-complex_numbers `#![feature(complex_numbers)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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