Skip to content

Commit

Permalink
Adjust to work on systems without CMPLX
Browse files Browse the repository at this point in the history
---
Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
  • Loading branch information
mppf committed Jan 19, 2024
1 parent 8cae620 commit c2f0424
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions frontend/lib/immediates/complex-support.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,56 @@

#include <complex.h>

static double complex makeDoubleComplex(double re, double im) {
// Some test environments don't have a working CMPLXF
// 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;
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;
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 = CMPLXF(x.r, x.i);
float complex c = makeFloatComplex(x.r, x.i);
float complex n = csqrtf(c);
struct complex64 ret;
ret.r = crealf(n);
ret.i = cimagf(n);
return ret;
}
struct complex128 complexSqrt128(struct complex128 x) {
double complex c = CMPLX(x.r, x.i);
double complex c = makeDoubleComplex(x.r, x.i);
double complex n = csqrt(c);
struct complex128 ret;
ret.r = creal(n);
Expand Down

0 comments on commit c2f0424

Please sign in to comment.