8000 MAINT: Simplify npymath by mattip · Pull Request #22090 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Simplify npymath #22090

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

Merged
merged 16 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
BUILD: convert HAVE_ macros to BLOCK_, cleanup
  • Loading branch information
mattip committed Aug 21, 2022
commit d1cbf3fe0316b1a97d9c3c8806b31fd7f90ab2e4
1 change: 0 additions & 1 deletion numpy/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ def check_funcs(funcs_name, headers=["feature_detection_math.h"]):
if config.check_decl(fname2def(f), headers=["Python.h"]):
OPTIONAL_FILE_FUNCS.remove(f)

check_funcs(OPTIONAL_STDFUNCS)
check_funcs(OPTIONAL_FILE_FUNCS, headers=["feature_detection_stdio.h"])
check_funcs(OPTIONAL_MISC_FUNCS, headers=["feature_detection_misc.h"])

Expand Down
51 changes: 19 additions & 32 deletions numpy/core/setup_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,31 @@ def set_sig(sig):
"expm1", "log1p", "acosh", "asinh", "atanh",
"rint", "trunc", "exp2",
"copysign", "nextafter", "strtoll", "strtoull", "cbrt",
"log2", "pow", "hypot", "atan2",
]

OPTIONAL_STDFUNCS_BASE = [
# cygwin
"log2",
# macos for powl
"pow",
# 32-bit windows
"hypot",
# 32-bit mingw, visual studio 2015
"atan2",
]

OPTIONAL_STDFUNCS = OPTIONAL_STDFUNCS_BASE[:]
OPTIONAL_STDFUNCS += [f + 'f' for f in OPTIONAL_STDFUNCS_BASE]
OPTIONAL_STDFUNCS += [f + 'l' for f in OPTIONAL_STDFUNCS_BASE]

OPTIONAL_LOCALE_FUNCS = ["strtold_l"]
OPTIONAL_FILE_FUNCS = ["ftello", "fseeko", "fallocate"]
OPTIONAL_MISC_FUNCS = ["backtrace", "madvise"]

# variable attributes tested via "int %s a" % attribute
OPTIONAL_VARIABLE_ATTRIBUTES = ["__thread", "__declspec(thread)"]

# Subset of OPTIONAL_*_FUNCS which may already have HAVE_* defined by Python.h
OPTIONAL_FUNCS_MAYBE = [
"ftello", "fseeko"
]

# TODO: make these mandatory and use BLOCK_ macros rather than HAVE_ macros
# in npy_config.h and throughout the code
C99_COMPLEX_TYPES = [
'complex double', 'complex float', 'complex long double'
]
C99_COMPLEX_FUNCS = [
"cabs", "cacos", "cacosh", "carg", "casin", "casinh", "catan",
"catanh", "ccos", "ccosh", "cexp", "cimag", "clog", "conj", "cpow",
"cproj", "creal", "csin", "csinh", "csqrt", "ctan", "ctanh"
]

OPTIONAL_HEADERS = [
# sse headers only enabled automatically on amd64/x32 builds
Expand Down Expand Up @@ -238,23 +242,6 @@ def set_sig(sig):
'immintrin.h'),
]

# variable attributes tested via "int %s a" % attribute
OPTIONAL_VARIABLE_ATTRIBUTES = ["__thread", "__declspec(thread)"]

# Subset of OPTIONAL_*_FUNCS which may already have HAVE_* defined by Python.h
OPTIONAL_FUNCS_MAYBE = [
"ftello", "fseeko"
]

C99_COMPLEX_TYPES = [
'complex double', 'complex float', 'complex long double'
]
C99_COMPLEX_FUNCS = [
"cabs", "cacos", "cacosh", "carg", "casin", "casinh", "catan",
"catanh", "ccos", "ccosh", "cexp", "cimag", "clog", "conj", "cpow",
"cproj", "creal", "csin", "csinh", "csqrt", "ctan", "ctanh"
]

def fname2def(name):
return "HAVE_%s" % name.upper()

Expand Down
32 changes: 16 additions & 16 deletions numpy/core/src/common/npy_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
/* Disable broken functions on z/OS */
#if defined (__MVS__)

#undef HAVE_POWF
#undef HAVE_EXPF
#define BLOCK_POWF
#define BLOCK_EXPF
#undef HAVE___THREAD

#endif

/* Disable broken MS math functions */
#if defined(__MINGW32_VERSION)

#undef HAVE_ATAN2
#undef HAVE_ATAN2F
#undef HAVE_ATAN2L
#define BLOCK_ATAN2
#define BLOCK_ATAN2F
#define BLOCK_ATAN2L

#undef HAVE_HYPOT
#undef HAVE_HYPOTF
#undef HAVE_HYPOTL
#define BLOCK_HYPOT
#define BLOCK_HYPOTF
#define BLOCK_HYPOTL

#endif

Expand Down Expand Up @@ -66,23 +66,23 @@
#undef HAVE_CABSF
#undef HAVE_CABSL

#undef HAVE_HYPOT
#undef HAVE_HYPOTF
#undef HAVE_HYPOTL
#define BLOCK_HYPOT
#define BLOCK_HYPOTF
#define BLOCK_HYPOTL

#endif


/* Intel C for Windows uses POW for 64 bits longdouble*/
#if defined(_MSC_VER) && defined(__INTEL_COMPILER)
#if defined(HAVE_POWL) && (NPY_SIZEOF_LONGDOUBLE == 8)
#undef HAVE_POWL
#if NPY_SIZEOF_LONGDOUBLE == 8
#define BLOCK_POWL
#endif
#endif /* defined(_MSC_VER) && defined(__INTEL_COMPILER) */

/* powl gives zero division warning on OS X, see gh-8307 */
#if defined(HAVE_POWL) && defined(NPY_OS_DARWIN)
#undef HAVE_POWL
#if defined(NPY_OS_DARWIN)
#define BLOCK_POWL
#endif

#ifdef __CYGWIN__
Expand Down Expand Up @@ -119,7 +119,7 @@
#undef HAVE_CACOS

/* log2(exp2(i)) off by a few eps */
#undef HAVE_LOG2
#define BLOCK_LOG2

/* np.power(..., dtype=np.complex256) doesn't report overflow */
#undef HAVE_CPOWL
Expand Down
95 changes: 63 additions & 32 deletions numpy/core/src/npymath/npy_math_internal.h.src
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,33 @@ static const npy_uint64 MAGIC64[] = {0x5555555555555555ull, 0x3333333333333333ul

/*
*****************************************************************************
** BASIC MATH FUNCTIONS **
** BLOCKLIST-ABLE BASIC MATH FUNCTIONS **
*****************************************************************************
*/

/* The double variant for these functions are never blocked */

NPY_INPLACE double npy_exp(double x){
return exp(x);
}

NPY_INPLACE double npy_pow(double x, double y)
{
return pow(x, y);
}

NPY_INPLACE double npy_sqrt(double x)
{
return sqrt(x);
}

NPY_INPLACE double npy_modf(double x, double *iptr)
{
return modf(x, iptr);
}

/* The following functions can be blocked, even for doubles */

/* Original code by Konrad Hinsen. */
/* Taken from FreeBSD mlib, adapted for numpy
*
Expand All @@ -96,7 +119,7 @@ static const npy_uint64 MAGIC64[] = {0x5555555555555555ull, 0x3333333333333333ul
*/
NPY_INPLACE double npy_log2(double x)
{
#ifdef HAVE_LOG2
#ifndef BLOCK_LOG2
return log2(x);
#else
if (!npy_isfinite(x) || x <= 0.) {
Expand Down Expand Up @@ -136,7 +159,7 @@ NPY_INPLACE double npy_log2(double x)
#define NPY_DBL_EPSILON 1.2246467991473531772E-16
NPY_INPLACE double npy_atan2(double y, double x)
{
#ifdef HAVE_ATAN2
#ifndef BLOCK_ATAN2
return atan2(y, x);
#else
npy_int32 k, m, iy, ix, hx, hy;
Expand Down Expand Up @@ -220,7 +243,7 @@ NPY_INPLACE double npy_atan2(double y, double x)

NPY_INPLACE double npy_hypot(double x, double y)
{
#ifdef HAVE_HYPOT
#ifndef BLOCK_HYPOT
return hypot(x, y);
#else
double yx;
Expand Down Expand Up @@ -296,12 +319,10 @@ NPY_INPLACE double npy_hypot(double x, double y)
#define WORKAROUND_APPLE_TRIG_BUG 0
#endif

/* mandatory C99 functions */

/**begin repeat1
* #kind = sin,cos,tan,sinh,cosh,tanh,fabs,floor,ceil,rint,trunc,sqrt,log10,
* log,exp,expm1,asin,acos,atan,asinh,acosh,atanh,log1p,exp2#
* #TRIG_WORKAROUND = WORKAROUND_APPLE_TRIG_BUG*3, 0*21#
* #kind = sin,cos,tan,sinh,cosh,tanh,fabs,floor,ceil,rint,trunc,log10,
* log,expm1,asin,acos,atan,asinh,acosh,atanh,log1p,exp2#
* #TRIG_WORKAROUND = WORKAROUND_APPLE_TRIG_BUG*3, 0*19#
*/
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
{
Expand Down Expand Up @@ -329,12 +350,6 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
}
/**end repeat1**/


NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr)
{
return NPY__FP_SFX(modf)(x, iptr);
}

NPY_INPLACE @type@ npy_ldexp@c@(@type@ x, int exp)
{
return NPY__FP_SFX(ldexp)(x, exp);
Expand All @@ -352,13 +367,13 @@ NPY_INPLACE @type@ npy_cbrt@c@(@type@ x)

/**end repeat**/

/* Optional C99 functions */
/* Blocklist-able C99 functions */

/**begin repeat
* #type = npy_longdouble, npy_float#
* #TYPE = LONGDOUBLE, FLOAT#
* #c = l,f#
* #C = L,F#
* #type = npy_float,npy_longdouble#
* #TYPE = FLOAT,LONGDOUBLE#
* #c = f,l#
* #C = F,L#
*/
#undef NPY__FP_SFX
#if NPY_SIZEOF_@TYPE@ == NPY_SIZEOF_DOUBLE
Expand All @@ -368,21 +383,21 @@ NPY_INPLACE @type@ npy_cbrt@c@(@type@ x)
#endif

/**begin repeat1
* #kind = log2#
* #KIND = LOG2#
* #kind = exp,log2,sqrt#
* #KIND = EXP,LOG2,SQRT#
*/

#ifdef @kind@@c@
#undef @kind@@c@
#endif
#ifndef HAVE_@KIND@@C@
#ifdef BLOCK_@KIND@@C@
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
{
return (@type@) npy_@kind@((double)x);
}
#endif

#ifdef HAVE_@KIND@@C@
#ifndef BLOCK_@KIND@@C@
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
{
return NPY__FP_SFX(@kind@)(x);
Expand All @@ -399,20 +414,42 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
#ifdef @kind@@c@
#undef @kind@@c@
#endif
#ifndef HAVE_@KIND@@C@
#ifdef BLOCK_@KIND@@C@
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
{
return (@type@) npy_@kind@((double)x, (double) y);
}
#endif

#ifdef HAVE_@KIND@@C@
#ifndef BLOCK_@KIND@@C@
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
{
return NPY__FP_SFX(@kind@)(x, y);
}
#endif
/**end repeat1**/

#ifdef modf@c@
#undef modf@c@
#endif
#ifdef BLOCK_MODF@C@
NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr)
{
double niptr;
double y = npy_modf((double)x, &niptr);
*iptr = (@type@) niptr;
return (@type@) y;
}
#endif

#ifndef BLOCK_MODF@C@
NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr)
{
return NPY__FP_SFX(modf)(x, iptr);
}
#endif


/**end repeat**/


Expand Down Expand Up @@ -753,9 +790,3 @@ npy_popcount@c@(npy_@type@ a)
}
/**end repeat**/

/* XXX: will moving this to a define break anything? */

NPY_INPLACE double npy_pow(double x, double y)
{
return pow(x, y);
}
0