add Div and Mul for Complex<{float}> - #162832
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.
|
@bjorn3 so this hits rust-lang/rustc_codegen_cranelift#1703, how should I disable this test for cranelift (for now, until that bug is fixed)? |
| } | ||
|
|
||
| #[test] | ||
| fn div() { |
There was a problem hiding this comment.
For consistency, you probably want to rename this function:
| fn div() { | |
| fn complex_division() { |
| let ad = a * d; | ||
| let bc = b * c; | ||
|
|
||
| let z = Complex::new(ac - bd, ad + bc); |
There was a problem hiding this comment.
Would it make sense to use algebraic operators here in case $ty is {float}? This would make it possible to use FMA and FMS instructions on architectures which have those, such as ARM.
There was a problem hiding this comment.
I don't think we should go with algebraic operators here - it is good to limit the maximum possible error, and algebraic operators don't have any accuracy guarantees at all.
There was a problem hiding this comment.
mul_add_relaxed could technically be used once that PR lands. But I think it's good to have a consistent result across platforms until we figure out exactly what we can say about allowed imprecision.
You can write a patch file for and call it |
|
A cranelift version with the fix is already out. If that isn't too difficult to upgrade to then I'm fine with waiting a couple more days. |
|
bytecodealliance/wasmtime#14361 missed the cutoff for the last Cranelift release (a release branch is cut on the 5th of the month, and then on the 20th it is released). In addition there is a regression in the last release on macOS that I still need to investigate, blocking the upgrade. |
ffaef7f to
5931727
Compare
|
cc @bjorn3 |
|
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. |
|
@bors try jobs=test-x86_64-msvc-2 |
This comment has been minimized.
This comment has been minimized.
add `Div` and `Mul` for `Complex<{float}>`
try-job: test-x86_64-msvc-2
|
I've added the @bors r=tgross35 |
…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.
…uwer Rollup of 9 pull requests Successful merges: - #162655 (next solver: prefer to select impl candidates over global where-clause candidates) - #162832 (add `Div` and `Mul` for `Complex<{float}>`) - #162862 (Fix intra doc link resolution when a doc comment is composed of both inner and outer doc comment) - #163024 (Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`) - #163200 (make `RustaceansAreAwesome` satisfy trait bounds) - #163210 (std: split stack overflow module) - #163331 (Move `Arc` and `Rc` into `rcs` mod) - #163183 (Add .seek_read_buf_exact() to std::os::windows::fs::FileExt) - #163450 (Force the correct type variable to never for method resolution on an adjusted never type)
…uwer Rollup of 9 pull requests Successful merges: - #158997 (Avoid recording unnameable `extern crate` aliases in diagnostic metadata) - #162655 (next solver: prefer to select impl candidates over global where-clause candidates) - #162832 (add `Div` and `Mul` for `Complex<{float}>`) - #162862 (Fix intra doc link resolution when a doc comment is composed of both inner and outer doc comment) - #163200 (make `RustaceansAreAwesome` satisfy trait bounds) - #163210 (std: split stack overflow module) - #163331 (Move `Arc` and `Rc` into `rcs` mod) - #163183 (Add .seek_read_buf_exact() to std::os::windows::fs::FileExt) - #163450 (Force the correct type variable to never for method resolution on an adjusted never type)
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#158997 (Avoid recording unnameable `extern crate` aliases in diagnostic metadata) - rust-lang/rust#162655 (next solver: prefer to select impl candidates over global where-clause candidates) - rust-lang/rust#162832 (add `Div` and `Mul` for `Complex<{float}>`) - rust-lang/rust#162862 (Fix intra doc link resolution when a doc comment is composed of both inner and outer doc comment) - rust-lang/rust#163200 (make `RustaceansAreAwesome` satisfy trait bounds) - rust-lang/rust#163210 (std: split stack overflow module) - rust-lang/rust#163331 (Move `Arc` and `Rc` into `rcs` mod) - rust-lang/rust#163183 (Add .seek_read_buf_exact() to std::os::windows::fs::FileExt) - rust-lang/rust#163450 (Force the correct type variable to never for method resolution on an adjusted never type)
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#158997 (Avoid recording unnameable `extern crate` aliases in diagnostic metadata) - rust-lang/rust#162655 (next solver: prefer to select impl candidates over global where-clause candidates) - rust-lang/rust#162832 (add `Div` and `Mul` for `Complex<{float}>`) - rust-lang/rust#162862 (Fix intra doc link resolution when a doc comment is composed of both inner and outer doc comment) - rust-lang/rust#163200 (make `RustaceansAreAwesome` satisfy trait bounds) - rust-lang/rust#163210 (std: split stack overflow module) - rust-lang/rust#163331 (Move `Arc` and `Rc` into `rcs` mod) - rust-lang/rust#163183 (Add .seek_read_buf_exact() to std::os::windows::fs::FileExt) - rust-lang/rust#163450 (Force the correct type variable to never for method resolution on an adjusted never type)
View all comments
tracking issue: #154023
There are no fallbacks for the libcalls, so Miri will currently fail.