diff --git a/Makefile.pre.in b/Makefile.pre.in index f03f535f6fa..4ee8fddbe14 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -611,10 +611,6 @@ pybuilddir.txt: $(BUILDPYTHON) exit 1 ; \ fi -# This is shared by the math and cmath modules -Modules/_math.o: Modules/_math.c Modules/_math.h - $(CC) -c $(CCSHARED) $(PY_CORE_CFLAGS) -o $@ $< - # blake2s is auto-generated from blake2b $(srcdir)/Modules/_blake2/blake2s_impl.c: $(srcdir)/Modules/_blake2/blake2b_impl.c $(srcdir)/Modules/_blake2/blake2b2s.py $(PYTHON_FOR_REGEN) $(srcdir)/Modules/_blake2/blake2b2s.py @@ -625,7 +621,7 @@ $(srcdir)/Modules/_blake2/blake2s_impl.c: $(srcdir)/Modules/_blake2/blake2b_impl # -s, --silent or --quiet is always the first char. # Under BSD make, MAKEFLAGS might be " -s -v x=y". # Ignore macros passed by GNU make, passed after -- -sharedmods: $(BUILDPYTHON) pybuilddir.txt Modules/_math.o +sharedmods: $(BUILDPYTHON) pybuilddir.txt @case "`echo X $$MAKEFLAGS | sed 's/^X //;s/ -- .*//'`" in \ *\ -s*|s*) quiet="-q";; \ *) quiet="";; \ diff --git a/Misc/NEWS.d/next/Build/2021-10-24-21-49-49.bpo-45548.UWx0UC.rst b/Misc/NEWS.d/next/Build/2021-10-24-21-49-49.bpo-45548.UWx0UC.rst new file mode 100644 index 00000000000..15fd56615ff --- /dev/null +++ b/Misc/NEWS.d/next/Build/2021-10-24-21-49-49.bpo-45548.UWx0UC.rst @@ -0,0 +1,3 @@ +The :mod:`math` and :mod:`cmath` implementation now require a C99 compatible +``libm`` and no longer ship with workarounds for missing acosh, asinh, atanh, +expm1, and log1p functions. diff --git a/Modules/Setup b/Modules/Setup index d06d733fc9c..99c7d4e909e 100644 --- a/Modules/Setup +++ b/Modules/Setup @@ -171,8 +171,8 @@ time timemodule.c #array arraymodule.c #audioop audioop.c #binascii binascii.c -#cmath cmathmodule.c _math.c # -lm -#math mathmodule.c _math.c # -lm +#cmath cmathmodule.c # -lm +#math mathmodule.c # -lm #pyexpat -I$(srcdir)/Modules/expat expat/xmlparse.c expat/xmlrole.c expat/xmltok.c pyexpat.c #unicodedata unicodedata.c diff --git a/Modules/_math.c b/Modules/_math.c deleted file mode 100644 index c1936a1088a..00000000000 --- a/Modules/_math.c +++ /dev/null @@ -1,270 +0,0 @@ -/* Definitions of some C99 math library functions, for those platforms - that don't implement these functions already. */ - -#ifndef Py_BUILD_CORE_BUILTIN -# define Py_BUILD_CORE_MODULE 1 -#endif - -#include "Python.h" -#include -#include "_math.h" - -/* The following copyright notice applies to the original - implementations of acosh, asinh and atanh. */ - -/* - * ==================================================== - * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. - * - * Developed at SunPro, a Sun Microsystems, Inc. business. - * Permission to use, copy, modify, and distribute this - * software is freely granted, provided that this notice - * is preserved. - * ==================================================== - */ - -#if !defined(HAVE_ACOSH) || !defined(HAVE_ASINH) -static const double ln2 = 6.93147180559945286227E-01; -static const double two_pow_p28 = 268435456.0; /* 2**28 */ -#endif -#if !defined(HAVE_ASINH) || !defined(HAVE_ATANH) -static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */ -#endif -#if !defined(HAVE_ATANH) && !defined(Py_NAN) -static const double zero = 0.0; -#endif - - -#ifndef HAVE_ACOSH -/* acosh(x) - * Method : - * Based on - * acosh(x) = log [ x + sqrt(x*x-1) ] - * we have - * acosh(x) := log(x)+ln2, if x is large; else - * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else - * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1. - * - * Special cases: - * acosh(x) is NaN with signal if x<1. - * acosh(NaN) is NaN without signal. - */ - -double -_Py_acosh(double x) -{ - if (Py_IS_NAN(x)) { - return x+x; - } - if (x < 1.) { /* x < 1; return a signaling NaN */ - errno = EDOM; -#ifdef Py_NAN - return Py_NAN; -#else - return (x-x)/(x-x); -#endif - } - else if (x >= two_pow_p28) { /* x > 2**28 */ - if (Py_IS_INFINITY(x)) { - return x+x; - } - else { - return log(x) + ln2; /* acosh(huge)=log(2x) */ - } - } - else if (x == 1.) { - return 0.0; /* acosh(1) = 0 */ - } - else if (x > 2.) { /* 2 < x < 2**28 */ - double t = x * x; - return log(2.0 * x - 1.0 / (x + sqrt(t - 1.0))); - } - else { /* 1 < x <= 2 */ - double t = x - 1.0; - return m_log1p(t + sqrt(2.0 * t + t * t)); - } -} -#endif /* HAVE_ACOSH */ - - -#ifndef HAVE_ASINH -/* asinh(x) - * Method : - * Based on - * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ] - * we have - * asinh(x) := x if 1+x*x=1, - * := sign(x)*(log(x)+ln2) for large |x|, else - * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else - * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2))) - */ - -double -_Py_asinh(double x) -{ - double w; - double absx = fabs(x); - - if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) { - return x+x; - } - if (absx < two_pow_m28) { /* |x| < 2**-28 */ - return x; /* return x inexact except 0 */ - } - if (absx > two_pow_p28) { /* |x| > 2**28 */ - w = log(absx) + ln2; - } - else if (absx > 2.0) { /* 2 < |x| < 2**28 */ - w = log(2.0 * absx + 1.0 / (sqrt(x * x + 1.0) + absx)); - } - else { /* 2**-28 <= |x| < 2= */ - double t = x*x; - w = m_log1p(absx + t / (1.0 + sqrt(1.0 + t))); - } - return copysign(w, x); - -} -#endif /* HAVE_ASINH */ - - -#ifndef HAVE_ATANH -/* atanh(x) - * Method : - * 1.Reduced x to positive by atanh(-x) = -atanh(x) - * 2.For x>=0.5 - * 1 2x x - * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * -------) - * 2 1 - x 1 - x - * - * For x<0.5 - * atanh(x) = 0.5*log1p(2x+2x*x/(1-x)) - * - * Special cases: - * atanh(x) is NaN if |x| >= 1 with signal; - * atanh(NaN) is that NaN with no signal; - * - */ - -double -_Py_atanh(double x) -{ - double absx; - double t; - - if (Py_IS_NAN(x)) { - return x+x; - } - absx = fabs(x); - if (absx >= 1.) { /* |x| >= 1 */ - errno = EDOM; -#ifdef Py_NAN - return Py_NAN; -#else - return x / zero; -#endif - } - if (absx < two_pow_m28) { /* |x| < 2**-28 */ - return x; - } - if (absx < 0.5) { /* |x| < 0.5 */ - t = absx+absx; - t = 0.5 * m_log1p(t + t*absx / (1.0 - absx)); - } - else { /* 0.5 <= |x| <= 1.0 */ - t = 0.5 * m_log1p((absx + absx) / (1.0 - absx)); - } - return copysign(t, x); -} -#endif /* HAVE_ATANH */ - - -#ifndef HAVE_EXPM1 -/* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed - to avoid the significant loss of precision that arises from direct - evaluation of the expression exp(x) - 1, for x near 0. */ - -double -_Py_expm1(double x) -{ - /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this - also works fine for infinities and nans. - - For smaller x, we can use a method due to Kahan that achieves close to - full accuracy. - */ - - if (fabs(x) < 0.7) { - double u; - u = exp(x); - if (u == 1.0) - return x; - else - return (u - 1.0) * x / log(u); - } - else - return exp(x) - 1.0; -} -#endif /* HAVE_EXPM1 */ - - -/* log1p(x) = log(1+x). The log1p function is designed to avoid the - significant loss of precision that arises from direct evaluation when x is - small. */ - -double -_Py_log1p(double x) -{ -#ifdef HAVE_LOG1P - /* Some platforms supply a log1p function but don't respect the sign of - zero: log1p(-0.0) gives 0.0 instead of the correct result of -0.0. - - To save fiddling with configure tests and platform checks, we handle the - special case of zero input directly on all platforms. - */ - if (x == 0.0) { - return x; - } - else { - return log1p(x); - } -#else - /* For x small, we use the following approach. Let y be the nearest float - to 1+x, then - - 1+x = y * (1 - (y-1-x)/y) - - so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the - second term is well approximated by (y-1-x)/y. If abs(x) >= - DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest - then y-1-x will be exactly representable, and is computed exactly by - (y-1)-x. - - If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be - round-to-nearest then this method is slightly dangerous: 1+x could be - rounded up to 1+DBL_EPSILON instead of down to 1, and in that case - y-1-x will not be exactly representable any more and the result can be - off by many ulps. But this is easily fixed: for a floating-point - number |x| < DBL_EPSILON/2., the closest floating-point number to - log(1+x) is exactly x. - */ - - double y; - if (fabs(x) < DBL_EPSILON / 2.) { - return x; - } - else if (-0.5 <= x && x <= 1.) { - /* WARNING: it's possible that an overeager compiler - will incorrectly optimize the following two lines - to the equivalent of "return log(1.+x)". If this - happens, then results from log1p will be inaccurate - for small x. */ - y = 1.+x; - return log(y) - ((y - 1.) - x) / y; - } - else { - /* NaNs and infinities should end up here */ - return log(1.+x); - } -#endif /* ifdef HAVE_LOG1P */ -} - diff --git a/Modules/_math.h b/Modules/_math.h index 398b7e8874a..4a6bc223ef5 100644 --- a/Modules/_math.h +++ b/Modules/_math.h @@ -1,41 +1,24 @@ -#ifdef HAVE_ACOSH -# define m_acosh acosh -#else -/* if the system doesn't have acosh, use the substitute - function defined in Modules/_math.c. */ -double _Py_acosh(double x); -# define m_acosh _Py_acosh -#endif +/* log1p(x) = log(1+x). The log1p function is designed to avoid the + significant loss of precision that arises from direct evaluation when x is + small. Use the substitute from _math.h on all platforms: it includes + workarounds for buggy handling of zeros. + */ -#ifdef HAVE_ASINH -# define m_asinh asinh -#else -/* if the system doesn't have asinh, use the substitute - function defined in Modules/_math.c. */ -double _Py_asinh(double x); -# define m_asinh _Py_asinh -#endif +static double +_Py_log1p(double x) +{ + /* Some platforms supply a log1p function but don't respect the sign of + zero: log1p(-0.0) gives 0.0 instead of the correct result of -0.0. -#ifdef HAVE_ATANH -# define m_atanh atanh -#else -/* if the system doesn't have atanh, use the substitute - function defined in Modules/_math.c. */ -double _Py_atanh(double x); -#define m_atanh _Py_atanh -#endif + To save fiddling with configure tests and platform checks, we handle the + special case of zero input directly on all platforms. + */ + if (x == 0.0) { + return x; + } + else { + return log1p(x); + } +} -#ifdef HAVE_EXPM1 -# define m_expm1 expm1 -#else -/* if the system doesn't have expm1, use the substitute - function defined in Modules/_math.c. */ -double _Py_expm1(double x); -#define m_expm1 _Py_expm1 -#endif - -double _Py_log1p(double x); - -/* Use the substitute from _math.c on all platforms: - it includes workarounds for buggy handling of zeros. */ #define m_log1p _Py_log1p diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index 0e0489c5fe2..281d3937e26 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -8,11 +8,13 @@ #include "Python.h" #include "pycore_dtoa.h" -#include "_math.h" /* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from float.h. We assume that FLT_RADIX is either 2 or 16. */ #include +/* For _Py_log1p with workarounds for buggy handling of zeros. */ +#include "_math.h" + #include "clinic/cmathmodule.c.h" /*[clinic input] module cmath @@ -246,7 +248,7 @@ cmath_acos_impl(PyObject *module, Py_complex z) s2.imag = z.imag; s2 = cmath_sqrt_impl(module, s2); r.real = 2.*atan2(s1.real, s2.real); - r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real); + r.imag = asinh(s2.real*s1.imag - s2.imag*s1.real); } errno = 0; return r; @@ -280,7 +282,7 @@ cmath_acosh_impl(PyObject *module, Py_complex z) s2.real = z.real + 1.; s2.imag = z.imag; s2 = cmath_sqrt_impl(module, s2); - r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag); + r.real = asinh(s1.real*s2.real + s1.imag*s2.imag); r.imag = 2.*atan2(s1.imag, s2.real); } errno = 0; @@ -340,7 +342,7 @@ cmath_asinh_impl(PyObject *module, Py_complex z) s2.real = 1.-z.imag; s2.imag = z.real; s2 = cmath_sqrt_impl(module, s2); - r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag); + r.real = asinh(s1.real*s2.imag-s2.real*s1.imag); r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag); } errno = 0; diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 6c12a4e70dd..67669f19bc2 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -61,6 +61,9 @@ raised for division by zero and mod by zero. #include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_dtoa.h" // _Py_dg_infinity() #include "pycore_long.h" // _PyLong_GetZero() +/* For DBL_EPSILON in _math.h */ +#include +/* For _Py_log1p with workarounds for buggy handling of zeros. */ #include "_math.h" #include "clinic/mathmodule.c.h" @@ -1166,14 +1169,14 @@ FUNC1(acos, acos, 0, "acos($module, x, /)\n--\n\n" "Return the arc cosine (measured in radians) of x.\n\n" "The result is between 0 and pi.") -FUNC1(acosh, m_acosh, 0, +FUNC1(acosh, acosh, 0, "acosh($module, x, /)\n--\n\n" "Return the inverse hyperbolic cosine of x.") FUNC1(asin, asin, 0, "asin($module, x, /)\n--\n\n" "Return the arc sine (measured in radians) of x.\n\n" "The result is between -pi/2 and pi/2.") -FUNC1(asinh, m_asinh, 0, +FUNC1(asinh, asinh, 0, "asinh($module, x, /)\n--\n\n" "Return the inverse hyperbolic sine of x.") FUNC1(atan, atan, 0, @@ -1184,7 +1187,7 @@ FUNC2(atan2, m_atan2, "atan2($module, y, x, /)\n--\n\n" "Return the arc tangent (measured in radians) of y/x.\n\n" "Unlike atan(y/x), the signs of both x and y are considered.") -FUNC1(atanh, m_atanh, 0, +FUNC1(atanh, atanh, 0, "atanh($module, x, /)\n--\n\n" "Return the inverse hyperbolic tangent of x.") FUNC1(cbrt, cbrt, 0, @@ -1245,7 +1248,7 @@ FUNC1A(erfc, m_erfc, FUNC1(exp, exp, 1, "exp($module, x, /)\n--\n\n" "Return e raised to the power of x.") -FUNC1(expm1, m_expm1, 1, +FUNC1(expm1, expm1, 1, "expm1($module, x, /)\n--\n\n" "Return exp(x)-1.\n\n" "This function avoids the loss of precision involved in the direct " diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index 1b484be5e7a..646bc8e4800 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -332,7 +332,6 @@ - diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters index c1519760d22..62aab5bccf9 100644 --- a/PCbuild/pythoncore.vcxproj.filters +++ b/PCbuild/pythoncore.vcxproj.filters @@ -689,9 +689,6 @@ Modules - - Modules - Modules diff --git a/configure b/configure index 198b0703fd6..b72660d24c2 100755 --- a/configure +++ b/configure @@ -15092,7 +15092,7 @@ fi LIBS_SAVE=$LIBS LIBS="$LIBS $LIBM" -for ac_func in acosh asinh atanh erf erfc expm1 finite gamma +for ac_func in acosh asinh atanh erf erfc expm1 finite gamma lgamma log1p log2 tgamma do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" @@ -15101,21 +15101,13 @@ if eval test \"x\$"$as_ac_var"\" = x"yes"; then : #define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 _ACEOF -fi -done - -for ac_func in lgamma log1p log2 tgamma -do : - as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` -ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" -if eval test \"x\$"$as_ac_var"\" = x"yes"; then : - cat >>confdefs.h <<_ACEOF -#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 -_ACEOF +else + as_fn_error $? "Python requires C99 compatible libm" "$LINENO" 5 fi done +LIBS=$LIBS_SAVE # For multiprocessing module, check that sem_open # actually works. For FreeBSD versions <= 7.2, diff --git a/configure.ac b/configure.ac index edda08daac1..4093938c209 100644 --- a/configure.ac +++ b/configure.ac @@ -4692,8 +4692,12 @@ fi LIBS_SAVE=$LIBS LIBS="$LIBS $LIBM" -AC_CHECK_FUNCS([acosh asinh atanh erf erfc expm1 finite gamma]) -AC_CHECK_FUNCS([lgamma log1p log2 tgamma]) +AC_CHECK_FUNCS( + [acosh asinh atanh erf erfc expm1 finite gamma lgamma log1p log2 tgamma], + [], + [AC_MSG_ERROR([Python requires C99 compatible libm])] +) +LIBS=$LIBS_SAVE # For multiprocessing module, check that sem_open # actually works. For FreeBSD versions <= 7.2, diff --git a/setup.py b/setup.py index 4bef3cb52f2..850cc1ba41e 100644 --- a/setup.py +++ b/setup.py @@ -904,18 +904,14 @@ class PyBuildExt(build_ext): # Context Variables self.add(Extension('_contextvars', ['_contextvarsmodule.c'])) - shared_math = 'Modules/_math.o' - # math library functions, e.g. sin() self.add(Extension('math', ['mathmodule.c'], - extra_objects=[shared_math], - depends=['_math.h', shared_math], + depends=['_math.h'], libraries=['m'])) # complex math library functions self.add(Extension('cmath', ['cmathmodule.c'], - extra_objects=[shared_math], - depends=['_math.h', shared_math], + depends=['_math.h'], libraries=['m'])) # time libraries: librt may be needed for clock_gettime()