Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust complex sqrt in compiler to use the C functions #24231

Merged
merged 5 commits into from Jan 25, 2024

Conversation

mppf
Copy link
Member

@mppf mppf commented Jan 18, 2024

Follow-up to PR #24127.

After #24127, the new sqrtparams test started to fail on Mac OS X system. Upon investigating, I identified that the difference in behavior from what the test expects is due to differences in how std::sqrt is implemented for complex numbers in libc++ vs stdlibc++. Also, I noticed that the issue is limited to the C++ implementation of the complex square root.

This PR changes the implementation to use a C implementation of complex square root since it appears to have more reliable precision. Note that this C implementation required some workarounds for CMPLX not being available on all systems (as with PR #24184 and #24211). In particular, CMPLX does not seem to be available when using clang on Ubuntu 23.10, even though it is available with gcc on that system.

I filed an issue against libc++ about the problem, but in discussion there it sounds like complex sqrt isn't required to have any particular precision.

Reviewed by @DanilaFe - thanks!

  • full comm=none testing

---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
@mppf mppf changed the title Add a suppressif for sqrtparams Adjust complex sqrt in compiler to use the C functions Jan 19, 2024
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
@mppf mppf requested a review from DanilaFe January 19, 2024 17:40
@@ -18,10 +18,14 @@
target_sources(ChplFrontend-obj
PRIVATE

ifa_vars.cpp
# C sources
complex-support.h
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh -- do header files usually get listed in sources? I don't recall that to be the case, but I could be forgetting.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have been listing them inconsistently. E.g. in frontend/lib/util/CMakeLists.txt. AFAIK it is harmless but unnecessary to list them.

#include <complex.h>

static double complex makeDoubleComplex(double re, double im) {
// Some test environments don't have a working CMPLXF
Copy link
Contributor

Choose a reason for hiding this comment

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

should this say "working CMPLX" (no F?)

Comment on lines +31 to +35
#define cmplx_re64(c) (((double *)&(c))[0])
#define cmplx_im64(c) (((double *)&(c))[1])
double complex val;
cmplx_re64(val) = re;
cmplx_im64(val) = im;
Copy link
Contributor

Choose a reason for hiding this comment

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

At this point, I would do away with the #defines and simply include the original expressions + a comment ("set the real part" .e.g.).

Suggested change
#define cmplx_re64(c) (((double *)&(c))[0])
#define cmplx_im64(c) (((double *)&(c))[1])
double complex val;
cmplx_re64(val) = re;
cmplx_im64(val) = im;
double complex val;
(((double *)&val)[0]) = re; /* set the real part of the complex number */
(((double *)&val)[1]) = im; /* set the imaginary part of the complex number */

Copy link
Member Author

Choose a reason for hiding this comment

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

Since this code is pretty much the same as _chpl_complex128 etc that Brad added to chpltypes.h in the runtime in #24184 / #24211 I wanted to follow the same pattern. I agree that the macro is not strictly necessary here, but it is arguably valuable to communicate intent -- which you handled with the comments.

@bradcray how would you feel about removing the macros in both of these places?

Copy link
Member

Choose a reason for hiding this comment

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

As someone who habitually doesn't read comments (like, I didn't even see them here until I saw Michael's "which you handled with the comments" and prefers self-descriptive code, I'd probably still stick with the macros, personally. But I'm admittedly less invested in the frontend/ directory than I am the runtime, so don't have a preference for this PR. Just not sure I'd remove them from the runtime for the sake of consistency.

Comment on lines +50 to +55
#ifndef CHPL_DONT_USE_CMPLX_PTR_ALIASING
#define cmplx_re32(c) (((float *)&(c))[0])
#define cmplx_im32(c) (((float *)&(c))[1])
float complex val;
cmplx_re32(val) = re;
cmplx_im32(val) = im;
Copy link
Contributor

Choose a reason for hiding this comment

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

if you take my prior suggestion, this is another place to apply it

Comment on lines +67 to +69
struct complex64 ret;
ret.r = crealf(n);
ret.i = cimagf(n);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're in C, could you simply write:

return { .r = crealf(n), .i = cimagf(n) };

Or perhaps:

  struct complex64 ret = { .r = crealf(n), .i = cimagf(n) };

Comment on lines +75 to +77
struct complex128 ret;
ret.r = creal(n);
ret.i = cimag(n);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto my previous comment.

---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
@mppf mppf merged commit d21d4f8 into chapel-lang:main Jan 25, 2024
7 checks passed
@mppf mppf deleted the follow-24127-libc++ branch January 25, 2024 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants