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