LibreOffice Module sc (master)  1
arraysumAVX.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 #define LO_ARRAYSUM_SPACE AVX
12 #include "arraysum.hxx"
13 
15 
16 #include <tools/simd.hxx>
17 #include <tools/simdsupport.hxx>
18 
19 #include <stdlib.h>
20 
21 namespace sc::op
22 {
23 #ifdef LO_AVX_AVAILABLE
24 
25 bool hasAVXCode() { return true; }
26 
27 using namespace AVX;
28 
31 static inline void sumAVX(__m256d& sum, __m256d& err, const __m256d& value)
32 {
33  const __m256d ANNULATE_SIGN_BIT
34  = _mm256_castsi256_pd(_mm256_set1_epi64x(0x7FFF'FFFF'FFFF'FFFF));
35  // Temporal parameter
36  __m256d t = _mm256_add_pd(sum, value);
37  // Absolute value of the total sum
38  __m256d asum = _mm256_and_pd(sum, ANNULATE_SIGN_BIT);
39  // Absolute value of the value to add
40  __m256d avalue = _mm256_and_pd(value, ANNULATE_SIGN_BIT);
41  // Compare the absolute values sum >= value
42  __m256d mask = _mm256_cmp_pd(asum, avalue, _CMP_GE_OQ);
43  // The following code has this form ( a - t + b)
44  // Case 1: a = sum b = value
45  // Case 2: a = value b = sum
46  __m256d a = _mm256_add_pd(_mm256_and_pd(mask, sum), _mm256_andnot_pd(mask, value));
47  __m256d b = _mm256_add_pd(_mm256_and_pd(mask, value), _mm256_andnot_pd(mask, sum));
48  err = _mm256_add_pd(err, _mm256_add_pd(_mm256_sub_pd(a, t), b));
49  // Store result
50  sum = t;
51 }
52 
55 KahanSumSimple executeAVX(size_t& i, size_t nSize, const double* pCurrent)
56 {
57  // Make sure we don't fall out of bounds.
58  // This works by sums of 8 terms.
59  // So the 8'th term is i+7
60  // If we iterate until nSize won't fall out of bounds
61  if (nSize > i + 7)
62  {
63  // Setup sums and errors as 0
64  __m256d sum1 = _mm256_setzero_pd();
65  __m256d err1 = _mm256_setzero_pd();
66  __m256d sum2 = _mm256_setzero_pd();
67  __m256d err2 = _mm256_setzero_pd();
68 
69  for (; i + 7 < nSize; i += 8)
70  {
71  // Kahan sum 1
72  __m256d load1 = _mm256_loadu_pd(pCurrent);
73  sumAVX(sum1, err1, load1);
74  pCurrent += 4;
75 
76  // Kahan sum 2
77  __m256d load2 = _mm256_loadu_pd(pCurrent);
78  sumAVX(sum2, err2, load2);
79  pCurrent += 4;
80  }
81 
82  // Now we combine pairwise summation with Kahan summation
83 
84  // sum 1 + sum 2 -> sum 1
85  sumAVX(sum1, err1, sum2);
86  sumAVX(sum1, err1, err2);
87 
88  // Store results
89  double sums[4];
90  double errs[4];
91  _mm256_storeu_pd(&sums[0], sum1);
92  _mm256_storeu_pd(&errs[0], err1);
93 
94  // First Kahan & pairwise summation
95  // 0+1 1+2 -> 0, 2
96  sumNeumanierNormal(sums[0], errs[0], sums[1]);
97  sumNeumanierNormal(sums[2], errs[2], sums[3]);
98  sumNeumanierNormal(sums[0], errs[0], errs[1]);
99  sumNeumanierNormal(sums[2], errs[2], errs[3]);
100 
101  // 0+2 -> 0
102  sumNeumanierNormal(sums[0], errs[0], sums[2]);
103  sumNeumanierNormal(sums[0], errs[0], errs[2]);
104 
105  // Store result
106  return { sums[0], errs[0] };
107  }
108  return { 0.0, 0.0 };
109 }
110 
111 #else // LO_AVX_AVAILABLE
112 
113 bool hasAVXCode() { return false; }
114 
115 KahanSumSimple executeAVX(size_t&, size_t, const double*) { abort(); }
116 
117 #endif
118 
119 } // end namespace sc::op
120 
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
KahanSumSimple executeAVX(size_t &, size_t, const double *)
err
int i
bool hasAVXCode()
XPropertyListType t
if(aStr!=aBuf) UpdateName_Impl(m_xFollowLb.get()
Any value
INLINE void sumNeumanierNormal(double &sum, double &err, const double &value)
Performs one step of the Neumanier sum between doubles Overwrites the summand and error sum...
Definition: arraysum.hxx:37