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>
22 using namespace formula
;
24 namespace sc
{ namespace opencl
{
25 void OpAnd::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
++)
34 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
37 ss
<< " int gid0 = get_global_id(0);\n";
38 ss
<< " double t = 1,tmp=0;\n";
39 for(unsigned int j
= 0; j
< vSubArguments
.size(); j
++)
41 ss
<< " double tmp"<<j
<<" = 1;\n";
42 FormulaToken
*tmpCur0
= vSubArguments
[j
]->GetFormulaToken();
43 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
45 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
46 formula::SingleVectorRefToken
*>(tmpCur0
);
47 ss
<< " int buffer_len"<<j
<<" = "<<pCurDVR
->GetArrayLength();
49 ss
<<" if(gid0 >= buffer_len"<<j
<<" || isNan(";
50 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
52 ss
<<" tmp = 1;\n else\n";
54 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
55 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" && tmp;\n";
57 else if(tmpCur0
->GetType() == formula::svDouble
)
60 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
61 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" && tmp;\n";
63 else if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
65 const formula::DoubleVectorRefToken
*pCurDVR
= static_cast<const
66 formula::DoubleVectorRefToken
*>(tmpCur0
);
67 size_t nCurWindowSize
= pCurDVR
->GetArrayLength() <
68 pCurDVR
->GetRefRowSize() ? pCurDVR
->GetArrayLength():
69 pCurDVR
->GetRefRowSize() ;
70 ss
<< " for(int i = ";
71 if (!pCurDVR
->IsStartFixed() && pCurDVR
->IsEndFixed()) {
72 ss
<< "gid0; i < " << nCurWindowSize
<< "; i++) {\n";
74 else if(pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed()){
75 ss
<< "0; i < gid0 + " << nCurWindowSize
<< "; i++) {\n";
78 ss
<< "0; i < " << nCurWindowSize
<< "; i++) {\n";
81 if(!pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed())
83 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
84 ss
<<")||i+gid0>="<<pCurDVR
->GetArrayLength();
86 ss
<<" tmp = 1;\n else\n";
90 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
91 ss
<<")||i>="<<pCurDVR
->GetArrayLength();
93 ss
<<" tmp = 1;\n else\n";
97 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
98 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" && tmp;\n";
103 ss
<<" tmp"<<j
<<" = ";
104 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
106 ss
<<" t = t && tmp"<<j
<<";\n";
108 ss
<< " return t;\n";
112 void OpOr::GenSlidingWindowFunction(std::stringstream
&ss
,
113 const std::string
&sSymName
, SubArguments
&vSubArguments
)
115 ss
<< "\ndouble " << sSymName
;
116 ss
<< "_"<< BinFuncName() <<"(";
117 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
121 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
124 ss
<< " int gid0 = get_global_id(0);\n";
125 ss
<< " double t = 0,tmp=0;\n";
126 for(unsigned int j
= 0; j
< vSubArguments
.size(); j
++)
128 ss
<< " double tmp"<<j
<<" = 0;\n";
129 FormulaToken
*tmpCur0
= vSubArguments
[j
]->GetFormulaToken();
130 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
133 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
134 formula::SingleVectorRefToken
*>(tmpCur0
);
135 ss
<< " int buffer_len"<<j
<<" = "<<pCurDVR
->GetArrayLength();
137 ss
<<" if(gid0 >= buffer_len"<<j
<<" || isNan(";
138 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
140 ss
<<" tmp = 0;\n else\n";
143 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
144 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" || tmp;\n";
146 else if(tmpCur0
->GetType() == formula::svDouble
)
149 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
150 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" || tmp;\n";
152 else if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
154 const formula::DoubleVectorRefToken
*pCurDVR
= static_cast<const
155 formula::DoubleVectorRefToken
*>(tmpCur0
);
156 size_t nCurWindowSize
= pCurDVR
->GetArrayLength() <
157 pCurDVR
->GetRefRowSize() ? pCurDVR
->GetArrayLength():
158 pCurDVR
->GetRefRowSize() ;
159 ss
<< " for(int i = ";
160 if (!pCurDVR
->IsStartFixed() && pCurDVR
->IsEndFixed()) {
161 ss
<< "gid0; i < " << nCurWindowSize
<< "; i++) {\n";
163 else if(pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed()){
164 ss
<< "0; i < gid0 + " << nCurWindowSize
<< "; i++) {\n";
167 ss
<< "0; i < " << nCurWindowSize
<< "; i++) {\n";
170 if(!pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed())
172 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
173 ss
<<")||i+gid0>="<<pCurDVR
->GetArrayLength();
175 ss
<<" tmp = 0;\n else\n";
179 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
180 ss
<<")||i>="<<pCurDVR
->GetArrayLength();
182 ss
<<" tmp = 0;\n else\n";
186 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
187 ss
<<" tmp"<<j
<<" = tmp"<<j
<<" || tmp;\n";
190 ss
<<" t = t || tmp"<<j
<<";\n";
192 ss
<< " return t;\n";
195 void OpNot::GenSlidingWindowFunction(std::stringstream
&ss
,
196 const std::string
&sSymName
, SubArguments
&vSubArguments
)
198 ss
<< "\ndouble " << sSymName
;
199 ss
<< "_"<< BinFuncName() <<"(";
200 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
204 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
207 ss
<< " int gid0 = get_global_id(0);\n";
208 ss
<< " double tmp=0;\n";
209 FormulaToken
*tmpCur0
= vSubArguments
[0]->GetFormulaToken();
210 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
213 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
214 formula::SingleVectorRefToken
*>(tmpCur0
);
215 ss
<<" if(gid0 >= "<<pCurDVR
->GetArrayLength()<<" || isNan(";
216 ss
<<vSubArguments
[0]->GenSlidingWindowDeclRef();
218 ss
<<" tmp = 0;\n else\n";
221 ss
<<vSubArguments
[0]->GenSlidingWindowDeclRef()<<";\n";
222 ss
<<" tmp = (tmp == 0.0);\n";
224 else if(tmpCur0
->GetType() == formula::svDouble
)
227 ss
<<vSubArguments
[0]->GenSlidingWindowDeclRef()<<";\n";
228 ss
<<" tmp = (tmp == 0.0);\n";
230 ss
<< " return tmp;\n";
234 void OpXor::GenSlidingWindowFunction(std::stringstream
&ss
,
235 const std::string
&sSymName
, SubArguments
&vSubArguments
)
237 ss
<< "\ndouble " << sSymName
;
238 ss
<< "_"<< BinFuncName() <<"(";
239 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
243 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
246 ss
<< " int gid0 = get_global_id(0);\n";
247 ss
<< " int t = 0,tmp0 = 0;\n";
248 ss
<< " double tmp = 0;\n";
249 for(unsigned int j
= 0; j
< vSubArguments
.size(); j
++)
251 FormulaToken
*tmpCur0
= vSubArguments
[j
]->GetFormulaToken();
252 if(tmpCur0
->GetType() == formula::svSingleVectorRef
)
255 const formula::SingleVectorRefToken
*pCurDVR
= static_cast<const
256 formula::SingleVectorRefToken
*>(tmpCur0
);
257 ss
<<" if(gid0 >= "<<pCurDVR
->GetArrayLength()<<" || isNan(";
258 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
260 ss
<<" tmp = 0;\n else\n";
263 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
264 ss
<<" tmp0 = (tmp != 0);\n";
265 ss
<<" t = t ^tmp0;\n";
267 else if(tmpCur0
->GetType() == formula::svDouble
)
270 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
271 ss
<<" tmp0 = (tmp != 0);\n";
272 ss
<<" t = t ^tmp0;\n";
274 else if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
276 const formula::DoubleVectorRefToken
*pCurDVR
= static_cast<const
277 formula::DoubleVectorRefToken
*>(tmpCur0
);
278 size_t nCurWindowSize
= pCurDVR
->GetArrayLength() <
279 pCurDVR
->GetRefRowSize() ? pCurDVR
->GetArrayLength():
280 pCurDVR
->GetRefRowSize() ;
281 ss
<< " for(int i = ";
282 if (!pCurDVR
->IsStartFixed() && pCurDVR
->IsEndFixed()) {
283 ss
<< "gid0; i < " << nCurWindowSize
<< "; i++) {\n";
285 else if(pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed()){
286 ss
<< "0; i < gid0 + " << nCurWindowSize
<< "; i++) {\n";
289 ss
<< "0; i < " << nCurWindowSize
<< "; i++) {\n";
292 if(!pCurDVR
->IsStartFixed() && !pCurDVR
->IsEndFixed())
294 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
295 ss
<<")||i+gid0>="<<pCurDVR
->GetArrayLength();
297 ss
<<" tmp = 0;\n else\n";
301 ss
<<" if(isNan("<<vSubArguments
[j
]->GenSlidingWindowDeclRef();
302 ss
<<")||i>="<<pCurDVR
->GetArrayLength();
304 ss
<<" tmp = 0;\n else\n";
308 ss
<<vSubArguments
[j
]->GenSlidingWindowDeclRef()<<";\n";
309 ss
<<" tmp0 = (tmp != 0);\n";
310 ss
<<" t = t ^tmp0;\n";
314 ss
<< " return t;\n";
317 void OpIf::GenSlidingWindowFunction(std::stringstream
&ss
,
318 const std::string
&sSymName
, SubArguments
&vSubArguments
)
320 ss
<< "\ndouble " << sSymName
;
321 ss
<< "_"<< BinFuncName() <<"(";
322 for (unsigned i
= 0; i
< vSubArguments
.size(); i
++)
326 vSubArguments
[i
]->GenSlidingWindowDecl(ss
);
329 ss
<< " int gid0 = get_global_id(0);\n";
331 FormulaToken
*tmpCur0
= vSubArguments
[0]->GetFormulaToken();
332 if(tmpCur0
->GetType() == formula::svDoubleVectorRef
)
334 throw UnhandledToken(tmpCur0
, "unknown operand for ocPush");
338 if(vSubArguments
.size()==3)
341 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
343 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
346 ss
<< vSubArguments
[2]->GenSlidingWindowDeclRef();
350 ss
<< vSubArguments
[1]->GenSlidingWindowDeclRef();
353 if(vSubArguments
.size()==2)
356 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
358 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
360 ss
<< " return 0;\n";
363 ss
<< vSubArguments
[1]->GenSlidingWindowDeclRef();
366 if(vSubArguments
.size()==1)
369 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
371 ss
<< vSubArguments
[0]->GenSlidingWindowDeclRef();
373 ss
<< " return 0;\n";
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */