1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include "op_logical.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>
21 using namespace formula
;
23 namespace sc
{ namespace opencl
{
24 void OpAnd::GenSlidingWindowFunction(std::stringstream
&ss
,
25 const std::string
&sSymName
, SubArguments
&vSubArguments
)
27 ss
<< "\ndouble " << sSymName
;
28 ss
<< "_"<< BinFuncName() <<"(";
29 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
33 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
36 ss
<< " int gid0 = get_global_id(0);\n";
37 ss
<< " double t = 1,tmp=0;\n";
38 for(unsigned int j
= 0; j
< vSubArguments
.size(); j
++)
40 ss
<< " double tmp"<<j
<<" = 1;\n";
41 FormulaToken
*tmpCur0
= vSubArguments
[j
]->GetFormulaToken();
42 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
44 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
45 formula::SingleVectorRefToken
*>(tmpCur0
);
46 ss
<< " int buffer_len"<<j
<<" = "<<pCurDVR
->GetArrayLength();
48 ss
<<" if(gid0 >= buffer_len"<<j
<<" || isNan(";
49 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
51 ss
<<" tmp = 1;\n else\n";
53 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
54 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" && tmp;\n";
56 else if(tmpCur0
->GetType() == formula::svDouble
)
59 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
60 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" && tmp;\n";
62 else if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
64 const formula::DoubleVectorRefToken
*pCurDVR
= static_cast<const
65 formula::DoubleVectorRefToken
*>(tmpCur0
);
66 size_t nCurWindowSize
= pCurDVR
->GetArrayLength() <
67 pCurDVR
->GetRefRowSize() ? pCurDVR
->GetArrayLength():
68 pCurDVR
->GetRefRowSize() ;
69 ss
<< " for(int i = ";
70 if (!pCurDVR
->IsStartFixed() && pCurDVR
->IsEndFixed()) {
71 ss
<< "gid0; i < " << nCurWindowSize
<< "; i++) {\n";
73 else if(pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed()){
74 ss
<< "0; i < gid0 + " << nCurWindowSize
<< "; i++) {\n";
77 ss
<< "0; i < " << nCurWindowSize
<< "; i++) {\n";
79 if(!pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed())
81 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
82 ss
<<")||i+gid0>="<<pCurDVR
->GetArrayLength();
84 ss
<<" tmp = 1;\n else\n";
88 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
89 ss
<<")||i>="<<pCurDVR
->GetArrayLength();
91 ss
<<" tmp = 1;\n else\n";
94 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
95 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" && tmp;\n";
100 ss
<<" tmp"<<j
<<" = ";
101 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
103 ss
<<" t = t && tmp"<<j
<<";\n";
105 ss
<< " return t;\n";
109 void OpOr::GenSlidingWindowFunction(std::stringstream
&ss
,
110 const std::string
&sSymName
, SubArguments
&vSubArguments
)
112 ss
<< "\ndouble " << sSymName
;
113 ss
<< "_"<< BinFuncName() <<"(";
114 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
118 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
121 ss
<< " int gid0 = get_global_id(0);\n";
122 ss
<< " double t = 0,tmp=0;\n";
123 for(unsigned int j
= 0; j
< vSubArguments
.size(); j
++)
125 ss
<< " double tmp"<<j
<<" = 0;\n";
126 FormulaToken
*tmpCur0
= vSubArguments
[j
]->GetFormulaToken();
127 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
129 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
130 formula::SingleVectorRefToken
*>(tmpCur0
);
131 ss
<< " int buffer_len"<<j
<<" = "<<pCurDVR
->GetArrayLength();
133 ss
<<" if(gid0 >= buffer_len"<<j
<<" || isNan(";
134 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
136 ss
<<" tmp = 0;\n else\n";
138 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
139 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" || tmp;\n";
141 else if(tmpCur0
->GetType() == formula::svDouble
)
144 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
145 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" || tmp;\n";
147 else if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
149 const formula::DoubleVectorRefToken
*pCurDVR
= static_cast<const
150 formula::DoubleVectorRefToken
*>(tmpCur0
);
151 size_t nCurWindowSize
= pCurDVR
->GetArrayLength() <
152 pCurDVR
->GetRefRowSize() ? pCurDVR
->GetArrayLength():
153 pCurDVR
->GetRefRowSize() ;
154 ss
<< " for(int i = ";
155 if (!pCurDVR
->IsStartFixed() && pCurDVR
->IsEndFixed()) {
156 ss
<< "gid0; i < " << nCurWindowSize
<< "; i++) {\n";
158 else if(pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed()){
159 ss
<< "0; i < gid0 + " << nCurWindowSize
<< "; i++) {\n";
162 ss
<< "0; i < " << nCurWindowSize
<< "; i++) {\n";
164 if(!pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed())
166 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
167 ss
<<")||i+gid0>="<<pCurDVR
->GetArrayLength();
169 ss
<<" tmp = 0;\n else\n";
173 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
174 ss
<<")||i>="<<pCurDVR
->GetArrayLength();
176 ss
<<" tmp = 0;\n else\n";
179 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
180 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" || tmp;\n";
183 ss
<<" t = t || tmp"<<j
<<";\n";
185 ss
<< " return t;\n";
188 void OpNot::GenSlidingWindowFunction(std::stringstream
&ss
,
189 const std::string
&sSymName
, SubArguments
&vSubArguments
)
191 ss
<< "\ndouble " << sSymName
;
192 ss
<< "_"<< BinFuncName() <<"(";
193 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
197 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
200 ss
<< " int gid0 = get_global_id(0);\n";
201 ss
<< " double tmp=0;\n";
202 FormulaToken
*tmpCur0
= vSubArguments
[0]->GetFormulaToken();
203 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
205 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
206 formula::SingleVectorRefToken
*>(tmpCur0
);
207 ss
<<" if(gid0 >= "<<pCurDVR
->GetArrayLength()<<" || isNan(";
208 ss
<<vSubArguments
[0]->GenSlidingWindowDeclRef();
210 ss
<<" tmp = 0;\n else\n";
212 ss
<<vSubArguments
[0]->GenSlidingWindowDeclRef()<<";\n";
213 ss
<<" tmp = (tmp == 0.0);\n";
215 else if(tmpCur0
->GetType() == formula::svDouble
)
218 ss
<<vSubArguments
[0]->GenSlidingWindowDeclRef()<<";\n";
219 ss
<<" tmp = (tmp == 0.0);\n";
221 ss
<< " return tmp;\n";
225 void OpXor::GenSlidingWindowFunction(std::stringstream
&ss
,
226 const std::string
&sSymName
, SubArguments
&vSubArguments
)
228 ss
<< "\ndouble " << sSymName
;
229 ss
<< "_"<< BinFuncName() <<"(";
230 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
234 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
237 ss
<< " int gid0 = get_global_id(0);\n";
238 ss
<< " int t = 0,tmp0 = 0;\n";
239 ss
<< " double tmp = 0;\n";
240 for(unsigned int j
= 0; j
< vSubArguments
.size(); j
++)
242 FormulaToken
*tmpCur0
= vSubArguments
[j
]->GetFormulaToken();
243 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
245 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
246 formula::SingleVectorRefToken
*>(tmpCur0
);
247 ss
<<" if(gid0 >= "<<pCurDVR
->GetArrayLength()<<" || isNan(";
248 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
250 ss
<<" tmp = 0;\n else\n";
252 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
253 ss
<<" tmp0 = (tmp != 0);\n";
254 ss
<<" t = t ^tmp0;\n";
256 else if(tmpCur0
->GetType() == formula::svDouble
)
259 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
260 ss
<<" tmp0 = (tmp != 0);\n";
261 ss
<<" t = t ^tmp0;\n";
263 else if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
265 const formula::DoubleVectorRefToken
*pCurDVR
= static_cast<const
266 formula::DoubleVectorRefToken
*>(tmpCur0
);
267 size_t nCurWindowSize
= pCurDVR
->GetArrayLength() <
268 pCurDVR
->GetRefRowSize() ? pCurDVR
->GetArrayLength():
269 pCurDVR
->GetRefRowSize() ;
270 ss
<< " for(int i = ";
271 if (!pCurDVR
->IsStartFixed() && pCurDVR
->IsEndFixed()) {
272 ss
<< "gid0; i < " << nCurWindowSize
<< "; i++) {\n";
274 else if(pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed()){
275 ss
<< "0; i < gid0 + " << nCurWindowSize
<< "; i++) {\n";
278 ss
<< "0; i < " << nCurWindowSize
<< "; i++) {\n";
280 if(!pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed())
282 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
283 ss
<<")||i+gid0>="<<pCurDVR
->GetArrayLength();
285 ss
<<" tmp = 0;\n else\n";
289 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
290 ss
<<")||i>="<<pCurDVR
->GetArrayLength();
292 ss
<<" tmp = 0;\n else\n";
295 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
296 ss
<<" tmp0 = (tmp != 0);\n";
297 ss
<<" t = t ^tmp0;\n";
301 ss
<< " return t;\n";
304 void OpIf::GenSlidingWindowFunction(std::stringstream
&ss
,
305 const std::string
&sSymName
, SubArguments
&vSubArguments
)
307 ss
<< "\ndouble " << sSymName
;
308 ss
<< "_"<< BinFuncName() <<"(";
309 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
313 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
316 ss
<< " int gid0 = get_global_id(0);\n";
318 FormulaToken
*tmpCur0
= vSubArguments
[0]->GetFormulaToken();
319 if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
321 throw UnhandledToken(tmpCur0
, "unknown operand for ocPush");
325 if(vSubArguments
.size()==3)
328 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
330 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
333 ss
<< vSubArguments
[2]->GenSlidingWindowDeclRef();
337 ss
<< vSubArguments
[1]->GenSlidingWindowDeclRef();
340 if(vSubArguments
.size()==2)
343 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
345 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
347 ss
<< " return 0;\n";
350 ss
<< vSubArguments
[1]->GenSlidingWindowDeclRef();
353 if(vSubArguments
.size()==1)
356 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
358 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
360 ss
<< " return 0;\n";
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */