Skip to content

Commit df3851f

Browse files
authored
GH-100485: Convert from Fast2Sum to 2Sum (GH-100836)
1 parent 951303f commit df3851f

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

Modules/mathmodule.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ based on ideas from three sources:
28472847
28482848
The double length routines allow for quite a bit of instruction
28492849
level parallelism. On a 3.22 Ghz Apple M1 Max, the incremental
2850-
cost of increasing the input vector size by one is 6.25 nsec.
2850+
cost of increasing the input vector size by one is 6.0 nsec.
28512851
28522852
dl_zero() returns an extended precision zero
28532853
dl_split() exactly splits a double into two half precision components.
@@ -2860,22 +2860,25 @@ dl_to_d() converts from extended precision to double precision.
28602860

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

2863+
static const DoubleLength dl_zero = {0.0, 0.0};
2864+
28632865
static inline DoubleLength
2864-
dl_zero()
2866+
twosum(double a, double b)
28652867
{
2866-
return (DoubleLength) {0.0, 0.0};
2868+
double s = a + b;
2869+
double ap = s - b;
2870+
double bp = s - a;
2871+
double da = a - ap;
2872+
double db = b - bp;
2873+
double t = da + db;
2874+
return (DoubleLength) {s, t};
28672875
}
2876+
28682877
static inline DoubleLength
28692878
dl_add(DoubleLength total, double x)
28702879
{
2871-
double s = total.hi + x;
2872-
double c = total.lo;
2873-
if (fabs(total.hi) >= fabs(x)) {
2874-
c += (total.hi - s) + x;
2875-
} else {
2876-
c += (x - s) + total.hi;
2877-
}
2878-
return (DoubleLength) {s, c};
2880+
DoubleLength s = twosum(total.hi, x);
2881+
return (DoubleLength) {s.hi, total.lo + s.lo};
28792882
}
28802883

28812884
static inline DoubleLength
@@ -2941,7 +2944,7 @@ math_sumprod_impl(PyObject *module, PyObject *p, PyObject *q)
29412944
bool int_path_enabled = true, int_total_in_use = false;
29422945
bool flt_path_enabled = true, flt_total_in_use = false;
29432946
long int_total = 0;
2944-
DoubleLength flt_total = dl_zero();
2947+
DoubleLength flt_total = dl_zero;
29452948

29462949
p_it = PyObject_GetIter(p);
29472950
if (p_it == NULL) {
@@ -3101,7 +3104,7 @@ math_sumprod_impl(PyObject *module, PyObject *p, PyObject *q)
31013104
Py_SETREF(total, new_total);
31023105
new_total = NULL;
31033106
Py_CLEAR(term_i);
3104-
flt_total = dl_zero();
3107+
flt_total = dl_zero;
31053108
flt_total_in_use = false;
31063109
}
31073110
}

0 commit comments

Comments
 (0)