LibreOffice Module sc (master)  1
arraysumfunctor.hxx
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 #ifndef INCLUDED_SC_SOURCE_CORE_INC_ARRAYSUMFUNCTOR_HXX
12 #define INCLUDED_SC_SOURCE_CORE_INC_ARRAYSUMFUNCTOR_HXX
13 
14 #include <cstdint>
15 #include <cmath>
16 
17 #include <sal/mathconf.h>
18 #include <sal/types.h>
19 #include <tools/simd.hxx>
20 #include <tools/cpuid.hxx>
21 
22 namespace sc
23 {
25 {
26 private:
27  const double* mpArray;
28  size_t mnSize;
29 
30 public:
31  ArraySumFunctor(const double* pArray, size_t nSize)
32  : mpArray(pArray)
33  , mnSize(nSize)
34  {
35  }
36 
37  double operator()()
38  {
39  const static bool hasSSE2 = cpuid::hasSSE2();
40 
41  double fSum = 0.0;
42  size_t i = 0;
43  const double* pCurrent = mpArray;
44 
45  if (hasSSE2)
46  {
47  while (i < mnSize && !simd::isAligned<double, 16>(pCurrent))
48  {
49  fSum += *pCurrent++;
50  i++;
51  }
52  if (i < mnSize)
53  {
54  fSum += executeSSE2(i, pCurrent);
55  }
56  }
57  else
58  fSum += executeUnrolled(i, pCurrent);
59 
60  // sum rest of the array
61 
62  for (; i < mnSize; ++i)
63  fSum += mpArray[i];
64 
65  // If the sum is a NaN, some of the terms were empty cells, probably.
66  // Re-calculate, carefully
67  if (!std::isfinite(fSum))
68  {
69  sal_uInt32 nErr = reinterpret_cast<sal_math_Double*>(&fSum)->nan_parts.fraction_lo;
70  if (nErr & 0xffff0000)
71  {
72  fSum = 0;
73  for (i = 0; i < mnSize; i++)
74  {
75  if (!std::isfinite(mpArray[i]))
76  {
77  nErr = reinterpret_cast<const sal_math_Double*>(&mpArray[i])
78  ->nan_parts.fraction_lo;
79  if (!(nErr & 0xffff0000))
80  fSum += mpArray[i]; // Let errors encoded as NaNs propagate ???
81  }
82  else
83  fSum += mpArray[i];
84  }
85  }
86  }
87  return fSum;
88  }
89 
90 private:
91  double executeSSE2(size_t& i, const double* pCurrent) const;
92  double executeUnrolled(size_t& i, const double* pCurrent) const
93  {
94  size_t nRealSize = mnSize - i;
95  size_t nUnrolledSize = nRealSize - (nRealSize % 4);
96 
97  if (nUnrolledSize > 0)
98  {
99  double sum0 = 0.0;
100  double sum1 = 0.0;
101  double sum2 = 0.0;
102  double sum3 = 0.0;
103 
104  for (; i < nUnrolledSize; i += 4)
105  {
106  sum0 += *pCurrent++;
107  sum1 += *pCurrent++;
108  sum2 += *pCurrent++;
109  sum3 += *pCurrent++;
110  }
111  return sum0 + sum1 + sum2 + sum3;
112  }
113  return 0.0;
114  }
115 };
116 
117 } // end namespace sc
118 
119 #endif
120 
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const double * mpArray
bool hasSSE2()
double executeUnrolled(size_t &i, const double *pCurrent) const
int i
ArraySumFunctor(const double *pArray, size_t nSize)
double executeSSE2(size_t &i, const double *pCurrent) const
if(!pCandidateA->getEnd().equal(pCandidateB->getStart()))