LibreOffice Module sc (master)  1
op_logical.cxx
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 #include "op_logical.hxx"
11 
12 #include <formula/vectortoken.hxx>
13 #include <sstream>
14 
15 using namespace formula;
16 
17 namespace sc::opencl {
18 void OpAnd::GenSlidingWindowFunction(std::stringstream &ss,
19  const std::string &sSymName, SubArguments &vSubArguments)
20 {
21  ss << "\ndouble " << sSymName;
22  ss << "_"<< BinFuncName() <<"(";
23  for (size_t i = 0; i < vSubArguments.size(); i++)
24  {
25  if (i)
26  ss << ",";
27  vSubArguments[i]->GenSlidingWindowDecl(ss);
28  }
29  ss << ") {\n";
30  ss << " int gid0 = get_global_id(0);\n";
31  ss << " double t = 1,tmp=0;\n";
32  for(size_t j = 0; j< vSubArguments.size(); j++)
33  {
34  ss << " double tmp"<<j<<" = 1;\n";
35  FormulaToken *tmpCur0 = vSubArguments[j]->GetFormulaToken();
36  if(tmpCur0->GetType() == formula::svSingleVectorRef)
37  {
38  const formula::SingleVectorRefToken*pCurDVR= static_cast<const
40  ss<< " int buffer_len"<<j<<" = "<<pCurDVR->GetArrayLength();
41  ss<< ";\n";
42  ss <<" if(gid0 >= buffer_len"<<j<<" || isnan(";
43  ss <<vSubArguments[j]->GenSlidingWindowDeclRef();
44  ss <<"))\n";
45  ss <<" tmp = 1;\n else\n";
46  ss <<" tmp = ";
47  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
48  ss <<" tmp"<<j<<" = tmp"<<j<<" && tmp;\n";
49  }
50  else if(tmpCur0->GetType() == formula::svDouble)
51  {
52  ss <<" tmp = ";
53  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
54  ss <<" tmp"<<j<<" = tmp"<<j<<" && tmp;\n";
55  }
56  else if(tmpCur0->GetType() == formula::svDoubleVectorRef)
57  {
58  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
60  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
61  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
62  pCurDVR->GetRefRowSize() ;
63  ss << " for(int i = ";
64  if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
65  ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
66  }
67  else if(pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()){
68  ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
69  }
70  else{
71  ss << "0; i < " << nCurWindowSize << "; i++) {\n";
72  }
73  if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
74  {
75  ss <<" if(isnan("<<vSubArguments[j]->GenSlidingWindowDeclRef();
76  ss <<")||i+gid0>="<<pCurDVR->GetArrayLength();
77  ss <<")\n";
78  ss <<" tmp = 1;\n else\n";
79  }
80  else
81  {
82  ss <<" if(isnan("<<vSubArguments[j]->GenSlidingWindowDeclRef();
83  ss <<")||i>="<<pCurDVR->GetArrayLength();
84  ss <<")\n";
85  ss <<" tmp = 1;\n else\n";
86  }
87  ss <<" tmp = ";
88  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
89  ss <<" tmp"<<j<<" = tmp"<<j<<" && tmp;\n";
90  ss <<" }\n";
91  }
92  else
93  {
94  ss <<" tmp"<<j<<" = ";
95  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
96  }
97  ss <<" t = t && tmp"<<j<<";\n";
98  }
99  ss << " return t;\n";
100  ss << "}\n";
101 }
102 
103 void OpOr::GenSlidingWindowFunction(std::stringstream &ss,
104  const std::string &sSymName, SubArguments &vSubArguments)
105 {
106  ss << "\ndouble " << sSymName;
107  ss << "_"<< BinFuncName() <<"(";
108  for (size_t i = 0; i < vSubArguments.size(); i++)
109  {
110  if (i)
111  ss << ",";
112  vSubArguments[i]->GenSlidingWindowDecl(ss);
113  }
114  ss << ") {\n";
115  ss << " int gid0 = get_global_id(0);\n";
116  ss << " double t = 0,tmp=0;\n";
117  for(size_t j = 0; j< vSubArguments.size(); j++)
118  {
119  ss << " double tmp"<<j<<" = 0;\n";
120  FormulaToken *tmpCur0 = vSubArguments[j]->GetFormulaToken();
121  if(tmpCur0->GetType() == formula::svSingleVectorRef)
122  {
123  const formula::SingleVectorRefToken*pCurDVR= static_cast<const
124  formula::SingleVectorRefToken *>(tmpCur0);
125  ss<< " int buffer_len"<<j<<" = "<<pCurDVR->GetArrayLength();
126  ss<< ";\n";
127  ss <<" if(gid0 >= buffer_len"<<j<<" || isnan(";
128  ss <<vSubArguments[j]->GenSlidingWindowDeclRef();
129  ss <<"))\n";
130  ss <<" tmp = 0;\n else\n";
131  ss <<" tmp = ";
132  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
133  ss <<" tmp"<<j<<" = tmp"<<j<<" || tmp;\n";
134  }
135  else if(tmpCur0->GetType() == formula::svDouble)
136  {
137  ss <<" tmp = ";
138  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
139  ss <<" tmp"<<j<<" = tmp"<<j<<" || tmp;\n";
140  }
141  else if(tmpCur0->GetType() == formula::svDoubleVectorRef)
142  {
143  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
144  formula::DoubleVectorRefToken *>(tmpCur0);
145  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
146  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
147  pCurDVR->GetRefRowSize() ;
148  ss << " for(int i = ";
149  if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
150  ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
151  }
152  else if(pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()){
153  ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
154  }
155  else{
156  ss << "0; i < " << nCurWindowSize << "; i++) {\n";
157  }
158  if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
159  {
160  ss <<" if(isnan("<<vSubArguments[j]->GenSlidingWindowDeclRef();
161  ss <<")||i+gid0>="<<pCurDVR->GetArrayLength();
162  ss <<")\n";
163  ss <<" tmp = 0;\n else\n";
164  }
165  else
166  {
167  ss <<" if(isnan("<<vSubArguments[j]->GenSlidingWindowDeclRef();
168  ss <<")||i>="<<pCurDVR->GetArrayLength();
169  ss <<")\n";
170  ss <<" tmp = 0;\n else\n";
171  }
172  ss <<" tmp = ";
173  ss <<vSubArguments[j]->GenSlidingWindowDeclRef()<<";\n";
174  ss <<" tmp"<<j<<" = tmp"<<j<<" || tmp;\n";
175  ss <<" }\n";
176  }
177  ss <<" t = t || tmp"<<j<<";\n";
178  }
179  ss << " return t;\n";
180  ss << "}\n";
181 }
182 void OpNot::GenSlidingWindowFunction(std::stringstream &ss,
183  const std::string &sSymName, SubArguments &vSubArguments)
184 {
185  ss << "\ndouble " << sSymName;
186  ss << "_"<< BinFuncName() <<"(";
187  for (size_t i = 0; i < vSubArguments.size(); i++)
188  {
189  if (i)
190  ss << ",";
191  vSubArguments[i]->GenSlidingWindowDecl(ss);
192  }
193  ss << ") {\n";
194  ss << " int gid0 = get_global_id(0);\n";
195  ss << " double tmp=0;\n";
196  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
197  if(tmpCur0->GetType() == formula::svSingleVectorRef)
198  {
199  const formula::SingleVectorRefToken*pCurDVR= static_cast<const
200  formula::SingleVectorRefToken *>(tmpCur0);
201  ss <<" if(gid0 >= "<<pCurDVR->GetArrayLength()<<" || isnan(";
202  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
203  ss <<"))\n";
204  ss <<" tmp = 0;\n else\n";
205  ss <<" tmp = ";
206  ss <<vSubArguments[0]->GenSlidingWindowDeclRef()<<";\n";
207  ss <<" tmp = (tmp == 0.0);\n";
208  }
209  else if(tmpCur0->GetType() == formula::svDouble)
210  {
211  ss <<" tmp = ";
212  ss <<vSubArguments[0]->GenSlidingWindowDeclRef()<<";\n";
213  ss <<" tmp = (tmp == 0.0);\n";
214  }
215  ss << " return tmp;\n";
216  ss << "}\n";
217 }
218 
219 void OpXor::GenSlidingWindowFunction(std::stringstream &ss,
220  const std::string &sSymName, SubArguments &vSubArguments)
221 {
222  ss << "\ndouble " << sSymName;
223  ss << "_"<< BinFuncName() <<"(";
224  for (size_t i = 0; i < vSubArguments.size(); i++)
225  {
226  if (i)
227  ss << ",";
228  vSubArguments[i]->GenSlidingWindowDecl(ss);
229  }
230  ss << ") {\n";
231  ss << " int gid0 = get_global_id(0);\n";
232  ss << " int t = 0,tmp0 = 0;\n";
233  ss << " double tmp = 0;\n";
234  for(DynamicKernelArgumentRef & rArg : vSubArguments)
235  {
236  FormulaToken *tmpCur0 = rArg->GetFormulaToken();
237  if(tmpCur0->GetType() == formula::svSingleVectorRef)
238  {
239  const formula::SingleVectorRefToken*pCurDVR= static_cast<const
240  formula::SingleVectorRefToken *>(tmpCur0);
241  ss <<" if(gid0 >= "<<pCurDVR->GetArrayLength()<<" || isnan(";
242  ss <<rArg->GenSlidingWindowDeclRef();
243  ss <<"))\n";
244  ss <<" tmp = 0;\n else\n";
245  ss <<" tmp = ";
246  ss <<rArg->GenSlidingWindowDeclRef()<<";\n";
247  ss <<" tmp0 = (tmp != 0);\n";
248  ss <<" t = t ^tmp0;\n";
249  }
250  else if(tmpCur0->GetType() == formula::svDouble)
251  {
252  ss <<" tmp = ";
253  ss <<rArg->GenSlidingWindowDeclRef()<<";\n";
254  ss <<" tmp0 = (tmp != 0);\n";
255  ss <<" t = t ^tmp0;\n";
256  }
257  else if(tmpCur0->GetType() == formula::svDoubleVectorRef)
258  {
259  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
260  formula::DoubleVectorRefToken *>(tmpCur0);
261  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
262  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
263  pCurDVR->GetRefRowSize() ;
264  ss << " for(int i = ";
265  if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
266  ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
267  }
268  else if(pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()){
269  ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
270  }
271  else{
272  ss << "0; i < " << nCurWindowSize << "; i++) {\n";
273  }
274  if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
275  {
276  ss <<" if(isnan("<<rArg->GenSlidingWindowDeclRef();
277  ss <<")||i+gid0>="<<pCurDVR->GetArrayLength();
278  ss <<")\n";
279  ss <<" tmp = 0;\n else\n";
280  }
281  else
282  {
283  ss <<" if(isnan("<<rArg->GenSlidingWindowDeclRef();
284  ss <<")||i>="<<pCurDVR->GetArrayLength();
285  ss <<")\n";
286  ss <<" tmp = 0;\n else\n";
287  }
288  ss <<" tmp = ";
289  ss <<rArg->GenSlidingWindowDeclRef()<<";\n";
290  ss <<" tmp0 = (tmp != 0);\n";
291  ss <<" t = t ^tmp0;\n";
292  ss <<" }\n";
293  }
294  }
295  ss << " return t;\n";
296  ss << "}\n";
297 }
298 void OpIf::GenSlidingWindowFunction(std::stringstream &ss,
299  const std::string &sSymName, SubArguments &vSubArguments)
300 {
301  ss << "\ndouble " << sSymName;
302  ss << "_"<< BinFuncName() <<"(";
303  for (size_t i = 0; i < vSubArguments.size(); i++)
304  {
305  if (i)
306  ss << ",";
307  vSubArguments[i]->GenSlidingWindowDecl(ss);
308  }
309  ss << ") {\n";
310  ss << " int gid0 = get_global_id(0);\n";
311 
312  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
313  if(tmpCur0->GetType() == formula::svDoubleVectorRef)
314  {
315  throw UnhandledToken("unknown operand for ocPush", __FILE__, __LINE__);
316  }
317  if(vSubArguments.size()==3)
318  {
319  ss << " if(isnan(";
320  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
321  ss << ")|| ";
322  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
323  ss << " == 0)\n";
324  ss << " return ";
325  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
326  ss << ";\n";
327  ss << " else";
328  ss <<" return ";
329  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
330  ss <<";\n";
331  }
332  if(vSubArguments.size()==2)
333  {
334  ss << " if(isnan(";
335  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
336  ss << ")|| ";
337  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
338  ss << " == 0)\n";
339  ss << " return 0;\n";
340  ss << " else";
341  ss <<" return ";
342  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
343  ss <<";\n";
344  }
345  if(vSubArguments.size()==1)
346  {
347  ss << " if(isnan(";
348  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
349  ss << ")|| ";
350  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
351  ss << " == 0)\n";
352  ss << " return 0;\n";
353  ss << " else";
354  ss <<" return 1;\n";
355  }
356  ss << "}\n";
357 }
358 
359 }
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< DynamicKernelArgument > DynamicKernelArgumentRef
Definition: opbase.hxx:152
int i
std::vector< DynamicKernelArgumentRef > SubArguments
Definition: opbase.hxx:215
Arguments that are actually compile-time constant string Currently, only the hash is passed...
size_t GetArrayLength() const
size_t GetArrayLength() const
StackVar GetType() const