Skip to content
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

GH-100425: Timing experiment: For builtin_sum, try replacing Fast2Sum with 2Sum #100860

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
24 changes: 17 additions & 7 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,19 @@ builtin_vars_impl(PyObject *module, PyObject *object)
return d;
}

typedef struct{ double hi; double lo; } DoubleLength;

static inline DoubleLength
twosum(double a, double b)
{
double s = a + b;
double ap = s - b;
double bp = s - a;
double da = a - ap;
double db = b - bp;
double t = da + db;
return (DoubleLength) {s, t};
}

/*[clinic input]
sum as builtin_sum
Expand Down Expand Up @@ -2561,14 +2574,11 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
if (PyFloat_CheckExact(item)) {
// Improved Kahan–Babuška algorithm by Arnold Neumaier
// https://www.mat.univie.ac.at/~neum/scan/01.pdf
// but with Fast2Sum replaced with 2Sum.
double x = PyFloat_AS_DOUBLE(item);
double t = f_result + x;
if (fabs(f_result) >= fabs(x)) {
c += (f_result - t) + x;
} else {
c += (x - t) + f_result;
}
f_result = t;
DoubleLength t = twosum(f_result, x);
f_result = t.hi;
c += t.lo;
_Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc);
continue;
}
Expand Down