diff options
author | Matti Picus <matti.picus@gmail.com> | 2022-11-28 18:06:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 17:06:11 +0100 |
commit | c687f2d16f8ba965828fee3a001844b4952474f5 (patch) | |
tree | b7360d2119fc8543edb2d8f51ae7ec85086a3673 /numpy | |
parent | bba74740e5236735f6ec5013d8d778145b758e89 (diff) | |
download | numpy-c687f2d16f8ba965828fee3a001844b4952474f5.tar.gz |
MAINT: npymath cleanups for isnan, isinf, isinfinite, signbit, nextafter (#22684)
* make isnan, isinf, isfinite, signbit, nextafter aliases
* fixes from review
Co-authored-by: Sebastian Berg <sebastianb@nvidia.com>
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/config.h.in | 5 | ||||
-rw-r--r-- | numpy/core/include/numpy/_numpyconfig.h.in | 4 | ||||
-rw-r--r-- | numpy/core/include/numpy/npy_math.h | 43 | ||||
-rw-r--r-- | numpy/core/meson.build | 1 | ||||
-rw-r--r-- | numpy/core/setup.py | 50 | ||||
-rw-r--r-- | numpy/core/src/npymath/_signbit.c | 32 | ||||
-rw-r--r-- | numpy/core/src/npymath/ieee754.c.src | 33 | ||||
-rw-r--r-- | numpy/core/src/npymath/ieee754.cpp | 37 |
8 files changed, 10 insertions, 195 deletions
diff --git a/numpy/core/config.h.in b/numpy/core/config.h.in index f4ccdcd94..a47968a7d 100644 --- a/numpy/core/config.h.in +++ b/numpy/core/config.h.in @@ -52,11 +52,6 @@ #mesondefine HAVE_ATTRIBUTE_TARGET_AVX512F_WITH_INTRINSICS #mesondefine HAVE_ATTRIBUTE_TARGET_AVX512_SKX_WITH_INTRINSICS -#mesondefine HAVE_DECL_ISNAN -#mesondefine HAVE_DECL_ISINF -#mesondefine HAVE_DECL_ISFINITE -#mesondefine HAVE_DECL_SIGNBIT - /* C99 complex support and complex.h are not universal */ #mesondefine HAVE_COMPLEX_H #mesondefine HAVE_CABS diff --git a/numpy/core/include/numpy/_numpyconfig.h.in b/numpy/core/include/numpy/_numpyconfig.h.in index 8e799971a..a21820026 100644 --- a/numpy/core/include/numpy/_numpyconfig.h.in +++ b/numpy/core/include/numpy/_numpyconfig.h.in @@ -14,10 +14,6 @@ #mesondefine NPY_SIZEOF_PY_LONG_LONG #mesondefine NPY_SIZEOF_LONGLONG -#mesondefine NPY_HAVE_DECL_ISNAN -#mesondefine NPY_HAVE_DECL_ISINF -#mesondefine NPY_HAVE_DECL_ISFINITE -#mesondefine NPY_HAVE_DECL_SIGNBIT #mesondefine NPY_USE_C99_COMPLEX #mesondefine NPY_HAVE_COMPLEX_DOUBLE #mesondefine NPY_HAVE_COMPLEX_FLOAT diff --git a/numpy/core/include/numpy/npy_math.h b/numpy/core/include/numpy/npy_math.h index d7c9fbb4f..a1fd11396 100644 --- a/numpy/core/include/numpy/npy_math.h +++ b/numpy/core/include/numpy/npy_math.h @@ -195,6 +195,7 @@ NPY_INPLACE double npy_atan2(double x, double y); #define npy_sqrt sqrt #define npy_pow pow #define npy_modf modf +#define npy_nextafter nextafter #if defined(__arm64__) && defined(__APPLE__) /* due to a build problem with scipy, export these as functions */ @@ -206,11 +207,11 @@ NPY_INPLACE double npy_log1p(double x); #define npy_copysign copysign #define npy_log1p log1p #endif -double npy_nextafter(double x, double y); + double npy_spacing(double x); /* - * IEEE 754 fpu handling. Those are guaranteed to be macros + * IEEE 754 fpu handling */ /* use builtins to avoid function calls in tight loops @@ -218,11 +219,7 @@ double npy_spacing(double x); #ifdef HAVE___BUILTIN_ISNAN #define npy_isnan(x) __builtin_isnan(x) #else - #ifndef NPY_HAVE_DECL_ISNAN - #define npy_isnan(x) ((x) != (x)) - #else - #define npy_isnan(x) isnan(x) - #endif + #define npy_isnan(x) isnan(x) #endif @@ -230,39 +227,17 @@ double npy_spacing(double x); #ifdef HAVE___BUILTIN_ISFINITE #define npy_isfinite(x) __builtin_isfinite(x) #else - #ifndef NPY_HAVE_DECL_ISFINITE - #ifdef _MSC_VER - #define npy_isfinite(x) _finite((x)) - #else - #define npy_isfinite(x) !npy_isnan((x) + (-x)) - #endif - #else - #define npy_isfinite(x) isfinite((x)) - #endif + #define npy_isfinite(x) isfinite((x)) #endif /* only available if npy_config.h is available (= numpys own build) */ #ifdef HAVE___BUILTIN_ISINF #define npy_isinf(x) __builtin_isinf(x) #else - #ifndef NPY_HAVE_DECL_ISINF - #define npy_isinf(x) (!npy_isfinite(x) && !npy_isnan(x)) - #else - #define npy_isinf(x) isinf((x)) - #endif + #define npy_isinf(x) isinf((x)) #endif -#ifndef NPY_HAVE_DECL_SIGNBIT - int _npy_signbit_f(float x); - int _npy_signbit_d(double x); - int _npy_signbit_ld(long double x); - #define npy_signbit(x) \ - (sizeof (x) == sizeof (long double) ? _npy_signbit_ld (x) \ - : sizeof (x) == sizeof (double) ? _npy_signbit_d (x) \ - : _npy_signbit_f (x)) -#else - #define npy_signbit(x) signbit((x)) -#endif +#define npy_signbit(x) signbit((x)) /* * float C99 math funcs that need fixups or are blocklist-able @@ -305,8 +280,8 @@ NPY_INPLACE float npy_modff(float x, float* y); #define npy_frexpf frexpf #define npy_ldexpf ldexpf #define npy_copysignf copysignf +#define npy_nextafterf nextafterf -float npy_nextafterf(float x, float y); float npy_spacingf(float x); /* @@ -349,8 +324,8 @@ NPY_INPLACE npy_longdouble npy_modfl(npy_longdouble x, npy_longdouble* y); #define npy_frexpl frexpl #define npy_ldexpl ldexpl #define npy_copysignl copysignl +#define npy_nextafterl nextafterl -npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y); npy_longdouble npy_spacingl(npy_longdouble x); /* diff --git a/numpy/core/meson.build b/numpy/core/meson.build index 507d0868d..2b23d3f14 100644 --- a/numpy/core/meson.build +++ b/numpy/core/meson.build @@ -459,7 +459,6 @@ npymath_sources = [ src_file.process('src/npymath/ieee754.c.src'), src_file.process('src/npymath/npy_math_complex.c.src'), npy_math_internal_h, - 'src/npymath/_signbit.c', 'src/npymath/halffloat.c', 'src/npymath/npy_math.c', ] diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 9a3b41637..4001f7ab0 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -60,14 +60,6 @@ class CallOnceOnly: out = copy.deepcopy(pickle.loads(self._check_types)) return out - def check_ieee_macros(self, *a, **kw): - if self._check_ieee_macros is None: - out = check_ieee_macros(*a, **kw) - self._check_ieee_macros = pickle.dumps(out) - else: - out = copy.deepcopy(pickle.loads(self._check_ieee_macros)) - return out - def check_complex(self, *a, **kw): if self._check_complex is None: out = check_complex(*a, **kw) @@ -293,43 +285,6 @@ def check_complex(config, mathlibs): return priv, pub -def check_ieee_macros(config): - priv = [] - pub = [] - - macros = [] - - def _add_decl(f): - priv.append(fname2def("decl_%s" % f)) - pub.append('NPY_%s' % fname2def("decl_%s" % f)) - - # XXX: hack to circumvent cpp pollution from python: python put its - # config.h in the public namespace, so we have a clash for the common - # functions we test. We remove every function tested by python's - # autoconf, hoping their own test are correct - _macros = ["isnan", "isinf", "signbit", "isfinite"] - for f in _macros: - py_symbol = fname2def("decl_%s" % f) - already_declared = config.check_decl(py_symbol, - headers=["Python.h", "math.h"]) - if already_declared: - if config.check_macro_true(py_symbol, - headers=["Python.h", "math.h"]): - pub.append('NPY_%s' % fname2def("decl_%s" % f)) - else: - macros.append(f) - # Normally, isnan and isinf are macro (C99), but some platforms only have - # func, or both func and macro version. Check for macro only, and define - # replacement ones if not found. - # Note: including Python.h is necessary because it modifies some math.h - # definitions - for f in macros: - st = config.check_decl(f, headers=["Python.h", "math.h"]) - if st: - _add_decl(f) - - return priv, pub - def check_types(config_cmd, ext, build_dir): private_defines = [] public_defines = [] @@ -510,7 +465,6 @@ def configuration(parent_package='',top_path=None): moredefs.append(('MATHLIB', ','.join(mathlibs))) check_math_capabilities(config_cmd, ext, moredefs, mathlibs) - moredefs.extend(cocache.check_ieee_macros(config_cmd)[0]) moredefs.extend(cocache.check_complex(config_cmd, mathlibs)[0]) # Signal check @@ -629,7 +583,6 @@ def configuration(parent_package='',top_path=None): moredefs.append(('NPY_NO_SMP', 0)) mathlibs = check_mathlib(config_cmd) - moredefs.extend(cocache.check_ieee_macros(config_cmd)[1]) moredefs.extend(cocache.check_complex(config_cmd, mathlibs)[1]) if NPY_RELAXED_STRIDES_DEBUG: @@ -710,8 +663,7 @@ def configuration(parent_package='',top_path=None): config.numpy_include_dirs.extend(config.paths('include')) - deps = [join('src', 'npymath', '_signbit.c'), - join('include', 'numpy', '*object.h'), + deps = [join('include', 'numpy', '*object.h'), join(codegen_dir, 'genapi.py'), ] diff --git a/numpy/core/src/npymath/_signbit.c b/numpy/core/src/npymath/_signbit.c deleted file mode 100644 index 12af55387..000000000 --- a/numpy/core/src/npymath/_signbit.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Adapted from cephes */ - -int -_npy_signbit_d(double x) -{ - union - { - double d; - short s[4]; - int i[2]; - } u; - - u.d = x; - -#if NPY_SIZEOF_INT == 4 - -#ifdef WORDS_BIGENDIAN /* defined in pyconfig.h */ - return u.i[0] < 0; -#else - return u.i[1] < 0; -#endif - -#else /* NPY_SIZEOF_INT != 4 */ - -#ifdef WORDS_BIGENDIAN - return u.s[0] < 0; -#else - return u.s[3] < 0; -#endif - -#endif /* NPY_SIZEOF_INT */ -} diff --git a/numpy/core/src/npymath/ieee754.c.src b/numpy/core/src/npymath/ieee754.c.src index 5d8cb06a4..8fccc9a69 100644 --- a/numpy/core/src/npymath/ieee754.c.src +++ b/numpy/core/src/npymath/ieee754.c.src @@ -15,20 +15,6 @@ #define LDBL_TRUE_MIN __LDBL_DENORM_MIN__ #endif -#if !defined(HAVE_DECL_SIGNBIT) -#include "_signbit.c" - -int _npy_signbit_f(float x) -{ - return _npy_signbit_d((double) x); -} - -int _npy_signbit_ld(long double x) -{ - return _npy_signbit_d((double) x); -} -#endif - /* * FIXME: There is a lot of redundancy between _next* and npy_nextafter*. * refactor this at some point @@ -326,25 +312,6 @@ static npy_longdouble _nextl(npy_longdouble x, int p) } /**end repeat**/ -/* - * Decorate all the math functions which are available on the current platform - */ - -float npy_nextafterf(float x, float y) -{ - return nextafterf(x, y); -} - -double npy_nextafter(double x, double y) -{ - return nextafter(x, y); -} - -npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y) -{ - return nextafterl(x, y); -} - int npy_clear_floatstatus() { char x=0; return npy_clear_floatstatus_barrier(&x); diff --git a/numpy/core/src/npymath/ieee754.cpp b/numpy/core/src/npymath/ieee754.cpp index ebc1dbeba..1c59bf300 100644 --- a/numpy/core/src/npymath/ieee754.cpp +++ b/numpy/core/src/npymath/ieee754.cpp @@ -17,21 +17,6 @@ #define LDBL_TRUE_MIN __LDBL_DENORM_MIN__ #endif -#if !defined(HAVE_DECL_SIGNBIT) -#include "_signbit.c" - -int -_npy_signbit_f(float x) -{ - return _npy_signbit_d((double)x); -} - -int -_npy_signbit_ld(long double x) -{ - return _npy_signbit_d((double)x); -} -#endif /* * FIXME: There is a lot of redundancy between _next* and npy_nextafter*. @@ -388,28 +373,6 @@ npy_spacingl(npy_longdouble x) } } -/* - * Decorate all the math functions which are available on the current platform - */ - -extern "C" float -npy_nextafterf(float x, float y) -{ - return nextafterf(x, y); -} - -extern "C" double -npy_nextafter(double x, double y) -{ - return nextafter(x, y); -} - -extern "C" npy_longdouble -npy_nextafterl(npy_longdouble x, npy_longdouble y) -{ - return nextafterl(x, y); -} - extern "C" int npy_clear_floatstatus() { |