Skip to content

Commit

Permalink
Necessary changes to only use C99 complex types (not std::complex)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou committed Jul 17, 2023
1 parent 045a8cd commit ddcdf86
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 150 deletions.
18 changes: 1 addition & 17 deletions numpy/core/include/numpy/ndarraytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NUMPY_CORE_INCLUDE_NUMPY_NDARRAYTYPES_H_

#include "npy_common.h"
#include "npy_math.h"
#include "npy_endian.h"
#include "npy_cpu.h"
#include "utils.h"
Expand Down Expand Up @@ -963,22 +964,6 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
#define PyArray_MAX(a,b) (((a)>(b))?(a):(b))
#define PyArray_MIN(a,b) (((a)<(b))?(a):(b))

#ifdef __cplusplus
#define PyArray_CLT(p,q, type) (p).real() == (q).real() \
? (p).imag() < (q).imag() \
: (p).real() < (q).real()
#define PyArray_CGT(p,q, type) (p).real() == (q).real() \
? (p).imag() > (q).imag() \
: (p).real() > (q).real()
#define PyArray_CLE(p,q, type) (p).real() == (q).real() \
? (p).imag() <= (q).imag() \
: (p).real() <= (q).real()
#define PyArray_CGE(p,q, type) (p).real() == (q).real() \
? (p).imag() >= (q).imag() \
: (p).real() >= (q).real()
#define PyArray_CEQ(p,q, type) (p) == (q)
#define PyArray_CNE(p,q, type) (p) != (q)
#else
#define PyArray_CLT(p,q, type) NPY_##type##_GET_REAL(&(p)) == NPY_##type##_GET_REAL(&(q)) \
? NPY_##type##_GET_IMAG(&(p)) < NPY_##type##_GET_IMAG(&(q)) \
: NPY_##type##_GET_REAL(&(p)) < NPY_##type##_GET_REAL(&(q))
Expand All @@ -993,7 +978,6 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
: NPY_##type##_GET_REAL(&(p)) >= NPY_##type##_GET_REAL(&(q))
#define PyArray_CEQ(p,q, type) (p) == (q)
#define PyArray_CNE(p,q, type) (p) != (q)
#endif

/*
* C API: consists of Macros and functions. The MACROS are defined
Expand Down
15 changes: 7 additions & 8 deletions numpy/core/include/numpy/npy_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,19 @@ typedef Py_hash_t npy_hash_t;
#endif


#if !defined(__cplusplus) && defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#include <complex.h>
typedef _Dcomplex npy_cdouble;
typedef _Fcomplex npy_cfloat;
typedef _Lcomplex npy_clongdouble;
#elif defined(__cplusplus) /* && (!defined(_MSC_VER) || defined(__INTEL_COMPILER)) */
extern "C++" {
#include <complex>
}
typedef std::complex<double> npy_cdouble;
typedef std::complex<float> npy_cfloat;
typedef std::complex<longdouble_t> npy_clongdouble;
#else /* !defined(__cplusplus) && (!defined(_MSC_VER) || defined(__INTEL_COMPILER)) */
#ifdef __cplusplus
extern "C++" {
#endif
#include <complex.h>
#ifdef __cplusplus
}
#endif
#undef complex
typedef double _Complex npy_cdouble;
typedef float _Complex npy_cfloat;
Expand Down
56 changes: 0 additions & 56 deletions numpy/core/include/numpy/npy_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ NPY_INPLACE npy_longdouble npy_heavisidel(npy_longdouble x, npy_longdouble h0);
* Complex declarations
*/

