diff --git a/Misc/NEWS.d/next/Library/2019-01-11-20-21-59.bpo-35719.qyRcpE.rst b/Misc/NEWS.d/next/Library/2019-01-11-20-21-59.bpo-35719.qyRcpE.rst new file mode 100644 index 00000000000..e46e1429647 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-01-11-20-21-59.bpo-35719.qyRcpE.rst @@ -0,0 +1,2 @@ +Sped up multi-argument :mod:`math` functions atan2(), copysign(), +remainder() and hypot() by 1.3--2.5 times. diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2db2b45dd20..a190f5ccf7e 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -997,14 +997,14 @@ math_1_to_int(PyObject *arg, double (*func) (double), int can_overflow) } static PyObject * -math_2(PyObject *args, double (*func) (double, double), const char *funcname) +math_2(PyObject *const *args, Py_ssize_t nargs, + double (*func) (double, double), const char *funcname) { - PyObject *ox, *oy; double x, y, r; - if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy)) + if (!_PyArg_CheckPositional(funcname, nargs, 2, 2)) return NULL; - x = PyFloat_AsDouble(ox); - y = PyFloat_AsDouble(oy); + x = PyFloat_AsDouble(args[0]); + y = PyFloat_AsDouble(args[1]); if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) return NULL; errno = 0; @@ -1042,8 +1042,8 @@ math_2(PyObject *args, double (*func) (double, double), const char *funcname) PyDoc_STRVAR(math_##funcname##_doc, docstring); #define FUNC2(funcname, func, docstring) \ - static PyObject * math_##funcname(PyObject *self, PyObject *args) { \ - return math_2(args, func, #funcname); \ + static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \ + return math_2(args, nargs, func, #funcname); \ }\ PyDoc_STRVAR(math_##funcname##_doc, docstring); @@ -2181,9 +2181,9 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q) /* AC: cannot convert yet, waiting for *args support */ static PyObject * -math_hypot(PyObject *self, PyObject *args) +math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { - Py_ssize_t i, n; + Py_ssize_t i; PyObject *item; double max = 0.0; double x, result; @@ -2191,15 +2191,14 @@ math_hypot(PyObject *self, PyObject *args) double coord_on_stack[NUM_STACK_ELEMS]; double *coordinates = coord_on_stack; - n = PyTuple_GET_SIZE(args); - if (n > NUM_STACK_ELEMS) { - coordinates = (double *) PyObject_Malloc(n * sizeof(double)); + if (nargs > NUM_STACK_ELEMS) { + coordinates = (double *) PyObject_Malloc(nargs * sizeof(double)); if (coordinates == NULL) { return PyErr_NoMemory(); } } - for (i=0 ; i