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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions frontend/lib/immediates/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

complex-support.c

# C++ sources
hash_multipliers.cpp
ifa_vars.cpp
num.cpp
num.h
prim_data.h

)
79 changes: 79 additions & 0 deletions frontend/lib/immediates/complex-support.c
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
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.

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
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

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
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) };

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
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.

return ret;
}
42 changes: 42 additions & 0 deletions frontend/lib/immediates/complex-support.h
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
18 changes: 0 additions & 18 deletions frontend/lib/immediates/num.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <cmath>
#include <cstdio>
#include <cstring>
#include <complex>

#include "num.h"
#include "prim_data.h"
Expand Down Expand Up @@ -489,23 +488,6 @@ coerce_immediate(chpl::Context* context, Immediate *from, Immediate *to) {
break; \
}

static complex64 complexSqrt64(complex64 x) {
auto c = std::complex<float>(x.r, x.i);
auto n = std::sqrt(c);
complex64 ret;
ret.r = std::real(n);
ret.i = std::imag(n);
return ret;
}
static complex128 complexSqrt128(complex128 x) {
auto c = std::complex<double>(x.r, x.i);
auto n = std::sqrt(c);
complex128 ret;
ret.r = std::real(n);
ret.i = std::imag(n);
return ret;
}

static void doFoldSqrt(chpl::Context* context,
Immediate &im1, /* input */
Immediate *imm /* output */) {
Expand Down
13 changes: 4 additions & 9 deletions frontend/lib/immediates/num.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "chpl/framework/Context.h"
#include "chpl/framework/UniqueString.h"

extern "C" {
#include "complex-support.h"
}

#include <cstdlib>
#include <cstring>
#include <sstream>
Expand All @@ -38,15 +42,6 @@

extern unsigned int open_hash_multipliers[256];

struct complex64 {
float r;
float i;
};
struct complex128 {
double r;
double i;
};

using ImmString = chpl::detail::PODUniqueString;

//
Expand Down