-
Notifications
You must be signed in to change notification settings - Fork 413
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
Changes from all commits
ca3f579
8cae620
c2f0424
58c304e
fbbc30c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,79 @@ | ||||||||||||||||||
/* | ||||||||||||||||||
* Copyright 2024 Hewlett Packard Enterprise Development LP | ||||||||||||||||||
* Other additional copyright holders may be indicated within. | ||||||||||||||||||
* | ||||||||||||||||||
* The entirety of this work is licensed under the Apache License, | ||||||||||||||||||
* Version 2.0 (the "License"); you may not use this file except | ||||||||||||||||||
* in compliance with the License. | ||||||||||||||||||
* | ||||||||||||||||||
* You may obtain a copy of the License at | ||||||||||||||||||
* | ||||||||||||||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||
* | ||||||||||||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||
* See the License for the specific language governing permissions and | ||||||||||||||||||
* limitations under the License. | ||||||||||||||||||
*/ | ||||||||||||||||||
|
||||||||||||||||||
#include "complex-support.h" | ||||||||||||||||||
|
||||||||||||||||||
#include <complex.h> | ||||||||||||||||||
|
||||||||||||||||||
static double complex makeDoubleComplex(double re, double im) { | ||||||||||||||||||
// Some test environments don't have a working CMPLX | ||||||||||||||||||
// so use another way to set if needed. | ||||||||||||||||||
#if defined(CMPLX) | ||||||||||||||||||
return CMPLX(re, im); | ||||||||||||||||||
#else | ||||||||||||||||||
#ifndef CHPL_DONT_USE_CMPLX_PTR_ALIASING | ||||||||||||||||||
#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; | ||||||||||||||||||
Comment on lines
+31
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, I would do away with the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this code is pretty much the same as @bradcray how would you feel about removing the macros in both of these places? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||
return val; | ||||||||||||||||||
#else | ||||||||||||||||||
// This can generate bad values in the face of inf/nan values | ||||||||||||||||||
return re + im*_Complex_I; | ||||||||||||||||||
#endif | ||||||||||||||||||
#endif | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
static float complex makeFloatComplex(float re, float im) { | ||||||||||||||||||
// Some test environments don't have a working CMPLXF | ||||||||||||||||||
// so use another way to set if needed. | ||||||||||||||||||
#if defined(CMPLXF) | ||||||||||||||||||
return CMPLXF(re, im); | ||||||||||||||||||
#else | ||||||||||||||||||
#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; | ||||||||||||||||||
Comment on lines
+50
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||
return val; | ||||||||||||||||||
#else | ||||||||||||||||||
// This can generate bad values in the face of inf/nan values | ||||||||||||||||||
return re + im*_Complex_I; | ||||||||||||||||||
#endif | ||||||||||||||||||
#endif | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
struct complex64 complexSqrt64(struct complex64 x) { | ||||||||||||||||||
float complex c = makeFloatComplex(x.r, x.i); | ||||||||||||||||||
float complex n = csqrtf(c); | ||||||||||||||||||
struct complex64 ret; | ||||||||||||||||||
ret.r = crealf(n); | ||||||||||||||||||
ret.i = cimagf(n); | ||||||||||||||||||
Comment on lines
+67
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) }; |
||||||||||||||||||
return ret; | ||||||||||||||||||
} | ||||||||||||||||||
struct complex128 complexSqrt128(struct complex128 x) { | ||||||||||||||||||
double complex c = makeDoubleComplex(x.r, x.i); | ||||||||||||||||||
double complex n = csqrt(c); | ||||||||||||||||||
struct complex128 ret; | ||||||||||||||||||
ret.r = creal(n); | ||||||||||||||||||
ret.i = cimag(n); | ||||||||||||||||||
Comment on lines
+75
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto my previous comment. |
||||||||||||||||||
return ret; | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2024 Hewlett Packard Enterprise Development LP | ||
* Other additional copyright holders may be indicated within. | ||
* | ||
* The entirety of this work is licensed under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef IMMEDIATES_COMPLEX_SQRT_H | ||
#define IMMEDIATES_COMPLEX_SQRT_H | ||
|
||
/* | ||
|
||
This header exists to work around a problem with complex square root being | ||
innacurate on libc++ by using C functions to do the complex square root. | ||
|
||
*/ | ||
|
||
struct complex64 { | ||
float r; | ||
float i; | ||
}; | ||
struct complex128 { | ||
double r; | ||
double i; | ||
}; | ||
|
||
struct complex64 complexSqrt64(struct complex64 x); | ||
struct complex128 complexSqrt128(struct complex128 x); | ||
|
||
#endif |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.