LibreOffice Module sc (master) 1
arraysumSSE2.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11#include "arraysum.hxx"
12
13#include <arraysumfunctor.hxx>
14
15#include <tools/simdsupport.hxx>
16
17#include <stdlib.h>
18
19#if SC_USE_SSE2
20
21namespace sc::op
22{
25static inline void sumSSE2(__m128d& sum, __m128d& err, const __m128d& value)
26{
27 const __m128d ANNULATE_SIGN_BIT = _mm_castsi128_pd(_mm_set1_epi64x(0x7FFF'FFFF'FFFF'FFFF));
28 // Temporal parameter
29 __m128d t = _mm_add_pd(sum, value);
30 // Absolute value of the total sum
31 __m128d asum = _mm_and_pd(sum, ANNULATE_SIGN_BIT);
32 // Absolute value of the value to add
33 __m128d avalue = _mm_and_pd(value, ANNULATE_SIGN_BIT);
34 // Compare the absolute values sum >= value
35 __m128d mask = _mm_cmpge_pd(asum, avalue);
36 // The following code has this form ( a - t + b)
37 // Case 1: a = sum b = value
38 // Case 2: a = value b = sum
39 __m128d a = _mm_add_pd(_mm_and_pd(mask, sum), _mm_andnot_pd(mask, value));
40 __m128d b = _mm_add_pd(_mm_and_pd(mask, value), _mm_andnot_pd(mask, sum));
41 err = _mm_add_pd(err, _mm_add_pd(_mm_sub_pd(a, t), b));
42 // Store result
43 sum = t;
44}
45
48KahanSum executeSSE2(size_t& i, size_t nSize, const double* pCurrent)
49{
50 // Make sure we don't fall out of bounds.
51 // This works by sums of 8 terms.
52 // So the 8'th term is i+7
53 // If we iterate until nSize won't fall out of bounds
54 if (nSize > i + 7)
55 {
56 // Setup sums and errors as 0
57 __m128d sum1 = _mm_setzero_pd();
58 __m128d err1 = _mm_setzero_pd();
59 __m128d sum2 = _mm_setzero_pd();
60 __m128d err2 = _mm_setzero_pd();
61 __m128d sum3 = _mm_setzero_pd();
62 __m128d err3 = _mm_setzero_pd();
63 __m128d sum4 = _mm_setzero_pd();
64 __m128d err4 = _mm_setzero_pd();
65
66 for (; i + 7 < nSize; i += 8)
67 {
68 // Kahan sum 1
69 __m128d load1 = _mm_loadu_pd(pCurrent);
70 sumSSE2(sum1, err1, load1);
71 pCurrent += 2;
72
73 // Kahan sum 2
74 __m128d load2 = _mm_loadu_pd(pCurrent);
75 sumSSE2(sum2, err2, load2);
76 pCurrent += 2;
77
78 // Kahan sum 3
79 __m128d load3 = _mm_loadu_pd(pCurrent);
80 sumSSE2(sum3, err3, load3);
81 pCurrent += 2;
82
83 // Kahan sum 4
84 __m128d load4 = _mm_loadu_pd(pCurrent);
85 sumSSE2(sum4, err4, load4);
86 pCurrent += 2;
87 }
88
89 // Now we combine pairwise summation with Kahan summation
90
91 // 1+2 3+4 -> 1, 3
92 sumSSE2(sum1, err1, sum2);
93 sumSSE2(sum1, err1, err2);
94 sumSSE2(sum3, err3, sum4);
95 sumSSE2(sum3, err3, err4);
96
97 // 1+3 -> 1
98 sumSSE2(sum1, err1, sum3);
99 sumSSE2(sum1, err1, err3);
100
101 // Store results
102 double sums[2];
103 double errs[2];
104 _mm_storeu_pd(&sums[0], sum1);
105 _mm_storeu_pd(&errs[0], err1);
106
107 // First Kahan & pairwise summation
108 // 0+1 -> 0
109 sumNeumanierNormal(sums[0], errs[0], sums[1]);
110 sumNeumanierNormal(sums[0], errs[0], errs[1]);
111
112 // Store result
113 return { sums[0], errs[0] };
114 }
115 return { 0.0, 0.0 };
116}
117
118} // namespace
119
120#endif
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
This class provides LO with Kahan summation algorithm About this algorithm: https://en....
Definition: kahan.hxx:26
uno_Any a
err
int i
KahanSum executeSSE2(size_t &i, size_t nSize, const double *pCurrent)
void sumNeumanierNormal(double &sum, double &err, const double &value)
Performs one step of the Neumanier sum between doubles Overwrites the summand and error @parma sum.
Definition: arraysum.hxx:24