Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / opencl / op_spreadsheet.cxx
blobabb75684a805971ec4c6d8304c9d1a491b8da41d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include "op_spreadsheet.hxx"
12 #include "formulagroup.hxx"
13 #include "document.hxx"
14 #include "formulacell.hxx"
15 #include "tokenarray.hxx"
16 #include "compiler.hxx"
17 #include "interpre.hxx"
18 #include "formula/vectortoken.hxx"
19 #include <sstream>
21 using namespace formula;
23 namespace sc { namespace opencl {
25 void OpVLookup::GenSlidingWindowFunction(std::stringstream &ss,
26 const std::string sSymName, SubArguments &vSubArguments)
28 ss << "\ndouble " << sSymName;
29 ss << "_"<< BinFuncName() <<"(";
30 for (unsigned i = 0; i < vSubArguments.size(); i++)
32 if (i)
33 ss << ",";
34 vSubArguments[i]->GenSlidingWindowDecl(ss);
36 ss << ")\n {\n";
37 ss << " int gid0=get_global_id(0);\n";
38 ss << " double tmp = NAN;\n";
39 ss << " double intermediate = DBL_MAX;\n";
40 ss << " int singleIndex = gid0;\n";
41 ss << " int rowNum = -1;\n";
42 GenTmpVariables(ss,vSubArguments);
43 int arg=0;
44 CheckSubArgumentIsNan(ss,vSubArguments,arg++);
45 int secondParaWidth = 1;
46 if(vSubArguments[1]->GetFormulaToken()->GetType() ==
47 formula::svDoubleVectorRef)
49 FormulaToken *tmpCur = vSubArguments[1]->GetFormulaToken();
50 const formula::DoubleVectorRefToken*pCurDVR= dynamic_cast<const
51 formula::DoubleVectorRefToken *>(tmpCur);
52 secondParaWidth = pCurDVR->GetArrays().size();
54 arg+=secondParaWidth;
55 CheckSubArgumentIsNan(ss,vSubArguments,arg++);
56 if(vSubArguments.size() == (unsigned int)(3+(secondParaWidth-1)))
58 ss << " double tmp";
59 ss << 3+(secondParaWidth-1);
60 ss << "= 1;\n";
62 else
64 CheckSubArgumentIsNan(ss,vSubArguments,arg++);
67 if(vSubArguments[1]->GetFormulaToken()->GetType() ==
68 formula::svDoubleVectorRef)
70 FormulaToken *tmpCur = vSubArguments[1]->GetFormulaToken();
71 const formula::DoubleVectorRefToken*pCurDVR= dynamic_cast<const
72 formula::DoubleVectorRefToken *>(tmpCur);
73 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
74 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
75 pCurDVR->GetRefRowSize() ;
76 int unrollSize = 8;
77 ss << " int loop;\n";
78 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
79 ss << " loop = ("<<nCurWindowSize<<" - gid0)/";
80 ss << unrollSize<<";\n";
82 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
83 ss << " loop = ("<<nCurWindowSize<<" + gid0)/";
84 ss << unrollSize<<";\n";
86 } else {
87 ss << " loop = "<<nCurWindowSize<<"/"<< unrollSize<<";\n";
90 for(int i=0;i<secondParaWidth;i++)
93 ss << " for ( int j = 0;j< loop; j++)\n";
94 ss << " {\n";
95 ss << " int i = ";
96 if (!pCurDVR->IsStartFixed()&& pCurDVR->IsEndFixed()) {
97 ss << "gid0 + j * "<< unrollSize <<";\n";
98 }else {
99 ss << "j * "<< unrollSize <<";\n";
101 if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
103 ss << " int doubleIndex = i+gid0;\n";
104 }else
106 ss << " int doubleIndex = i;\n";
108 ss << " if(tmp";
109 ss << 3+(secondParaWidth-1);
110 ss << " == 1)\n";
111 ss << " {\n";
113 for(int j =0;j < unrollSize;j++)
115 CheckSubArgumentIsNan(ss,vSubArguments,1+i);
117 ss << " if((tmp0 - tmp";
118 ss << 1+i;
119 ss << ")>=0 && intermediate > ( tmp0 -tmp";
120 ss << 1+i;
121 ss << "))\n";
122 ss << " {\n";
123 ss << " rowNum = doubleIndex;\n";
124 ss << " intermediate = tmp0 - tmp";
125 ss << 1+i;
126 ss << ";\n";
127 ss << " }\n";
128 ss << " i++;\n";
129 ss << " doubleIndex++;\n";
132 ss << " }else\n";
133 ss << " {\n";
134 for(int j =0;j < unrollSize;j++)
136 CheckSubArgumentIsNan(ss,vSubArguments,1+i);
138 ss << " if(tmp0 == tmp";
139 ss << 1+i;
140 ss << " && rowNum == -1)\n";
141 ss << " {\n";
142 ss << " rowNum = doubleIndex;\n";
143 ss << " }\n";
144 ss << " i++;\n";
145 ss << " doubleIndex++;\n";
147 ss << " }\n\n";
149 ss << " }\n";
150 ss << " if(rowNum!=-1)\n";
151 ss << " {\n";
152 for(int j=0;j< secondParaWidth; j++)
155 ss << " if(tmp";
156 ss << 2+(secondParaWidth-1);
157 ss << " == ";
158 ss << j+1;
159 ss << ")\n";
160 ss << " tmp = ";
161 vSubArguments[1+j]->GenDeclRef(ss);
162 ss << "[rowNum];\n";
165 ss << " return tmp;\n";
166 ss << " }\n";
167 ss << " for (int i = ";
168 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
169 ss << "gid0 + loop *"<<unrollSize<<"; i < ";
170 ss << nCurWindowSize <<"; i++)\n";
171 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
172 ss << "0 + loop *"<<unrollSize<<"; i < gid0+";
173 ss << nCurWindowSize <<"; i++)\n";
174 } else {
175 ss << "0 + loop *"<<unrollSize<<"; i < ";
176 ss << nCurWindowSize <<"; i++)\n";
178 ss << " {\n";
179 if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
181 ss << " int doubleIndex = i+gid0;\n";
182 }else
184 ss << " int doubleIndex = i;\n";
186 CheckSubArgumentIsNan(ss,vSubArguments,1+i);
187 ss << " if(tmp";
188 ss << 3+(secondParaWidth-1);
189 ss << " == 1)\n";
190 ss << " {\n";
191 ss << " if((tmp0 - tmp";
192 ss << 1+i;
193 ss << ")>=0 && intermediate > ( tmp0 -tmp";
194 ss << 1+i;
195 ss << "))\n";
196 ss << " {\n";
197 ss << " rowNum = doubleIndex;\n";
198 ss << " intermediate = tmp0 - tmp";
199 ss << 1+i;
200 ss << ";\n";
201 ss << " }\n";
202 ss << " }else\n";
203 ss << " {\n";
204 ss << " if(tmp0 == tmp";
205 ss << 1+i;
206 ss << " && rowNum == -1)\n";
207 ss << " {\n";
208 ss << " rowNum = doubleIndex;\n";
209 ss << " }\n";
210 ss << " }\n";
212 ss << " }\n\n";
213 ss << " if(rowNum!=-1)\n";
214 ss << " {\n";
216 for(int j=0;j< secondParaWidth; j++)
219 ss << " if(tmp";
220 ss << 2+(secondParaWidth-1);
221 ss << " == ";
222 ss << j+1;
223 ss << ")\n";
224 ss << " tmp = ";
225 vSubArguments[1+j]->GenDeclRef(ss);
226 ss << "[rowNum];\n";
229 ss << " return tmp;\n";
230 ss << " }\n";
235 else
237 CheckSubArgumentIsNan(ss,vSubArguments,1);
238 ss << " if(tmp3 == 1)\n";
239 ss << " {\n";
240 ss << " tmp = tmp1;\n";
241 ss << " }else\n";
242 ss << " {\n";
243 ss << " if(tmp0 == tmp1)\n";
244 ss << " tmp = tmp1;\n";
245 ss << " }\n";
247 ss << " return tmp;\n";
248 ss << "}";
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */