LibreOffice Module sc (master)  1
op_math.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_math.hxx"
11 
12 #include <formula/vectortoken.hxx>
13 #include "opinlinefun_math.hxx"
14 #include <sstream>
15 
16 using namespace formula;
17 
18 namespace sc::opencl {
19 
20 void OpCos::GenSlidingWindowFunction(std::stringstream &ss,
21  const std::string &sSymName, SubArguments &vSubArguments)
22 {
23  ss << "\ndouble " << sSymName;
24  ss << "_"<< BinFuncName() <<"(";
25  for (size_t i = 0; i < vSubArguments.size(); i++)
26  {
27  if (i) ss << ",";
28  vSubArguments[i]->GenSlidingWindowDecl(ss);
29  }
30  ss << ")\n";
31  ss << "{\n";
32  ss << " int gid0=get_global_id(0);\n";
33  ss << " double arg0 = 0.0f;\n";
34  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
35  assert(tmpCur);
36  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
37  {
38  if(tmpCur->GetType() == formula::svSingleVectorRef)
39  {
40  const formula::SingleVectorRefToken*tmpCurDVR=
41  static_cast
42  <const formula::SingleVectorRefToken *>(tmpCur);
43  ss << " arg0 = ";
44  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
45  ss << ";\n";
46  ss << " if(isnan(";
47  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
48  ss << ")||(gid0>=";
49  ss << tmpCurDVR->GetArrayLength();
50  ss << "))\n";
51  ss << " { arg0 = 0.0f; }\n";
52  }
53  else if(tmpCur->GetType() == formula::svDouble)
54  {
55  ss << " arg0=" << tmpCur->GetDouble() << ";\n";
56  }
57  }
58  else
59  {
60  ss << " arg0 = ";
61  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
62  ss << ";\n";
63  }
64  ss << " return cos(arg0);\n";
65  ss << "}";
66 
67 }
68 void OpSec::GenSlidingWindowFunction(std::stringstream &ss,
69  const std::string &sSymName, SubArguments &vSubArguments)
70 {
71  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
72  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
74  ss << "\ndouble " << sSymName;
75  ss << "_"<< BinFuncName() <<"(";
76  for (size_t i = 0; i < vSubArguments.size(); i++)
77  {
78  if (i)
79  ss << ",";
80  vSubArguments[i]->GenSlidingWindowDecl(ss);
81  }
82  ss << ") {\n";
83  ss <<" int gid0=get_global_id(0);\n";
84  ss <<" double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
85  ss <<";\n";
86  ss<<" if(isnan(arg0)||(gid0>=";
87  ss<<tmpCurDVR->GetArrayLength();
88  ss<<"))\n";
89  ss<<" arg0 = 0;\n";
90  ss << " return pow(cos(arg0),-1 );\n";
91  ss << "}";
92 }
93 void OpCosh::BinInlineFun(std::set<std::string>& decls,
94  std::set<std::string>& funs)
95 {
96  decls.insert(local_coshDecl);
97  funs.insert(local_cosh);
98 }
99 void OpSecH::GenSlidingWindowFunction(std::stringstream &ss,
100  const std::string &sSymName, SubArguments &vSubArguments)
101 {
102  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
103  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
105  ss << "\ndouble " << sSymName;
106  ss << "_"<< BinFuncName() <<"(";
107  for (size_t i = 0; i < vSubArguments.size(); i++)
108  {
109  if (i)
110  ss << ",";
111  vSubArguments[i]->GenSlidingWindowDecl(ss);
112  }
113  ss << ") {\n";
114  ss <<" int gid0=get_global_id(0);\n";
115  ss <<" double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
116  ss <<";\n";
117  ss<<" if(isnan(arg0)||(gid0>=";
118  ss<<tmpCurDVR->GetArrayLength();
119  ss<<"))\n";
120  ss<<" arg0 = 0;\n";
121  ss << " return pow(cosh(arg0),-1 );\n";
122  ss << "}";
123 }
124 void OpMROUND::GenSlidingWindowFunction(std::stringstream &ss,
125  const std::string &sSymName, SubArguments &vSubArguments)
126 {
127  CHECK_PARAMETER_COUNT(2, 2);
128  ss << "\ndouble " << sSymName;
129  ss << "_"<< BinFuncName() <<"(";
130  for (size_t i = 0; i < vSubArguments.size(); i++)
131  {
132  if (i)
133  ss << ", ";
134  vSubArguments[i]->GenSlidingWindowDecl(ss);
135  }
136  ss<<") {\n";
137  ss<<" double tmp = 0;\n";
138  ss<<" int gid0 = get_global_id(0);\n";
139  ss<<" double arg0=0;\n";
140  ss<<" double arg1=0;\n";
141  ss <<"\n ";
142  //while (i-- > 1)
143  for (size_t i = 0; i < vSubArguments.size(); i++)
144  {
145  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
146  assert(pCur);
147  if (pCur->GetType() == formula::svSingleVectorRef)
148  {
149  const formula::SingleVectorRefToken* pSVR =
150  static_cast< const formula::SingleVectorRefToken* >(pCur);
151  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
152  }
153  else if (pCur->GetType() == formula::svDouble)
154  {
155  ss << "{\n";
156  }
157 
158  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
159  {
160  ss << " tmp=";
161  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
162  ss << ";\n";
163  ss << " if (isnan(tmp))\n";
164  ss << " arg"<<i<<"= 0;\n";
165  ss << " else\n";
166  ss << " arg"<<i<<"=tmp;\n";
167  ss << " }\n";
168  }
169  else
170  {
171  ss<<" arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
172  ss<<";\n";
173  }
174  }
175  ss<<" if(arg1==0)\n";
176  ss<<" return arg1;\n";
177  ss<<" tmp=arg1 * round(arg0 * pow(arg1,-1));\n";
178  ss<<" return tmp;\n";
179  ss<<"}";
180 }
181 void OpCosh::GenSlidingWindowFunction(std::stringstream &ss,
182  const std::string &sSymName, SubArguments &vSubArguments)
183 {
184  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
185  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
187  ss << "\ndouble " << sSymName;
188  ss << "_"<< BinFuncName() <<"(";
189  for (size_t i = 0; i < vSubArguments.size(); i++)
190  {
191  if (i)
192  ss << ",";
193  vSubArguments[i]->GenSlidingWindowDecl(ss);
194  }
195  ss << ")\n{\n";
196  ss <<" int gid0=get_global_id(0);\n";
197  ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
198  ss << ";\n";
199  ss<< " if(isnan(arg0)||(gid0>=";
200  ss<<tmpCurDVR->GetArrayLength();
201  ss<<"))\n";
202  ss<<" arg0 = 0;\n";
203  ss << " double tmp=local_cosh(arg0);\n";
204  ss << " return tmp;\n";
205  ss << "}";
206 }
207 
208 void OpCot::GenSlidingWindowFunction(std::stringstream &ss,
209  const std::string &sSymName, SubArguments &vSubArguments)
210 {
211  ss << "\ndouble " << sSymName;
212  ss << "_"<< BinFuncName() <<"(";
213  for (size_t i = 0; i < vSubArguments.size(); i++)
214  {
215  if (i)
216  ss << ",";
217  vSubArguments[i]->GenSlidingWindowDecl(ss);
218  }
219  ss << ")\n";
220  ss << "{\n";
221  ss << " int gid0=get_global_id(0);\n";
222  ss << " double arg0 = 0.0f;\n";
223  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
224  assert(tmpCur);
225  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
226  {
227  if(tmpCur->GetType() == formula::svSingleVectorRef)
228  {
229  const formula::SingleVectorRefToken*tmpCurDVR=
230  static_cast
231  <const formula::SingleVectorRefToken *>(tmpCur);
232  ss << " arg0 = ";
233  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
234  ss << ";\n";
235  ss << " if(isnan(";
236  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
237  ss << ")||(gid0>=";
238  ss << tmpCurDVR->GetArrayLength();
239  ss << "))\n";
240  ss << " { arg0 = 0.0f; }\n";
241  }
242  else if(tmpCur->GetType() == formula::svDouble)
243  {
244  ss << " arg0=";
245  ss << tmpCur->GetDouble() << ";\n";
246  }
247  }
248  else
249  {
250  ss << " arg0 = ";
251  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
252  ss << ";\n";
253  }
254  ss << " arg0 = arg0 * M_1_PI;\n";
255  ss << " return cospi(arg0) * pow(sinpi(arg0), -1);\n";
256  ss << "}";
257 }
258 
259 void OpCoth::BinInlineFun(std::set<std::string>& decls,
260  std::set<std::string>& funs)
261 {
262  decls.insert(local_cothDecl);
263  funs.insert(local_coth);
264 }
265 
266 void OpCoth::GenSlidingWindowFunction(std::stringstream &ss,
267  const std::string &sSymName, SubArguments &vSubArguments)
268 {
269  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
270  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
272  ss << "\ndouble " << sSymName;
273  ss << "_"<< BinFuncName() <<"(";
274  for (size_t i = 0; i < vSubArguments.size(); i++)
275  {
276  if (i)
277  ss << ",";
278  vSubArguments[i]->GenSlidingWindowDecl(ss);
279  }
280  ss << ")\n{\n";
281  ss <<" int gid0=get_global_id(0);\n";
282  ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
283  ss << ";\n";
284  ss<< " if(isnan(arg0)||(gid0>=";
285  ss<<tmpCurDVR->GetArrayLength();
286  ss<<"))\n";
287  ss<<" arg0 = 0;\n";
288  ss << " double tmp=local_coth(arg0);\n";
289  ss << " return tmp;\n";
290  ss << "}";
291 }
292 
293 void OpCombinA::BinInlineFun(std::set<std::string>& decls,
294  std::set<std::string>& funs)
295 {
296  decls.insert(bikDecl);
297  funs.insert(bik);
298 }
299 
300 void OpCombinA::GenSlidingWindowFunction(std::stringstream &ss,
301  const std::string &sSymName, SubArguments &vSubArguments)
302 {
303  ss << "\ndouble " << sSymName;
304  ss << "_"<< BinFuncName() <<"(";
305  for (size_t i = 0; i < vSubArguments.size(); i++)
306  {
307  if (i)
308  ss << ",";
309  vSubArguments[i]->GenSlidingWindowDecl(ss);
310  }
311  ss << ")\n{\n";
312  ss << " int gid0 = get_global_id(0);\n";
313  ss << " double tem;\n";
314  ss << " double arg0,arg1;\n";
315  for (size_t i = 0; i < vSubArguments.size(); i++)
316  {
317  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
318  assert(pCur);
319  ss << " arg"<<i<<" = "<<vSubArguments[i]->GenSlidingWindowDeclRef();
320  ss << ";\n";
321  if(pCur->GetType() == formula::svSingleVectorRef)
322  {
323  const formula::SingleVectorRefToken* pSVR =
324  static_cast< const formula::SingleVectorRefToken* >(pCur);
325  ss << " if(isnan(arg" << i <<")||(gid0 >= ";
326  ss << pSVR->GetArrayLength();
327  ss << "))\n";
328  ss << " arg" << i << " = 0;\n";
329  }
330  else if (pCur->GetType() == formula::svDouble)
331  {
332  ss << " if(isnan(arg" << i <<"))\n";
333  ss << " arg" << i << " = 0;\n";
334  }
335  }
336  ss << " arg0 = trunc(arg0);\n";
337  ss << " arg1 = trunc(arg1);\n";
338  ss << " if(arg0 >= arg1 && arg0 > 0 && arg1 > 0)\n";
339  ss << " tem = bik(arg0+arg1-1,arg1);\n";
340  ss << " else if(arg0 == 0 && arg1 == 0)\n";
341  ss << " tem = 0;\n";
342  ss << " else if(arg0 > 0 && arg1 == 0)\n";
343  ss << " tem = 1;\n";
344  ss << " else\n";
345  ss << " tem = -1;\n";
346  ss << " double i = tem - trunc(tem);\n";
347  ss << " if(i < 0.5)\n";
348  ss << " tem = trunc(tem);\n";
349  ss << " else\n";
350  ss << " tem = trunc(tem) + 1;\n";
351  ss << " return tem;\n";
352  ss << "}";
353 }
354 void OpEven::GenSlidingWindowFunction(std::stringstream &ss,
355  const std::string &sSymName, SubArguments &vSubArguments)
356 {
357  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
358  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
360  ss << "\ndouble " << sSymName;
361  ss << "_"<< BinFuncName() <<"(";
362  for (size_t i = 0; i < vSubArguments.size(); i++)
363  {
364  if (i)
365  ss << ",";
366  vSubArguments[i]->GenSlidingWindowDecl(ss);
367  }
368  ss << ")\n{\n";
369  ss <<" int gid0=get_global_id(0);\n";
370  ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
371  ss << ";\n";
372  ss<< " if(isnan(arg0)||(gid0>=";
373  ss<<tmpCurDVR->GetArrayLength();
374  ss<<"))\n";
375  ss<<" arg0 = 0;\n";
376  ss << " double tmp;\n";
377  ss << " tmp = fabs(arg0 / 2);\n";
378  ss << " if ( trunc(tmp) == tmp )\n";
379  ss << " tmp = tmp * 2;\n";
380  ss << " else\n";
381  ss << " tmp = (trunc(tmp) + 1) * 2;\n";
382  ss << " if (arg0 < 0)\n";
383  ss << " tmp = tmp * -1.0;\n";
384  ss << " return tmp;\n";
385  ss << "}";
386 }
387 
388 void OpMod::GenSlidingWindowFunction(std::stringstream &ss,
389  const std::string &sSymName, SubArguments &vSubArguments)
390 {
391  ss << "\ndouble " << sSymName;
392  ss << "_"<< BinFuncName() <<"(";
393  for (size_t i = 0; i < vSubArguments.size(); i++)
394  {
395  if (i)
396  ss << ",";
397  vSubArguments[i]->GenSlidingWindowDecl(ss);
398  }
399  ss << ")\n{\n";
400  ss <<" int gid0=get_global_id(0);\n";
401  ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
402  ss << ";\n";
403  ss << " double arg1 =" << vSubArguments[1]->GenSlidingWindowDeclRef();
404  ss << ";\n";
405  ss << " if(isnan(arg0)||arg0 == 0)\n";
406  ss << " return 0;\n";
407  ss << " if(isnan(arg1) || arg1 ==0)\n";
408  ss << " return NAN;\n";
409  ss << " double tem;\n";
410  ss << " if(arg0 < 0 && arg1 > 0)\n";
411  ss << " while(arg0 < 0)\n";
412  ss << " arg0 += arg1;\n";
413  ss << " else if (arg0 > 0 && arg1 < 0)\n";
414  ss << " while(arg0 > 0)\n";
415  ss << " arg0 += arg1;\n";
416  ss << " tem = fmod(arg0,arg1);\n";
417  ss << " if(arg1 < 0 && tem > 0)\n";
418  ss << " tem = -tem;\n";
419  ss << " return tem;\n";
420  ss << "}";
421 }
422 void OpLog::GenSlidingWindowFunction(std::stringstream &ss,
423  const std::string &sSymName, SubArguments &vSubArguments)
424 {
425  ss << "\ndouble " << sSymName;
426  ss << "_"<< BinFuncName() <<"(";
427  for (size_t i = 0; i < vSubArguments.size(); i++)
428  {
429  if (i)
430  ss << ",";
431  vSubArguments[i]->GenSlidingWindowDecl(ss);
432  }
433  ss << ")\n{\n";
434  ss << " int gid0 = get_global_id(0);\n";
435  ss << " double tem;\n";
436  ss << " double arg0,arg1;\n";
437  for (size_t i = 0; i < vSubArguments.size(); i++)
438  {
439  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
440  assert(pCur);
441  ss << " arg"<<i<<" = "<<vSubArguments[i]->GenSlidingWindowDeclRef();
442  ss << ";\n";
443  if(pCur->GetType() == formula::svSingleVectorRef)
444  {
445  const formula::SingleVectorRefToken* pSVR =
446  static_cast< const formula::SingleVectorRefToken* >(pCur);
447  ss << " if(isnan(arg" << i <<")||(gid0 >= ";
448  ss << pSVR->GetArrayLength();
449  ss << "))\n";
450  if( i == 0)
451  ss << " arg0 = 0;\n";
452  else if ( i == 1)
453  ss << " arg1 = 10;\n";
454  }
455  else if (pCur->GetType() == formula::svDouble)
456  {
457  ss << " if(isnan(arg" << i <<"))\n";
458  if( i == 0)
459  ss << " arg0 = 0;\n";
460  else if ( i == 1)
461  ss << " arg1 = 10;\n";
462  }
463  }
464  if (vSubArguments.size() < 2)
465  ss << " arg1 = 10;\n";
466  ss << " tem = log10(arg0)/log10(arg1);;\n";
467  ss << " return tem;\n";
468  ss << "}";
469 }
470 void OpCsc::GenSlidingWindowFunction(
471  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
472 {
473  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
474  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
476  ss << "\ndouble " << sSymName;
477  ss << "_"<< BinFuncName() <<"(";
478  for (size_t i = 0; i < vSubArguments.size(); i++)
479  {
480  if (i)
481  ss << ",";
482  vSubArguments[i]->GenSlidingWindowDecl(ss);
483  }
484  ss << ")\n{\n\t";
485  ss <<"int gid0=get_global_id(0);\n\t";
486  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
487  ss << ";\n\t";
488  ss<< "if(isnan(arg0)||(gid0>=";
489  ss<<tmpCurDVR->GetArrayLength();
490  ss<<"))\n\t\t";
491  ss<<"arg0 = 0;\n\t";
492  ss << "double tmp=1/sin(arg0);\n\t";
493  ss << "return tmp;\n";
494  ss << "}";
495 }
496 void OpCountIfs::GenSlidingWindowFunction(std::stringstream &ss,
497  const std::string &sSymName, SubArguments &vSubArguments)
498 {
499  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
500  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
502  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
503  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
504  pCurDVR->GetRefRowSize() ;
505  ss << "\ndouble " << sSymName;
506  ss << "_"<< BinFuncName() <<"(";
507  for (size_t i = 0; i < vSubArguments.size(); i++)
508  {
509  if (i)
510  ss << ",";
511  vSubArguments[i]->GenSlidingWindowDecl(ss);
512  }
513  ss << ")\n {\n";
514  ss <<" int gid0=get_global_id(0);\n";
515  ss << " int tmp =0;\n";
516  ss << " int loop;\n";
517  GenTmpVariables(ss,vSubArguments);
518 
519  ss<< " int singleIndex =gid0;\n";
520  int m=0;
521 
522  std::stringstream tmpss;
523 
524  for(size_t j=0;j<vSubArguments.size();j+=2,m++)
525  {
526  CheckSubArgumentIsNan(tmpss,vSubArguments,j);
527  CheckSubArgumentIsNan(ss,vSubArguments,j+1);
528  tmpss <<" if(isequal(";
529  tmpss <<"tmp";
530  tmpss <<j;
531  tmpss <<" , ";
532  tmpss << "tmp";
533  tmpss << j+1;
534  tmpss << ")){\n";
535  }
536  tmpss << " tmp ++;\n";
537  for(size_t j=0;j<vSubArguments.size();j+=2,m--)
538  {
539  for(int n = 0;n<m+1;n++)
540  {
541  tmpss << " ";
542  }
543  tmpss<< "}\n";
544  }
545  UnrollDoubleVector(ss,tmpss,pCurDVR,nCurWindowSize);
546 
547  ss << "return tmp;\n";
548  ss << "}";
549 }
550 void OpSumIfs::GenSlidingWindowFunction(std::stringstream &ss,
551  const std::string &sSymName, SubArguments &vSubArguments)
552 {
553  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
554  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
556  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
557  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
558  pCurDVR->GetRefRowSize() ;
559 
560  mNeedReductionKernel = vSubArguments[0]->NeedParallelReduction();
561  if (mNeedReductionKernel)
562  {
563  // generate reduction functions
564 
565  ss << "__kernel void ";
566  ss << vSubArguments[0]->GetName();
567  ss << "_SumIfs_reduction( ";
568  for (size_t i = 0; i < vSubArguments.size(); i++)
569  {
570  if (i)
571  ss << ",";
572  vSubArguments[i]->GenSlidingWindowDecl(ss);
573  }
574  ss << ", __global double *result,int arrayLength,int windowSize";
575 
576  ss << ")\n{\n";
577  ss << " double tmp =0;\n";
578  ss << " int i ;\n";
579 
580  GenTmpVariables(ss,vSubArguments);
581  ss << " double current_result = 0.0;\n";
582  ss << " int writePos = get_group_id(1);\n";
583  if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
584  ss << " int offset = 0;\n";
585  else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
586  ss << " int offset = get_group_id(1);\n";
587  else
588  throw Unhandled(__FILE__, __LINE__);
589  // actually unreachable
590  ss << " int lidx = get_local_id(0);\n";
591  ss << " __local double shm_buf[256];\n";
592  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
593  ss << " int loop = arrayLength/512 + 1;\n";
594  ss << " for (int l=0; l<loop; l++){\n";
595  ss << " tmp = 0.0;\n";
596  ss << " int loopOffset = l*512;\n";
597 
598  ss << " int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
599  ss << " if (p2 < min(offset + windowSize, arrayLength)) {\n";
600  ss << " tmp0 = 0.0;\n";
601  int mm=0;
602  std::string p1 = "p1";
603  std::string p2 = "p2";
604  for(size_t j=1;j<vSubArguments.size();j+=2,mm++)
605  {
606  CheckSubArgumentIsNan2(ss,vSubArguments,j,p1);
607  CheckSubArgumentIsNan2(ss,vSubArguments,j+1,p1);
608  ss << "";
609  ss <<" if(isequal(";
610  ss <<"tmp";
611  ss <<j;
612  ss <<" , ";
613  ss << "tmp";
614  ss << j+1;
615  ss << "))";
616  ss << "{\n";
617  }
618  CheckSubArgumentIsNan2(ss,vSubArguments,0,p1);
619  ss << " tmp += tmp0;\n";
620  for(size_t j=1;j<vSubArguments.size();j+=2,mm--)
621  {
622  for(int n = 0;n<mm+1;n++)
623  {
624  ss << " ";
625  }
626  ss<< "}\n\n";
627  }
628  mm=0;
629  for(size_t j=1;j<vSubArguments.size();j+=2,mm++)
630  {
631  CheckSubArgumentIsNan2(ss,vSubArguments,j,p2);
632  CheckSubArgumentIsNan2(ss,vSubArguments,j+1,p2);
633  ss <<" if(isequal(";
634  ss <<"tmp";
635  ss <<j;
636  ss <<" , ";
637  ss << "tmp";
638  ss << j+1;
639  ss << ")){\n";
640  }
641  CheckSubArgumentIsNan2(ss,vSubArguments,0,p2);
642  ss << " tmp += tmp0;\n";
643  for(size_t j=1;j< vSubArguments.size();j+=2,mm--)
644  {
645  for(int n = 0;n<mm+1;n++)
646  {
647  ss << " ";
648  }
649  ss<< "}\n";
650  }
651  ss << " }\n";
652 
653  ss << " else if (p1 < min(arrayLength, offset + windowSize)) {\n";
654  mm=0;
655  for(size_t j=1;j<vSubArguments.size();j+=2,mm++)
656  {
657  CheckSubArgumentIsNan2(ss,vSubArguments,j,p1);
658  CheckSubArgumentIsNan2(ss,vSubArguments,j+1,p1);
659 
660  ss <<" if(isequal(";
661  ss <<"tmp";
662  ss <<j;
663  ss <<" , ";
664  ss << "tmp";
665  ss << j+1;
666  ss << ")){\n";
667  }
668  CheckSubArgumentIsNan2(ss,vSubArguments,0,p1);
669  ss << " tmp += tmp0;\n";
670  for(size_t j=1;j<vSubArguments.size();j+=2,mm--)
671  {
672  for(int n = 0;n<mm+1;n++)
673  {
674  ss << " ";
675  }
676  ss<< "}\n\n";
677  }
678 
679  ss << " }\n";
680  ss << " shm_buf[lidx] = tmp;\n";
681  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
682  ss << " for (int i = 128; i >0; i/=2) {\n";
683  ss << " if (lidx < i)\n";
684  ss << " shm_buf[lidx] += shm_buf[lidx + i];\n";
685  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
686  ss << " }\n";
687  ss << " if (lidx == 0)\n";
688  ss << " current_result += shm_buf[0];\n";
689  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
690  ss << " }\n";
691 
692  ss << " if (lidx == 0)\n";
693  ss << " result[writePos] = current_result;\n";
694  ss << "}\n";
695  }// finish generate reduction code
696  // generate functions as usual
697  ss << "\ndouble " << sSymName;
698  ss << "_"<< BinFuncName() <<"(";
699  for (size_t i = 0; i < vSubArguments.size(); i++)
700  {
701  if (i)
702  ss << ",";
703  vSubArguments[i]->GenSlidingWindowDecl(ss);
704  }
705  ss << ")\n {\n";
706  ss <<" int gid0=get_global_id(0);\n";
707  ss << " double tmp =0;\n";
708  if (!mNeedReductionKernel)
709  {
710  ss << " int i ;\n";
711  GenTmpVariables(ss,vSubArguments);
712  ss << " for (i = ";
713  if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
714  ss << "gid0; i < "<< nCurWindowSize <<"; i++)\n";
715  } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
716  ss << "0; i < gid0+"<< nCurWindowSize <<"; i++)\n";
717  } else {
718  ss << "0; i < "<< nCurWindowSize <<"; i++)\n";
719  }
720  ss << " {\n";
721  if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
722  {
723  ss<< " int doubleIndex =i+gid0;\n";
724  }else
725  {
726  ss<< " int doubleIndex =i;\n";
727  }
728  ss<< " int singleIndex =gid0;\n";
729  int m=0;
730  for(size_t j=1;j<vSubArguments.size();j+=2,m++)
731  {
732  CheckSubArgumentIsNan(ss,vSubArguments,j);
733  CheckSubArgumentIsNan(ss,vSubArguments,j+1);
734  ss <<" if(isequal(";
735  ss <<"tmp";
736  ss <<j;
737  ss <<" , ";
738  ss << "tmp";
739  ss << j+1;
740  ss << ")){\n";
741  }
742  CheckSubArgumentIsNan(ss,vSubArguments,0);
743  ss << " tmp += tmp0;\n";
744  for(size_t j=1;j<=vSubArguments.size();j+=2,m--)
745  {
746  for(int n = 0;n<m+1;n++)
747  {
748  ss << " ";
749  }
750  ss<< "}\n";
751  }
752  }
753  if (mNeedReductionKernel)
754  {
755  ss << "tmp =";
756  vSubArguments[0]->GenDeclRef(ss);
757  ss << "[gid0];\n";
758  }
759  ss << "return tmp;\n";
760  ss << "}";
761 }
762 void OpCscH::GenSlidingWindowFunction(
763  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
764 {
765  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
766  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
768  ss << "\ndouble " << sSymName;
769  ss << "_"<< BinFuncName() <<"(";
770  for (size_t i = 0; i < vSubArguments.size(); i++)
771  {
772  if (i)
773  ss << ",";
774  vSubArguments[i]->GenSlidingWindowDecl(ss);
775  }
776  ss << ")\n{\n\t";
777  ss <<"int gid0=get_global_id(0);\n\t";
778  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
779  ss << ";\n\t";
780  ss<< "if(isnan(arg0)||(gid0>=";
781  ss<<tmpCurDVR->GetArrayLength();
782  ss<<"))\n\t\t";
783  ss<<"arg0 = 0;\n\t";
784  ss << "double tmp=1/sinh(arg0);\n\t";
785  ss << "return tmp;\n";
786  ss << "}";
787 }
788 void OpExp::GenSlidingWindowFunction(std::stringstream &ss,
789  const std::string &sSymName, SubArguments &vSubArguments)
790 {
791  ss << "\ndouble " << sSymName;
792  ss << "_"<< BinFuncName() <<"(";
793  for (size_t i = 0; i < vSubArguments.size(); i++)
794  {
795  if (i) ss << ",";
796  vSubArguments[i]->GenSlidingWindowDecl(ss);
797  }
798  ss << ")\n";
799  ss << "{\n";
800  ss << " int gid0=get_global_id(0);\n";
801  ss << " double arg0 = 0.0f;\n";
802  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
803  assert(tmpCur);
804  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
805  {
806  if(tmpCur->GetType() == formula::svSingleVectorRef)
807  {
808  const formula::SingleVectorRefToken*tmpCurDVR=
809  static_cast
810  <const formula::SingleVectorRefToken *>(tmpCur);
811  ss << " arg0 = ";
812  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
813  ss << ";\n";
814  ss << " if(isnan(";
815  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
816  ss << ")||(gid0>=";
817  ss << tmpCurDVR->GetArrayLength();
818  ss << "))\n";
819  ss << " { arg0 = 0.0f; }\n";
820  }
821  else if(tmpCur->GetType() == formula::svDouble)
822  {
823  ss << " arg0=" << tmpCur->GetDouble() << ";\n";
824  }
825  }
826  else
827  {
828  ss << " arg0 = ";
829  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
830  ss << ";\n";
831  }
832  ss << " return pow(M_E, arg0);\n";
833  ss << "}";
834 }
835 
836 void OpAverageIfs::GenSlidingWindowFunction(std::stringstream &ss,
837  const std::string &sSymName, SubArguments &vSubArguments)
838 {
839  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
840  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
842  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
843  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
844  pCurDVR->GetRefRowSize() ;
845  ss << "\ndouble " << sSymName;
846  ss << "_"<< BinFuncName() <<"(";
847  for (size_t i = 0; i < vSubArguments.size(); i++)
848  {
849  if (i)
850  ss << ",";
851  vSubArguments[i]->GenSlidingWindowDecl(ss);
852  }
853  ss << ")\n {\n";
854  ss <<" int gid0=get_global_id(0);\n";
855  ss << " double tmp =0;\n";
856  ss << " int count=0;\n";
857  ss << " int loop;";
858  GenTmpVariables(ss,vSubArguments);
859  ss<< " int singleIndex =gid0;\n";
860  int m=0;
861  std::stringstream tmpss;
862  for(size_t j=1;j<vSubArguments.size();j+=2,m++)
863  {
864  CheckSubArgumentIsNan(tmpss,vSubArguments,j);
865  CheckSubArgumentIsNan(ss,vSubArguments,j+1);
866  tmpss <<" if(isequal(";
867  tmpss <<"tmp";
868  tmpss <<j;
869  tmpss <<" , ";
870  tmpss << "tmp";
871  tmpss << j+1;
872  tmpss << ")){\n";
873  }
874  CheckSubArgumentIsNan(tmpss,vSubArguments,0);
875  tmpss << " tmp += tmp0;\n";
876  tmpss << " count++;\n";
877  for(size_t j=1;j<vSubArguments.size();j+=2,m--)
878  {
879  for(int n = 0;n<m+1;n++)
880  {
881  tmpss << " ";
882  }
883  tmpss<< "}\n";
884  }
885 
886  UnrollDoubleVector(ss,tmpss,pCurDVR,nCurWindowSize);
887 
888  ss << " if(count!=0)\n";
889  ss << " tmp=tmp/count;\n";
890  ss << " else\n";
891  ss << " tmp= 0 ;\n";
892  ss << "return tmp;\n";
893  ss << "}";
894 }
895 
896 void OpLog10::GenSlidingWindowFunction(std::stringstream &ss,
897  const std::string &sSymName, SubArguments &vSubArguments)
898 {
899  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
900  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
902  ss << "\ndouble " << sSymName;
903  ss << "_"<< BinFuncName() <<"(";
904  for (size_t i = 0; i < vSubArguments.size(); i++)
905  {
906  if (i)
907  ss << ",";
908  vSubArguments[i]->GenSlidingWindowDecl(ss);
909  }
910  ss << ")\n{\n\t";
911  ss <<"int gid0=get_global_id(0);\n\t";
912  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
913  ss << ";\n\t";
914  ss<< "if(isnan(arg0)||(gid0>=";
915  ss<<tmpCurDVR->GetArrayLength();
916  ss<<"))\n\t\t";
917  ss<<"arg0 = 0;\n\t";
918  ss << "double tmp=log10(arg0);\n\t";
919  ss << "return tmp;\n";
920  ss << "}";
921 }
922 
923 void OpSinh::GenSlidingWindowFunction(std::stringstream &ss,
924  const std::string &sSymName, SubArguments &vSubArguments)
925 {
926  ss << "\ndouble " << sSymName;
927  ss << "_"<< BinFuncName() <<"(";
928  for (size_t i = 0; i < vSubArguments.size(); i++)
929  {
930  if (i)
931  ss << ",";
932  vSubArguments[i]->GenSlidingWindowDecl(ss);
933  }
934  ss <<") {\n";
935  ss <<" int gid0=get_global_id(0);\n";
936  ss <<" double arg0 = " <<
937  vSubArguments[0]->GenSlidingWindowDeclRef();
938  ss <<";\n";
939  ss<< " if(isnan(arg0))\n";
940  ss<<" arg0 = 0;\n";
941  ss << " double tmp=( exp(arg0)-exp(-arg0) )/2;\n";
942  ss << " return tmp;\n";
943  ss << "}";
944 }
945 void OpSin::GenSlidingWindowFunction(std::stringstream &ss,
946  const std::string &sSymName, SubArguments &vSubArguments)
947 {
948  ss << "\ndouble " << sSymName;
949  ss << "_"<< BinFuncName() <<"(";
950  for (size_t i = 0; i < vSubArguments.size(); i++)
951  {
952  if (i) ss << ",";
953  vSubArguments[i]->GenSlidingWindowDecl(ss);
954  }
955  ss << ")\n";
956  ss << "{\n";
957  ss << " int gid0=get_global_id(0);\n";
958  ss << " double arg0 = 0.0f;\n";
959  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
960  assert(tmpCur);
961  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
962  {
963  if(tmpCur->GetType() == formula::svSingleVectorRef)
964  {
965  const formula::SingleVectorRefToken*tmpCurDVR=
966  static_cast
967  <const formula::SingleVectorRefToken *>(tmpCur);
968  ss << " arg0 = ";
969  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
970  ss << ";\n";
971  ss << " if(isnan(";
972  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
973  ss << ")||(gid0>=";
974  ss << tmpCurDVR->GetArrayLength();
975  ss << "))\n";
976  ss << " { arg0 = 0.0f; }\n";
977  }
978  else if(tmpCur->GetType() == formula::svDouble)
979  {
980  ss << " arg0=" << tmpCur->GetDouble() << ";\n";
981  }
982  }
983  else
984  {
985  ss << " arg0 = ";
986  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
987  ss << ";\n";
988  }
989  ss << " arg0 = arg0 * M_1_PI;\n";
990  ss << " return sinpi(arg0);\n";
991  ss << "}";
992 }
993 
994 void OpAbs::GenSlidingWindowFunction(std::stringstream &ss,
995  const std::string &sSymName, SubArguments &vSubArguments)
996 {
997  ss << "\ndouble " << sSymName;
998  ss << "_"<< BinFuncName() <<"(";
999  for (size_t i = 0; i < vSubArguments.size(); i++)
1000  {
1001  if (i)
1002  ss << ",";
1003  vSubArguments[i]->GenSlidingWindowDecl(ss);
1004  }
1005  ss << ") {\n";
1006  ss << " int gid0 = get_global_id(0);\n";
1007  ss << " double tmp = " << GetBottom() << ";\n";
1008  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1009  const formula::SingleVectorRefToken*tmpCurDVR0=
1010  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1011  ss << " int buffer_len = ";
1012  ss << tmpCurDVR0->GetArrayLength();
1013  ss << ";\n";
1014  ss << " if((gid0)>=buffer_len || isnan(";
1015  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1016  ss << "))\n";
1017  ss << " tmp = " << GetBottom() << ";\n else \n";
1018  ss << " tmp = ";
1019  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1020  ss << ";\n";
1021  ss << " return fabs(tmp);\n";
1022  ss << "}";
1023 }
1024 void OpArcCos::BinInlineFun(std::set<std::string>& decls,
1025  std::set<std::string>& funs)
1026 {
1027  decls.insert(atan2Decl);
1028  funs.insert(atan2Content);
1029 }
1030 void OpArcCos::GenSlidingWindowFunction(std::stringstream &ss,
1031  const std::string &sSymName, SubArguments &vSubArguments)
1032 {
1033  ss << "\ndouble " << sSymName;
1034  ss << "_"<< BinFuncName() <<"(";
1035  for (size_t i = 0; i < vSubArguments.size(); i++)
1036  {
1037  if (i) ss << ",";
1038  vSubArguments[i]->GenSlidingWindowDecl(ss);
1039  }
1040  ss << ") {\n";
1041  ss << " int gid0 = get_global_id(0);\n";
1042  ss << " double tmp = " << GetBottom() << ";\n";
1043  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1044  const formula::SingleVectorRefToken*tmpCurDVR0=
1045  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1046  ss << " int buffer_len = "<< tmpCurDVR0->GetArrayLength()<< ";\n";
1047  ss << " if((gid0)>=buffer_len || isnan(";
1048  ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< "))\n";
1049  ss << " tmp = " << GetBottom() << ";\n";
1050  ss << " else \n ";
1051  ss << " tmp = ";
1052  ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< ";\n";
1053  ss << " return arctan2(sqrt(1.0 - pow(tmp, 2)), tmp);\n";
1054  ss << "}";
1055 }
1056 void OpArcCosHyp::GenSlidingWindowFunction(std::stringstream &ss,
1057  const std::string &sSymName, SubArguments &vSubArguments)
1058 {
1059  ss << "\ndouble " << sSymName;
1060  ss << "_"<< BinFuncName() <<"(";
1061  for (size_t i = 0; i < vSubArguments.size(); i++)
1062  {
1063  if (i) ss << ",";
1064  vSubArguments[i]->GenSlidingWindowDecl(ss);
1065  }
1066  ss << ") {\n";
1067  ss << " int gid0 = get_global_id(0);\n";
1068  ss << " double tmp = " << GetBottom() << ";\n";
1069  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1070  assert(tmpCur0);
1071  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1072  {
1073  if(tmpCur0->GetType() == formula::svSingleVectorRef)
1074  {
1075  const formula::SingleVectorRefToken*tmpCurDVR0=
1076  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1077  ss << " int buffer_len = "<<tmpCurDVR0->GetArrayLength()<<";\n";
1078  ss << " if((gid0)>=buffer_len || isnan(";
1079  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1080  ss << " tmp = " << GetBottom() << ";\n";
1081  ss << " else \n ";
1082  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1083  ss << ";\n";
1084  }
1085  else if(tmpCur0->GetType() == formula::svDouble)
1086  {
1087  ss << " tmp = " << tmpCur0->GetDouble() << ";\n";
1088  }
1089  }
1090  else
1091  {
1092  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1093  ss << ";\n";
1094  }
1095 
1096  ss << " return log( tmp + pow( (pown(tmp, 2) - 1.0), 0.5));\n";
1097  ss << "}";
1098 }
1099 void OpTan::GenSlidingWindowFunction(std::stringstream &ss,
1100  const std::string &sSymName, SubArguments &vSubArguments)
1101 {
1102  ss << "\ndouble " << sSymName;
1103  ss << "_"<< BinFuncName() <<"(";
1104  for (size_t i = 0; i < vSubArguments.size(); i++)
1105  {
1106  if (i) ss << ",";
1107  vSubArguments[i]->GenSlidingWindowDecl(ss);
1108  }
1109  ss << ")\n";
1110  ss << "{\n";
1111  ss << " int gid0=get_global_id(0);\n";
1112  ss << " double arg0 = 0.0f;\n";
1113  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1114  assert(tmpCur);
1115  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1116  {
1117  if(tmpCur->GetType() == formula::svSingleVectorRef)
1118  {
1119  const formula::SingleVectorRefToken*tmpCurDVR=
1120  static_cast
1121  <const formula::SingleVectorRefToken *>(tmpCur);
1122  ss << " arg0 = ";
1123  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1124  ss << ";\n";
1125  ss << " if(isnan(";
1126  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1127  ss << ")||(gid0>=";
1128  ss << tmpCurDVR->GetArrayLength();
1129  ss << "))\n";
1130  ss << " { arg0 = 0.0f; }\n";
1131  }
1132  else if(tmpCur->GetType() == formula::svDouble)
1133  {
1134  ss << " arg0=" << tmpCur->GetDouble() << ";\n";
1135  }
1136  }
1137  else
1138  {
1139  ss << " arg0 = ";
1140  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1141  ss << ";\n";
1142  }
1143  ss << " arg0 = arg0 * M_1_PI;\n";
1144  ss << " return sinpi(arg0) * pow(cospi(arg0), -1);\n";
1145  ss << "}";
1146 }
1147 void OpTanH::GenSlidingWindowFunction(std::stringstream &ss,
1148  const std::string &sSymName, SubArguments &vSubArguments)
1149 {
1150  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1151  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
1152  formula::SingleVectorRefToken *>(tmpCur);
1153  ss << "\ndouble " << sSymName;
1154  ss << "_"<< BinFuncName() <<"(";
1155  for (size_t i = 0; i < vSubArguments.size(); i++)
1156  {
1157  if (i)
1158  ss << ",";
1159  vSubArguments[i]->GenSlidingWindowDecl(ss);
1160  }
1161  ss << ")\n";
1162  ss << "{\n";
1163  ss << " int gid0=get_global_id(0);\n";
1164  ss << " double arg0 = "<< vSubArguments[0]->GenSlidingWindowDeclRef();
1165  ss << ";\n";
1166  ss << " if(isnan(arg0)||(gid0>=";
1167  ss << tmpCurDVR->GetArrayLength();
1168  ss << "))\n";
1169  ss << " arg0 = 0;\n";
1170  ss << " double tmp=tanh(arg0);\n";
1171  ss << " return tmp;\n";
1172  ss << "}";
1173 }
1174 void OpPower::GenSlidingWindowFunction(std::stringstream &ss,
1175  const std::string &sSymName, SubArguments &vSubArguments)
1176 {
1177  ss << "\ndouble " << sSymName;
1178  ss << "_"<< BinFuncName() <<"(";
1179  for (size_t i = 0; i < vSubArguments.size(); i++)
1180  {
1181  if (i)
1182  ss << ",";
1183  vSubArguments[i]->GenSlidingWindowDecl(ss);
1184  }
1185  ss << ")\n";
1186  ss << "{\n";
1187  ss << " int gid0=get_global_id(0);\n";
1188  ss << " double arg[2];\n";
1189  for( size_t i=0; i < vSubArguments.size(); ++i)
1190  {
1191  FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
1192  assert(tmpCur);
1193  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1194  {
1195  if(tmpCur->GetType() == formula::svDoubleVectorRef)
1196  {
1197  const formula::DoubleVectorRefToken* tmpCurDVR =
1198  static_cast<
1199  const formula::DoubleVectorRefToken *>(tmpCur);
1200  ss << " int i = 0;\n";
1201  ss << " arg["<<i<<"] = ";
1202  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1203  ss << ";\n";
1204  ss << " if(isnan(arg["<<i;
1205  ss << "])||(gid0>=";
1206  ss << tmpCurDVR->GetArrayLength();
1207  ss << "))\n";
1208  ss << " arg["<<i;
1209  ss << "] = 0;\n";
1210  }
1211  else if(tmpCur->GetType() == formula::svSingleVectorRef)
1212  {
1213  const formula::SingleVectorRefToken* tmpCurDVR=
1214  static_cast<
1215  const formula::SingleVectorRefToken *>(tmpCur);
1216  ss << " arg["<<i<<"] = ";
1217  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1218  ss << ";\n";
1219  ss << " if(isnan(arg["<<i;
1220  ss << "])||(gid0>=";
1221  ss << tmpCurDVR->GetArrayLength();
1222  ss << "))\n";
1223  ss << " arg["<<i;
1224  ss << "] = 0;\n";
1225  }
1226  else if(tmpCur->GetType() == formula::svDouble)
1227  {
1228  ss << " arg["<<i<<"] = ";
1229  ss << tmpCur->GetDouble() << ";\n";
1230  }
1231  }
1232  else
1233  {
1234  ss << " arg["<<i<<"] = ";
1235  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1236  ss << ";\n";
1237  }
1238  }
1239  ss << " return pow(arg[0],arg[1]);\n";
1240  ss << "}";
1241 }
1242 void OpSqrt::GenSlidingWindowFunction(std::stringstream &ss,
1243  const std::string &sSymName, SubArguments &vSubArguments)
1244 {
1245  ss << "\ndouble " << sSymName;
1246  ss << "_"<< BinFuncName() <<"(";
1247  for (size_t i = 0; i < vSubArguments.size(); i++)
1248  {
1249  if (i)
1250  ss << ",";
1251  vSubArguments[i]->GenSlidingWindowDecl(ss);
1252  }
1253  ss << ")\n";
1254  ss << "{\n";
1255  ss << " int gid0=get_global_id(0);\n";
1256  ss << " double arg0 = 0.0f;\n";
1257  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1258  assert(tmpCur);
1259  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1260  {
1261  if(tmpCur->GetType() == formula::svSingleVectorRef)
1262  {
1263  const formula::SingleVectorRefToken*tmpCurDVR=
1264  static_cast
1265  <const formula::SingleVectorRefToken *>(tmpCur);
1266  ss << " arg0 = ";
1267  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1268  ss << ";\n";
1269  ss << " if(isnan(";
1270  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1271  ss << ")||(gid0>=";
1272  ss << tmpCurDVR->GetArrayLength();
1273  ss << "))\n";
1274  ss << " { arg0 = 0; }\n";
1275  }
1276  else if(tmpCur->GetType() == formula::svDouble)
1277  {
1278  ss << " arg0=";
1279  ss << tmpCur->GetDouble() << ";\n";
1280  }
1281  else
1282  {
1283  throw Unhandled( __FILE__, __LINE__ );
1284  }
1285  }
1286  else
1287  {
1288  ss << " arg0 = ";
1289  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1290  ss << ";\n";
1291  }
1292  ss << " if( arg0 < 0 )\n";
1293  ss << " return CreateDoubleError(IllegalArgument);\n";
1294  ss << " return sqrt(arg0);\n";
1295  ss << "}";
1296 }
1297 void OpArcCot::GenSlidingWindowFunction(std::stringstream &ss,
1298  const std::string &sSymName, SubArguments &vSubArguments)
1299 {
1300  ss << "\ndouble " << sSymName;
1301  ss << "_"<< BinFuncName() <<"(";
1302  for (size_t i = 0; i < vSubArguments.size(); i++)
1303  {
1304  if (i) ss << ",";
1305  vSubArguments[i]->GenSlidingWindowDecl(ss);
1306  }
1307  ss << ") {\n";
1308  ss << " int gid0 = get_global_id(0);\n";
1309  ss << " double tmp = " << GetBottom() << ";\n";
1310  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1311  const formula::SingleVectorRefToken*tmpCurDVR0=
1312  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1313  ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength()<< ";\n";
1314  ss << " if((gid0)>=buffer_len || isnan(";
1315  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1316  ss << " tmp = " << GetBottom() << ";\n";
1317  ss << " else \n ";
1318  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() <<";\n";
1319  ss << " return M_PI_2 - atan(tmp);\n";
1320  ss << "}";
1321 }
1322 void OpArcCotHyp::GenSlidingWindowFunction(std::stringstream &ss,
1323  const std::string &sSymName, SubArguments &vSubArguments)
1324 {
1325  ss << "\ndouble " << sSymName;
1326  ss << "_"<< BinFuncName() <<"(";
1327  for (size_t i = 0; i < vSubArguments.size(); i++)
1328  {
1329  if (i) ss << ",";
1330  vSubArguments[i]->GenSlidingWindowDecl(ss);
1331  }
1332  ss << ")\n";
1333  ss << "{\n";
1334  ss << " int gid0=get_global_id(0);\n";
1335  ss << " double arg0 = 0.0f;\n";
1336  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1337  assert(tmpCur);
1338  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1339  {
1340  if(tmpCur->GetType() == formula::svSingleVectorRef)
1341  {
1342  const formula::SingleVectorRefToken*tmpCurDVR=
1343  static_cast
1344  <const formula::SingleVectorRefToken *>(tmpCur);
1345  ss << " arg0 = ";
1346  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1347  ss << ";\n";
1348  ss << " if(isnan(";
1349  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1350  ss << ")||(gid0>=";
1351  ss << tmpCurDVR->GetArrayLength();
1352  ss << "))\n";
1353  ss << " { arg0 = 0.0f; }\n";
1354  }
1355  else if(tmpCur->GetType() == formula::svDouble)
1356  {
1357  ss << " arg0=";
1358  ss << tmpCur->GetDouble() << ";\n";
1359  }
1360  }
1361  else
1362  {
1363  ss << " arg0 = ";
1364  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1365  ss << ";\n";
1366  }
1367  ss << " return 0.5 * log(1 + 2 * pown(arg0 - 1.0, -1));\n";
1368  ss << "}";
1369 }
1370 void OpArcSin::BinInlineFun(std::set<std::string>& decls,
1371  std::set<std::string>& funs)
1372 {
1373  decls.insert(atan2Decl);
1374  funs.insert(atan2Content);
1375 }
1376 void OpArcSin::GenSlidingWindowFunction(std::stringstream &ss,
1377  const std::string &sSymName, SubArguments &vSubArguments)
1378 {
1379  ss << "\ndouble " << sSymName;
1380  ss << "_"<< BinFuncName() <<"(";
1381  for (size_t i = 0; i < vSubArguments.size(); i++)
1382  {
1383  if (i) ss << ",";
1384  vSubArguments[i]->GenSlidingWindowDecl(ss);
1385  }
1386  ss << ") {\n";
1387  ss << " int gid0 = get_global_id(0);\n";
1388  ss << " double tmp = " << GetBottom() << ";\n";
1389  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1390  const formula::SingleVectorRefToken*tmpCurDVR0=
1391  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1392  ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength() << ";\n";
1393  ss << " if((gid0)>=buffer_len || isnan(";
1394  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1395  ss << " tmp = " << GetBottom() << ";\n";
1396  ss << " else \n ";
1397  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1398  ss << " return arctan2(tmp, sqrt(1.0 - pow(tmp, 2)));\n";
1399  ss << "}";
1400 }
1401 void OpArcSinHyp::GenSlidingWindowFunction(std::stringstream &ss,
1402  const std::string &sSymName, SubArguments &vSubArguments)
1403 {
1404  ss << "\ndouble " << sSymName;
1405  ss << "_"<< BinFuncName() <<"(";
1406  for (size_t i = 0; i < vSubArguments.size(); i++)
1407  {
1408  if (i) ss << ",";
1409  vSubArguments[i]->GenSlidingWindowDecl(ss);
1410  }
1411  ss << ") {\n";
1412  ss << " int gid0 = get_global_id(0);\n";
1413  ss << " double tmp = " << GetBottom() << ";\n";
1414  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1415  assert(tmpCur0);
1416  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1417  {
1418  if(tmpCur0->GetType() == formula::svSingleVectorRef)
1419  {
1420  const formula::SingleVectorRefToken*tmpCurDVR0=
1421  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1422  ss << " int buffer_len = "<<tmpCurDVR0->GetArrayLength()<<";\n";
1423  ss << " if((gid0)>=buffer_len || isnan(";
1424  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1425  ss << " tmp = " << GetBottom() << ";\n";
1426  ss << " else \n ";
1427  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1428  ss << ";\n";
1429  }
1430  else if(tmpCur0->GetType() == formula::svDouble)
1431  {
1432  ss << " tmp = " << tmpCur0->GetDouble() << ";\n";
1433  }
1434  }
1435  else
1436  {
1437  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1438  ss << ";\n";
1439  }
1440  ss << " return log( tmp + pow((pown(tmp, 2) + 1.0), 0.5));\n";
1441  ss << "}";
1442 }
1443 void OpArcTan2::BinInlineFun(std::set<std::string>& decls,
1444  std::set<std::string>& funs)
1445 {
1446  decls.insert(atan2Decl);
1447  funs.insert(atan2Content);
1448 }
1449 void OpArcTan2::GenSlidingWindowFunction(std::stringstream &ss,
1450  const std::string &sSymName, SubArguments &vSubArguments)
1451 {
1452  ss << "\ndouble " << sSymName;
1453  ss << "_"<< BinFuncName() <<"(";
1454  for (size_t i = 0; i < vSubArguments.size(); i++)
1455  {
1456  if (i) ss << ",";
1457  vSubArguments[i]->GenSlidingWindowDecl(ss);
1458  }
1459  ss << ") {\n";
1460  ss << " int gid0 = get_global_id(0);\n";
1461  ss << " double x_num = " << GetBottom() << ";\n";
1462  ss << " double y_num = " << GetBottom() << ";\n";
1463  FormulaToken *iXNum = vSubArguments[0]->GetFormulaToken();
1464  const formula::SingleVectorRefToken*tmpCurDVRX=
1465  static_cast<const formula::SingleVectorRefToken *>(iXNum);
1466  FormulaToken *iYNum = vSubArguments[1]->GetFormulaToken();
1467  const formula::SingleVectorRefToken*tmpCurDVRY=
1468  static_cast<const formula::SingleVectorRefToken *>(iYNum);
1469  ss << " int buffer_x_len = " << tmpCurDVRX->GetArrayLength() << ";\n";
1470  ss << " int buffer_y_len = " << tmpCurDVRY->GetArrayLength() << ";\n";
1471  ss << " if((gid0)>=buffer_x_len || isnan(";
1472  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1473  ss << " x_num = " << GetBottom() << ";\n";
1474  ss << " else \n ";
1475  ss << " x_num = "<< vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1476  ss << " if((gid0)>=buffer_y_len || isnan(";
1477  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
1478  ss << " y_num = " << GetBottom() << ";\n";
1479  ss << " else \n ";
1480  ss << " y_num = "<< vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1481  ss << " return arctan2(y_num, x_num);\n";
1482  ss << "}";
1483 }
1484 void OpArcTan::GenSlidingWindowFunction(std::stringstream &ss,
1485  const std::string &sSymName, SubArguments &vSubArguments)
1486 {
1487  ss << "\ndouble " << sSymName;
1488  ss << "_"<< BinFuncName() <<"(";
1489  for (size_t i = 0; i < vSubArguments.size(); i++)
1490  {
1491  if (i) ss << ",";
1492  vSubArguments[i]->GenSlidingWindowDecl(ss);
1493  }
1494  ss << ")\n";
1495  ss << "{\n";
1496  ss << " int gid0=get_global_id(0);\n";
1497  ss << " double arg0 = 0.0f;\n";
1498  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1499  assert(tmpCur);
1500  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1501  {
1502  if(tmpCur->GetType() == formula::svSingleVectorRef)
1503  {
1504  const formula::SingleVectorRefToken*tmpCurDVR=
1505  static_cast
1506  <const formula::SingleVectorRefToken *>(tmpCur);
1507  ss << " arg0 = ";
1508  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1509  ss << ";\n";
1510  ss << " if(isnan(";
1511  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1512  ss << ")||(gid0>=";
1513  ss << tmpCurDVR->GetArrayLength();
1514  ss << "))\n";
1515  ss << " { arg0 = 0.0f; }\n";
1516  }
1517  else if(tmpCur->GetType() == formula::svDouble)
1518  {
1519  ss << " arg0=" << tmpCur->GetDouble() << ";\n";
1520  }
1521  }
1522  else
1523  {
1524  ss << " arg0 = ";
1525  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1526  ss << ";\n";
1527  }
1528  ss << " return atan(arg0);\n";
1529  ss << "}";
1530 }
1531 void OpArcTanH::GenSlidingWindowFunction(std::stringstream &ss,
1532  const std::string &sSymName, SubArguments &vSubArguments)
1533 {
1534  ss << "\ndouble " << sSymName;
1535  ss << "_"<< BinFuncName() <<"(";
1536  for (size_t i = 0; i < vSubArguments.size(); i++)
1537  {
1538  if (i) ss << ",";
1539  vSubArguments[i]->GenSlidingWindowDecl(ss);
1540  }
1541  ss << ") {\n";
1542  ss << " int gid0 = get_global_id(0);\n";
1543  ss << " double tmp = " << GetBottom() << ";\n";
1544  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1545  const formula::SingleVectorRefToken*tmpCurDVR0=
1546  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1547  ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength() << ";\n";
1548  ss << " if((gid0)>=buffer_len || isnan(";
1549  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1550  ss << " tmp = " << GetBottom() << ";\n";
1551  ss << " else \n ";
1552  ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1553  ss << " double a = 1.0 + tmp;\n";
1554  ss << " double b = 1.0 - tmp;\n";
1555  ss << " return log(pow(a/b, 0.5));\n";
1556  ss << "}";
1557 }
1558 void OpBitAnd::GenSlidingWindowFunction(std::stringstream &ss,
1559  const std::string &sSymName, SubArguments &vSubArguments)
1560 {
1561  ss << "\ndouble " << sSymName;
1562  ss << "_"<< BinFuncName() <<"(";
1563  for (size_t i = 0; i < vSubArguments.size(); i++)
1564  {
1565  if (i) ss << ",";
1566  vSubArguments[i]->GenSlidingWindowDecl(ss);
1567  }
1568  ss << ") {\n";
1569  ss << " int gid0 = get_global_id(0);\n";
1570  ss << " double num1 = " << GetBottom() << ";\n";
1571  ss << " double num2 = " << GetBottom() << ";\n";
1572  FormulaToken *iNum1 = vSubArguments[0]->GetFormulaToken();
1573  const formula::SingleVectorRefToken* tmpCurDVRNum1=
1574  static_cast<const formula::SingleVectorRefToken *>(iNum1);
1575  FormulaToken *iNum2 = vSubArguments[1]->GetFormulaToken();
1576  const formula::SingleVectorRefToken* tmpCurDVRNum2=
1577  static_cast<const formula::SingleVectorRefToken *>(iNum2);
1578  ss << " int buffer_num1_len = "<<tmpCurDVRNum1->GetArrayLength()<<";\n";
1579  ss << " int buffer_num2_len = "<<tmpCurDVRNum2->GetArrayLength()<<";\n";
1580  ss << " if((gid0)>=buffer_num1_len || isnan(";
1581  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1582  ss << " num1 = " << GetBottom() << ";\n";
1583  ss << " else \n ";
1584  ss << " num1 = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1585  ss << " if((gid0)>=buffer_num2_len || isnan(";
1586  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
1587  ss << " num2 = " << GetBottom() << ";\n";
1588  ss << " else \n ";
1589  ss << " num2 = " << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1590  ss << " return (long)num1 & (long)num2;\n";
1591  ss << "}";
1592 }
1593 void OpLn::GenSlidingWindowFunction(
1594  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1595 {
1596  ss << "\ndouble " << sSymName;
1597  ss << "_"<< BinFuncName() <<"(";
1598  for (size_t i = 0; i < vSubArguments.size(); i++)
1599  {
1600  if (i)
1601  ss << ",";
1602  vSubArguments[i]->GenSlidingWindowDecl(ss);
1603  }
1604  ss << ")\n{\n";
1605  ss << " int gid0=get_global_id(0);\n";
1606  ss << " int singleIndex = gid0;\n";
1607 
1608  GenTmpVariables(ss,vSubArguments);
1609  CheckAllSubArgumentIsNan(ss,vSubArguments);
1610 
1611  ss << " double tmp=log1p(tmp0-1);\n";
1612  ss << " return tmp;\n";
1613  ss << "}";
1614 }
1615 
1616 void OpRound::GenSlidingWindowFunction(std::stringstream &ss,
1617  const std::string &sSymName, SubArguments &vSubArguments)
1618 {
1619  CHECK_PARAMETER_COUNT( 1, 2 );
1620  ss << "\ndouble " << sSymName;
1621  ss << "_"<< BinFuncName() <<"(";
1622  for (size_t i = 0; i < vSubArguments.size(); i++)
1623  {
1624  if (i)
1625  ss << ",";
1626  vSubArguments[i]->GenSlidingWindowDecl(ss);
1627  }
1628  ss << ")\n{\n";
1629  ss << " int gid0=get_global_id(0);\n";
1630  ss << " int singleIndex = gid0;\n";
1631  GenTmpVariables(ss,vSubArguments);
1632  CheckAllSubArgumentIsNan(ss,vSubArguments);
1633  if(vSubArguments.size() ==2)
1634  {
1635  ss << " for(int i=0;i<tmp1;i++)\n";
1636  ss << " tmp0 = tmp0 * 10;\n";
1637  ss << " for(int i=0;i>tmp1;i--)\n";
1638  ss << " tmp0 = tmp0 / 10;\n";
1639  }
1640  ss << " double tmp=round(tmp0);\n";
1641  if(vSubArguments.size() ==2)
1642  {
1643  ss << " for(int i=0;i<tmp1;i++)\n";
1644  ss << " tmp = tmp / 10;\n";
1645  ss << " for(int i=0;i>tmp1;i--)\n";
1646  ss << " tmp = tmp * 10;\n";
1647  }
1648  ss << " return tmp;\n";
1649  ss << "}";
1650 }
1651 void OpRoundUp::GenSlidingWindowFunction(std::stringstream &ss,
1652  const std::string &sSymName, SubArguments &vSubArguments)
1653 {
1654  ss << "\ndouble " << sSymName;
1655  ss << "_"<< BinFuncName() <<"(";
1656  for (size_t i = 0; i < vSubArguments.size(); i++)
1657  {
1658  if (i)
1659  ss << ",";
1660  vSubArguments[i]->GenSlidingWindowDecl(ss);
1661  }
1662  ss << ")\n{\n";
1663  ss << " int gid0=get_global_id(0);\n";
1664  ss << " int singleIndex = gid0;\n";
1665  ss << " int intTmp;\n";
1666  ss << " double doubleTmp;\n";
1667  ss << " double tmp;\n";
1668  GenTmpVariables(ss,vSubArguments);
1669  CheckAllSubArgumentIsNan(ss,vSubArguments);
1670  ss << " if(tmp1 >20 || tmp1 < -20)";
1671  ss << " {\n";
1672  ss << " tmp = NAN;\n";
1673  ss << " }else\n";
1674  ss << " {\n";
1675  ss << " for(int i=0;i<tmp1;i++)\n";
1676  ss << " tmp0 = tmp0 * 10;\n";
1677  ss << " intTmp = (int)tmp0;\n";
1678  ss << " doubleTmp = intTmp;\n";
1679  ss << " if(isequal(doubleTmp,tmp0))\n";
1680  ss << " tmp = doubleTmp;\n";
1681  ss << " else\n";
1682  ss << " tmp = doubleTmp + 1;\n";
1683  ss << " for(int i=0;i<tmp1;i++)\n";
1684  ss << " tmp = tmp / 10;\n";
1685  ss << " }\n";
1686  ss << " return tmp;\n";
1687  ss << "}";
1688 }
1689 void OpRoundDown::GenSlidingWindowFunction(std::stringstream &ss,
1690  const std::string &sSymName, SubArguments &vSubArguments)
1691 {
1692  ss << "\ndouble " << sSymName;
1693  ss << "_"<< BinFuncName() <<"(";
1694  for (size_t i = 0; i < vSubArguments.size(); i++)
1695  {
1696  if (i)
1697  ss << ",";
1698  vSubArguments[i]->GenSlidingWindowDecl(ss);
1699  }
1700  ss << ")\n{\n";
1701  ss << " int gid0=get_global_id(0);\n";
1702  ss << " int singleIndex = gid0;\n";
1703  ss << " int intTmp;\n";
1704  ss << " double tmp;\n";
1705  GenTmpVariables(ss,vSubArguments);
1706  CheckAllSubArgumentIsNan(ss,vSubArguments);
1707  ss << " if(tmp1 >20 || tmp1 < -20)";
1708  ss << " {\n";
1709  ss << " tmp = NAN;\n";
1710  ss << " }else\n";
1711  ss << " {\n";
1712  ss << " for(int i=0;i<tmp1;i++)\n";
1713  ss << " tmp0 = tmp0 * 10;\n";
1714  ss << " intTmp = (int)tmp0;\n";
1715  ss << " tmp = intTmp;\n";
1716  ss << " for(int i=0;i<tmp1;i++)\n";
1717  ss << " tmp = tmp / 10;\n";
1718  ss << " }\n";
1719  ss << " return tmp;\n";
1720  ss << "}";
1721 }
1722 void OpInt::GenSlidingWindowFunction(std::stringstream &ss,
1723  const std::string &sSymName, SubArguments &vSubArguments)
1724 {
1725  ss << "\ndouble " << sSymName;
1726  ss << "_"<< BinFuncName() <<"(";
1727  for (size_t i = 0; i < vSubArguments.size(); i++)
1728  {
1729  if (i)
1730  ss << ",";
1731  vSubArguments[i]->GenSlidingWindowDecl(ss);
1732  }
1733  ss << ")\n{\n";
1734  ss << " int gid0=get_global_id(0);\n";
1735  ss << " int singleIndex = gid0;\n";
1736  ss << " int intTmp;\n";
1737  ss << " double tmp;\n";
1738  GenTmpVariables(ss,vSubArguments);
1739  CheckAllSubArgumentIsNan(ss,vSubArguments);
1740  ss << " intTmp = (int)tmp0;\n";
1741  ss << " tmp = intTmp;\n";
1742  ss << " return tmp;\n";
1743  ss << "}";
1744 }
1745 void OpNegSub::GenSlidingWindowFunction(std::stringstream &ss,
1746  const std::string &sSymName, SubArguments &vSubArguments)
1747 {
1748  ss << "\ndouble " << sSymName;
1749  ss << "_"<< BinFuncName() <<"(";
1750  for (size_t i = 0; i < vSubArguments.size(); i++)
1751  {
1752  if (i)
1753  ss << ",";
1754  vSubArguments[i]->GenSlidingWindowDecl(ss);
1755  }
1756  ss << ")\n{\n";
1757  ss << " int gid0=get_global_id(0);\n";
1758  ss << " int singleIndex = gid0;\n";
1759  GenTmpVariables(ss,vSubArguments);
1760  CheckAllSubArgumentIsNan(ss,vSubArguments);
1761  ss << " return -tmp0;\n";
1762  ss << "}";
1763 }
1764 
1765 void OpRadians::GenSlidingWindowFunction(std::stringstream &ss,
1766  const std::string &sSymName, SubArguments &vSubArguments)
1767 {
1768  ss << "\ndouble " << sSymName;
1769  ss << "_"<< BinFuncName() <<"(";
1770  for (size_t i = 0; i < vSubArguments.size(); i++)
1771  {
1772  if (i)
1773  ss << ",";
1774  vSubArguments[i]->GenSlidingWindowDecl(ss);
1775  }
1776  ss << ")\n{\n";
1777  ss << " int gid0=get_global_id(0);\n";
1778  ss << " int singleIndex = gid0;\n";
1779  ss << " double tmp;\n";
1780  GenTmpVariables(ss,vSubArguments);
1781  CheckAllSubArgumentIsNan(ss,vSubArguments);
1782  ss << " tmp = tmp0 * 3.14159265358979 * pow(180.0,-1);\n";
1783  ss << " return tmp;\n";
1784  ss << "}";
1785 }
1786 void OpIsEven::GenSlidingWindowFunction(std::stringstream &ss,
1787  const std::string &sSymName, SubArguments &vSubArguments)
1788 {
1789  ss << "\ndouble " << sSymName;
1790  ss << "_"<< BinFuncName() <<"(";
1791  for (size_t i = 0; i < vSubArguments.size(); i++)
1792  {
1793  if (i)
1794  ss << ",";
1795  vSubArguments[i]->GenSlidingWindowDecl(ss);
1796  }
1797  ss << ")\n{\n";
1798  ss << " int gid0=get_global_id(0);\n";
1799  ss << " int singleIndex = gid0;\n";
1800  ss << " double tmp;\n";
1801  GenTmpVariables(ss,vSubArguments);
1802  CheckAllSubArgumentIsNan(ss,vSubArguments);
1803  ss << " tmp = (fmod(floor(fabs(tmp0)), 2.0)<0.5);\n";
1804  ss << " return tmp;\n";
1805  ss << "}";
1806 }
1807 void OpIsOdd::GenSlidingWindowFunction(std::stringstream &ss,
1808  const std::string &sSymName, SubArguments &vSubArguments)
1809 {
1810  ss << "\ndouble " << sSymName;
1811  ss << "_"<< BinFuncName() <<"(";
1812  for (size_t i = 0; i < vSubArguments.size(); i++)
1813  {
1814  if (i)
1815  ss << ",";
1816  vSubArguments[i]->GenSlidingWindowDecl(ss);
1817  }
1818  ss << ")\n{\n";
1819  ss << " int gid0=get_global_id(0);\n";
1820  ss << " int singleIndex = gid0;\n";
1821  ss << " double tmp;\n";
1822  GenTmpVariables(ss,vSubArguments);
1823  CheckAllSubArgumentIsNan(ss,vSubArguments);
1824  ss << " tmp = !(fmod(floor(fabs(tmp0)), 2.0)<0.5);\n";
1825  ss << " return tmp;\n";
1826  ss << "}";
1827 }
1828 void OpOdd::GenSlidingWindowFunction(
1829  std::stringstream &ss, const std::string &sSymName,
1830  SubArguments &vSubArguments)
1831 {
1832  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1833  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
1834  formula::SingleVectorRefToken *>(tmpCur);
1835  ss << Math_Intg_Str;
1836  ss << "\ndouble " << sSymName;
1837  ss << "_"<< BinFuncName() <<"(";
1838  for (size_t i = 0; i < vSubArguments.size(); i++)
1839  {
1840  if (i)
1841  ss << ",";
1842  vSubArguments[i]->GenSlidingWindowDecl(ss);
1843  }
1844  ss << ")\n{\n";
1845  ss <<" int gid0=get_global_id(0);\n";
1846  ss << " double tmp=0;\n";
1847  ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1848  ss << ";\n";
1849  ss<< " if(isnan(arg0)||(gid0>=";
1850  ss<<tmpCurDVR->GetArrayLength();
1851  ss<<"))\n";
1852  ss<<" arg0 = 0;\n";
1853  ss << " if (arg0 > 0.0 ){\n";
1854  ss << " tmp=Intg(arg0);\n";
1855  ss << " if(tmp-trunc(tmp/2)*2 == 0)\n";
1856  ss << " tmp=tmp+1;\n";
1857  ss << " }else if (arg0 < 0.0 ){\n";
1858  ss << " tmp=Intg(arg0);\n";
1859  ss << " if(tmp-trunc(tmp/2)*2 == 0)\n";
1860  ss << " tmp=tmp-1.0;\n";
1861  ss << " }else if (arg0 == 0.0 )\n";
1862  ss << " tmp=1.0;\n";
1863  ss << " return tmp;\n";
1864  ss << "}";
1865 }
1866 void OpCountIf::GenSlidingWindowFunction(std::stringstream &ss,
1867  const std::string &sSymName, SubArguments &vSubArguments)
1868 {
1869  ss << "\ndouble " << sSymName;
1870  ss << "_"<< BinFuncName() <<"(";
1871  for (unsigned i = 0; i < 2; i++)
1872  {
1873  if (i)
1874  ss << ",";
1875  vSubArguments[i]->GenSlidingWindowDecl(ss);
1876  }
1877  ss << ")\n";
1878  ss << "{\n";
1879  ss << " int gid0=get_global_id(0);\n";
1880  ss << " double vara, varb;\n";
1881  ss << " int varc = 0;\n";
1882  FormulaToken *tmpCur = vSubArguments[1]->GetFormulaToken();
1883  assert(tmpCur);
1884  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1885  {
1886  if(tmpCur->GetType() == formula::svSingleVectorRef)
1887  {
1888  const formula::SingleVectorRefToken* tmpCurDVR=
1889  static_cast<
1890  const formula::SingleVectorRefToken *>(tmpCur);
1891  ss << " varb = ";
1892  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1893  ss << ";\n";
1894  ss << " if(isnan(varb)||(gid0>=";
1895  ss << tmpCurDVR->GetArrayLength();
1896  ss << "))\n";
1897  ss << " varb = 0;\n";
1898  }
1899  else if(tmpCur->GetType() == formula::svDouble)
1900  {
1901  ss << " varb = ";
1902  ss << tmpCur->GetDouble() << ";\n";
1903  }
1904  }
1905  else
1906  {
1907  ss << " varb = ";
1908  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1909  ss << ";\n";
1910  }
1911  tmpCur = vSubArguments[0]->GetFormulaToken();
1912  assert(tmpCur);
1913  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1914  {
1915  //TODO DoubleVector
1916  if (tmpCur->GetType() == formula::svDoubleVectorRef)
1917  {
1918  const formula::DoubleVectorRefToken* pDVR =
1919  static_cast<const formula::DoubleVectorRefToken *>(tmpCur);
1920  size_t nCurWindowSize = pDVR->GetRefRowSize();
1921  ss << " for (int i = ";
1922  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1923  {
1924  ss << "gid0; i < " << pDVR->GetArrayLength();
1925  ss << " && i < " << nCurWindowSize << "; ++i)\n";
1926  ss << " {\n";
1927  }
1928  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1929  {
1930  ss << "0; i < " << pDVR->GetArrayLength();
1931  ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
1932  ss << " {\n";
1933  }
1934  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1935  {
1936  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1937  ss << " && i < "<< nCurWindowSize << "; ++i)\n";
1938  ss << " {\n";
1939  }
1940  else
1941  {
1942  ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
1943  ss << " {\n";
1944  }
1945  ss << " vara = ";
1946  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1947  ss << ";\n";
1948  ss << " if (isnan(vara))\n";
1949  ss << " continue;\n";
1950  ss << " (vara == varb) && varc++;\n";
1951  ss << " }\n";
1952  }
1953  else if(tmpCur->GetType() == formula::svSingleVectorRef)
1954  {
1955  const formula::SingleVectorRefToken* tmpCurDVR=
1956  static_cast<
1957  const formula::SingleVectorRefToken *>(tmpCur);
1958  ss << " vara = ";
1959  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1960  ss << ";\n";
1961  ss << " if(isnan(vara)||(gid0>=";
1962  ss << tmpCurDVR->GetArrayLength();
1963  ss << "))\n";
1964  ss << " return 0;\n";
1965  ss << " (vara == varb) && varc++;\n";
1966  }
1967  }
1968  ss << " return varc;\n";
1969  ss << "}";
1970 }
1971 void OpSumIf::GenSlidingWindowFunction(std::stringstream &ss,
1972  const std::string &sSymName, SubArguments &vSubArguments)
1973 {
1974  ss << "\ndouble " << sSymName;
1975  ss << "_"<< BinFuncName() <<"(";
1976  int flag = 3 == vSubArguments.size() ? 2 : 0;
1977  for (size_t i = 0; i < vSubArguments.size(); i++)
1978  {
1979  if (i)
1980  ss << ",";
1981  vSubArguments[i]->GenSlidingWindowDecl(ss);
1982  }
1983  ss << ")\n";
1984  ss << "{\n";
1985  ss << " int gid0=get_global_id(0);\n";
1986  ss << " double vara, varb, varc, sum = 0.0f;\n";
1987  FormulaToken *tmpCur = vSubArguments[1]->GetFormulaToken();
1988  assert(tmpCur);
1989  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1990  {
1991  if(tmpCur->GetType() == formula::svSingleVectorRef)
1992  {
1993  const formula::SingleVectorRefToken* tmpCurDVR=
1994  static_cast<
1995  const formula::SingleVectorRefToken *>(tmpCur);
1996  ss << " varb = ";
1997  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1998  ss << ";\n";
1999  ss << " if(isnan(varb)||(gid0>=";
2000  ss << tmpCurDVR->GetArrayLength();
2001  ss << "))\n";
2002  ss << " varb = 0;\n";
2003  }
2004  else if(tmpCur->GetType() == formula::svDouble)
2005  {
2006  ss << " varb = ";
2007  ss << tmpCur->GetDouble() << ";\n";
2008  }
2009  }
2010  else
2011  {
2012  ss << " varb = ";
2013  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2014  ss << ";\n";
2015  }
2016  tmpCur = vSubArguments[0]->GetFormulaToken();
2017  assert(tmpCur);
2018  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2019  {
2020  //TODO DoubleVector
2021  if (tmpCur->GetType() == formula::svDoubleVectorRef)
2022  {
2023  const formula::DoubleVectorRefToken* pDVR =
2024  static_cast<const formula::DoubleVectorRefToken *>(tmpCur);
2025  size_t nCurWindowSize = pDVR->GetRefRowSize();
2026  ss << " for (int i = ";
2027  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2028  {
2029  ss << "gid0; i < " << pDVR->GetArrayLength();
2030  ss << " && i < " << nCurWindowSize << "; ++i)\n";
2031  ss << " {\n";
2032  }
2033  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2034  {
2035  ss << "0; i < " << pDVR->GetArrayLength();
2036  ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
2037  ss << " {\n";
2038  }
2039  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2040  {
2041  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2042  ss << " && i < "<< nCurWindowSize << "; ++i)\n";
2043  ss << " {\n";
2044  }
2045  else
2046  {
2047  ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2048  ss << " {\n";
2049  }
2050  ss << " vara = ";
2051  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2052  ss << ";\n";
2053  ss << " if (isnan(vara))\n";
2054  ss << " continue;\n";
2055  ss << " varc = ";
2056  ss << vSubArguments[flag]->GenSlidingWindowDeclRef();
2057  ss << ";\n";
2058  ss << " if (isnan(varc))\n";
2059  ss << " varc = 0.0f;\n";
2060  ss << " (vara == varb)&&(sum = sum + varc);\n";
2061  ss << " }\n";
2062  }
2063  else if(tmpCur->GetType() == formula::svSingleVectorRef)
2064  {
2065  const formula::SingleVectorRefToken* tmpCurDVR=
2066  static_cast<
2067  const formula::SingleVectorRefToken *>(tmpCur);
2068  ss << " vara = ";
2069  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2070  ss << ";\n";
2071  ss << " if(isnan(vara)||(gid0>=";
2072  ss << tmpCurDVR->GetArrayLength();
2073  ss << "))\n";
2074  ss << " return 0;\n";
2075  ss << " int i = 0;\n";
2076  ss << " varc = ";
2077  ss << vSubArguments[flag]->GenSlidingWindowDeclRef();
2078  ss << ";\n";
2079  ss << " if(isnan(varc)||(gid0>=";
2080  ss << tmpCurDVR->GetArrayLength();
2081  ss << "))\n";
2082  ss << " varc = 0.0f;\n";
2083 
2084  ss << " (vara == varb)&&(sum = sum + varc);\n";
2085 
2086  }
2087  }
2088  ss << " return sum;\n";
2089  ss << "}";
2090 }
2091 void OpTrunc::GenSlidingWindowFunction(std::stringstream &ss,
2092  const std::string &sSymName, SubArguments &vSubArguments)
2093 {
2094  ss << "\ndouble " << sSymName;
2095  ss << "_"<< BinFuncName() <<"(";
2096  for (size_t i = 0; i < vSubArguments.size(); i++)
2097  {
2098  if (i)
2099  ss << ",";
2100  vSubArguments[i]->GenSlidingWindowDecl(ss);
2101  }
2102  ss << ")\n";
2103  ss << "{\n";
2104  ss << " int gid0=get_global_id(0);\n";
2105  ss << " double arg[2];\n";
2106  for( size_t i=0; i < vSubArguments.size(); ++i)
2107  {
2108  FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
2109  assert(tmpCur);
2110  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2111  {
2112  if(tmpCur->GetType() == formula::svDoubleVectorRef)
2113  {
2114  const formula::DoubleVectorRefToken* tmpCurDVR =
2115  static_cast<
2116  const formula::DoubleVectorRefToken *>(tmpCur);
2117  ss << " int i = 0;\n";
2118  ss << " arg["<<i<<"] = ";
2119  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2120  ss << ";\n";
2121  ss << " if(isnan(arg["<<i;
2122  ss << "])||(gid0>=";
2123  ss << tmpCurDVR->GetArrayLength();
2124  ss << "))\n";
2125  ss << " arg["<<i;
2126  ss << "] = 0;\n";
2127  }
2128  else if(tmpCur->GetType() == formula::svSingleVectorRef)
2129  {
2130  const formula::SingleVectorRefToken* tmpCurDVR=
2131  static_cast<
2132  const formula::SingleVectorRefToken *>(tmpCur);
2133  ss << " arg["<<i<<"] = ";
2134  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2135  ss << ";\n";
2136  ss << " if(isnan(arg["<<i;
2137  ss << "])||(gid0>=";
2138  ss << tmpCurDVR->GetArrayLength();
2139  ss << "))\n";
2140  ss << " arg["<<i;
2141  ss << "] = 0;\n";
2142  }
2143  else if(tmpCur->GetType() == formula::svDouble)
2144  {
2145  ss << " arg["<<i<<"] = ";
2146  ss << tmpCur->GetDouble() << ";\n";
2147  }
2148  }
2149  else
2150  {
2151  ss << " arg["<<i<<"] = ";
2152  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2153  ss << ";\n";
2154  }
2155  }
2156  ss << " double argm = arg[0];\n";
2157  ss << " int n = (int)arg[1];\n";
2158  ss << " double nn = 1.0f;\n";
2159  ss << " for(int i = 0; i < n; ++i)\n";
2160  ss << " {\n";
2161  ss << " argm = argm * 10;\n";
2162  ss << " nn = nn * 10;\n";
2163  ss << " }\n";
2164  ss << " modf(argm, &argm);\n";
2165  ss << " return argm / nn;\n";
2166  ss << "}";
2167 }
2168 void OpFloor::GenSlidingWindowFunction(
2169  std::stringstream &ss, const std::string &sSymName,
2170  SubArguments &vSubArguments)
2171 {
2172  ss << "\ndouble " << sSymName;
2173  ss << "_"<< BinFuncName() <<"(";
2174  for (size_t i = 0; i < vSubArguments.size(); i++)
2175  {
2176  if (i)
2177  ss << ",";
2178  vSubArguments[i]->GenSlidingWindowDecl(ss);
2179  }
2180  ss << ")\n{\n";
2181  ss << " int gid0=get_global_id(0);\n";
2182  ss << " double arg0,arg1,arg2=0.0;\n";
2183  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2184  ss << ";\n";
2185  ss << " arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef();
2186  ss << ";\n";
2187  if ( 3 == vSubArguments.size() )
2188  {
2189  ss << " arg2 = " << vSubArguments[2]->GenSlidingWindowDeclRef();
2190  ss << ";\n";
2191  }
2192  ss << " if(isnan(arg0) || isnan(arg1))\n";
2193  ss << " return 0;\n";
2194  ss << " if(isnan(arg2))\n";
2195  ss << " arg2 = 0.0;\n";
2196  ss << " if(arg0*arg1<0)\n";
2197  ss << " return NAN;\n";
2198  ss << " else if(arg2==0.0&&arg0<0.0)\n";
2199  ss << " return (trunc(arg0/arg1)+1)*arg1;\n";
2200  ss << " else\n";
2201  ss << " return trunc(arg0/arg1)*arg1;\n";
2202  ss << "}\n";
2203 }
2204 void OpBitOr::GenSlidingWindowFunction(std::stringstream &ss,
2205  const std::string &sSymName, SubArguments &vSubArguments)
2206 {
2207  ss << "\ndouble " << sSymName;
2208  ss << "_"<< BinFuncName() <<"(";
2209  for (size_t i = 0; i < vSubArguments.size(); i++)
2210  {
2211  if (i) ss << ",";
2212  vSubArguments[i]->GenSlidingWindowDecl(ss);
2213  }
2214  ss << ") {\n";
2215  ss << " int gid0 = get_global_id(0);\n";
2216  ss << " double num1 = " << GetBottom() << ";\n";
2217  ss << " double num2 = " << GetBottom() << ";\n";
2218  FormulaToken *iNum1 = vSubArguments[0]->GetFormulaToken();
2219  const formula::SingleVectorRefToken* tmpCurDVRNum1=
2220  static_cast<const formula::SingleVectorRefToken *>(iNum1);
2221  FormulaToken *iNum2 = vSubArguments[1]->GetFormulaToken();
2222  const formula::SingleVectorRefToken* tmpCurDVRNum2=
2223  static_cast<const formula::SingleVectorRefToken *>(iNum2);
2224  ss << " int buffer_num1_len = "<<tmpCurDVRNum1->GetArrayLength()<<";\n";
2225  ss << " int buffer_num2_len = "<<tmpCurDVRNum2->GetArrayLength()<<";\n";
2226  ss << " if((gid0)>=buffer_num1_len || isnan(";
2227  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2228  ss << " num1 = " << GetBottom() << ";\n";
2229  ss << " else \n ";
2230  ss << " num1 = floor(" << vSubArguments[0]->GenSlidingWindowDeclRef();
2231  ss << ");\n";
2232  ss << " if((gid0)>=buffer_num2_len || isnan(";
2233  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2234  ss << " num2 = " << GetBottom() << ";\n";
2235  ss << " else\n ";
2236  ss << " num2 = floor(" << vSubArguments[1]->GenSlidingWindowDeclRef();
2237  ss << ");\n";
2238  ss << " return (long)num1 | (long)num2;\n";
2239  ss << "}";
2240 }
2241 void OpBitXor::GenSlidingWindowFunction(std::stringstream &ss,
2242  const std::string &sSymName, SubArguments &vSubArguments)
2243 {
2244  ss << "\ndouble " << sSymName;
2245  ss << "_"<< BinFuncName() <<"(";
2246  for (size_t i = 0; i < vSubArguments.size(); i++)
2247  {
2248  if (i) ss << ",";
2249  vSubArguments[i]->GenSlidingWindowDecl(ss);
2250  }
2251  ss << ") {\n";
2252  ss << " int gid0 = get_global_id(0);\n";
2253  ss << " double num1 = " << GetBottom() << ";\n";
2254  ss << " double num2 = " << GetBottom() << ";\n";
2255  FormulaToken *iNum1 = vSubArguments[0]->GetFormulaToken();
2256  const formula::SingleVectorRefToken* tmpCurDVRNum1=
2257  static_cast<const formula::SingleVectorRefToken *>(iNum1);
2258  FormulaToken *iNum2 = vSubArguments[1]->GetFormulaToken();
2259  const formula::SingleVectorRefToken* tmpCurDVRNum2=
2260  static_cast<const formula::SingleVectorRefToken *>(iNum2);
2261  ss << " int buffer_num1_len = " << tmpCurDVRNum1->GetArrayLength() << ";\n";
2262  ss << " int buffer_num2_len = " << tmpCurDVRNum2->GetArrayLength() << ";\n";
2263 
2264  ss << " if((gid0)>=buffer_num1_len || isnan(";
2265  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2266  ss << " num1 = " << GetBottom() << ";\n";
2267  ss << " else\n ";
2268  ss << " num1 = floor(" << vSubArguments[0]->GenSlidingWindowDeclRef();
2269  ss << ");\n";
2270  ss << " if((gid0)>=buffer_num2_len || isnan(";
2271  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2272  ss << " num2 = " << GetBottom() << ";\n";
2273  ss << " else\n ";
2274  ss << " num2 = floor(" << vSubArguments[1]->GenSlidingWindowDeclRef();
2275  ss << ");\n";
2276  ss << " return (long)num1 ^ (long)num2;\n";
2277  ss << "}";
2278 }
2279 void OpBitLshift::GenSlidingWindowFunction(std::stringstream &ss,
2280  const std::string &sSymName, SubArguments &vSubArguments)
2281 {
2282  ss << "\ndouble " << sSymName;
2283  ss << "_"<< BinFuncName() <<"(";
2284  for (size_t i = 0; i < vSubArguments.size(); i++)
2285  {
2286  if (i) ss << ",";
2287  vSubArguments[i]->GenSlidingWindowDecl(ss);
2288  }
2289  ss << ") {\n";
2290  ss << " int gid0 = get_global_id(0);\n";
2291  ss << " double num = " << GetBottom() << ";\n";
2292  ss << " double shift_amount = " << GetBottom() << ";\n";
2293  FormulaToken *iNum = vSubArguments[0]->GetFormulaToken();
2294  const formula::SingleVectorRefToken* tmpCurDVRNum=
2295  static_cast<const formula::SingleVectorRefToken*>(iNum);
2296  FormulaToken *iShiftAmount = vSubArguments[1]->GetFormulaToken();
2297  const formula::SingleVectorRefToken* tmpCurDVRShiftAmount=
2298  static_cast<const formula::SingleVectorRefToken*>(iShiftAmount);
2299  ss << " int buffer_num_len = "<< tmpCurDVRNum->GetArrayLength()<<";\n";
2300  ss << " int buffer_shift_amount_len = ";
2301  ss << tmpCurDVRShiftAmount->GetArrayLength() << ";\n";
2302  ss << " if((gid0)>=buffer_num_len || isnan(";
2303  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2304  ss << " num = " << GetBottom() << ";\n";
2305  ss << " else\n ";
2306  ss << " num = floor(";
2307  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2308  ss << " if((gid0)>=buffer_shift_amount_len || isnan(";
2309  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2310  ss << " shift_amount = " << GetBottom() << ";\n";
2311  ss << " else\n ";
2312  ss << " shift_amount = floor(";
2313  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2314  ss << " return floor(shift_amount >= 0 ? ";
2315  ss << "num * pow(2.0, shift_amount) : ";
2316  ss << "num / pow(2.0, fabs(shift_amount)));\n";
2317  ss << "}";
2318 }
2319 void OpBitRshift::GenSlidingWindowFunction(std::stringstream &ss,
2320  const std::string &sSymName, SubArguments &vSubArguments)
2321 {
2322  ss << "\ndouble " << sSymName;
2323  ss << "_"<< BinFuncName() <<"(";
2324  for (size_t i = 0; i < vSubArguments.size(); i++)
2325  {
2326  if (i) ss << ",";
2327  vSubArguments[i]->GenSlidingWindowDecl(ss);
2328  }
2329  ss << ") {\n";
2330  ss << " int gid0 = get_global_id(0);\n";
2331  ss << " double num = " << GetBottom() << ";\n";
2332  ss << " double shift_amount = " << GetBottom() << ";\n";
2333  FormulaToken *iNum = vSubArguments[0]->GetFormulaToken();
2334  const formula::SingleVectorRefToken* tmpCurDVRNum=
2335  static_cast<const formula::SingleVectorRefToken*>(iNum);
2336  FormulaToken *iShiftAmount = vSubArguments[1]->GetFormulaToken();
2337  const formula::SingleVectorRefToken* tmpCurDVRShiftAmount=
2338  static_cast<const formula::SingleVectorRefToken*>(iShiftAmount);
2339  ss << " int buffer_num_len = ";
2340  ss << tmpCurDVRNum->GetArrayLength() << ";\n";
2341  ss << " int buffer_shift_amount_len = ";
2342  ss << tmpCurDVRShiftAmount->GetArrayLength() << ";\n";
2343 
2344  ss << " if((gid0)>=buffer_num_len || isnan(";
2345  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2346  ss << " num = " << GetBottom() << ";\n";
2347  ss << " else\n ";
2348  ss << " num = floor(";
2349  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2350  ss << " if((gid0)>=buffer_shift_amount_len || isnan(";
2351  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2352  ss << " shift_amount = " <<GetBottom()<< ";\n";
2353  ss << " else\n ";
2354  ss << " shift_amount = floor(";
2355  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2356  ss << " return floor(";
2357  ss << "shift_amount >= 0 ? num / pow(2.0, shift_amount) : ";
2358  ss << "num * pow(2.0, fabs(shift_amount)));\n";
2359  ss << "}";
2360 }
2361 void OpSumSQ::GenSlidingWindowFunction(std::stringstream &ss,
2362  const std::string &sSymName, SubArguments &vSubArguments)
2363 {
2364  ss << "\ndouble " << sSymName;
2365  ss << "_"<< BinFuncName() <<"(";
2366  for (size_t i = 0; i < vSubArguments.size(); ++i)
2367  {
2368  if (i)
2369  ss << ",";
2370  vSubArguments[i]->GenSlidingWindowDecl(ss);
2371  }
2372  ss << ")\n";
2373  ss << "{\n";
2374  ss << " int gid0=get_global_id(0);\n";
2375  ss << " double sum = 0.0f, arg;\n";
2376  for(const DynamicKernelArgumentRef & rArg : vSubArguments)
2377  {
2378  FormulaToken *tmpCur = rArg->GetFormulaToken();
2379  assert(tmpCur);
2380  if(ocPush == rArg->GetFormulaToken()->GetOpCode())
2381  {
2382  if (tmpCur->GetType() == formula::svDoubleVectorRef)
2383  {
2384  const formula::DoubleVectorRefToken* pDVR =
2385  static_cast<const formula::DoubleVectorRefToken *>(tmpCur);
2386  size_t nCurWindowSize = pDVR->GetRefRowSize();
2387  ss << " for (int i = ";
2388  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2389  {
2390  ss << "gid0; i < " << pDVR->GetArrayLength();
2391  ss << " && i < " << nCurWindowSize << "; ++i)\n";
2392  ss << " {\n";
2393  }
2394  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2395  {
2396  ss << "0; i < " << pDVR->GetArrayLength();
2397  ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
2398  ss << " {\n";
2399  }
2400  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2401  {
2402  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2403  ss << " && i < "<< nCurWindowSize << "; ++i)\n";
2404  ss << " {\n";
2405  }
2406  else
2407  {
2408  ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2409  ss << " {\n";
2410  }
2411  ss << " arg = ";
2412  ss << rArg->GenSlidingWindowDeclRef();
2413  ss << ";\n";
2414  ss << " if (isnan(arg))\n";
2415  ss << " continue;\n";
2416  ss << " sum += pown(arg, 2);\n";
2417  ss << " }\n";
2418  }
2419  else if(tmpCur->GetType() == formula::svSingleVectorRef)
2420  {
2421  const formula::SingleVectorRefToken* tmpCurDVR=
2422  static_cast<
2423  const formula::SingleVectorRefToken *>(tmpCur);
2424  ss << " arg = ";
2425  ss << rArg->GenSlidingWindowDeclRef();
2426  ss << ";\n";
2427  ss << " if(isnan(arg)||(gid0>=";
2428  ss << tmpCurDVR->GetArrayLength();
2429  ss << "))\n";
2430  ss << " arg = 0.0f;\n";
2431  ss << " sum += pown(arg, 2);\n";
2432  }
2433  else if(tmpCur->GetType() == formula::svDouble)
2434  {
2435  ss << " arg = ";
2436  ss << tmpCur->GetDouble() << ";\n";
2437  ss << " sum += pown(arg, 2);\n";
2438  }
2439  }
2440  else
2441  {
2442  ss << " arg = ";
2443  ss << rArg->GenSlidingWindowDeclRef();
2444  ss << ";\n";
2445  ss << " sum += pown(arg, 2);\n";
2446  }
2447  }
2448  ss << " return sum;\n";
2449  ss << "}";
2450 }
2451 void OpSqrtPi::GenSlidingWindowFunction(std::stringstream &ss,
2452  const std::string &sSymName, SubArguments &vSubArguments)
2453 {
2454  ss << "\ndouble " << sSymName;
2455  ss << "_"<< BinFuncName() <<"(";
2456  for (size_t i = 0; i < vSubArguments.size(); i++)
2457  {
2458  if (i)
2459  ss << ",";
2460  vSubArguments[i]->GenSlidingWindowDecl(ss);
2461  }
2462  ss << ")\n";
2463  ss << "{\n";
2464  ss << " int gid0=get_global_id(0);\n";
2465  ss << " double arg0 = 0.0f;\n";
2466  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2467  assert(tmpCur);
2468  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2469  {
2470  if(tmpCur->GetType() == formula::svSingleVectorRef)
2471  {
2472  const formula::SingleVectorRefToken*tmpCurDVR=
2473  static_cast
2474  <const formula::SingleVectorRefToken *>(tmpCur);
2475  ss << " arg0 = ";
2476  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2477  ss << ";\n";
2478  ss << " if(isnan(";
2479  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2480  ss << ")||(gid0>=";
2481  ss << tmpCurDVR->GetArrayLength();
2482  ss << "))\n";
2483  ss << " { arg0 = 0.0f; }\n";
2484  }
2485  else if(tmpCur->GetType() == formula::svDouble)
2486  {
2487  ss << " arg0=";
2488  ss << tmpCur->GetDouble() << ";\n";
2489  }
2490  }
2491  else
2492  {
2493  ss << " arg0 = ";
2494  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2495  ss << ";\n";
2496  }
2497  ss << " return (double)sqrt(arg0 *";
2498  ss << " 3.1415926535897932);\n";
2499  ss << "}";
2500 }
2501 void OpCeil::GenSlidingWindowFunction(std::stringstream &ss,
2502  const std::string &sSymName, SubArguments &vSubArguments)
2503 {
2504  ss << "\ndouble " << sSymName;
2505  ss << "_"<< BinFuncName() <<"(";
2506  for (size_t i = 0; i < vSubArguments.size(); i++)
2507  {
2508  if (i) ss << ",";
2509  vSubArguments[i]->GenSlidingWindowDecl(ss);
2510  }
2511  ss << ") {\n";
2512  ss << " int gid0 = get_global_id(0);\n";
2513  ss << " double num = " << GetBottom() << ";\n";
2514  ss << " double significance = " << GetBottom() << ";\n";
2515  ss << " double bAbs = 0;\n";
2516  ss << " if(isnan(";
2517  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2518  ss << " num = " << GetBottom() << ";\n";
2519  ss << " else\n ";
2520  ss << " num = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2521  ss << " if(isnan(";
2522  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2523  ss << " return 0.0;\n";
2524  ss << " else\n ";
2525  ss << " significance = ";
2526  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2527  if (vSubArguments.size() > 2)
2528  {
2529  FormulaToken *bAbs = vSubArguments[2]->GetFormulaToken();
2530  if(bAbs->GetType() == formula::svSingleVectorRef)
2531  {
2532  const formula::SingleVectorRefToken* tmpCurSVRIsAbs=
2533  static_cast<const formula::SingleVectorRefToken*>(bAbs);
2534  ss<< " if((gid0)>=" << tmpCurSVRIsAbs->GetArrayLength() << " ||";
2535  }
2536  if(bAbs->GetType() == formula::svDoubleVectorRef)
2537  {
2538  const formula::DoubleVectorRefToken* tmpCurDVRIsAbs=
2539  static_cast<const formula::DoubleVectorRefToken*>(bAbs);
2540  ss<< " if((gid0)>=" << tmpCurDVRIsAbs->GetArrayLength() << " ||";
2541  }
2542  if(bAbs->GetType() == formula::svDouble)
2543  {
2544  ss<< " if(";
2545  }
2546  ss << "isnan(";
2547  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << "))\n";
2548  ss << " bAbs = 0;\n";
2549  ss << " else\n ";
2550  ss << " bAbs = "<<vSubArguments[2]->GenSlidingWindowDeclRef()<<";\n";
2551  }
2552  ss << " if(significance == 0.0)\n";
2553  ss << " return 0.0;\n";
2554  ss << " return ";
2555  ss << "( !(int)bAbs && num < 0.0 ? floor( num / significance ) : ";
2556  ss << "ceil( num / significance ) )";
2557  ss << "*significance;\n";
2558  ss << "}";
2559 }
2560 void OpCombin::GenSlidingWindowFunction(std::stringstream &ss,
2561  const std::string &sSymName, SubArguments &vSubArguments)
2562 {
2563  ss << "\ndouble " << sSymName;
2564  ss << "_"<< BinFuncName() <<"(";
2565  for (size_t i = 0; i < vSubArguments.size(); i++)
2566  {
2567  if (i) ss << ",";
2568  vSubArguments[i]->GenSlidingWindowDecl(ss);
2569  }
2570  ss << ") {\n";
2571  ss << " int gid0 = get_global_id(0);\n";
2572  ss << " double num = " << GetBottom() << ";\n";
2573  ss << " double num_chosen = " << GetBottom() << ";\n";
2574  ss << " double result = -1.0;\n";
2575  FormulaToken *iNum = vSubArguments[0]->GetFormulaToken();
2576  FormulaToken *iNumChosen = vSubArguments[1]->GetFormulaToken();
2577 
2578  assert(iNum);
2579  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2580  {
2581  if(iNum->GetType() == formula::svSingleVectorRef &&
2582  iNumChosen->GetType() == formula::svSingleVectorRef)
2583  {
2584  ss << " if(isnan(";
2585  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2586  ss << " num = " << GetBottom() << ";\n";
2587  ss << " else\n ";
2588  ss << " num = floor(";
2589  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2590  ss << " if(isnan(";
2591  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2592  ss << " num_chosen = " << GetBottom() << ";\n";
2593  ss << " else\n ";
2594  ss << " num_chosen = floor(";
2595  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2596  }
2597  else if(iNum->GetType() == formula::svDouble &&
2598  iNumChosen->GetType() == formula::svDouble)
2599  {
2600  ss << " num = floor(" << iNum->GetDouble() << ");\n";
2601  ss << " num_chosen = floor("<< iNumChosen->GetDouble()<< ");\n";
2602  }
2603  }
2604  else
2605  {
2606  ss << " num = floor(";
2607  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2608  ss << " num_chosen = floor(";
2609  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2610  }
2611  ss << " result = select(result, 0.0, (ulong)(num < num_chosen));\n";
2612  ss << " result = select(result, 1.0, (ulong)(num_chosen == 0.0));\n";
2613  ss << " if(result == 0 || result ==1)\n";
2614  ss << " return result;\n";
2615  ss << " double4 db4num;\n";
2616  ss << " double4 db4num_chosen;\n";
2617  ss << " double4 db4result;\n";
2618  ss << " double2 db2result;\n";
2619  ss << " result = 1.0;\n";
2620  ss << " int loop = num_chosen/4;\n";
2621  ss << " for(int i=0; i<loop; i++)\n";
2622  ss << " {\n";
2623  ss << " db4num = (double4){num,\n";
2624  ss << " num-1.0,\n";
2625  ss << " num-2.0,\n";
2626  ss << " num-3.0};\n";
2627  ss << " db4num_chosen = (double4){num_chosen,\n";
2628  ss << " num_chosen-1.0,\n";
2629  ss << " num_chosen-2.0,\n";
2630  ss << " num_chosen-3.0};\n";
2631  ss << " db4result = db4num * pown(db4num_chosen, -1);\n";
2632  ss << " db2result = db4result.xy * db4result.zw;\n";
2633  ss << " result *= db2result.x * db2result.y;\n";
2634  ss << " num = num - 4.0;\n";
2635  ss << " num_chosen = num_chosen - 4.0;\n";
2636  ss << " }\n";
2637  ss << " while ( num_chosen > 0){\n";
2638  ss << " result *= num / num_chosen;\n";
2639  ss << " num = num - 1.0;\n";
2640  ss << " num_chosen = num_chosen - 1.0;\n";
2641  ss << " }\n";
2642  ss << " return result;\n";
2643  ss << "}\n";
2644 }
2645 void OpConvert::GenSlidingWindowFunction(
2646  std::stringstream &ss, const std::string &sSymName,
2647  SubArguments &vSubArguments)
2648 {
2649  // The CONVERT function converts a value from one unit of
2650  // measurement to another. It takes the units of measurements to
2651  // convert between as string arguments. This implementation
2652  // handles just a very small subset of such conversions.
2653 
2654  int arg1=vSubArguments[1]->GetFormulaToken()->GetString().
2655  getString().toAsciiUpperCase().hashCode();
2656  int arg2=vSubArguments[2]->GetFormulaToken()->GetString().
2657  getString().toAsciiUpperCase().hashCode();
2658 
2659  // Check if the from and to units are those combinations that the
2660  // code below supports.
2661  if( !((arg1==5584&&arg2==108)||
2662  (arg1==108&&arg2==5584)||
2663  (arg1==5665&&arg2==268206)||
2664  (arg1==268206&&arg2==5665)) )
2665  throw Unhandled(__FILE__, __LINE__);
2666 
2667  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2668  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
2669  formula::SingleVectorRefToken *>(tmpCur);
2670  ss << "\ndouble " << sSymName;
2671  ss << "_"<< BinFuncName() <<"(";
2672  for (size_t i = 0; i < vSubArguments.size(); i++)
2673  {
2674  if (i)
2675  ss << ",";
2676  vSubArguments[i]->GenSlidingWindowDecl(ss);
2677  }
2678  ss << ")\n{\n";
2679  ss <<" int gid0=get_global_id(0);\n";
2680  ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2681  ss << ";\n";
2682  ss << " double arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef();
2683  ss << ";\n";
2684  ss << " double arg2 = " << vSubArguments[2]->GenSlidingWindowDeclRef();
2685  ss << ";\n";
2686  ss<< " if(isnan(arg0)||(gid0>=";
2687  ss<<tmpCurDVR->GetArrayLength();
2688  ss<<"))\n";
2689  ss<<" arg0 = 0;\n";
2690  ss<< " if(isnan(arg1)||(gid0>=";
2691  ss<<tmpCurDVR->GetArrayLength();
2692  ss<<"))\n";
2693  ss<<" arg1 = 0;\n";
2694  ss<< " if(isnan(arg2)||(gid0>=";
2695  ss<<tmpCurDVR->GetArrayLength();
2696  ss<<"))\n";
2697  ss<<" arg2 = 0;\n";
2698  ss<<" if(arg1==5584U&&arg2==108U)\n";
2699  ss<<" return arg0*1000.0;\n";
2700  ss<<" else if(arg1==108U&&arg2==3385U)\n";
2701  ss<<" return arg0/1000.0;\n";
2702  ss<<" else if(arg1==5665U&&arg2==268206U)\n";
2703  ss<<" return arg0*60.0;\n";
2704  ss<<" else if(arg1==268206U&&arg2==5665U)\n";
2705  ss<<" return arg0/60.0;\n";
2706  ss<<" else\n";
2707  ss<<" return -9999999999;\n";
2708  ss << "}\n";
2709 
2710 }
2711 
2712 void OpProduct::GenSlidingWindowFunction(std::stringstream &ss,
2713  const std::string &sSymName, SubArguments &vSubArguments)
2714 {
2715  ss << "\ndouble " << sSymName;
2716  ss << "_"<< BinFuncName() <<"( ";
2717  for (size_t i = 0; i < vSubArguments.size(); i++)
2718  {
2719  if (i)
2720  ss << ",";
2721  vSubArguments[i]->GenSlidingWindowDecl(ss);
2722  }
2723  ss << ") {\n";
2724  ss << " int gid0 = get_global_id(0);\n";
2725  ss << " int i = 0;\n";
2726  ss << " double product=1.0;\n";
2727  ss << " int count = 0;\n\n";
2728  for (DynamicKernelArgumentRef & rArg : vSubArguments)
2729  {
2730  FormulaToken *pCur = rArg->GetFormulaToken();
2731  assert(pCur);
2732  if (pCur->GetType() == formula::svDoubleVectorRef)
2733  {
2734  const formula::DoubleVectorRefToken* pDVR =
2735  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2736  size_t nCurWindowSize = pDVR->GetRefRowSize();
2737 
2738  ss << " for (int i = ";
2739  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2740  {
2741  ss << "gid0; i < " << pDVR->GetArrayLength();
2742  ss << " && i < " << nCurWindowSize << "; i++)\n";
2743  ss << " {\n";
2744  }
2745  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2746  {
2747  ss << "0; i < " << pDVR->GetArrayLength();
2748  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2749  ss << " {\n";
2750  }
2751  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2752  {
2753  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2754  ss << " && i < " << nCurWindowSize << "; i++)\n";
2755  ss << " {\n";
2756  }
2757  else if (pDVR->IsStartFixed() && pDVR->IsEndFixed())
2758  {
2759 
2760  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2761  ss << " {\n";
2762  }
2763  ss << " if(!isnan("<<rArg->GenSlidingWindowDeclRef()<<"))\n";
2764  ss << " {\n";
2765  ss << " product = product*";
2766  ss << rArg->GenSlidingWindowDeclRef()<<";\n";
2767  ss << " ++count;\n";
2768  ss << " }\n";
2769  ss << " }\n";
2770  }
2771  else if (pCur->GetType() == formula::svSingleVectorRef)
2772  {
2773  ss << " if(!isnan("<<rArg->GenSlidingWindowDeclRef()<<"))\n";
2774  ss << " {\n";
2775  ss << " product = product*";
2776  ss << rArg->GenSlidingWindowDeclRef()<<";\n";
2777  ss << " ++count;\n";
2778  ss << " }\n";
2779  }
2780  else
2781  {
2782  ss << " if(!isnan("<<rArg->GenSlidingWindowDeclRef()<<"))\n";
2783  ss << " {\n";
2784  ss << " product = product*";
2785  ss << rArg->GenSlidingWindowDeclRef()<<";\n";
2786  ss << " ++count;\n";
2787  ss << " }\n";
2788  }
2789  }
2790  ss << " if(count == 0)\n";
2791  ss << " return 0;\n";
2792  ss << " return product;\n";
2793  ss << "}";
2794 }
2795 void OpAverageIf::GenSlidingWindowFunction(std::stringstream &ss,
2796  const std::string &sSymName, SubArguments &vSubArguments)
2797 {
2798 
2799  ss << "\ndouble " << sSymName;
2800  ss << "_"<< BinFuncName() <<"(";
2801  for (size_t i = 0; i < vSubArguments.size(); i++)
2802  {
2803  if (i) ss << ",";
2804  vSubArguments[i]->GenSlidingWindowDecl(ss);
2805  }
2806  ss << ")\n{\n";
2807  ss << " int gid0=get_global_id(0);\n";
2808  ss << " double tmp =0;\n";
2809  ss << " double count=0;\n";
2810  ss << " int singleIndex =gid0;\n";
2811  ss << " int doubleIndex;\n";
2812  ss << " int i ;\n";
2813  ss << " int j ;\n";
2814  GenTmpVariables(ss,vSubArguments);
2815 
2816  unsigned paraOneIsDoubleVector = 0;
2817  unsigned paraOneWidth = 1;
2818  unsigned paraTwoWidth = 1;
2819  unsigned loopTimes = 0;
2820 
2821  if(vSubArguments[0]->GetFormulaToken()->GetType() == formula::svDoubleVectorRef)
2822  {
2823  paraOneIsDoubleVector = 1;
2824  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2825  const formula::DoubleVectorRefToken*pCurDVR0= static_cast<const
2826  formula::DoubleVectorRefToken *>(tmpCur0);
2827  paraOneWidth = pCurDVR0->GetArrays().size();
2828  loopTimes = paraOneWidth;
2829  if(paraOneWidth > 1)
2830  {
2831  throw Unhandled(__FILE__, __LINE__);
2832  }
2833  }
2834 
2835  if(vSubArguments[paraOneWidth]->GetFormulaToken()->GetType() ==
2837 
2838  {
2839  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2840  const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
2841  formula::DoubleVectorRefToken *>(tmpCur1);
2842  paraTwoWidth = pCurDVR1->GetArrays().size();
2843  if(paraTwoWidth > 1)
2844  {
2845  throw Unhandled(__FILE__, __LINE__);
2846  }
2847  ss << " i = ";
2848  if (!pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed()) {
2849  ss << "gid0;\n";
2850  } else {
2851  ss << "0;\n";
2852  }
2853  if(!pCurDVR1->IsStartFixed() && !pCurDVR1->IsEndFixed())
2854  {
2855  ss << " doubleIndex =i+gid0;\n";
2856  }else
2857  {
2858  ss << " doubleIndex =i;\n";
2859  }
2860  }
2861 
2862  CheckSubArgumentIsNan(ss,vSubArguments,paraOneWidth);
2863 
2864  unsigned paraThreeIndex = paraOneWidth + paraTwoWidth;
2865  if(vSubArguments.size() > paraThreeIndex)
2866  {
2867  if(vSubArguments[paraThreeIndex]->GetFormulaToken()->GetType() ==
2869  {
2870  FormulaToken *tmpCur2 =
2871  vSubArguments[paraThreeIndex]->GetFormulaToken();
2872  const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
2873  formula::DoubleVectorRefToken *>(tmpCur2);
2874  unsigned paraThreeWidth = pCurDVR2->GetArrays().size();
2875  if(paraThreeWidth > 1)
2876  {
2877  throw Unhandled(__FILE__, __LINE__);
2878  }
2879  }
2880  }
2881 
2882  if(paraOneIsDoubleVector)
2883  {
2884  unsigned loopIndex = 0;
2885  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2886  const formula::DoubleVectorRefToken*pCurDVR0= static_cast<const
2887  formula::DoubleVectorRefToken *>(tmpCur0);
2888  size_t nCurWindowSize = pCurDVR0->GetArrayLength() <
2889  pCurDVR0->GetRefRowSize() ? pCurDVR0->GetArrayLength():
2890  pCurDVR0->GetRefRowSize() ;
2891 
2892  for(loopIndex =0; loopIndex < loopTimes; loopIndex++)
2893  {
2894  ss << " for (i = ";
2895  if (!pCurDVR0->IsStartFixed() && pCurDVR0->IsEndFixed()) {
2896  ss << "gid0; i < "<< nCurWindowSize <<"; i++)\n";
2897  } else if (pCurDVR0->IsStartFixed() && !pCurDVR0->IsEndFixed()) {
2898  ss << "0; i < gid0+"<< nCurWindowSize <<"; i++)\n";
2899  } else {
2900  ss << "0; i < "<< nCurWindowSize <<"; i++)\n";
2901  }
2902  ss << " {\n";
2903  if(!pCurDVR0->IsStartFixed() && !pCurDVR0->IsEndFixed())
2904  {
2905  ss << " doubleIndex =i+gid0;\n";
2906  }else
2907  {
2908  ss << " doubleIndex =i;\n";
2909  }
2910 
2911  CheckSubArgumentIsNan(ss,vSubArguments, loopIndex);
2912 
2913  ss << " if ( isequal( tmp";
2914  ss << loopIndex<<" , tmp"<<paraOneWidth<<") ) \n";
2915  ss << " {\n";
2916  if(vSubArguments.size() == paraThreeIndex)
2917  ss << " tmp += tmp"<<loopIndex<<";\n";
2918  else
2919  {
2920  CheckSubArgumentIsNan(ss,vSubArguments,
2921  paraThreeIndex+loopIndex);
2922  ss << " tmp += tmp";
2923  ss << paraThreeIndex+loopIndex<<";\n";
2924  }
2925  ss << " count+=1.0;\n";
2926  ss << " }\n";
2927  ss << " }\n";
2928  }
2929  }
2930  else
2931  {
2932  CheckSubArgumentIsNan(ss,vSubArguments, 0);
2933  ss << " if ( isequal( tmp0 , tmp1 ) ) \n";
2934  ss << " {\n";
2935  if(vSubArguments.size() == 2)
2936  ss << " tmp += tmp0;\n";
2937  else
2938  {
2939  CheckSubArgumentIsNan(ss,vSubArguments,2);
2940  ss << " tmp += tmp2;\n";
2941  }
2942  ss << " count+=1.0;\n";
2943  ss << " }\n";
2944  }
2945 
2946  ss << " if(count!=0)\n";
2947  ss << " tmp=tmp/count;\n";
2948  ss << " else\n";
2949  ss << " tmp= 0 ;\n";
2950  ss << " return tmp;\n";
2951  ss << "}";
2952 }
2953 void OpDeg::GenSlidingWindowFunction(std::stringstream &ss,
2954  const std::string &sSymName, SubArguments &vSubArguments)
2955 {
2956  ss << "\ndouble " << sSymName;
2957  ss << "_"<< BinFuncName() <<"(";
2958  for (size_t i = 0; i < vSubArguments.size(); i++)
2959  {
2960  if (i)
2961  ss << ",";
2962  vSubArguments[i]->GenSlidingWindowDecl(ss);
2963  }
2964  ss << ")\n";
2965  ss << "{\n";
2966  ss << " int gid0=get_global_id(0);\n";
2967  ss << " double arg0 = 0.0f;\n";
2968  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2969  assert(tmpCur);
2970  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2971  {
2972  if(tmpCur->GetType() == formula::svSingleVectorRef)
2973  {
2974  const formula::SingleVectorRefToken*tmpCurDVR=
2975  static_cast
2976  <const formula::SingleVectorRefToken *>(tmpCur);
2977  ss << " arg0 = ";
2978  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2979  ss << ";\n";
2980  ss << " if(isnan(";
2981  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2982  ss << ")||(gid0>=";
2983  ss << tmpCurDVR->GetArrayLength();
2984  ss << "))\n";
2985  ss << " { arg0 = 0.0f; }\n";
2986  }
2987  else if(tmpCur->GetType() == formula::svDouble)
2988  {
2989  ss << " arg0=";
2990  ss << tmpCur->GetDouble() << ";\n";
2991  }
2992  }
2993  else
2994  {
2995  ss << " arg0 = ";
2996  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2997  ss << ";\n";
2998  }
2999  ss << " return arg0 * pown(M_PI, -1) * 180;;\n";
3000  ss << "}";
3001 }
3002 
3003 void OpFact::GenSlidingWindowFunction(std::stringstream& ss,
3004  const std::string &sSymName, SubArguments& vSubArguments)
3005 {
3006  ss << "\ndouble " << sSymName;
3007  ss << "_" << BinFuncName() << "(";
3008  for (size_t i = 0; i < vSubArguments.size(); i++)
3009  {
3010  if (i)
3011  ss << ",";
3012  vSubArguments[i]->GenSlidingWindowDecl(ss);
3013  }
3014  ss << ") {\n";
3015  ss << " double tmp = " << GetBottom() << ";\n";
3016  ss << " int gid0 = get_global_id(0);\n";
3017  ss << " double arg0 = " << GetBottom() << ";\n";
3018  FormulaToken* pCur = vSubArguments[0]->GetFormulaToken();
3019  assert(pCur);
3020  if (pCur->GetType() == formula::svSingleVectorRef)
3021  {
3022  const formula::SingleVectorRefToken* pSVR =
3023  static_cast< const formula::SingleVectorRefToken* >(pCur);
3024  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3025  }
3026  else if (pCur->GetType() == formula::svDouble)
3027  {
3028  ss << " {\n";
3029  }
3030  if(ocPush==vSubArguments[0]->GetFormulaToken()->GetOpCode())
3031  {
3032  ss << " if (isnan(";
3033  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3034  ss << "))\n";
3035  ss << " arg0 = 0;\n";
3036  ss << " else\n";
3037  ss << " arg0 = ";
3038  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3039  ss << " arg0 = floor(arg0);\n";
3040  ss << " if (arg0 < 0.0)\n";
3041  ss << " return 0.0;\n";
3042  ss << " else if (arg0 == 0.0)\n";
3043  ss << " return 1.0;\n";
3044  ss << " else if (arg0 <= 170.0)\n";
3045  ss << " {\n";
3046  ss << " double fTemp = arg0;\n";
3047  ss << " while (fTemp > 2.0)\n";
3048  ss << " {\n";
3049  ss << " fTemp = fTemp - 1;\n";
3050  ss << " arg0 = arg0 * fTemp;\n";
3051  ss << " }\n";
3052  ss << " }\n";
3053  ss << " else\n";
3054  ss << " return -DBL_MAX;\n";
3055  ss << " }\n";
3056  }
3057  else
3058  {
3059  ss << " arg0 = ";
3060  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3061  }
3062  ss << " return arg0;\n";
3063  ss << "}";
3064 }
3065 void OpQuotient::GenSlidingWindowFunction(std::stringstream &ss,
3066  const std::string &sSymName, SubArguments &vSubArguments)
3067 {
3068  ss << "\ndouble " << sSymName;
3069  ss << "_"<< BinFuncName() <<"(";
3070  for (size_t i = 0; i < vSubArguments.size(); i++)
3071  {
3072  if (i) ss << ",";
3073  vSubArguments[i]->GenSlidingWindowDecl(ss);
3074  }
3075  ss << ") {\n";
3076  ss << " int gid0 = get_global_id(0);\n";
3077  ss << " double num1 = 1.0;\n";
3078  ss << " double num2 = 1.0;\n";
3079  ss << " if(isnan(";
3080  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
3081  ss << " num1 = 1.0;\n";
3082  ss << " else \n ";
3083  ss << " num1 = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3084  ss << " if(isnan(";
3085  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
3086  ss << " num2 = 1.0;\n";
3087  ss << " else \n ";
3088  ss << " num2 = " << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
3089  ss << " return trunc(num1/num2);\n";
3090  ss << "}";
3091 }
3092 void OpSeriesSum::GenSlidingWindowFunction(std::stringstream &ss,
3093  const std::string &sSymName, SubArguments &vSubArguments)
3094 {
3095  CHECK_PARAMETER_COUNT(4,4);
3096  ss << "\ndouble " << sSymName;
3097  ss << "_"<< BinFuncName() <<"(";
3098  for (size_t i = 0; i < vSubArguments.size(); i++)
3099  {
3100  if (i)
3101  ss << ",";
3102  vSubArguments[i]->GenSlidingWindowDecl(ss);
3103  }
3104  ss << ")\n";
3105  ss << "{\n";
3106  ss << " int gid0=get_global_id(0);\n";
3107  ss << " double var[3], coeff, res = 0.0f;\n";
3108  FormulaToken *tmpCur;
3109  for(int i = 0; i < 3; ++i)
3110  {
3111  tmpCur = vSubArguments[i]->GetFormulaToken();
3112  assert(tmpCur);
3113  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
3114  {
3115  if(tmpCur->GetType() == formula::svSingleVectorRef)
3116  {
3117  const formula::SingleVectorRefToken* tmpCurDVR=
3118  static_cast<
3119  const formula::SingleVectorRefToken *>(tmpCur);
3120  ss << " var["<<i<<"] = ";
3121  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3122  ss << ";\n";
3123  ss << " if(isnan(var["<<i<<"])||(gid0>=";
3124  ss << tmpCurDVR->GetArrayLength();
3125  ss << "))\n";
3126  ss << " var["<<i<<"] = 0;\n";
3127  }
3128  else if(tmpCur->GetType() == formula::svDouble)
3129  {
3130  ss << " var["<<i<<"] = ";
3131  ss << tmpCur->GetDouble() << ";\n";
3132  }
3133  }
3134  else
3135  {
3136  ss << " var["<<i<<"] = ";
3137  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3138  ss << ";\n";
3139  }
3140  }
3141  tmpCur = vSubArguments[3]->GetFormulaToken();
3142  assert(tmpCur);
3143  if(ocPush == vSubArguments[3]->GetFormulaToken()->GetOpCode())
3144  {
3145  //TODO DoubleVector
3146  if (tmpCur->GetType() == formula::svDoubleVectorRef)
3147  {
3148  const formula::DoubleVectorRefToken* pDVR =
3149  static_cast<const formula::DoubleVectorRefToken *>(tmpCur);
3150  size_t nCurWindowSize = pDVR->GetRefRowSize();
3151  ss << " int j = 0;\n";
3152  ss << " for (int i = ";
3153  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
3154  {
3155  ss << "gid0; i < " << pDVR->GetArrayLength();
3156  ss << " && i < " << nCurWindowSize << "; ++i)\n";
3157  ss << " {\n";
3158  }
3159  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
3160  {
3161  ss << "0; i < " << pDVR->GetArrayLength();
3162  ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
3163  ss << " {\n";
3164  }
3165  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
3166  {
3167  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3168  ss << " && i < "<< nCurWindowSize << "; ++i)\n";
3169  ss << " {\n";
3170  }
3171  else
3172  {
3173  ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
3174  ss << " {\n";
3175  }
3176  ss << " coeff = ";
3177  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3178  ss << ";\n";
3179  ss << " if (isnan(coeff))\n";
3180  ss << " continue;\n";
3181  ss << " res = res + coeff * pow(var[0],";
3182  ss << " var[1] + j * var[2]);\n";
3183  ss << " ++j;\n";
3184  ss << " }\n";
3185  }
3186  else if(tmpCur->GetType() == formula::svSingleVectorRef)
3187  {
3188  const formula::SingleVectorRefToken* tmpCurDVR=
3189  static_cast<
3190  const formula::SingleVectorRefToken *>(tmpCur);
3191  ss << " coeff = ";
3192  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3193  ss << ";\n";
3194  ss << " if(isnan(coeff)||(gid0>=";
3195  ss << tmpCurDVR->GetArrayLength();
3196  ss << "))\n";
3197  ss << " return 0;\n";
3198  }
3199  else
3200  throw Unhandled(__FILE__, __LINE__);
3201  }
3202  ss << " return res;\n";
3203  ss << "}";
3204 }
3205 }
3206 
3207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::string Math_Intg_Str
std::shared_ptr< DynamicKernelArgument > DynamicKernelArgumentRef
Definition: opbase.hxx:152
std::string local_coth
Inconsistent state.
Definition: opbase.hxx:54
std::string atan2Content
sal_Int64 n
const std::vector< VectorRefArray > & GetArrays() const
std::string bik
virtual double GetDouble() const
std::string local_cosh
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
std::string atan2Decl
int i
ocPush
std::vector< DynamicKernelArgumentRef > SubArguments
Definition: opbase.hxx:215
std::string local_cothDecl
Arguments that are actually compile-time constant string Currently, only the hash is passed...
#define CHECK_PARAMETER_COUNT(min, max)
Definition: opbase.hxx:75
std::string bikDecl
size_t GetArrayLength() const
size_t GetArrayLength() const
tuple m
OUString getString(const Any &_rAny)
std::string local_coshDecl
StackVar GetType() const