#ifndef __cplusplus
typedef union {
double *arr;
const npy_cdouble *comp;
Expand All @@ -372,132 +371,77 @@ typedef union {
longdouble_t *arr;
const npy_clongdouble *comp;
} _npy_clongdouble_to_arr;
#endif

static inline double NPY_CDOUBLE_GET_REAL(const npy_cdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const double *>(c)[0];
#else
_npy_cdouble_to_arr tmp;
tmp.comp = c;
return tmp.arr[0];
#endif
}

static inline double NPY_CDOUBLE_GET_IMAG(const npy_cdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const double *>(c)[1];
#else
_npy_cdouble_to_arr tmp;
tmp.comp = c;
return tmp.arr[1];
#endif
}

static inline void NPY_CDOUBLE_SET_REAL(npy_cdouble *c, double real) {
#ifdef __cplusplus
double *tmp = reinterpret_cast<double *>(c);
tmp[0] = real;
#else
_npy_cdouble_to_arr tmp;
tmp.comp = c;
tmp.arr[0] = real;
#endif
}

static inline void NPY_CDOUBLE_SET_IMAG(npy_cdouble *c, double imag) {
#ifdef __cplusplus
double *tmp = reinterpret_cast<double *>(c);
tmp[1] = imag;
#else
_npy_cdouble_to_arr tmp;
tmp.comp = c;
tmp.arr[1] = imag;
#endif
}

static inline float NPY_CFLOAT_GET_REAL(const npy_cfloat *c) {
#ifdef __cplusplus
return reinterpret_cast<const float *>(c)[0];
#else
_npy_cfloat_to_arr tmp;
tmp.comp = c;
return tmp.arr[0];
#endif
}

static inline float NPY_CFLOAT_GET_IMAG(const npy_cfloat *c) {
#ifdef __cplusplus
return reinterpret_cast<const float *>(c)[1];
#else
_npy_cfloat_to_arr tmp;
tmp.comp = c;
return tmp.arr[1];
#endif
}

static inline void NPY_CFLOAT_SET_REAL(npy_cfloat *c, float real) {
#ifdef __cplusplus
float *tmp = reinterpret_cast<float *>(c);
tmp[0] = real;
#else
_npy_cfloat_to_arr tmp;
tmp.comp = c;
tmp.arr[0] = real;
#endif
}

static inline void NPY_CFLOAT_SET_IMAG(npy_cfloat *c, float imag) {
#ifdef __cplusplus
float *tmp = reinterpret_cast<float *>(c);
tmp[1] = imag;
#else
_npy_cfloat_to_arr tmp;
tmp.comp = c;
tmp.arr[1] = imag;
#endif
}

static inline longdouble_t NPY_CLONGDOUBLE_GET_REAL(const npy_clongdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const longdouble_t *>(c)[0];
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = c;
return tmp.arr[0];
#endif
}

static inline longdouble_t NPY_CLONGDOUBLE_GET_IMAG(const npy_clongdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const longdouble_t *>(c)[1];
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = c;
return tmp.arr[1];
#endif
}

static inline void NPY_CLONGDOUBLE_SET_REAL(npy_clongdouble *c, longdouble_t real) {
#ifdef __cplusplus
longdouble_t *tmp = reinterpret_cast<longdouble_t *>(c);
tmp[0] = real;
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = c;
tmp.arr[0] = real;
#endif
}

static inline void NPY_CLONGDOUBLE_SET_IMAG(npy_clongdouble *c, longdouble_t imag) {
#ifdef __cplusplus
longdouble_t *tmp = reinterpret_cast<longdouble_t *>(c);
tmp[1] = imag;
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = c;
tmp.arr[1] = imag;
#endif
}

static inline npy_cdouble npy_cpack(double x, double y)
Expand Down
4 changes: 1 addition & 3 deletions numpy/core/src/npymath/npy_math_complex.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ static const volatile npy_float tiny = 3.9443045e-31f;
/*==========================================================
* Constants
*=========================================================*/
#if !defined(__cplusplus) && defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
static const @ctype@ c_1@c@ = {1.0@C@, 0.0@C@};
#elif defined(__cplusplus)
static const @ctype@ c_1@c@ (1.0@C@, 0.0@C@);
#else
static const @ctype@ c_1@c@ = 1.0@C@;
#endif
Expand Down
82 changes: 46 additions & 36 deletions numpy/core/src/umath/clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,47 +55,57 @@ _NPY_MAX(T a, T b, npy::floating_point_tag const &)
return npy_isnan(a) ? (a) : PyArray_MAX(a, b);
}

template <class T>
T
_NPY_MIN(T a, T b, npy::complex_tag const &)
npy_cdouble
_NPY_MIN(npy_cdouble a, npy_cdouble b, npy::complex_tag const &)
{
if (std::is_same<T, npy_cdouble>::value) {
return npy_isnan(a.real()) || npy_isnan(a.imag()) || PyArray_CLT(a, b, CDOUBLE)
? (a)
: (b);
}
else if (std::is_same<T, npy_cfloat>::value) {
return npy_isnan(a.real()) || npy_isnan(a.imag()) || PyArray_CLT(a, b, CFLOAT)
? (a)
: (b);
}
else {
return npy_isnan(a.real()) || npy_isnan(a.imag()) || PyArray_CLT(a, b, CLONGDOUBLE)
? (a)
: (b);
}
return npy_isnan(NPY_CDOUBLE_GET_REAL(&a)) || npy_isnan(NPY_CDOUBLE_GET_IMAG(&a)) || PyArray_CLT(a, b, CDOUBLE)
? (a)
: (b);
}

