Skip to content

Commit

Permalink
More changes to build without std::complex
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou committed Jul 17, 2023
1 parent 4bfcc49 commit 457e7c0
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 129 deletions.
1 change: 1 addition & 0 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
6 changes: 6 additions & 0 deletions numpy/core/include/numpy/npy_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ typedef _Dcomplex npy_cdouble;
typedef _Fcomplex npy_cfloat;
typedef _Lcomplex 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
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
Loading

0 comments on commit 457e7c0

Please sign in to comment.