add Div and Mul for Complex<{float}> - #162832
folkertdev wants to merge 1 commit into
Conversation
|
@bors try jobs=test-various,test-aarch64-apple-,-gnu-nopt-,test-x86_64-mingw-,test-aarch64-msvc-*,test-arm-android |
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
This comment has been minimized.
This comment has been minimized.
|
@RalfJung I'm a bit unsure on how to get Miri to support this. Is this a case where We can add a 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 |
|
Typically we just implement the libcalls in Miri. |
|
Ok, that should work. We can merge this then and I'll work on a Miri implementation in parallel. r? tgross35 or @beetrees |
|
|
|
@bors try jobs=test-x86_64-gnu-aux |
This comment has been minimized.
This comment has been minimized.
add `Div` and `Mul` for `Complex<{float}>`
try-job: test-x86_64-gnu-aux
| #[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 } | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Also I don't think this is blocked on anything, the same mul/div algorithms could be implemented in apfloat for CTFE/miri. |
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)
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)
ac91627 to
381f005
Compare
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)
This comment has been minimized.
This comment has been minimized.
| #[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 } | ||
| } |
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
381f005 to
ffaef7f
Compare
|
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. |
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.