template <class T>
T
_NPY_MAX(T a, T b, npy::complex_tag const &)
npy_cfloat
_NPY_MIN(npy_cfloat a, npy_cfloat b, npy::complex_tag const &)
{
if (std::is_same<T, npy_cdouble>::value) {
return npy_isnan(a.real()) || npy_isnan(a.imag()) || PyArray_CGT(a, b, CDOUBLE)
? (a)
: (b);
}
else if (std::is_same<T, npy_cfloat>::value) {
return npy_isnan(a.real()) || npy_isnan(a.imag()) || PyArray_CGT(a, b, CFLOAT)
? (a)
: (b);
}
else {
return npy_isnan(a.real()) || npy_isnan(a.imag()) || PyArray_CGT(a, b, CLONGDOUBLE)
? (a)
: (b);
}
return npy_isnan(NPY_CFLOAT_GET_REAL(&a)) || npy_isnan(NPY_CFLOAT_GET_IMAG(&a)) || PyArray_CLT(a, b, CFLOAT)
? (a)
: (b);
}

#if NPY_SIZEOF_COMPLEX_LONGDOUBLE != NPY_SIZEOF_COMPLEX_DOUBLE
npy_clongdouble
_NPY_MIN(npy_clongdouble a, npy_clongdouble b, npy::complex_tag const &)
{
return npy_isnan(NPY_CLONGDOUBLE_GET_REAL(&a)) || npy_isnan(NPY_CLONGDOUBLE_GET_IMAG(&a)) || PyArray_CLT(a, b, CLONGDOUBLE)
? (a)
: (b);
}
#endif

npy_cdouble
_NPY_MAX(npy_cdouble a, npy_cdouble b, npy::complex_tag const &)
{
return npy_isnan(NPY_CDOUBLE_GET_REAL(&a)) || npy_isnan(NPY_CDOUBLE_GET_IMAG(&a)) || PyArray_CGT(a, b, CDOUBLE)
? (a)
: (b);
}

npy_cfloat
_NPY_MAX(npy_cfloat a, npy_cfloat b, npy::complex_tag const &)
{
return npy_isnan(NPY_CFLOAT_GET_REAL(&a)) || npy_isnan(NPY_CFLOAT_GET_IMAG(&a)) || PyArray_CGT(a, b, CFLOAT)
? (a)
: (b);
}

#if NPY_SIZEOF_COMPLEX_LONGDOUBLE != NPY_SIZEOF_COMPLEX_DOUBLE
npy_clongdouble
_NPY_MAX(npy_clongdouble a, npy_clongdouble b, npy::complex_tag const &)
{
return npy_isnan(NPY_CLONGDOUBLE_GET_REAL(&a)) || npy_isnan(NPY_CLONGDOUBLE_GET_IMAG(&a)) || PyArray_CGT(a, b, CLONGDOUBLE)
? (a)
: (b);
}
#endif

template <class T>
T
Expand Down
7 changes: 1 addition & 6 deletions numpy/core/src/umath/matmul.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,9 @@ is_blasable2d(npy_intp byte_stride1, npy_intp byte_stride2,
return NPY_FALSE;
}

#if !defined(__cplusplus) && defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
static const npy_cdouble oneD = {1.0, 0.0}, zeroD = {0.0, 0.0};
static const npy_cfloat oneF = {1.0f, 0.0f}, zeroF = {0.0f, 0.0f};
#elif defined(__cplusplus)
static const npy_cdouble oneD (1.0, 0.0);
static const npy_cdouble zeroD (0.0, 0.0);
static const npy_cfloat oneF (1.0f, 0.0f);
static const npy_cfloat zeroF (0.0f, 0.0f);
#else
static const npy_cdouble oneD = 1.0, zeroD = 0.0;
static const npy_cfloat oneF = 1.0f, zeroF = 0.0f;
Expand Down
Loading

0 comments on commit ddcdf86

Please sign in to comment.