Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / opencl / op_math.cxx
blob9b7dca065a6d055da1f0ec9d94830872d2fe9d12
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "op_math.hxx"
12 #include "formulagroup.hxx"
13 #include "document.hxx"
14 #include "formulacell.hxx"
15 #include "tokenarray.hxx"
16 #include "compiler.hxx"
17 #include "interpre.hxx"
18 #include "formula/vectortoken.hxx"
19 #include "opinlinefun_math.hxx"
20 #include <sstream>
22 using namespace formula;
24 namespace sc { namespace opencl {
26 void OpCos::GenSlidingWindowFunction(std::stringstream &ss,
27 const std::string sSymName, SubArguments &vSubArguments)
29 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
30 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
31 formula::SingleVectorRefToken *>(tmpCur);
32 ss << "\ndouble " << sSymName;
33 ss << "_"<< BinFuncName() <<"(";
34 for (unsigned i = 0; i < vSubArguments.size(); i++)
36 if (i)
37 ss << ",";
38 vSubArguments[i]->GenSlidingWindowDecl(ss);
40 ss << ") {\n\t";
41 ss <<"int gid0=get_global_id(0);\n\t";
42 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
43 ss << ";\n\t";
44 #ifdef ISNAN
45 ss<< "if(isNan(arg0)||(gid0>=";
46 ss<<tmpCurDVR->GetArrayLength();
47 ss<<"))\n\t\t";
48 ss<<"arg0 = 0;\n\t";
49 #endif
50 ss << "double tmp=cos(arg0);\n\t";
51 ss << "return tmp;\n";
52 ss << "}";
54 void OpSec::GenSlidingWindowFunction(std::stringstream &ss,
55 const std::string sSymName, SubArguments &vSubArguments)
57 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
58 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
59 formula::SingleVectorRefToken *>(tmpCur);
60 ss << "\ndouble " << sSymName;
61 ss << "_"<< BinFuncName() <<"(";
62 for (unsigned i = 0; i < vSubArguments.size(); i++)
64 if (i)
65 ss << ",";
66 vSubArguments[i]->GenSlidingWindowDecl(ss);
68 ss << ") {\n";
69 ss <<" int gid0=get_global_id(0);\n";
70 ss <<" double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
71 ss <<";\n";
72 #ifdef ISNAN
73 ss<<" if(isNan(arg0)||(gid0>=";
74 ss<<tmpCurDVR->GetArrayLength();
75 ss<<"))\n";
76 ss<<" arg0 = 0;\n";
77 #endif
78 ss << " return pow(cos(arg0),-1 );\n";
79 ss << "}";
81 void OpCosh::BinInlineFun(std::set<std::string>& decls,
82 std::set<std::string>& funs)
84 decls.insert(local_coshDecl);
85 funs.insert(local_cosh);
87 void OpSecH::GenSlidingWindowFunction(std::stringstream &ss,
88 const std::string sSymName, SubArguments &vSubArguments)
90 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
91 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
92 formula::SingleVectorRefToken *>(tmpCur);
93 ss << "\ndouble " << sSymName;
94 ss << "_"<< BinFuncName() <<"(";
95 for (unsigned i = 0; i < vSubArguments.size(); i++)
97 if (i)
98 ss << ",";
99 vSubArguments[i]->GenSlidingWindowDecl(ss);
101 ss << ") {\n";
102 ss <<" int gid0=get_global_id(0);\n";
103 ss <<" double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
104 ss <<";\n";
105 #ifdef ISNAN
106 ss<<" if(isNan(arg0)||(gid0>=";
107 ss<<tmpCurDVR->GetArrayLength();
108 ss<<"))\n";
109 ss<<" arg0 = 0;\n";
110 #endif
111 ss << " return pow(cosh(arg0),-1 );\n";
112 ss << "}";
114 void OpMROUND::GenSlidingWindowFunction(std::stringstream &ss,
115 const std::string sSymName, SubArguments &vSubArguments)
117 ss << "\ndouble " << sSymName;
118 ss << "_"<< BinFuncName() <<"(";
119 for (unsigned i = 0; i < vSubArguments.size(); i++)
121 if (i)
122 ss << ", ";
123 vSubArguments[i]->GenSlidingWindowDecl(ss);
125 ss<<") {\n";
126 ss<<" double tmp = 0;\n";
127 ss<<" int gid0 = get_global_id(0);\n";
128 ss<<" double arg0=0;\n";
129 ss<<" double arg1=0;\n";
130 ss <<"\n ";
131 //while (i-- > 1)
132 for (size_t i = 0; i < vSubArguments.size(); i++)
134 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
135 assert(pCur);
136 if (pCur->GetType() == formula::svSingleVectorRef)
138 #ifdef ISNAN
139 const formula::SingleVectorRefToken* pSVR =
140 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
141 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
142 #else
143 #endif
145 else if (pCur->GetType() == formula::svDouble)
147 #ifdef ISNAN
148 ss << "{\n";
149 #endif
151 else
153 #ifdef ISNAN
154 #endif
156 #ifdef ISNAN
157 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
159 ss << " tmp=";
160 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
161 ss << ";\n";
162 ss << " if (isNan(tmp))\n";
163 ss << " arg"<<i<<"= 0;\n";
164 ss << " else\n";
165 ss << " arg"<<i<<"=tmp;\n";
166 ss << " }\n";
168 else
170 ss<<" arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
171 ss<<";\n";
173 #else
174 ss<<"arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
175 ss<<";\n";
176 #endif
178 ss<<" if(arg1==0)\n";
179 ss<<" return arg1;\n";
180 ss<<" tmp=arg1 * round(arg0 * pow(arg1,-1));\n";
181 ss<<" return tmp;\n";
182 ss<<"}";
184 void OpCosh::GenSlidingWindowFunction(std::stringstream &ss,
185 const std::string sSymName, SubArguments &vSubArguments)
187 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
188 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
189 formula::SingleVectorRefToken *>(tmpCur);
190 ss << "\ndouble " << sSymName;
191 ss << "_"<< BinFuncName() <<"(";
192 for (unsigned i = 0; i < vSubArguments.size(); i++)
194 if (i)
195 ss << ",";
196 vSubArguments[i]->GenSlidingWindowDecl(ss);
198 ss << ")\n{\n";
199 ss <<" int gid0=get_global_id(0);\n";
200 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
201 ss << ";\n";
202 #ifdef ISNAN
203 ss<< " if(isNan(arg0)||(gid0>=";
204 ss<<tmpCurDVR->GetArrayLength();
205 ss<<"))\n";
206 ss<<" arg0 = 0;\n";
207 #endif
208 ss << " double tmp=local_cosh(arg0);\n";
209 ss << " return tmp;\n";
210 ss << "}";
213 void OpCot::GenSlidingWindowFunction(std::stringstream &ss,
214 const std::string sSymName, SubArguments &vSubArguments)
216 ss << "\ndouble " << sSymName;
217 ss << "_"<< BinFuncName() <<"(";
218 for (unsigned i = 0; i < vSubArguments.size(); i++)
220 if (i)
221 ss << ",";
222 vSubArguments[i]->GenSlidingWindowDecl(ss);
224 ss << ")\n";
225 ss << "{\n";
226 ss << " int gid0=get_global_id(0);\n";
227 ss << " double arg0 = 0.0f;\n";
228 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
229 assert(tmpCur);
230 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
232 if(tmpCur->GetType() == formula::svSingleVectorRef)
234 const formula::SingleVectorRefToken*tmpCurDVR=
235 dynamic_cast
236 <const formula::SingleVectorRefToken *>(tmpCur);
237 ss << " arg0 = ";
238 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
239 ss << ";\n";
240 #ifdef ISNAN
241 ss << " if(isNan(";
242 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
243 ss << ")||(gid0>=";
244 ss << tmpCurDVR->GetArrayLength();
245 ss << "))\n";
246 ss << " { arg0 = 0.0f; }\n";
247 #endif
249 else if(tmpCur->GetType() == formula::svDouble)
251 ss << " arg0=";
252 ss << tmpCur->GetDouble() << ";\n";
255 else
257 ss << " arg0 = ";
258 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
259 ss << ";\n";
261 ss << " arg0 = arg0 * M_1_PI;\n";
262 ss << " return cospi(arg0) * pow(sinpi(arg0), -1);\n";
263 ss << "}";
267 void OpCoth::BinInlineFun(std::set<std::string>& decls,
268 std::set<std::string>& funs)
270 decls.insert(local_cothDecl);
271 funs.insert(local_coth);
274 void OpCoth::GenSlidingWindowFunction(std::stringstream &ss,
275 const std::string sSymName, SubArguments &vSubArguments)
277 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
278 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
279 formula::SingleVectorRefToken *>(tmpCur);
280 ss << "\ndouble " << sSymName;
281 ss << "_"<< BinFuncName() <<"(";
282 for (unsigned i = 0; i < vSubArguments.size(); i++)
284 if (i)
285 ss << ",";
286 vSubArguments[i]->GenSlidingWindowDecl(ss);
288 ss << ")\n{\n";
289 ss <<" int gid0=get_global_id(0);\n";
290 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
291 ss << ";\n";
292 #ifdef ISNAN
293 ss<< " if(isNan(arg0)||(gid0>=";
294 ss<<tmpCurDVR->GetArrayLength();
295 ss<<"))\n";
296 ss<<" arg0 = 0;\n";
297 #endif
298 ss << " double tmp=local_coth(arg0);\n";
299 ss << " return tmp;\n";
300 ss << "}";
303 void OpCombina::BinInlineFun(std::set<std::string>& decls,
304 std::set<std::string>& funs)
306 decls.insert(bikDecl);
307 funs.insert(bik);
310 void OpCombina::GenSlidingWindowFunction(std::stringstream &ss,
311 const std::string sSymName, SubArguments &vSubArguments)
313 ss << "\ndouble " << sSymName;
314 ss << "_"<< BinFuncName() <<"(";
315 for (unsigned i = 0; i < vSubArguments.size(); i++)
317 if (i)
318 ss << ",";
319 vSubArguments[i]->GenSlidingWindowDecl(ss);
321 ss << ")\n{\n";
322 ss << " int gid0 = get_global_id(0);\n";
323 ss << " double tem;\n";
324 ss << " double arg0,arg1;\n";
325 for (unsigned int i = 0; i < vSubArguments.size(); i++)
327 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
328 assert(pCur);
329 ss << " arg"<<i<<" = "<<vSubArguments[i]->GenSlidingWindowDeclRef();
330 ss << ";\n";
331 if(pCur->GetType() == formula::svSingleVectorRef)
333 #ifdef ISNAN
334 const formula::SingleVectorRefToken* pSVR =
335 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
336 ss << " if(isNan(arg" << i <<")||(gid0 >= ";
337 ss << pSVR->GetArrayLength();
338 ss << "))\n";
339 ss << " arg" << i << " = 0;\n";
341 else if (pCur->GetType() == formula::svDouble)
343 ss << " if(isNan(arg" << i <<"))\n";
344 ss << " arg" << i << " = 0;\n";
346 #endif
348 ss << " arg0 = trunc(arg0);\n";
349 ss << " arg1 = trunc(arg1);\n";
350 ss << " if(arg0 >= arg1 && arg0 > 0 && arg1 > 0)\n";
351 ss << " tem = bik(arg0+arg1-1,arg1);\n";
352 ss << " else if(arg0 == 0 && arg1 == 0)\n";
353 ss << " tem = 0;\n";
354 ss << " else if(arg0 > 0 && arg1 == 0)\n";
355 ss << " tem = 1;\n";
356 ss << " else\n";
357 ss << " tem = -1;\n";
358 ss << " double i = tem - trunc(tem);\n";
359 ss << " if(i < 0.5)\n";
360 ss << " tem = trunc(tem);\n";
361 ss << " else\n";
362 ss << " tem = trunc(tem) + 1;\n";
363 ss << " return tem;\n";
364 ss << "}";
366 void OpEven::GenSlidingWindowFunction(std::stringstream &ss,
367 const std::string sSymName, SubArguments &vSubArguments)
369 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
370 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
371 formula::SingleVectorRefToken *>(tmpCur);
372 ss << "\ndouble " << sSymName;
373 ss << "_"<< BinFuncName() <<"(";
374 for (unsigned i = 0; i < vSubArguments.size(); i++)
376 if (i)
377 ss << ",";
378 vSubArguments[i]->GenSlidingWindowDecl(ss);
380 ss << ")\n{\n";
381 ss <<" int gid0=get_global_id(0);\n";
382 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
383 ss << ";\n";
384 #ifdef ISNAN
385 ss<< " if(isNan(arg0)||(gid0>=";
386 ss<<tmpCurDVR->GetArrayLength();
387 ss<<"))\n";
388 ss<<" arg0 = 0;\n";
389 #endif
390 ss << " double tmp;\n";
391 ss << " tmp = fabs(arg0 / 2);\n";
392 ss << " if ( trunc(tmp) == tmp )\n";
393 ss << " tmp = tmp * 2;\n";
394 ss << " else\n";
395 ss << " tmp = (trunc(tmp) + 1) * 2;\n";
396 ss << " if (arg0 < 0)\n";
397 ss << " tmp = tmp * -1.0;\n";
398 ss << " return tmp;\n";
399 ss << "}";
402 void OpMod::GenSlidingWindowFunction(std::stringstream &ss,
403 const std::string sSymName, SubArguments &vSubArguments)
405 #ifdef ISNAN
406 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
407 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
408 formula::SingleVectorRefToken *>(tmpCur0);
409 FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
410 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
411 formula::SingleVectorRefToken *>(tmpCur1);
412 #endif
413 ss << "\ndouble " << sSymName;
414 ss << "_"<< BinFuncName() <<"(";
415 for (unsigned i = 0; i < vSubArguments.size(); i++)
417 if (i)
418 ss << ",";
419 vSubArguments[i]->GenSlidingWindowDecl(ss);
421 ss << ")\n{\n";
422 ss <<" int gid0=get_global_id(0);\n";
423 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
424 ss << ";\n";
425 ss <<" double arg1 =" << vSubArguments[1]->GenSlidingWindowDeclRef();
426 ss << ";\n";
427 #ifdef ISNAN
428 ss<< " if(isNan(arg0)||(gid0>=";
429 ss<<tmpCurDVR0->GetArrayLength();
430 ss<<"))\n";
431 ss<<" arg0 = 0;\n";
432 #endif
433 #ifdef ISNAN
434 ss<< " if(isNan(arg1)||(gid0>=";
435 ss<<tmpCurDVR1->GetArrayLength();
436 ss<<"))\n";
437 ss<<" arg1 = 0;\n";
438 #endif
439 ss << " double tem;\n";
440 ss << " if(arg1 != 0) {\n";
441 ss << " if(arg0 < 0 && arg1 > 0)\n";
442 ss << " while(arg0 < 0)\n";
443 ss << " arg0 += arg1;\n";
444 ss << " else if (arg0 > 0 && arg1 < 0)\n";
445 ss << " while(arg0 > 0)\n";
446 ss << " arg0 += arg1;\n";
447 ss << " tem = fmod(arg0,arg1);\n";
448 ss << " }\n";
449 ss << " else\n";
450 ss << " tem = 0;\n";
451 ss << " if(arg1 < 0 && tem > 0)\n";
452 ss << " tem = -tem;\n";
453 ss << " return tem;\n";
454 ss << "}";
456 void OpLog::GenSlidingWindowFunction(std::stringstream &ss,
457 const std::string sSymName, SubArguments &vSubArguments)
459 ss << "\ndouble " << sSymName;
460 ss << "_"<< BinFuncName() <<"(";
461 for (unsigned i = 0; i < vSubArguments.size(); i++)
463 if (i)
464 ss << ",";
465 vSubArguments[i]->GenSlidingWindowDecl(ss);
467 ss << ")\n{\n";
468 ss << " int gid0 = get_global_id(0);\n";
469 ss << " double tem;\n";
470 ss << " double arg0,arg1;\n";
471 for (unsigned int i = 0; i < vSubArguments.size(); i++)
473 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
474 assert(pCur);
475 ss << " arg"<<i<<" = "<<vSubArguments[i]->GenSlidingWindowDeclRef();
476 ss << ";\n";
477 if(pCur->GetType() == formula::svSingleVectorRef)
479 #ifdef ISNAN
480 const formula::SingleVectorRefToken* pSVR =
481 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
482 ss << " if(isNan(arg" << i <<")||(gid0 >= ";
483 ss << pSVR->GetArrayLength();
484 ss << "))\n";
485 if( i == 0)
486 ss << " arg0 = 0;\n";
487 else if ( i == 1)
488 ss << " arg1 = 10;\n";
490 else if (pCur->GetType() == formula::svDouble)
492 ss << " if(isNan(arg" << i <<"))\n";
493 if( i == 0)
494 ss << " arg0 = 0;\n";
495 else if ( i == 1)
496 ss << " arg1 = 10;\n";
498 #endif
500 if (vSubArguments.size() < 2)
501 ss << " arg1 = 10;\n";
502 ss << " tem = log10(arg0)/log10(arg1);;\n";
503 ss << " return tem;\n";
504 ss << "}";
506 void OpCsc::GenSlidingWindowFunction(
507 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
509 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
510 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
511 formula::SingleVectorRefToken *>(tmpCur);
512 ss << "\ndouble " << sSymName;
513 ss << "_"<< BinFuncName() <<"(";
514 for (unsigned i = 0; i < vSubArguments.size(); i++)
516 if (i)
517 ss << ",";
518 vSubArguments[i]->GenSlidingWindowDecl(ss);
520 ss << ")\n{\n\t";
521 ss <<"int gid0=get_global_id(0);\n\t";
522 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
523 ss << ";\n\t";
524 #ifdef ISNAN
525 ss<< "if(isNan(arg0)||(gid0>=";
526 ss<<tmpCurDVR->GetArrayLength();
527 ss<<"))\n\t\t";
528 ss<<"arg0 = 0;\n\t";
529 #endif
530 ss << "double tmp=1/sin(arg0);\n\t";
531 ss << "return tmp;\n";
532 ss << "}";
534 void OpCountIfs::GenSlidingWindowFunction(std::stringstream &ss,
535 const std::string sSymName, SubArguments &vSubArguments)
537 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
538 const formula::DoubleVectorRefToken*pCurDVR= dynamic_cast<const
539 formula::DoubleVectorRefToken *>(tmpCur);
540 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
541 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
542 pCurDVR->GetRefRowSize() ;
543 ss << "\ndouble " << sSymName;
544 ss << "_"<< BinFuncName() <<"(";
545 for (unsigned i = 0; i < vSubArguments.size(); i++)
547 if (i)
548 ss << ",";
549 vSubArguments[i]->GenSlidingWindowDecl(ss);
551 ss << ")\n {\n";
552 ss <<" int gid0=get_global_id(0);\n";
553 ss << " int tmp =0;\n";
554 ss << " int loop;\n";
555 GenTmpVariables(ss,vSubArguments);
557 ss<< " int singleIndex =gid0;\n";
558 int m=0;
560 std::stringstream tmpss;
562 for(unsigned j=0;j<vSubArguments.size();j+=2,m++)
564 CheckSubArgumentIsNan(tmpss,vSubArguments,j);
565 CheckSubArgumentIsNan(ss,vSubArguments,j+1);
566 tmpss <<" if(isequal(";
567 tmpss <<"tmp";
568 tmpss <<j;
569 tmpss <<" , ";
570 tmpss << "tmp";
571 tmpss << j+1;
572 tmpss << ")){\n";
574 tmpss << " tmp ++;\n";
575 for(unsigned j=0;j<vSubArguments.size();j+=2,m--)
577 for(int n = 0;n<m+1;n++)
579 tmpss << " ";
581 tmpss<< "}\n";
583 UnrollDoubleVector(ss,tmpss,pCurDVR,nCurWindowSize);
585 ss << "return tmp;\n";
586 ss << "}";
588 void OpSumIfs::GenSlidingWindowFunction(std::stringstream &ss,
589 const std::string sSymName, SubArguments &vSubArguments)
591 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
592 const formula::DoubleVectorRefToken*pCurDVR= dynamic_cast<const
593 formula::DoubleVectorRefToken *>(tmpCur);
594 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
595 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
596 pCurDVR->GetRefRowSize() ;
598 mNeedReductionKernel = vSubArguments[0]->NeedParallelReduction();
599 if (mNeedReductionKernel)
601 // generate reduction functions
603 ss << "__kernel void ";
604 ss << vSubArguments[0]->GetName();
605 ss << "_SumIfs_reduction( ";
606 for (unsigned i = 0; i < vSubArguments.size(); i++)
608 if (i)
609 ss << ",";
610 vSubArguments[i]->GenSlidingWindowDecl(ss);
612 ss << ", __global double *result,int arrayLength,int windowSize";
614 ss << ")\n{\n";
615 ss << " double tmp =0;\n";
616 ss << " int i ;\n";
618 GenTmpVariables(ss,vSubArguments);
619 ss << " double current_result = 0.0;\n";
620 ss << " int writePos = get_group_id(1);\n";
621 if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
622 ss << " int offset = 0;\n";
623 else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
624 ss << " int offset = get_group_id(1);\n";
625 else
626 throw Unhandled();
627 // actually unreachable
628 ss << " int lidx = get_local_id(0);\n";
629 ss << " __local double shm_buf[256];\n";
630 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
631 ss << " int loop = arrayLength/512 + 1;\n";
632 ss << " for (int l=0; l<loop; l++){\n";
633 ss << " tmp = 0.0;\n";
634 ss << " int loopOffset = l*512;\n";
636 ss << " int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
637 ss << " if (p2 < min(offset + windowSize, arrayLength)) {\n";
638 ss << " tmp0 = 0.0;\n";
639 int mm=0;
640 std::string p1 = "p1";
641 std::string p2 = "p2";
642 for(unsigned j=1;j<vSubArguments.size();j+=2,mm++)
644 CheckSubArgumentIsNan2(ss,vSubArguments,j,p1);
645 CheckSubArgumentIsNan2(ss,vSubArguments,j+1,p1);
646 ss << "";
647 ss <<" if(isequal(";
648 ss <<"tmp";
649 ss <<j;
650 ss <<" , ";
651 ss << "tmp";
652 ss << j+1;
653 ss << "))";
654 ss << "{\n";
656 CheckSubArgumentIsNan2(ss,vSubArguments,0,p1);
657 ss << " tmp += tmp0;\n";
658 for(unsigned j=1;j<vSubArguments.size();j+=2,mm--)
660 for(int n = 0;n<mm+1;n++)
662 ss << " ";
664 ss<< "}\n\n";
666 mm=0;
667 for(unsigned j=1;j<vSubArguments.size();j+=2,mm++)
669 CheckSubArgumentIsNan2(ss,vSubArguments,j,p2);
670 CheckSubArgumentIsNan2(ss,vSubArguments,j+1,p2);
671 ss <<" if(isequal(";
672 ss <<"tmp";
673 ss <<j;
674 ss <<" , ";
675 ss << "tmp";
676 ss << j+1;
677 ss << ")){\n";
679 CheckSubArgumentIsNan2(ss,vSubArguments,0,p2);
680 ss << " tmp += tmp0;\n";
681 for(unsigned j=1;j< vSubArguments.size();j+=2,mm--)
683 for(int n = 0;n<mm+1;n++)
685 ss << " ";
687 ss<< "}\n";
689 ss << " }\n";
691 ss << " else if (p1 < min(arrayLength, offset + windowSize)) {\n";
692 mm=0;
693 for(unsigned j=1;j<vSubArguments.size();j+=2,mm++)
695 CheckSubArgumentIsNan2(ss,vSubArguments,j,p1);
696 CheckSubArgumentIsNan2(ss,vSubArguments,j+1,p1);
698 ss <<" if(isequal(";
699 ss <<"tmp";
700 ss <<j;
701 ss <<" , ";
702 ss << "tmp";
703 ss << j+1;
704 ss << ")){\n";
706 CheckSubArgumentIsNan2(ss,vSubArguments,0,p1);
707 ss << " tmp += tmp0;\n";
708 for(unsigned j=1;j<vSubArguments.size();j+=2,mm--)
710 for(int n = 0;n<mm+1;n++)
712 ss << " ";
714 ss<< "}\n\n";
717 ss << " }\n";
718 ss << " shm_buf[lidx] = tmp;\n";
719 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
720 ss << " for (int i = 128; i >0; i/=2) {\n";
721 ss << " if (lidx < i)\n";
722 ss << " shm_buf[lidx] += shm_buf[lidx + i];\n";
723 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
724 ss << " }\n";
725 ss << " if (lidx == 0)\n";
726 ss << " current_result += shm_buf[0];\n";
727 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
728 ss << " }\n";
730 ss << " if (lidx == 0)\n";
731 ss << " result[writePos] = current_result;\n";
732 ss << "}\n";
733 }// finish generate reduction code
734 // generate functions as usual
735 ss << "\ndouble " << sSymName;
736 ss << "_"<< BinFuncName() <<"(";
737 for (unsigned i = 0; i < vSubArguments.size(); i++)
739 if (i)
740 ss << ",";
741 vSubArguments[i]->GenSlidingWindowDecl(ss);
743 ss << ")\n {\n";
744 ss <<" int gid0=get_global_id(0);\n";
745 ss << " double tmp =0;\n";
746 if (!mNeedReductionKernel)
748 ss << " int i ;\n";
749 GenTmpVariables(ss,vSubArguments);
750 ss << " for (i = ";
751 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
752 ss << "gid0; i < "<< nCurWindowSize <<"; i++)\n";
753 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
754 ss << "0; i < gid0+"<< nCurWindowSize <<"; i++)\n";
755 } else {
756 ss << "0; i < "<< nCurWindowSize <<"; i++)\n";
758 ss << " {\n";
759 if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
761 ss<< " int doubleIndex =i+gid0;\n";
762 }else
764 ss<< " int doubleIndex =i;\n";
766 ss<< " int singleIndex =gid0;\n";
767 int m=0;
768 for(unsigned j=1;j<vSubArguments.size();j+=2,m++)
770 CheckSubArgumentIsNan(ss,vSubArguments,j);
771 CheckSubArgumentIsNan(ss,vSubArguments,j+1);
772 ss <<" if(isequal(";
773 ss <<"tmp";
774 ss <<j;
775 ss <<" , ";
776 ss << "tmp";
777 ss << j+1;
778 ss << ")){\n";
780 CheckSubArgumentIsNan(ss,vSubArguments,0);
781 ss << " tmp += tmp0;\n";
782 for(unsigned j=1;j<=vSubArguments.size();j+=2,m--)
784 for(int n = 0;n<m+1;n++)
786 ss << " ";
788 ss<< "}\n";
791 if (mNeedReductionKernel)
793 ss << "tmp =";
794 vSubArguments[0]->GenDeclRef(ss);
795 ss << "[gid0];\n";
797 ss << "return tmp;\n";
798 ss << "}";
800 void OpCscH::GenSlidingWindowFunction(
801 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
803 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
804 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
805 formula::SingleVectorRefToken *>(tmpCur);
806 ss << "\ndouble " << sSymName;
807 ss << "_"<< BinFuncName() <<"(";
808 for (unsigned i = 0; i < vSubArguments.size(); i++)
810 if (i)
811 ss << ",";
812 vSubArguments[i]->GenSlidingWindowDecl(ss);
814 ss << ")\n{\n\t";
815 ss <<"int gid0=get_global_id(0);\n\t";
816 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
817 ss << ";\n\t";
818 #ifdef ISNAN
819 ss<< "if(isNan(arg0)||(gid0>=";
820 ss<<tmpCurDVR->GetArrayLength();
821 ss<<"))\n\t\t";
822 ss<<"arg0 = 0;\n\t";
823 #endif
824 ss << "double tmp=1/sinh(arg0);\n\t";
825 ss << "return tmp;\n";
826 ss << "}";
828 void OpExp::GenSlidingWindowFunction(std::stringstream &ss,
829 const std::string sSymName, SubArguments &vSubArguments)
831 ss << "\ndouble " << sSymName;
832 ss << "_"<< BinFuncName() <<"(";
833 for (unsigned i = 0; i < vSubArguments.size(); i++)
835 if (i) ss << ",";
836 vSubArguments[i]->GenSlidingWindowDecl(ss);
838 ss << ")\n";
839 ss << "{\n";
840 ss << " int gid0=get_global_id(0);\n";
841 ss << " double arg0 = 0.0f;\n";
842 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
843 assert(tmpCur);
844 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
846 if(tmpCur->GetType() == formula::svSingleVectorRef)
848 const formula::SingleVectorRefToken*tmpCurDVR=
849 dynamic_cast
850 <const formula::SingleVectorRefToken *>(tmpCur);
851 ss << " arg0 = ";
852 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
853 ss << ";\n";
854 #ifdef ISNAN
855 ss << " if(isNan(";
856 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
857 ss << ")||(gid0>=";
858 ss << tmpCurDVR->GetArrayLength();
859 ss << "))\n";
860 ss << " { arg0 = 0.0f; }\n";
861 #endif
863 else if(tmpCur->GetType() == formula::svDouble)
865 ss << " arg0=" << tmpCur->GetDouble() << ";\n";
868 else
870 ss << " arg0 = ";
871 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
872 ss << ";\n";
874 ss << " return pow(M_E, arg0);\n";
875 ss << "}";
878 void OpAverageIfs::GenSlidingWindowFunction(std::stringstream &ss,
879 const std::string sSymName, SubArguments &vSubArguments)
881 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
882 const formula::DoubleVectorRefToken*pCurDVR= dynamic_cast<const
883 formula::DoubleVectorRefToken *>(tmpCur);
884 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
885 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
886 pCurDVR->GetRefRowSize() ;
887 ss << "\ndouble " << sSymName;
888 ss << "_"<< BinFuncName() <<"(";
889 for (unsigned i = 0; i < vSubArguments.size(); i++)
891 if (i)
892 ss << ",";
893 vSubArguments[i]->GenSlidingWindowDecl(ss);
895 ss << ")\n {\n";
896 ss <<" int gid0=get_global_id(0);\n";
897 ss << " double tmp =0;\n";
898 ss << " int count=0;\n";
899 ss << " int loop;";
900 GenTmpVariables(ss,vSubArguments);
901 ss<< " int singleIndex =gid0;\n";
902 int m=0;
903 std::stringstream tmpss;
904 for(unsigned j=1;j<vSubArguments.size();j+=2,m++)
906 CheckSubArgumentIsNan(tmpss,vSubArguments,j);
907 CheckSubArgumentIsNan(ss,vSubArguments,j+1);
908 tmpss <<" if(isequal(";
909 tmpss <<"tmp";
910 tmpss <<j;
911 tmpss <<" , ";
912 tmpss << "tmp";
913 tmpss << j+1;
914 tmpss << ")){\n";
916 CheckSubArgumentIsNan(tmpss,vSubArguments,0);
917 tmpss << " tmp += tmp0;\n";
918 tmpss << " count++;\n";
919 for(unsigned j=1;j<vSubArguments.size();j+=2,m--)
921 for(int n = 0;n<m+1;n++)
923 tmpss << " ";
925 tmpss<< "}\n";
928 UnrollDoubleVector(ss,tmpss,pCurDVR,nCurWindowSize);
930 ss << " if(count!=0)\n";
931 ss << " tmp=tmp/count;\n";
932 ss << " else\n";
933 ss << " tmp= 0 ;\n";
934 ss << "return tmp;\n";
935 ss << "}";
938 void OpLog10::GenSlidingWindowFunction(std::stringstream &ss,
939 const std::string sSymName, SubArguments &vSubArguments)
941 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
942 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
943 formula::SingleVectorRefToken *>(tmpCur);
944 ss << "\ndouble " << sSymName;
945 ss << "_"<< BinFuncName() <<"(";
946 for (unsigned i = 0; i < vSubArguments.size(); i++)
948 if (i)
949 ss << ",";
950 vSubArguments[i]->GenSlidingWindowDecl(ss);
952 ss << ")\n{\n\t";
953 ss <<"int gid0=get_global_id(0);\n\t";
954 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
955 ss << ";\n\t";
956 #ifdef ISNAN
957 ss<< "if(isNan(arg0)||(gid0>=";
958 ss<<tmpCurDVR->GetArrayLength();
959 ss<<"))\n\t\t";
960 ss<<"arg0 = 0;\n\t";
961 #endif
962 ss << "double tmp=log10(arg0);\n\t";
963 ss << "return tmp;\n";
964 ss << "}";
967 void OpSinh::GenSlidingWindowFunction(std::stringstream &ss,
968 const std::string sSymName, SubArguments &vSubArguments)
970 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
971 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
972 formula::SingleVectorRefToken *>(tmpCur);
973 ss << "\ndouble " << sSymName;
974 ss << "_"<< BinFuncName() <<"(";
975 for (unsigned i = 0; i < vSubArguments.size(); i++)
977 if (i)
978 ss << ",";
979 vSubArguments[i]->GenSlidingWindowDecl(ss);
981 ss <<") {\n";
982 ss <<" int gid0=get_global_id(0);\n";
983 ss <<" double arg0 = " <<
984 vSubArguments[0]->GenSlidingWindowDeclRef();
985 ss <<";\n";
986 #ifdef ISNAN
987 ss<< " if(isNan(arg0)||(gid0>=";
988 ss<<tmpCurDVR->GetArrayLength();
989 ss<<"))\n";
990 ss<<" arg0 = 0;\n";
991 #endif
992 ss << " double tmp=( exp(arg0)-exp(-arg0) )/2;\n";
993 ss << " return tmp;\n";
994 ss << "}";
996 void OpSin::GenSlidingWindowFunction(std::stringstream &ss,
997 const std::string sSymName, SubArguments &vSubArguments)
999 ss << "\ndouble " << sSymName;
1000 ss << "_"<< BinFuncName() <<"(";
1001 for (unsigned i = 0; i < vSubArguments.size(); i++)
1003 if (i) ss << ",";
1004 vSubArguments[i]->GenSlidingWindowDecl(ss);
1006 ss << ")\n";
1007 ss << "{\n";
1008 ss << " int gid0=get_global_id(0);\n";
1009 ss << " double arg0 = 0.0f;\n";
1010 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1011 assert(tmpCur);
1012 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1014 if(tmpCur->GetType() == formula::svSingleVectorRef)
1016 const formula::SingleVectorRefToken*tmpCurDVR=
1017 dynamic_cast
1018 <const formula::SingleVectorRefToken *>(tmpCur);
1019 ss << " arg0 = ";
1020 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1021 ss << ";\n";
1022 #ifdef ISNAN
1023 ss << " if(isNan(";
1024 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1025 ss << ")||(gid0>=";
1026 ss << tmpCurDVR->GetArrayLength();
1027 ss << "))\n";
1028 ss << " { arg0 = 0.0f; }\n";
1029 #endif
1031 else if(tmpCur->GetType() == formula::svDouble)
1033 ss << " arg0=" << tmpCur->GetDouble() << ";\n";
1036 else
1038 ss << " arg0 = ";
1039 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1040 ss << ";\n";
1042 ss << " arg0 = arg0 * M_1_PI;\n";
1043 ss << " return sinpi(arg0);\n";
1044 ss << "}";
1047 void OpAbs::GenSlidingWindowFunction(std::stringstream &ss,
1048 const std::string sSymName, SubArguments &vSubArguments)
1050 ss << "\ndouble " << sSymName;
1051 ss << "_"<< BinFuncName() <<"(";
1052 for (unsigned i = 0; i < vSubArguments.size(); i++)
1054 if (i)
1055 ss << ",";
1056 vSubArguments[i]->GenSlidingWindowDecl(ss);
1058 ss << ") {\n";
1059 ss << " int gid0 = get_global_id(0);\n";
1060 ss << " double tmp = " << GetBottom() << ";\n";
1061 #ifdef ISNAN
1062 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1063 const formula::SingleVectorRefToken*tmpCurDVR0=
1064 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1065 ss << " int buffer_len = ";
1066 ss << tmpCurDVR0->GetArrayLength();
1067 ss << ";\n";
1068 ss << " if((gid0)>=buffer_len || isNan(";
1069 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1070 ss << "))\n";
1071 ss << " tmp = " << GetBottom() << ";\n else \n";
1072 #endif
1073 ss << " tmp = ";
1074 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1075 ss << ";\n";
1076 ss << " return fabs(tmp);\n";
1077 ss << "}";
1079 void OpArcCos::BinInlineFun(std::set<std::string>& decls,
1080 std::set<std::string>& funs)
1082 decls.insert(atan2Decl);
1083 funs.insert(atan2Content);
1085 void OpArcCos::GenSlidingWindowFunction(std::stringstream &ss,
1086 const std::string sSymName, SubArguments &vSubArguments)
1088 ss << "\ndouble " << sSymName;
1089 ss << "_"<< BinFuncName() <<"(";
1090 for (unsigned i = 0; i < vSubArguments.size(); i++)
1092 if (i) ss << ",";
1093 vSubArguments[i]->GenSlidingWindowDecl(ss);
1095 ss << ") {\n";
1096 ss << " int gid0 = get_global_id(0);\n";
1097 ss << " double tmp = " << GetBottom() << ";\n";
1098 #ifdef ISNAN
1099 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1100 const formula::SingleVectorRefToken*tmpCurDVR0=
1101 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1102 ss << " int buffer_len = "<< tmpCurDVR0->GetArrayLength()<< ";\n";
1103 ss << " if((gid0)>=buffer_len || isNan(";
1104 ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< "))\n";
1105 ss << " tmp = " << GetBottom() << ";\n";
1106 ss << " else \n ";
1107 #endif
1108 ss << " tmp = ";
1109 ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< ";\n";
1110 ss << " return arctan2(sqrt(1.0 - pow(tmp, 2)), tmp);\n";
1111 ss << "}";
1113 void OpArcCosHyp::GenSlidingWindowFunction(std::stringstream &ss,
1114 const std::string sSymName, SubArguments &vSubArguments)
1116 ss << "\ndouble " << sSymName;
1117 ss << "_"<< BinFuncName() <<"(";
1118 for (unsigned i = 0; i < vSubArguments.size(); i++)
1120 if (i) ss << ",";
1121 vSubArguments[i]->GenSlidingWindowDecl(ss);
1123 ss << ") {\n";
1124 ss << " int gid0 = get_global_id(0);\n";
1125 ss << " double tmp = " << GetBottom() << ";\n";
1126 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1127 assert(tmpCur0);
1128 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1130 if(tmpCur0->GetType() == formula::svSingleVectorRef)
1132 const formula::SingleVectorRefToken*tmpCurDVR0=
1133 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1134 #ifdef ISNAN
1135 ss << " int buffer_len = "<<tmpCurDVR0->GetArrayLength()<<";\n";
1136 ss << " if((gid0)>=buffer_len || isNan(";
1137 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1138 ss << " tmp = " << GetBottom() << ";\n";
1139 ss << " else \n ";
1140 #endif
1141 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1142 ss << ";\n";
1144 else if(tmpCur0->GetType() == formula::svDouble)
1146 ss << " tmp = " << tmpCur0->GetDouble() << ";\n";
1149 else
1151 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1152 ss << ";\n";
1155 ss << " return log( tmp + pow( (pown(tmp, 2) - 1.0), 0.5));\n";
1156 ss << "}";
1158 void OpTan::GenSlidingWindowFunction(std::stringstream &ss,
1159 const std::string sSymName, SubArguments &vSubArguments)
1161 ss << "\ndouble " << sSymName;
1162 ss << "_"<< BinFuncName() <<"(";
1163 for (unsigned i = 0; i < vSubArguments.size(); i++)
1165 if (i) ss << ",";
1166 vSubArguments[i]->GenSlidingWindowDecl(ss);
1168 ss << ")\n";
1169 ss << "{\n";
1170 ss << " int gid0=get_global_id(0);\n";
1171 ss << " double arg0 = 0.0f;\n";
1172 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1173 assert(tmpCur);
1174 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1176 if(tmpCur->GetType() == formula::svSingleVectorRef)
1178 const formula::SingleVectorRefToken*tmpCurDVR=
1179 dynamic_cast
1180 <const formula::SingleVectorRefToken *>(tmpCur);
1181 ss << " arg0 = ";
1182 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1183 ss << ";\n";
1184 #ifdef ISNAN
1185 ss << " if(isNan(";
1186 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1187 ss << ")||(gid0>=";
1188 ss << tmpCurDVR->GetArrayLength();
1189 ss << "))\n";
1190 ss << " { arg0 = 0.0f; }\n";
1191 #endif
1193 else if(tmpCur->GetType() == formula::svDouble)
1195 ss << " arg0=" << tmpCur->GetDouble() << ";\n";
1198 else
1200 ss << " arg0 = ";
1201 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1202 ss << ";\n";
1204 ss << " arg0 = arg0 * M_1_PI;\n";
1205 ss << " return sinpi(arg0) * pow(cospi(arg0), -1);\n";
1206 ss << "}";
1208 void OpTanH::GenSlidingWindowFunction(std::stringstream &ss,
1209 const std::string sSymName, SubArguments &vSubArguments)
1211 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1212 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
1213 formula::SingleVectorRefToken *>(tmpCur);
1214 ss << "\ndouble " << sSymName;
1215 ss << "_"<< BinFuncName() <<"(";
1216 for (unsigned i = 0; i < vSubArguments.size(); i++)
1218 if (i)
1219 ss << ",";
1220 vSubArguments[i]->GenSlidingWindowDecl(ss);
1222 ss << ")\n";
1223 ss << "{\n";
1224 ss << " int gid0=get_global_id(0);\n";
1225 ss << " double arg0 = "<< vSubArguments[0]->GenSlidingWindowDeclRef();
1226 ss << ";\n";
1227 #ifdef ISNAN
1228 ss << " if(isNan(arg0)||(gid0>=";
1229 ss << tmpCurDVR->GetArrayLength();
1230 ss << "))\n";
1231 ss << " arg0 = 0;\n";
1232 #endif
1233 ss << " double tmp=tanh(arg0);\n";
1234 ss << " return tmp;\n";
1235 ss << "}";
1237 void OpPower::GenSlidingWindowFunction(std::stringstream &ss,
1238 const std::string sSymName, SubArguments &vSubArguments)
1240 ss << "\ndouble " << sSymName;
1241 ss << "_"<< BinFuncName() <<"(";
1242 for (unsigned i = 0; i < vSubArguments.size(); i++)
1244 if (i)
1245 ss << ",";
1246 vSubArguments[i]->GenSlidingWindowDecl(ss);
1248 ss << ")\n";
1249 ss << "{\n";
1250 ss << " int gid0=get_global_id(0);\n";
1251 ss << " double arg[2];\n";
1252 for( unsigned i=0; i < vSubArguments.size(); ++i)
1254 FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
1255 assert(tmpCur);
1256 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1258 if(tmpCur->GetType() == formula::svDoubleVectorRef)
1260 const formula::DoubleVectorRefToken* tmpCurDVR =
1261 dynamic_cast<
1262 const formula::DoubleVectorRefToken *>(tmpCur);
1263 ss << " int i = 0;\n";
1264 ss << " arg["<<i<<"] = ";
1265 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1266 ss << ";\n";
1267 #ifdef ISNAN
1268 ss << " if(isNan(arg["<<i;
1269 ss << "])||(gid0>=";
1270 ss << tmpCurDVR->GetArrayLength();
1271 ss << "))\n";
1272 ss << " arg["<<i;
1273 ss << "] = 0;\n";
1274 #endif
1276 else if(tmpCur->GetType() == formula::svSingleVectorRef)
1278 const formula::SingleVectorRefToken* tmpCurDVR=
1279 dynamic_cast<
1280 const formula::SingleVectorRefToken *>(tmpCur);
1281 ss << " arg["<<i<<"] = ";
1282 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1283 ss << ";\n";
1284 #ifdef ISNAN
1285 ss << " if(isNan(arg["<<i;
1286 ss << "])||(gid0>=";
1287 ss << tmpCurDVR->GetArrayLength();
1288 ss << "))\n";
1289 ss << " arg["<<i;
1290 ss << "] = 0;\n";
1291 #endif
1293 else if(tmpCur->GetType() == formula::svDouble)
1295 ss << " arg["<<i<<"] = ";
1296 ss << tmpCur->GetDouble() << ";\n";
1299 else
1301 ss << " arg["<<i<<"] = ";
1302 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1303 ss << ";\n";
1306 ss << " return pow(arg[0],arg[1]);\n";
1307 ss << "}";
1309 void OpSqrt::GenSlidingWindowFunction(std::stringstream &ss,
1310 const std::string sSymName, SubArguments &vSubArguments)
1312 ss << "\ndouble " << sSymName;
1313 ss << "_"<< BinFuncName() <<"(";
1314 for (unsigned i = 0; i < vSubArguments.size(); i++)
1316 if (i)
1317 ss << ",";
1318 vSubArguments[i]->GenSlidingWindowDecl(ss);
1320 ss << ")\n";
1321 ss << "{\n";
1322 ss << " int gid0=get_global_id(0);\n";
1323 ss << " double arg0 = 0.0f;\n";
1324 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1325 assert(tmpCur);
1326 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1328 if(tmpCur->GetType() == formula::svSingleVectorRef)
1330 const formula::SingleVectorRefToken*tmpCurDVR=
1331 dynamic_cast
1332 <const formula::SingleVectorRefToken *>(tmpCur);
1333 ss << " arg0 = ";
1334 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1335 ss << ";\n";
1336 #ifdef ISNAN
1337 ss << " if(isNan(";
1338 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1339 ss << ")||(gid0>=";
1340 ss << tmpCurDVR->GetArrayLength();
1341 ss << "))\n";
1342 ss << " { arg0 = 0; }\n";
1343 #endif
1345 else if(tmpCur->GetType() == formula::svDouble)
1347 ss << " arg0=";
1348 ss << tmpCur->GetDouble() << ";\n";
1351 else
1353 ss << " arg0 = ";
1354 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1355 ss << ";\n";
1357 ss << " return sqrt(arg0);\n";
1358 ss << "}";
1360 void OpArcCot::GenSlidingWindowFunction(std::stringstream &ss,
1361 const std::string sSymName, SubArguments &vSubArguments)
1363 ss << "\ndouble " << sSymName;
1364 ss << "_"<< BinFuncName() <<"(";
1365 for (unsigned i = 0; i < vSubArguments.size(); i++)
1367 if (i) ss << ",";
1368 vSubArguments[i]->GenSlidingWindowDecl(ss);
1370 ss << ") {\n";
1371 ss << " int gid0 = get_global_id(0);\n";
1372 ss << " double tmp = " << GetBottom() << ";\n";
1373 #ifdef ISNAN
1374 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1375 const formula::SingleVectorRefToken*tmpCurDVR0=
1376 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1377 ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength()<< ";\n";
1378 ss << " if((gid0)>=buffer_len || isNan(";
1379 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1380 ss << " tmp = " << GetBottom() << ";\n";
1381 ss << " else \n ";
1382 #endif
1383 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() <<";\n";
1384 ss << " return M_PI_2 - atan(tmp);\n";
1385 ss << "}";
1387 void OpArcCotHyp::GenSlidingWindowFunction(std::stringstream &ss,
1388 const std::string sSymName, SubArguments &vSubArguments)
1390 ss << "\ndouble " << sSymName;
1391 ss << "_"<< BinFuncName() <<"(";
1392 for (unsigned i = 0; i < vSubArguments.size(); i++)
1394 if (i) ss << ",";
1395 vSubArguments[i]->GenSlidingWindowDecl(ss);
1397 ss << ")\n";
1398 ss << "{\n";
1399 ss << " int gid0=get_global_id(0);\n";
1400 ss << " double arg0 = 0.0f;\n";
1401 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1402 assert(tmpCur);
1403 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1405 if(tmpCur->GetType() == formula::svSingleVectorRef)
1407 const formula::SingleVectorRefToken*tmpCurDVR=
1408 dynamic_cast
1409 <const formula::SingleVectorRefToken *>(tmpCur);
1410 ss << " arg0 = ";
1411 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1412 ss << ";\n";
1413 #ifdef ISNAN
1414 ss << " if(isNan(";
1415 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1416 ss << ")||(gid0>=";
1417 ss << tmpCurDVR->GetArrayLength();
1418 ss << "))\n";
1419 ss << " { arg0 = 0.0f; }\n";
1420 #endif
1422 else if(tmpCur->GetType() == formula::svDouble)
1424 ss << " arg0=";
1425 ss << tmpCur->GetDouble() << ";\n";
1428 else
1430 ss << " arg0 = ";
1431 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1432 ss << ";\n";
1434 ss << " return 0.5 * log(1 + 2 * pown(arg0 - 1.0, -1));\n";
1435 ss << "}";
1437 void OpArcSin::BinInlineFun(std::set<std::string>& decls,
1438 std::set<std::string>& funs)
1440 decls.insert(atan2Decl);
1441 funs.insert(atan2Content);
1443 void OpArcSin::GenSlidingWindowFunction(std::stringstream &ss,
1444 const std::string sSymName, SubArguments &vSubArguments)
1446 ss << "\ndouble " << sSymName;
1447 ss << "_"<< BinFuncName() <<"(";
1448 for (unsigned i = 0; i < vSubArguments.size(); i++)
1450 if (i) ss << ",";
1451 vSubArguments[i]->GenSlidingWindowDecl(ss);
1453 ss << ") {\n";
1454 ss << " int gid0 = get_global_id(0);\n";
1455 ss << " double tmp = " << GetBottom() << ";\n";
1456 #ifdef ISNAN
1457 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1458 const formula::SingleVectorRefToken*tmpCurDVR0=
1459 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1460 ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength() << ";\n";
1461 ss << " if((gid0)>=buffer_len || isNan(";
1462 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1463 ss << " tmp = " << GetBottom() << ";\n";
1464 ss << " else \n ";
1465 #endif
1466 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1467 ss << " return arctan2(tmp, sqrt(1.0 - pow(tmp, 2)));\n";
1468 ss << "}";
1470 void OpArcSinHyp::GenSlidingWindowFunction(std::stringstream &ss,
1471 const std::string sSymName, SubArguments &vSubArguments)
1473 ss << "\ndouble " << sSymName;
1474 ss << "_"<< BinFuncName() <<"(";
1475 for (unsigned i = 0; i < vSubArguments.size(); i++)
1477 if (i) ss << ",";
1478 vSubArguments[i]->GenSlidingWindowDecl(ss);
1480 ss << ") {\n";
1481 ss << " int gid0 = get_global_id(0);\n";
1482 ss << " double tmp = " << GetBottom() << ";\n";
1483 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1484 assert(tmpCur0);
1485 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1487 if(tmpCur0->GetType() == formula::svSingleVectorRef)
1489 const formula::SingleVectorRefToken*tmpCurDVR0=
1490 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1491 #ifdef ISNAN
1492 ss << " int buffer_len = "<<tmpCurDVR0->GetArrayLength()<<";\n";
1493 ss << " if((gid0)>=buffer_len || isNan(";
1494 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1495 ss << " tmp = " << GetBottom() << ";\n";
1496 ss << " else \n ";
1497 #endif
1498 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1499 ss << ";\n";
1501 else if(tmpCur0->GetType() == formula::svDouble)
1503 ss << " tmp = " << tmpCur0->GetDouble() << ";\n";
1506 else
1508 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1509 ss << ";\n";
1511 ss << " return log( tmp + pow((pown(tmp, 2) + 1.0), 0.5));\n";
1512 ss << "}";
1514 void OpArcTan2::BinInlineFun(std::set<std::string>& decls,
1515 std::set<std::string>& funs)
1517 decls.insert(atan2Decl);
1518 funs.insert(atan2Content);
1520 void OpArcTan2::GenSlidingWindowFunction(std::stringstream &ss,
1521 const std::string sSymName, SubArguments &vSubArguments)
1523 ss << "\ndouble " << sSymName;
1524 ss << "_"<< BinFuncName() <<"(";
1525 for (unsigned i = 0; i < vSubArguments.size(); i++)
1527 if (i) ss << ",";
1528 vSubArguments[i]->GenSlidingWindowDecl(ss);
1530 ss << ") {\n";
1531 ss << " int gid0 = get_global_id(0);\n";
1532 ss << " double x_num = " << GetBottom() << ";\n";
1533 ss << " double y_num = " << GetBottom() << ";\n";
1534 #ifdef ISNAN
1535 FormulaToken *iXNum = vSubArguments[0]->GetFormulaToken();
1536 const formula::SingleVectorRefToken*tmpCurDVRX=
1537 dynamic_cast<const formula::SingleVectorRefToken *>(iXNum);
1538 FormulaToken *iYNum = vSubArguments[1]->GetFormulaToken();
1539 const formula::SingleVectorRefToken*tmpCurDVRY=
1540 dynamic_cast<const formula::SingleVectorRefToken *>(iYNum);
1541 ss << " int buffer_x_len = " << tmpCurDVRX->GetArrayLength() << ";\n";
1542 ss << " int buffer_y_len = " << tmpCurDVRY->GetArrayLength() << ";\n";
1543 ss << " if((gid0)>=buffer_x_len || isNan(";
1544 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1545 ss << " x_num = " << GetBottom() << ";\n";
1546 ss << " else \n ";
1547 #endif
1548 ss << " x_num = "<< vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1549 #ifdef ISNAN
1550 ss << " if((gid0)>=buffer_y_len || isNan(";
1551 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
1552 ss << " y_num = " << GetBottom() << ";\n";
1553 ss << " else \n ";
1554 #endif
1555 ss << " y_num = "<< vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1556 ss << " return arctan2(y_num, x_num);\n";
1557 ss << "}";
1559 void OpArcTan::GenSlidingWindowFunction(std::stringstream &ss,
1560 const std::string sSymName, SubArguments &vSubArguments)
1562 ss << "\ndouble " << sSymName;
1563 ss << "_"<< BinFuncName() <<"(";
1564 for (unsigned i = 0; i < vSubArguments.size(); i++)
1566 if (i) ss << ",";
1567 vSubArguments[i]->GenSlidingWindowDecl(ss);
1569 ss << ") {\n";
1570 ss << " int gid0 = get_global_id(0);\n";
1571 ss << " double tmp = " << GetBottom() << ";\n";
1572 #ifdef ISNAN
1573 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1574 const formula::SingleVectorRefToken*tmpCurDVR0=
1575 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1576 ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength() << ";\n";
1577 ss << " if((gid0)>=buffer_len || isNan(";
1578 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1579 ss << " tmp = " << GetBottom() << ";\n";
1580 ss << " else\n ";
1581 #endif
1582 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1583 ss << " return atan(tmp);\n";
1584 ss << "}";
1586 void OpArcTanH::GenSlidingWindowFunction(std::stringstream &ss,
1587 const std::string sSymName, SubArguments &vSubArguments)
1589 ss << "\ndouble " << sSymName;
1590 ss << "_"<< BinFuncName() <<"(";
1591 for (unsigned i = 0; i < vSubArguments.size(); i++)
1593 if (i) ss << ",";
1594 vSubArguments[i]->GenSlidingWindowDecl(ss);
1596 ss << ") {\n";
1597 ss << " int gid0 = get_global_id(0);\n";
1598 ss << " double tmp = " << GetBottom() << ";\n";
1599 #ifdef ISNAN
1600 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1601 const formula::SingleVectorRefToken*tmpCurDVR0=
1602 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1603 ss << " int buffer_len = " << tmpCurDVR0->GetArrayLength() << ";\n";
1604 ss << " if((gid0)>=buffer_len || isNan(";
1605 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1606 ss << " tmp = " << GetBottom() << ";\n";
1607 ss << " else \n ";
1608 #endif
1609 ss << " tmp = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1610 ss << " double a = 1.0 + tmp;\n";
1611 ss << " double b = 1.0 - tmp;\n";
1612 ss << " return log(pow(a/b, 0.5));\n";
1613 ss << "}";
1615 void OpBitAnd::GenSlidingWindowFunction(std::stringstream &ss,
1616 const std::string sSymName, SubArguments &vSubArguments)
1618 ss << "\ndouble " << sSymName;
1619 ss << "_"<< BinFuncName() <<"(";
1620 for (unsigned i = 0; i < vSubArguments.size(); i++)
1622 if (i) ss << ",";
1623 vSubArguments[i]->GenSlidingWindowDecl(ss);
1625 ss << ") {\n";
1626 ss << " int gid0 = get_global_id(0);\n";
1627 ss << " double num1 = " << GetBottom() << ";\n";
1628 ss << " double num2 = " << GetBottom() << ";\n";
1629 #ifdef ISNAN
1630 FormulaToken *iNum1 = vSubArguments[0]->GetFormulaToken();
1631 const formula::SingleVectorRefToken* tmpCurDVRNum1=
1632 dynamic_cast<const formula::SingleVectorRefToken *>(iNum1);
1633 FormulaToken *iNum2 = vSubArguments[1]->GetFormulaToken();
1634 const formula::SingleVectorRefToken* tmpCurDVRNum2=
1635 dynamic_cast<const formula::SingleVectorRefToken *>(iNum2);
1636 ss << " int buffer_num1_len = "<<tmpCurDVRNum1->GetArrayLength()<<";\n";
1637 ss << " int buffer_num2_len = "<<tmpCurDVRNum2->GetArrayLength()<<";\n";
1638 ss << " if((gid0)>=buffer_num1_len || isNan(";
1639 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
1640 ss << " num1 = " << GetBottom() << ";\n";
1641 ss << " else \n ";
1642 #endif
1643 ss << " num1 = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1644 #ifdef ISNAN
1645 ss << " if((gid0)>=buffer_num2_len || isNan(";
1646 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
1647 ss << " num2 = " << GetBottom() << ";\n";
1648 ss << " else \n ";
1649 #endif
1650 ss << " num2 = " << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1651 ss << " return (long)num1 & (long)num2;\n";
1652 ss << "}";
1654 void OpLn::GenSlidingWindowFunction(
1655 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
1657 ss << "\ndouble " << sSymName;
1658 ss << "_"<< BinFuncName() <<"(";
1659 for (unsigned i = 0; i < vSubArguments.size(); i++)
1661 if (i)
1662 ss << ",";
1663 vSubArguments[i]->GenSlidingWindowDecl(ss);
1665 ss << ")\n{\n";
1666 ss << " int gid0=get_global_id(0);\n";
1667 ss << " int singleIndex = gid0;\n";
1669 GenTmpVariables(ss,vSubArguments);
1670 CheckAllSubArgumentIsNan(ss,vSubArguments);
1672 ss << " double tmp=log1p(tmp0-1);\n";
1673 ss << " return tmp;\n";
1674 ss << "}";
1677 void OpRound::GenSlidingWindowFunction(std::stringstream &ss,
1678 const std::string sSymName, SubArguments &vSubArguments)
1680 ss << "\ndouble " << sSymName;
1681 ss << "_"<< BinFuncName() <<"(";
1682 for (unsigned i = 0; i < vSubArguments.size(); i++)
1684 if (i)
1685 ss << ",";
1686 vSubArguments[i]->GenSlidingWindowDecl(ss);
1688 ss << ")\n{\n";
1689 ss << " int gid0=get_global_id(0);\n";
1690 ss << " int singleIndex = gid0;\n";
1691 GenTmpVariables(ss,vSubArguments);
1692 CheckAllSubArgumentIsNan(ss,vSubArguments);
1693 ss << " for(int i=0;i<tmp1;i++)\n";
1694 ss << " tmp0 = tmp0 * 10;\n";
1695 ss << " double tmp=round(tmp0);\n";
1696 ss << " for(int i=0;i<tmp1;i++)\n";
1697 ss << " tmp = tmp / 10;\n";
1698 ss << " return tmp;\n";
1699 ss << "}";
1701 void OpRoundUp::GenSlidingWindowFunction(std::stringstream &ss,
1702 const std::string sSymName, SubArguments &vSubArguments)
1704 ss << "\ndouble " << sSymName;
1705 ss << "_"<< BinFuncName() <<"(";
1706 for (unsigned i = 0; i < vSubArguments.size(); i++)
1708 if (i)
1709 ss << ",";
1710 vSubArguments[i]->GenSlidingWindowDecl(ss);
1712 ss << ")\n{\n";
1713 ss << " int gid0=get_global_id(0);\n";
1714 ss << " int singleIndex = gid0;\n";
1715 ss << " int intTmp;\n";
1716 ss << " double doubleTmp;\n";
1717 ss << " double tmp;\n";
1718 GenTmpVariables(ss,vSubArguments);
1719 CheckAllSubArgumentIsNan(ss,vSubArguments);
1720 ss << " if(tmp1 >20 || tmp1 < -20)";
1721 ss << " {\n";
1722 ss << " tmp = NAN;\n";
1723 ss << " }else\n";
1724 ss << " {\n";
1725 ss << " for(int i=0;i<tmp1;i++)\n";
1726 ss << " tmp0 = tmp0 * 10;\n";
1727 ss << " intTmp = (int)tmp0;\n";
1728 ss << " doubleTmp = intTmp;\n";
1729 ss << " if(isequal(doubleTmp,tmp0))\n";
1730 ss << " tmp = doubleTmp;\n";
1731 ss << " else\n";
1732 ss << " tmp = doubleTmp + 1;\n";
1733 ss << " for(int i=0;i<tmp1;i++)\n";
1734 ss << " tmp = tmp / 10;\n";
1735 ss << " }\n";
1736 ss << " return tmp;\n";
1737 ss << "}";
1739 void OpRoundDown::GenSlidingWindowFunction(std::stringstream &ss,
1740 const std::string sSymName, SubArguments &vSubArguments)
1742 ss << "\ndouble " << sSymName;
1743 ss << "_"<< BinFuncName() <<"(";
1744 for (unsigned i = 0; i < vSubArguments.size(); i++)
1746 if (i)
1747 ss << ",";
1748 vSubArguments[i]->GenSlidingWindowDecl(ss);
1750 ss << ")\n{\n";
1751 ss << " int gid0=get_global_id(0);\n";
1752 ss << " int singleIndex = gid0;\n";
1753 ss << " int intTmp;\n";
1754 ss << " double tmp;\n";
1755 GenTmpVariables(ss,vSubArguments);
1756 CheckAllSubArgumentIsNan(ss,vSubArguments);
1757 ss << " if(tmp1 >20 || tmp1 < -20)";
1758 ss << " {\n";
1759 ss << " tmp = NAN;\n";
1760 ss << " }else\n";
1761 ss << " {\n";
1762 ss << " for(int i=0;i<tmp1;i++)\n";
1763 ss << " tmp0 = tmp0 * 10;\n";
1764 ss << " intTmp = (int)tmp0;\n";
1765 ss << " tmp = intTmp;\n";
1766 ss << " for(int i=0;i<tmp1;i++)\n";
1767 ss << " tmp = tmp / 10;\n";
1768 ss << " }\n";
1769 ss << " return tmp;\n";
1770 ss << "}";
1772 void OpInt::GenSlidingWindowFunction(std::stringstream &ss,
1773 const std::string sSymName, SubArguments &vSubArguments)
1775 ss << "\ndouble " << sSymName;
1776 ss << "_"<< BinFuncName() <<"(";
1777 for (unsigned i = 0; i < vSubArguments.size(); i++)
1779 if (i)
1780 ss << ",";
1781 vSubArguments[i]->GenSlidingWindowDecl(ss);
1783 ss << ")\n{\n";
1784 ss << " int gid0=get_global_id(0);\n";
1785 ss << " int singleIndex = gid0;\n";
1786 ss << " int intTmp;\n";
1787 ss << " double tmp;\n";
1788 GenTmpVariables(ss,vSubArguments);
1789 CheckAllSubArgumentIsNan(ss,vSubArguments);
1790 ss << " intTmp = (int)tmp0;\n";
1791 ss << " tmp = intTmp;\n";
1792 ss << " return tmp;\n";
1793 ss << "}";
1795 void OpNegSub::GenSlidingWindowFunction(std::stringstream &ss,
1796 const std::string sSymName, SubArguments &vSubArguments)
1798 ss << "\ndouble " << sSymName;
1799 ss << "_"<< BinFuncName() <<"(";
1800 for (unsigned i = 0; i < vSubArguments.size(); i++)
1802 if (i)
1803 ss << ",";
1804 vSubArguments[i]->GenSlidingWindowDecl(ss);
1806 ss << ")\n{\n";
1807 ss << " int gid0=get_global_id(0);\n";
1808 ss << " int singleIndex = gid0;\n";
1809 GenTmpVariables(ss,vSubArguments);
1810 CheckAllSubArgumentIsNan(ss,vSubArguments);
1811 ss << " return -tmp0;\n";
1812 ss << "}";
1815 void OpRadians::GenSlidingWindowFunction(std::stringstream &ss,
1816 const std::string sSymName, SubArguments &vSubArguments)
1818 ss << "\ndouble " << sSymName;
1819 ss << "_"<< BinFuncName() <<"(";
1820 for (unsigned i = 0; i < vSubArguments.size(); i++)
1822 if (i)
1823 ss << ",";
1824 vSubArguments[i]->GenSlidingWindowDecl(ss);
1826 ss << ")\n{\n";
1827 ss << " int gid0=get_global_id(0);\n";
1828 ss << " int singleIndex = gid0;\n";
1829 ss << " double tmp;\n";
1830 GenTmpVariables(ss,vSubArguments);
1831 CheckAllSubArgumentIsNan(ss,vSubArguments);
1832 ss << " tmp = tmp0 * 3.141592657 * pow(180.0,-1);\n";
1833 ss << " return tmp;\n";
1834 ss << "}";
1836 void OpIsEven::GenSlidingWindowFunction(std::stringstream &ss,
1837 const std::string sSymName, SubArguments &vSubArguments)
1839 ss << "\ndouble " << sSymName;
1840 ss << "_"<< BinFuncName() <<"(";
1841 for (unsigned i = 0; i < vSubArguments.size(); i++)
1843 if (i)
1844 ss << ",";
1845 vSubArguments[i]->GenSlidingWindowDecl(ss);
1847 ss << ")\n{\n";
1848 ss << " int gid0=get_global_id(0);\n";
1849 ss << " int singleIndex = gid0;\n";
1850 ss << " double tmp;\n";
1851 GenTmpVariables(ss,vSubArguments);
1852 CheckAllSubArgumentIsNan(ss,vSubArguments);
1853 ss << " tmp = (fmod(floor(fabs(tmp0)), 2.0)<0.5);\n";
1854 ss << " return tmp;\n";
1855 ss << "}";
1857 void OpIsOdd::GenSlidingWindowFunction(std::stringstream &ss,
1858 const std::string sSymName, SubArguments &vSubArguments)
1860 ss << "\ndouble " << sSymName;
1861 ss << "_"<< BinFuncName() <<"(";
1862 for (unsigned i = 0; i < vSubArguments.size(); i++)
1864 if (i)
1865 ss << ",";
1866 vSubArguments[i]->GenSlidingWindowDecl(ss);
1868 ss << ")\n{\n";
1869 ss << " int gid0=get_global_id(0);\n";
1870 ss << " int singleIndex = gid0;\n";
1871 ss << " double tmp;\n";
1872 GenTmpVariables(ss,vSubArguments);
1873 CheckAllSubArgumentIsNan(ss,vSubArguments);
1874 ss << " tmp = !(fmod(floor(fabs(tmp0)), 2.0)<0.5);\n";
1875 ss << " return tmp;\n";
1876 ss << "}";
1878 void OpOdd::GenSlidingWindowFunction(
1879 std::stringstream &ss, const std::string sSymName,
1880 SubArguments &vSubArguments)
1882 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1883 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
1884 formula::SingleVectorRefToken *>(tmpCur);
1885 ss << Math_Intg_Str;
1886 ss << "\ndouble " << sSymName;
1887 ss << "_"<< BinFuncName() <<"(";
1888 for (unsigned i = 0; i < vSubArguments.size(); i++)
1890 if (i)
1891 ss << ",";
1892 vSubArguments[i]->GenSlidingWindowDecl(ss);
1894 ss << ")\n{\n";
1895 ss <<" int gid0=get_global_id(0);\n";
1896 ss << " double tmp=0;\n";
1897 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1898 ss << ";\n";
1899 #ifdef ISNAN
1900 ss<< " if(isNan(arg0)||(gid0>=";
1901 ss<<tmpCurDVR->GetArrayLength();
1902 ss<<"))\n";
1903 ss<<" arg0 = 0;\n";
1904 #endif
1905 ss << " if (arg0 > 0.0 ){\n";
1906 ss << " tmp=Intg(arg0);\n";
1907 ss << " if(tmp-trunc(tmp/2)*2 == 0)\n";
1908 ss << " tmp=tmp+1;\n";
1909 ss << " }else if (arg0 < 0.0 ){\n";
1910 ss << " tmp=Intg(arg0);\n";
1911 ss << " if(tmp-trunc(tmp/2)*2 == 0)\n";
1912 ss << " tmp=tmp-1.0;\n";
1913 ss << " }else if (arg0 == 0.0 )\n";
1914 ss << " tmp=1.0;\n";
1915 ss << " return tmp;\n";
1916 ss << "}";
1918 void OpCountIf::GenSlidingWindowFunction(std::stringstream &ss,
1919 const std::string sSymName, SubArguments &vSubArguments)
1921 ss << "\ndouble " << sSymName;
1922 ss << "_"<< BinFuncName() <<"(";
1923 for (unsigned i = 0; i < 2; i++)
1925 if (i)
1926 ss << ",";
1927 vSubArguments[i]->GenSlidingWindowDecl(ss);
1929 ss << ")\n";
1930 ss << "{\n";
1931 ss << " int gid0=get_global_id(0);\n";
1932 ss << " double vara, varb;\n";
1933 ss << " int varc = 0;\n";
1934 FormulaToken *tmpCur = vSubArguments[1]->GetFormulaToken();
1935 assert(tmpCur);
1936 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1938 if(tmpCur->GetType() == formula::svSingleVectorRef)
1940 const formula::SingleVectorRefToken* tmpCurDVR=
1941 dynamic_cast<
1942 const formula::SingleVectorRefToken *>(tmpCur);
1943 ss << " varb = ";
1944 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1945 ss << ";\n";
1946 #ifdef ISNAN
1947 ss << " if(isNan(varb)||(gid0>=";
1948 ss << tmpCurDVR->GetArrayLength();
1949 ss << "))\n";
1950 ss << " varb = 0;\n";
1951 #endif
1953 else if(tmpCur->GetType() == formula::svDouble)
1955 ss << " varb = ";
1956 ss << tmpCur->GetDouble() << ";\n";
1959 else
1961 ss << " varb = ";
1962 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1963 ss << ";\n";
1965 tmpCur = vSubArguments[0]->GetFormulaToken();
1966 assert(tmpCur);
1967 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1969 //TODO DoubleVector
1970 if (tmpCur->GetType() == formula::svDoubleVectorRef)
1972 const formula::DoubleVectorRefToken* pDVR =
1973 dynamic_cast<const formula::DoubleVectorRefToken *>(tmpCur);
1974 size_t nCurWindowSize = pDVR->GetRefRowSize();
1975 ss << " for (int i = ";
1976 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1978 #ifdef ISNAN
1979 ss << "gid0; i < " << pDVR->GetArrayLength();
1980 ss << " && i < " << nCurWindowSize << "; ++i)\n";
1981 ss << " {\n";
1982 #else
1983 ss << "gid0; i < "<< nCurWindowSize << "; ++i)\n";
1984 ss << " {\n";
1985 #endif
1987 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1989 #ifdef ISNAN
1990 ss << "0; i < " << pDVR->GetArrayLength();
1991 ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
1992 ss << " {\n";
1993 #else
1994 ss << "0; i < gid0+"<< nCurWindowSize << "; ++i)\n";
1995 ss << " {\n";
1996 #endif
1998 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2000 #ifdef ISNAN
2001 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2002 ss << " && i < "<< nCurWindowSize << "; ++i)\n";
2003 ss << " {\n";
2004 #else
2005 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2006 ss << " {\n";
2007 #endif
2009 else
2011 #ifdef ISNAN
2012 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2013 ss << " {\n";
2014 #else
2015 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2016 ss << " {\n";
2017 #endif
2019 ss << " vara = ";
2020 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2021 ss << ";\n";
2022 #ifdef ISNAN
2023 ss << " if (isNan(vara))\n";
2024 ss << " continue;\n";
2025 #endif
2026 ss << " (vara == varb) && varc++;\n";
2027 ss << " }\n";
2029 else if(tmpCur->GetType() == formula::svSingleVectorRef)
2031 const formula::SingleVectorRefToken* tmpCurDVR=
2032 dynamic_cast<
2033 const formula::SingleVectorRefToken *>(tmpCur);
2034 ss << " vara = ";
2035 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2036 ss << ";\n";
2037 #ifdef ISNAN
2038 ss << " if(isNan(vara)||(gid0>=";
2039 ss << tmpCurDVR->GetArrayLength();
2040 ss << "))\n";
2041 ss << " return 0;\n";
2042 ss << " (vara == varb) && varc++;\n";
2043 #endif
2046 ss << " return varc;\n";
2047 ss << "}";
2049 void OpSumIf::GenSlidingWindowFunction(std::stringstream &ss,
2050 const std::string sSymName, SubArguments &vSubArguments)
2052 ss << "\ndouble " << sSymName;
2053 ss << "_"<< BinFuncName() <<"(";
2054 int flag = 0;
2055 (3 == vSubArguments.size()) && (flag = 2);
2056 for (unsigned i = 0; i < vSubArguments.size(); i++)
2058 if (i)
2059 ss << ",";
2060 vSubArguments[i]->GenSlidingWindowDecl(ss);
2062 ss << ")\n";
2063 ss << "{\n";
2064 ss << " int gid0=get_global_id(0);\n";
2065 ss << " double vara, varb, varc, sum = 0.0f;\n";
2066 FormulaToken *tmpCur = vSubArguments[1]->GetFormulaToken();
2067 assert(tmpCur);
2068 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
2070 if(tmpCur->GetType() == formula::svSingleVectorRef)
2072 const formula::SingleVectorRefToken* tmpCurDVR=
2073 dynamic_cast<
2074 const formula::SingleVectorRefToken *>(tmpCur);
2075 ss << " varb = ";
2076 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2077 ss << ";\n";
2078 #ifdef ISNAN
2079 ss << " if(isNan(varb)||(gid0>=";
2080 ss << tmpCurDVR->GetArrayLength();
2081 ss << "))\n";
2082 ss << " varb = 0;\n";
2083 #endif
2085 else if(tmpCur->GetType() == formula::svDouble)
2087 ss << " varb = ";
2088 ss << tmpCur->GetDouble() << ";\n";
2091 else
2093 ss << " varb = ";
2094 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2095 ss << ";\n";
2097 tmpCur = vSubArguments[0]->GetFormulaToken();
2098 assert(tmpCur);
2099 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2101 //TODO DoubleVector
2102 if (tmpCur->GetType() == formula::svDoubleVectorRef)
2104 const formula::DoubleVectorRefToken* pDVR =
2105 dynamic_cast<const formula::DoubleVectorRefToken *>(tmpCur);
2106 size_t nCurWindowSize = pDVR->GetRefRowSize();
2107 ss << " for (int i = ";
2108 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2110 #ifdef ISNAN
2111 ss << "gid0; i < " << pDVR->GetArrayLength();
2112 ss << " && i < " << nCurWindowSize << "; ++i)\n";
2113 ss << " {\n";
2114 #else
2115 ss << "gid0; i < "<< nCurWindowSize << "; ++i)\n";
2116 ss << " {\n";
2117 #endif
2119 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2121 #ifdef ISNAN
2122 ss << "0; i < " << pDVR->GetArrayLength();
2123 ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
2124 ss << " {\n";
2125 #else
2126 ss << "0; i < gid0+"<< nCurWindowSize << "; ++i)\n";
2127 ss << " {\n";
2128 #endif
2130 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2132 #ifdef ISNAN
2133 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2134 ss << " && i < "<< nCurWindowSize << "; ++i)\n";
2135 ss << " {\n";
2136 #else
2137 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2138 ss << " {\n";
2139 #endif
2141 else
2143 #ifdef ISNAN
2144 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2145 ss << " {\n";
2146 #else
2147 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2148 ss << " {\n";
2149 #endif
2151 ss << " vara = ";
2152 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2153 ss << ";\n";
2154 #ifdef ISNAN
2155 ss << " if (isNan(vara))\n";
2156 ss << " continue;\n";
2157 #endif
2158 ss << " varc = ";
2159 ss << vSubArguments[flag]->GenSlidingWindowDeclRef();
2160 ss << ";\n";
2161 #ifdef ISNAN
2162 ss << " if (isNan(varc))\n";
2163 ss << " varc = 0.0f;\n";
2164 #endif
2165 ss << " (vara == varb)&&(sum = sum + varc);\n";
2166 ss << " }\n";
2168 else if(tmpCur->GetType() == formula::svSingleVectorRef)
2170 const formula::SingleVectorRefToken* tmpCurDVR=
2171 dynamic_cast<
2172 const formula::SingleVectorRefToken *>(tmpCur);
2173 ss << " vara = ";
2174 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2175 ss << ";\n";
2176 #ifdef ISNAN
2177 ss << " if(isNan(vara)||(gid0>=";
2178 ss << tmpCurDVR->GetArrayLength();
2179 ss << "))\n";
2180 ss << " return 0;\n";
2181 #endif
2182 ss << " int i = 0;\n";
2183 ss << " varc = ";
2184 ss << vSubArguments[flag]->GenSlidingWindowDeclRef();
2185 ss << ";\n";
2186 #ifdef ISNAN
2187 ss << " if(isNan(varc)||(gid0>=";
2188 ss << tmpCurDVR->GetArrayLength();
2189 ss << "))\n";
2190 ss << " varc = 0.0f;\n";
2191 #endif
2193 ss << " (vara == varb)&&(sum = sum + varc);\n";
2197 ss << " return sum;\n";
2198 ss << "}";
2200 void OpTrunc::GenSlidingWindowFunction(std::stringstream &ss,
2201 const std::string sSymName, SubArguments &vSubArguments)
2203 ss << "\ndouble " << sSymName;
2204 ss << "_"<< BinFuncName() <<"(";
2205 for (unsigned i = 0; i < vSubArguments.size(); i++)
2207 if (i)
2208 ss << ",";
2209 vSubArguments[i]->GenSlidingWindowDecl(ss);
2211 ss << ")\n";
2212 ss << "{\n";
2213 ss << " int gid0=get_global_id(0);\n";
2214 ss << " double arg[2];\n";
2215 for( unsigned i=0; i < vSubArguments.size(); ++i)
2217 FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
2218 assert(tmpCur);
2219 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2221 if(tmpCur->GetType() == formula::svDoubleVectorRef)
2223 const formula::DoubleVectorRefToken* tmpCurDVR =
2224 dynamic_cast<
2225 const formula::DoubleVectorRefToken *>(tmpCur);
2226 ss << " int i = 0;\n";
2227 ss << " arg["<<i<<"] = ";
2228 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2229 ss << ";\n";
2230 #ifdef ISNAN
2231 ss << " if(isNan(arg["<<i;
2232 ss << "])||(gid0>=";
2233 ss << tmpCurDVR->GetArrayLength();
2234 ss << "))\n";
2235 ss << " arg["<<i;
2236 ss << "] = 0;\n";
2237 #endif
2239 else if(tmpCur->GetType() == formula::svSingleVectorRef)
2241 const formula::SingleVectorRefToken* tmpCurDVR=
2242 dynamic_cast<
2243 const formula::SingleVectorRefToken *>(tmpCur);
2244 ss << " arg["<<i<<"] = ";
2245 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2246 ss << ";\n";
2247 #ifdef ISNAN
2248 ss << " if(isNan(arg["<<i;
2249 ss << "])||(gid0>=";
2250 ss << tmpCurDVR->GetArrayLength();
2251 ss << "))\n";
2252 ss << " arg["<<i;
2253 ss << "] = 0;\n";
2254 #endif
2256 else if(tmpCur->GetType() == formula::svDouble)
2258 ss << " arg["<<i<<"] = ";
2259 ss << tmpCur->GetDouble() << ";\n";
2262 else
2264 ss << " arg["<<i<<"] = ";
2265 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2266 ss << ";\n";
2269 ss << " double argm = arg[0];\n";
2270 ss << " int n = (int)arg[1];\n";
2271 ss << " double nn = 1.0f;\n";
2272 ss << " for(int i = 0; i < n; ++i)\n";
2273 ss << " {\n";
2274 ss << " argm = argm * 10;\n";
2275 ss << " nn = nn * 10;\n";
2276 ss << " }\n";
2277 ss << " modf(argm, &argm);\n";
2278 ss << " return argm / nn;\n";
2279 ss << "}";
2281 void OpFloor::GenSlidingWindowFunction(
2282 std::stringstream &ss, const std::string sSymName,
2283 SubArguments &vSubArguments)
2285 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2286 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
2287 formula::SingleVectorRefToken *>(tmpCur);
2288 ss << "\ndouble " << sSymName;
2289 ss << "_"<< BinFuncName() <<"(";
2290 for (unsigned i = 0; i < vSubArguments.size(); i++)
2292 if (i)
2293 ss << ",";
2294 vSubArguments[i]->GenSlidingWindowDecl(ss);
2296 ss << ")\n{\n";
2297 ss <<" int gid0=get_global_id(0);\n";
2298 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2299 ss << ";\n";
2300 ss << " double arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef();
2301 ss << ";\n";
2302 ss << " double arg2 = " << vSubArguments[2]->GenSlidingWindowDeclRef();
2303 ss << ";\n";
2304 #ifdef ISNAN
2305 ss<< " if(isNan(arg0)||(gid0>=";
2306 ss<<tmpCurDVR->GetArrayLength();
2307 ss<<"))\n";
2308 ss<<" arg0 = 0;\n";
2309 ss<< " if(isNan(arg1)||(gid0>=";
2310 ss<<tmpCurDVR->GetArrayLength();
2311 ss<<"))\n";
2312 ss<<" arg1 = 0;\n";
2313 ss<< " if(isNan(arg2)||(gid0>=";
2314 ss<<tmpCurDVR->GetArrayLength();
2315 ss<<"))\n";
2316 ss<<" arg2 = 0;\n";
2317 #endif
2318 ss <<" if(arg1==0.0)\n";
2319 ss <<" return 0.0;\n";
2320 ss <<" else if(arg0*arg1<0.0)\n";
2321 ss <<" return 0.0000000001;\n";
2322 ss <<" else if(arg2==0.0&&arg0<0.0)\n";
2323 ss <<" return (trunc(arg0/arg1)+1)*arg1;\n";
2324 ss <<" else\n";
2325 ss <<" return trunc(arg0/arg1)*arg1;\n";
2326 ss << "}\n";
2328 void OpBitOr::GenSlidingWindowFunction(std::stringstream &ss,
2329 const std::string sSymName, SubArguments &vSubArguments)
2331 ss << "\ndouble " << sSymName;
2332 ss << "_"<< BinFuncName() <<"(";
2333 for (unsigned i = 0; i < vSubArguments.size(); i++)
2335 if (i) ss << ",";
2336 vSubArguments[i]->GenSlidingWindowDecl(ss);
2338 ss << ") {\n";
2339 ss << " int gid0 = get_global_id(0);\n";
2340 ss << " double num1 = " << GetBottom() << ";\n";
2341 ss << " double num2 = " << GetBottom() << ";\n";
2342 #ifdef ISNAN
2343 FormulaToken *iNum1 = vSubArguments[0]->GetFormulaToken();
2344 const formula::SingleVectorRefToken* tmpCurDVRNum1=
2345 dynamic_cast<const formula::SingleVectorRefToken *>(iNum1);
2346 FormulaToken *iNum2 = vSubArguments[1]->GetFormulaToken();
2347 const formula::SingleVectorRefToken* tmpCurDVRNum2=
2348 dynamic_cast<const formula::SingleVectorRefToken *>(iNum2);
2349 ss << " int buffer_num1_len = "<<tmpCurDVRNum1->GetArrayLength()<<";\n";
2350 ss << " int buffer_num2_len = "<<tmpCurDVRNum2->GetArrayLength()<<";\n";
2351 ss << " if((gid0)>=buffer_num1_len || isNan(";
2352 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2353 ss << " num1 = " << GetBottom() << ";\n";
2354 ss << " else \n ";
2355 #endif
2356 ss << " num1 = floor(" << vSubArguments[0]->GenSlidingWindowDeclRef();
2357 ss << ");\n";
2358 #ifdef ISNAN
2359 ss << " if((gid0)>=buffer_num2_len || isNan(";
2360 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2361 ss << " num2 = " << GetBottom() << ";\n";
2362 ss << " else\n ";
2363 #endif
2364 ss << " num2 = floor(" << vSubArguments[1]->GenSlidingWindowDeclRef();
2365 ss << ");\n";
2366 ss << " return (long)num1 | (long)num2;\n";
2367 ss << "}";
2369 void OpBitXor::GenSlidingWindowFunction(std::stringstream &ss,
2370 const std::string sSymName, SubArguments &vSubArguments)
2372 ss << "\ndouble " << sSymName;
2373 ss << "_"<< BinFuncName() <<"(";
2374 for (unsigned i = 0; i < vSubArguments.size(); i++)
2376 if (i) ss << ",";
2377 vSubArguments[i]->GenSlidingWindowDecl(ss);
2379 ss << ") {\n";
2380 ss << " int gid0 = get_global_id(0);\n";
2381 ss << " double num1 = " << GetBottom() << ";\n";
2382 ss << " double num2 = " << GetBottom() << ";\n";
2383 #ifdef ISNAN
2384 FormulaToken *iNum1 = vSubArguments[0]->GetFormulaToken();
2385 const formula::SingleVectorRefToken* tmpCurDVRNum1=
2386 dynamic_cast<const formula::SingleVectorRefToken *>(iNum1);
2387 FormulaToken *iNum2 = vSubArguments[1]->GetFormulaToken();
2388 const formula::SingleVectorRefToken* tmpCurDVRNum2=
2389 dynamic_cast<const formula::SingleVectorRefToken *>(iNum2);
2390 ss << " int buffer_num1_len = " << tmpCurDVRNum1->GetArrayLength() << ";\n";
2391 ss << " int buffer_num2_len = " << tmpCurDVRNum2->GetArrayLength() << ";\n";
2393 ss << " if((gid0)>=buffer_num1_len || isNan(";
2394 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2395 ss << " num1 = " << GetBottom() << ";\n";
2396 ss << " else\n ";
2397 #endif
2398 ss << " num1 = floor(" << vSubArguments[0]->GenSlidingWindowDeclRef();
2399 ss << ");\n";
2400 #ifdef ISNAN
2401 ss << " if((gid0)>=buffer_num2_len || isNan(";
2402 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2403 ss << " num2 = " << GetBottom() << ";\n";
2404 ss << " else\n ";
2405 #endif
2406 ss << " num2 = floor(" << vSubArguments[1]->GenSlidingWindowDeclRef();
2407 ss << ");\n";
2408 ss << " return (long)num1 ^ (long)num2;\n";
2409 ss << "}";
2411 void OpBitLshift::GenSlidingWindowFunction(std::stringstream &ss,
2412 const std::string sSymName, SubArguments &vSubArguments)
2414 ss << "\ndouble " << sSymName;
2415 ss << "_"<< BinFuncName() <<"(";
2416 for (unsigned i = 0; i < vSubArguments.size(); i++)
2418 if (i) ss << ",";
2419 vSubArguments[i]->GenSlidingWindowDecl(ss);
2421 ss << ") {\n";
2422 ss << " int gid0 = get_global_id(0);\n";
2423 ss << " double num = " << GetBottom() << ";\n";
2424 ss << " double shift_amount = " << GetBottom() << ";\n";
2425 #ifdef ISNAN
2426 FormulaToken *iNum = vSubArguments[0]->GetFormulaToken();
2427 const formula::SingleVectorRefToken* tmpCurDVRNum=
2428 dynamic_cast<const formula::SingleVectorRefToken*>(iNum);
2429 FormulaToken *iShiftAmount = vSubArguments[1]->GetFormulaToken();
2430 const formula::SingleVectorRefToken* tmpCurDVRShiftAmount=
2431 dynamic_cast<const formula::SingleVectorRefToken*>(iShiftAmount);
2432 ss << " int buffer_num_len = "<< tmpCurDVRNum->GetArrayLength()<<";\n";
2433 ss << " int buffer_shift_amount_len = ";
2434 ss << tmpCurDVRShiftAmount->GetArrayLength() << ";\n";
2435 ss << " if((gid0)>=buffer_num_len || isNan(";
2436 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2437 ss << " num = " << GetBottom() << ";\n";
2438 ss << " else\n ";
2439 #endif
2440 ss << " num = floor(";
2441 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2442 #ifdef ISNAN
2443 ss << " if((gid0)>=buffer_shift_amount_len || isNan(";
2444 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2445 ss << " shift_amount = " << GetBottom() << ";\n";
2446 ss << " else\n ";
2447 #endif
2448 ss << " shift_amount = floor(";
2449 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2450 ss << " return floor(" << "shift_amount >= 0 ? ";
2451 ss << "num * pow(2.0, shift_amount) : ";
2452 ss << "num / pow(2.0, fabs(shift_amount)));\n";
2453 ss << "}";
2455 void OpBitRshift::GenSlidingWindowFunction(std::stringstream &ss,
2456 const std::string sSymName, SubArguments &vSubArguments)
2458 ss << "\ndouble " << sSymName;
2459 ss << "_"<< BinFuncName() <<"(";
2460 for (unsigned i = 0; i < vSubArguments.size(); i++)
2462 if (i) ss << ",";
2463 vSubArguments[i]->GenSlidingWindowDecl(ss);
2465 ss << ") {\n";
2466 ss << " int gid0 = get_global_id(0);\n";
2467 ss << " double num = " << GetBottom() << ";\n";
2468 ss << " double shift_amount = " << GetBottom() << ";\n";
2469 #ifdef ISNAN
2470 FormulaToken *iNum = vSubArguments[0]->GetFormulaToken();
2471 const formula::SingleVectorRefToken* tmpCurDVRNum=
2472 dynamic_cast<const formula::SingleVectorRefToken*>(iNum);
2473 FormulaToken *iShiftAmount = vSubArguments[1]->GetFormulaToken();
2474 const formula::SingleVectorRefToken* tmpCurDVRShiftAmount=
2475 dynamic_cast<const formula::SingleVectorRefToken*>(iShiftAmount);
2476 ss << " int buffer_num_len = ";
2477 ss << tmpCurDVRNum->GetArrayLength() << ";\n";
2478 ss << " int buffer_shift_amount_len = ";
2479 ss << tmpCurDVRShiftAmount->GetArrayLength() << ";\n";
2481 ss << " if((gid0)>=buffer_num_len || isNan(";
2482 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2483 ss << " num = " << GetBottom() << ";\n";
2484 ss << " else\n ";
2485 #endif
2486 ss << " num = floor(";
2487 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2488 #ifdef ISNAN
2489 ss << " if((gid0)>=buffer_shift_amount_len || isNan(";
2490 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2491 ss << " shift_amount = " <<GetBottom()<< ";\n";
2492 ss << " else\n ";
2493 #endif
2494 ss << " shift_amount = floor(";
2495 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2496 ss << " return floor(";
2497 ss << "shift_amount >= 0 ? num / pow(2.0, shift_amount) : ";
2498 ss << "num * pow(2.0, fabs(shift_amount)));\n";
2499 ss << "}";
2501 void OpSumSQ::GenSlidingWindowFunction(std::stringstream &ss,
2502 const std::string sSymName, SubArguments &vSubArguments)
2504 ss << "\ndouble " << sSymName;
2505 ss << "_"<< BinFuncName() <<"(";
2506 for (unsigned i = 0; i < vSubArguments.size(); ++i)
2508 if (i)
2509 ss << ",";
2510 vSubArguments[i]->GenSlidingWindowDecl(ss);
2512 ss << ")\n";
2513 ss << "{\n";
2514 ss << " int gid0=get_global_id(0);\n";
2515 ss << " double sum = 0.0f, arg;\n";
2516 for( unsigned i=0; i < vSubArguments.size(); ++i)
2518 FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
2519 assert(tmpCur);
2520 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2522 if (tmpCur->GetType() == formula::svDoubleVectorRef)
2524 const formula::DoubleVectorRefToken* pDVR =
2525 dynamic_cast<const formula::DoubleVectorRefToken *>(tmpCur);
2526 size_t nCurWindowSize = pDVR->GetRefRowSize();
2527 ss << " for (int i = ";
2528 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2530 #ifdef ISNAN
2531 ss << "gid0; i < " << pDVR->GetArrayLength();
2532 ss << " && i < " << nCurWindowSize << "; ++i)\n";
2533 ss << " {\n";
2534 #else
2535 ss << "gid0; i < "<< nCurWindowSize << "; ++i)\n";
2536 ss << " {\n";
2537 #endif
2539 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2541 #ifdef ISNAN
2542 ss << "0; i < " << pDVR->GetArrayLength();
2543 ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
2544 ss << " {\n";
2545 #else
2546 ss << "0; i < gid0+"<< nCurWindowSize << "; ++i)\n";
2547 ss << " {\n";
2548 #endif
2550 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2552 #ifdef ISNAN
2553 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2554 ss << " && i < "<< nCurWindowSize << "; ++i)\n";
2555 ss << " {\n";
2556 #else
2557 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2558 ss << " {\n";
2559 #endif
2561 else
2563 #ifdef ISNAN
2564 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2565 ss << " {\n";
2566 #else
2567 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
2568 ss << " {\n";
2569 #endif
2571 ss << " arg = ";
2572 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2573 ss << ";\n";
2574 #ifdef ISNAN
2575 ss << " if (isNan(arg))\n";
2576 ss << " continue;\n";
2577 #endif
2578 ss << " sum += pown(arg, 2);\n";
2579 ss << " }\n";
2581 else if(tmpCur->GetType() == formula::svSingleVectorRef)
2583 const formula::SingleVectorRefToken* tmpCurDVR=
2584 dynamic_cast<
2585 const formula::SingleVectorRefToken *>(tmpCur);
2586 ss << " arg = ";
2587 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2588 ss << ";\n";
2589 #ifdef ISNAN
2590 ss << " if(isNan(arg)||(gid0>=";
2591 ss << tmpCurDVR->GetArrayLength();
2592 ss << "))\n";
2593 ss << " arg = 0.0f;\n";
2594 ss << " sum += pown(arg, 2);\n";
2595 #endif
2597 else if(tmpCur->GetType() == formula::svDouble)
2599 ss << " arg = ";
2600 ss << tmpCur->GetDouble() << ";\n";
2601 ss << " sum += pown(arg, 2);\n";
2604 else
2606 ss << " arg = ";
2607 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2608 ss << ";\n";
2609 ss << " sum += pown(arg, 2);\n";
2612 ss << " return sum;\n";
2613 ss << "}";
2615 void OpSqrtPi::GenSlidingWindowFunction(std::stringstream &ss,
2616 const std::string sSymName, SubArguments &vSubArguments)
2618 ss << "\ndouble " << sSymName;
2619 ss << "_"<< BinFuncName() <<"(";
2620 for (unsigned i = 0; i < vSubArguments.size(); i++)
2622 if (i)
2623 ss << ",";
2624 vSubArguments[i]->GenSlidingWindowDecl(ss);
2626 ss << ")\n";
2627 ss << "{\n";
2628 ss << " int gid0=get_global_id(0);\n";
2629 ss << " double arg0 = 0.0f;\n";
2630 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2631 assert(tmpCur);
2632 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2634 if(tmpCur->GetType() == formula::svSingleVectorRef)
2636 const formula::SingleVectorRefToken*tmpCurDVR=
2637 dynamic_cast
2638 <const formula::SingleVectorRefToken *>(tmpCur);
2639 ss << " arg0 = ";
2640 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2641 ss << ";\n";
2642 #ifdef ISNAN
2643 ss << " if(isNan(";
2644 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2645 ss << ")||(gid0>=";
2646 ss << tmpCurDVR->GetArrayLength();
2647 ss << "))\n";
2648 ss << " { arg0 = 0.0f; }\n";
2649 #endif
2651 else if(tmpCur->GetType() == formula::svDouble)
2653 ss << " arg0=";
2654 ss << tmpCur->GetDouble() << ";\n";
2657 else
2659 ss << " arg0 = ";
2660 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2661 ss << ";\n";
2663 ss << " return (double)sqrt(arg0 *";
2664 ss << " 3.1415926535897932384626433832795f);\n";
2665 ss << "}";
2667 void OpCeil::GenSlidingWindowFunction(std::stringstream &ss,
2668 const std::string sSymName, SubArguments &vSubArguments)
2670 ss << "\ndouble " << sSymName;
2671 ss << "_"<< BinFuncName() <<"(";
2672 for (unsigned i = 0; i < vSubArguments.size(); i++)
2674 if (i) ss << ",";
2675 vSubArguments[i]->GenSlidingWindowDecl(ss);
2677 ss << ") {\n";
2678 ss << " int gid0 = get_global_id(0);\n";
2679 ss << " double num = " << GetBottom() << ";\n";
2680 ss << " double significance = " << GetBottom() << ";\n";
2681 ss << " double bAbs = 0;\n";
2682 #ifdef ISNAN
2683 ss << " if(isNan(";
2684 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2685 ss << " num = " << GetBottom() << ";\n";
2686 ss << " else\n ";
2687 #endif
2688 ss << " num = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2689 #ifdef ISNAN
2690 ss << " if(isNan(";
2691 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2692 ss << " return 0.0;\n";
2693 ss << " else\n ";
2694 #endif
2695 ss << " significance = ";
2696 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2697 if (vSubArguments.size() > 2)
2699 #ifdef ISNAN
2700 FormulaToken *bAbs = vSubArguments[2]->GetFormulaToken();
2701 if(bAbs->GetType() == formula::svSingleVectorRef)
2703 const formula::SingleVectorRefToken* tmpCurSVRIsAbs=
2704 dynamic_cast<const formula::SingleVectorRefToken*>(bAbs);
2705 ss<< " if((gid0)>=" << tmpCurSVRIsAbs->GetArrayLength() << " ||";
2707 if(bAbs->GetType() == formula::svDoubleVectorRef)
2709 const formula::DoubleVectorRefToken* tmpCurDVRIsAbs=
2710 dynamic_cast<const formula::DoubleVectorRefToken*>(bAbs);
2711 ss<< " if((gid0)>=" << tmpCurDVRIsAbs->GetArrayLength() << " ||";
2713 if(bAbs->GetType() == formula::svDouble)
2715 ss<< " if(";
2717 ss << "isNan(";
2718 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << "))\n";
2719 ss << " bAbs = 0;\n";
2720 ss << " else\n ";
2721 #endif
2722 ss << " bAbs = "<<vSubArguments[2]->GenSlidingWindowDeclRef()<<";\n";
2724 ss << " if(significance == 0.0)\n";
2725 ss << " return 0.0;\n";
2726 ss << " return ";
2727 ss << "( !(int)bAbs && num < 0.0 ? floor( num / significance ) : ";
2728 ss << "ceil( num / significance ) )";
2729 ss << "*significance;\n";
2730 ss << "}";
2732 void OpKombin::GenSlidingWindowFunction(std::stringstream &ss,
2733 const std::string sSymName, SubArguments &vSubArguments)
2735 ss << "\ndouble " << sSymName;
2736 ss << "_"<< BinFuncName() <<"(";
2737 for (unsigned i = 0; i < vSubArguments.size(); i++)
2739 if (i) ss << ",";
2740 vSubArguments[i]->GenSlidingWindowDecl(ss);
2742 ss << ") {\n";
2743 ss << " int gid0 = get_global_id(0);\n";
2744 ss << " double num = " << GetBottom() << ";\n";
2745 ss << " double num_chosen = " << GetBottom() << ";\n";
2746 ss << " double result = -1.0;\n";
2747 FormulaToken *iNum = vSubArguments[0]->GetFormulaToken();
2748 FormulaToken *iNumChosen = vSubArguments[1]->GetFormulaToken();
2750 assert(iNum);
2751 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2753 if(iNum->GetType() == formula::svSingleVectorRef &&
2754 iNumChosen->GetType() == formula::svSingleVectorRef)
2756 #ifdef ISNAN
2757 ss << " if(isNan(";
2758 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
2759 ss << " num = " << GetBottom() << ";\n";
2760 ss << " else\n ";
2761 #endif
2762 ss << " num = floor(";
2763 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2764 #ifdef ISNAN
2765 ss << " if(isNan(";
2766 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
2767 ss << " num_chosen = " << GetBottom() << ";\n";
2768 ss << " else\n ";
2769 #endif
2770 ss << " num_chosen = floor(";
2771 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2773 else if(iNum->GetType() == formula::svDouble &&
2774 iNumChosen->GetType() == formula::svDouble)
2776 ss << " num = floor(" << iNum->GetDouble() << ");\n";
2777 ss << " num_chosen = floor("<< iNumChosen->GetDouble()<< ");\n";
2780 else
2782 ss << " num = floor(";
2783 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ");\n";
2784 ss << " num_chosen = floor(";
2785 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2787 ss << " result = select(result, 0.0, (ulong)(num < num_chosen));\n";
2788 ss << " result = select(result, 1.0, (ulong)(num_chosen == 0.0));\n";
2789 ss << " if(result == 0 || result ==1)\n";
2790 ss << " return result;\n";
2791 ss << " double4 db4num;\n";
2792 ss << " double4 db4num_chosen;\n";
2793 ss << " double4 db4result;\n";
2794 ss << " double2 db2result;\n";
2795 ss << " result = 1.0;\n";
2796 ss << " int loop = num_chosen/4;\n";
2797 ss << " for(int i=0; i<loop; i++)\n";
2798 ss << " {\n";
2799 ss << " db4num = (double4){num,\n";
2800 ss << " num-1.0,\n";
2801 ss << " num-2.0,\n";
2802 ss << " num-3.0};\n";
2803 ss << " db4num_chosen = (double4){num_chosen,\n";
2804 ss << " num_chosen-1.0,\n";
2805 ss << " num_chosen-2.0,\n";
2806 ss << " num_chosen-3.0};\n";
2807 ss << " db4result = db4num * pown(db4num_chosen, -1);\n";
2808 ss << " db2result = db4result.xy * db4result.zw;\n";
2809 ss << " result *= db2result.x * db2result.y;\n";
2810 ss << " num = num - 4.0;\n";
2811 ss << " num_chosen = num_chosen - 4.0;\n";
2812 ss << " }\n";
2813 ss << " while ( num_chosen > 0){\n";
2814 ss << " result *= num / num_chosen;\n";
2815 ss << " num = num - 1.0;\n";
2816 ss << " num_chosen = num_chosen - 1.0;\n";
2817 ss << " }\n";
2818 ss << " return result;\n";
2819 ss << "}\n";
2821 void OpConvert::GenSlidingWindowFunction(
2822 std::stringstream &ss, const std::string sSymName,
2823 SubArguments &vSubArguments)
2825 int arg1=vSubArguments[1]->GetFormulaToken()->GetString().\
2826 getString().toAsciiUpperCase().hashCode();
2827 int arg2=vSubArguments[2]->GetFormulaToken()->GetString().\
2828 getString().toAsciiUpperCase().hashCode();
2829 if( !((arg1==5584&&arg2==108)||
2830 (arg1==108&&arg2==5584)||
2831 (arg1==5665&&arg2==268206)||
2832 (arg1==268206&&arg2==5665)) )
2833 throw Unhandled();
2835 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2836 const formula::SingleVectorRefToken*tmpCurDVR= dynamic_cast<const
2837 formula::SingleVectorRefToken *>(tmpCur);
2838 ss << "\ndouble " << sSymName;
2839 ss << "_"<< BinFuncName() <<"(";
2840 for (unsigned i = 0; i < vSubArguments.size(); i++)
2842 if (i)
2843 ss << ",";
2844 vSubArguments[i]->GenSlidingWindowDecl(ss);
2846 ss << ")\n{\n";
2847 ss <<" int gid0=get_global_id(0);\n";
2848 ss << " double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2849 ss << ";\n";
2850 ss << " double arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef();
2851 ss << ";\n";
2852 ss << " double arg2 = " << vSubArguments[2]->GenSlidingWindowDeclRef();
2853 ss << ";\n";
2854 #ifdef ISNAN
2855 ss<< " if(isNan(arg0)||(gid0>=";
2856 ss<<tmpCurDVR->GetArrayLength();
2857 ss<<"))\n";
2858 ss<<" arg0 = 0;\n";
2859 ss<< " if(isNan(arg1)||(gid0>=";
2860 ss<<tmpCurDVR->GetArrayLength();
2861 ss<<"))\n";
2862 ss<<" arg1 = 0;\n";
2863 ss<< " if(isNan(arg2)||(gid0>=";
2864 ss<<tmpCurDVR->GetArrayLength();
2865 ss<<"))\n";
2866 ss<<" arg2 = 0;\n";
2867 #endif
2868 ss<<" if(arg1==5584U&&arg2==108U)\n";
2869 ss<<" return arg0*1000.0;\n";
2870 ss<<" else if(arg1==108U&&arg2==3385U)\n";
2871 ss<<" return arg0/1000.0;\n";
2872 ss<<" else if(arg1==5665U&&arg2==268206U)\n";
2873 ss<<" return arg0*60.0;\n";
2874 ss<<" else if(arg1==268206U&&arg2==5665U)\n";
2875 ss<<" return arg0/60.0;\n";
2876 ss<<" else\n";
2877 ss<<" return -9999999999;\n";
2878 ss << "}\n";
2882 void OpProduct::GenSlidingWindowFunction(std::stringstream &ss,
2883 const std::string sSymName, SubArguments &vSubArguments)
2885 ss << "\ndouble " << sSymName;
2886 ss << "_"<< BinFuncName() <<"( ";
2887 for (unsigned i = 0; i < vSubArguments.size(); i++)
2889 if (i)
2890 ss << ",";
2891 vSubArguments[i]->GenSlidingWindowDecl(ss);
2893 ss << ") {\n";
2894 ss << " int gid0 = get_global_id(0);\n";
2895 ss << " int i = 0;\n";
2896 ss << " double product=0.0;\n\n";
2897 for (unsigned i = 0; i < vSubArguments.size(); i++)
2899 std::stringstream ssArgNoI;
2900 ssArgNoI << i;
2901 std::string sArgNoI = ssArgNoI.str();
2902 ss << std::string(" double arg")+sArgNoI+";\n";
2903 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2904 assert(pCur);
2905 if (pCur->GetType() == formula::svDoubleVectorRef)
2907 const formula::DoubleVectorRefToken* pDVR =
2908 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
2909 size_t nCurWindowSize = pDVR->GetRefRowSize();
2910 ss << std::string(" arg")+sArgNoI+" = ";
2911 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2912 ss <<";\n";
2913 #ifdef ISNAN
2914 ss << std::string(" if(isNan(arg")+sArgNoI+")||((gid0+i)>=";
2915 ss << pDVR->GetArrayLength();
2916 ss << "))\n";
2917 ss << " {\n";
2918 ss << std::string(" arg")+sArgNoI+" = 0;\n";
2919 ss << " }\n";
2920 #endif
2921 ss << std::string(" product = arg")+sArgNoI+";\n";
2922 ss << " for (i = ";
2923 ss << "1; i < "<< nCurWindowSize << "; i++)\n";
2924 ss << " {\n";
2925 ss << std::string(" arg")+sArgNoI+" = ";
2926 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2927 ss << ";\n";
2928 #ifdef ISNAN
2929 ss <<std::string(" if(isNan(arg")+sArgNoI+")||((gid0+i)>=";
2930 ss << pDVR->GetArrayLength();
2931 ss << "))\n";
2932 ss << " {\n";
2933 ss << std::string(" arg")+sArgNoI+" = 0;\n";
2934 ss << " }\n";
2935 #endif
2936 ss << std::string(" product*=arg")+sArgNoI+";\n";
2937 ss << " }\n";
2939 else if (pCur->GetType() == formula::svSingleVectorRef)
2941 #ifdef ISNAN
2942 ss << std::string(" arg")+sArgNoI+" = ";
2943 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2944 ss << ";\n";
2945 ss << std::string(" product*=arg")+sArgNoI+";\n";
2946 #endif
2948 else if (pCur->GetType() == formula::svDouble)
2950 #ifdef ISNAN
2951 ss << std::string(" arg")+sArgNoI+" = ";
2952 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2953 ss << ";\n";
2954 ss << std::string(" product*=arg")+sArgNoI+";\n";
2955 #endif
2958 ss << " return product;\n";
2959 ss << "}";
2961 void OpAverageIf::GenSlidingWindowFunction(std::stringstream &ss,
2962 const std::string sSymName, SubArguments &vSubArguments)
2965 ss << "\ndouble " << sSymName;
2966 ss << "_"<< BinFuncName() <<"(";
2967 for (unsigned i = 0; i < vSubArguments.size(); i++)
2969 if (i) ss << ",";
2970 vSubArguments[i]->GenSlidingWindowDecl(ss);
2972 ss << ")\n{\n";
2973 ss << " int gid0=get_global_id(0);\n";
2974 ss << " double tmp =0;\n";
2975 ss << " double count=0;\n";
2976 ss << " int singleIndex =gid0;\n";
2977 ss << " int doubleIndex;\n";
2978 ss << " int i ;\n";
2979 ss << " int j ;\n";
2980 GenTmpVariables(ss,vSubArguments);
2982 unsigned paraOneIsDoubleVector = 0;
2983 unsigned paraOneWidth = 1;
2984 unsigned paraTwoWidth = 1;
2985 unsigned paraThreeWidth = 1;
2986 unsigned loopTimes = 0;
2987 unsigned loopIndex = 0;
2988 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
2989 formula::svDoubleVectorRef)
2991 paraOneIsDoubleVector = 1;
2992 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2993 const formula::DoubleVectorRefToken*pCurDVR0= dynamic_cast<const
2994 formula::DoubleVectorRefToken *>(tmpCur0);
2995 paraOneWidth = pCurDVR0->GetArrays().size();
2996 loopTimes = paraOneWidth;
2997 if(paraOneWidth > 1)
2999 throw Unhandled();
3003 if(vSubArguments[paraOneWidth]->GetFormulaToken()->GetType() ==
3004 formula::svDoubleVectorRef)
3007 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
3008 const formula::DoubleVectorRefToken*pCurDVR1= dynamic_cast<const
3009 formula::DoubleVectorRefToken *>(tmpCur1);
3010 paraTwoWidth = pCurDVR1->GetArrays().size();
3011 if(paraTwoWidth > 1)
3013 throw Unhandled();
3015 ss << " i = ";
3016 if (!pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed()) {
3017 ss << "gid0;\n";
3018 } else {
3019 ss << "0;\n";
3021 if(!pCurDVR1->IsStartFixed() && !pCurDVR1->IsEndFixed())
3023 ss << " doubleIndex =i+gid0;\n";
3024 }else
3026 ss << " doubleIndex =i;\n";
3030 CheckSubArgumentIsNan(ss,vSubArguments,paraOneWidth);
3032 unsigned paraThreeIndex = paraOneWidth + paraTwoWidth;
3033 if(vSubArguments.size() > paraThreeIndex)
3035 if(vSubArguments[paraThreeIndex]->GetFormulaToken()->GetType() ==
3036 formula::svDoubleVectorRef)
3038 FormulaToken *tmpCur2 =
3039 vSubArguments[paraThreeIndex]->GetFormulaToken();
3040 const formula::DoubleVectorRefToken*pCurDVR2= dynamic_cast<const
3041 formula::DoubleVectorRefToken *>(tmpCur2);
3042 paraThreeWidth = pCurDVR2->GetArrays().size();
3043 if(paraThreeWidth > 1)
3045 throw Unhandled();
3050 if(paraOneIsDoubleVector)
3052 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
3053 const formula::DoubleVectorRefToken*pCurDVR0= dynamic_cast<const
3054 formula::DoubleVectorRefToken *>(tmpCur0);
3055 size_t nCurWindowSize = pCurDVR0->GetArrayLength() <
3056 pCurDVR0->GetRefRowSize() ? pCurDVR0->GetArrayLength():
3057 pCurDVR0->GetRefRowSize() ;
3059 for(loopIndex =0; loopIndex < loopTimes; loopIndex++)
3061 ss << " for (i = ";
3062 if (!pCurDVR0->IsStartFixed() && pCurDVR0->IsEndFixed()) {
3063 ss << "gid0; i < "<< nCurWindowSize <<"; i++)\n";
3064 } else if (pCurDVR0->IsStartFixed() && !pCurDVR0->IsEndFixed()) {
3065 ss << "0; i < gid0+"<< nCurWindowSize <<"; i++)\n";
3066 } else {
3067 ss << "0; i < "<< nCurWindowSize <<"; i++)\n";
3069 ss << " {\n";
3070 if(!pCurDVR0->IsStartFixed() && !pCurDVR0->IsEndFixed())
3072 ss << " doubleIndex =i+gid0;\n";
3073 }else
3075 ss << " doubleIndex =i;\n";
3078 CheckSubArgumentIsNan(ss,vSubArguments, loopIndex);
3080 ss << " if ( isequal( tmp";
3081 ss << loopIndex<<" , tmp"<<paraOneWidth<<") ) \n";
3082 ss << " {\n";
3083 if(vSubArguments.size() == paraThreeIndex)
3084 ss << " tmp += tmp"<<loopIndex<<";\n";
3085 else
3087 CheckSubArgumentIsNan(ss,vSubArguments,
3088 paraThreeIndex+loopIndex);
3089 ss << " tmp += tmp";
3090 ss << paraThreeIndex+loopIndex<<";\n";
3092 ss << " count+=1.0;\n";
3093 ss << " }\n";
3094 ss << " }\n";
3097 else
3099 CheckSubArgumentIsNan(ss,vSubArguments, 0);
3100 ss << " if ( isequal( tmp0 , tmp1 ) ) \n";
3101 ss << " {\n";
3102 if(vSubArguments.size() == 2)
3103 ss << " tmp += tmp0;\n";
3104 else
3106 CheckSubArgumentIsNan(ss,vSubArguments,2);
3107 ss << " tmp += tmp2;\n";
3109 ss << " count+=1.0;\n";
3110 ss << " }\n";
3113 ss << " if(count!=0)\n";
3114 ss << " tmp=tmp/count;\n";
3115 ss << " else\n";
3116 ss << " tmp= 0 ;\n";
3117 ss << " return tmp;\n";
3118 ss << "}";
3120 void OpDeg::GenSlidingWindowFunction(std::stringstream &ss,
3121 const std::string sSymName, SubArguments &vSubArguments)
3123 ss << "\ndouble " << sSymName;
3124 ss << "_"<< BinFuncName() <<"(";
3125 for (unsigned i = 0; i < vSubArguments.size(); i++)
3127 if (i)
3128 ss << ",";
3129 vSubArguments[i]->GenSlidingWindowDecl(ss);
3131 ss << ")\n";
3132 ss << "{\n";
3133 ss << " int gid0=get_global_id(0);\n";
3134 ss << " double arg0 = 0.0f;\n";
3135 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
3136 assert(tmpCur);
3137 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
3139 if(tmpCur->GetType() == formula::svSingleVectorRef)
3141 const formula::SingleVectorRefToken*tmpCurDVR=
3142 dynamic_cast
3143 <const formula::SingleVectorRefToken *>(tmpCur);
3144 ss << " arg0 = ";
3145 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3146 ss << ";\n";
3147 #ifdef ISNAN
3148 ss << " if(isNan(";
3149 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3150 ss << ")||(gid0>=";
3151 ss << tmpCurDVR->GetArrayLength();
3152 ss << "))\n";
3153 ss << " { arg0 = 0.0f; }\n";
3154 #endif
3156 else if(tmpCur->GetType() == formula::svDouble)
3158 ss << " arg0=";
3159 ss << tmpCur->GetDouble() << ";\n";
3162 else
3164 ss << " arg0 = ";
3165 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3166 ss << ";\n";
3168 ss << " return arg0 * pown(M_PI, -1) * 180;;\n";
3169 ss << "}";
3172 void OpFact::GenSlidingWindowFunction(std::stringstream& ss,
3173 const std::string sSymName, SubArguments& vSubArguments)
3175 ss << "\ndouble " << sSymName;
3176 ss << "_" << BinFuncName() << "(";
3177 for (unsigned i = 0; i < vSubArguments.size(); i++)
3179 if (i)
3180 ss << ",";
3181 vSubArguments[i]->GenSlidingWindowDecl(ss);
3183 ss << ") {\n";
3184 ss << " double tmp = " << GetBottom() << ";\n";
3185 ss << " int gid0 = get_global_id(0);\n";
3186 ss << " double arg0 = " << GetBottom() << ";\n";
3187 FormulaToken* pCur = vSubArguments[0]->GetFormulaToken();
3188 assert(pCur);
3189 if (pCur->GetType() == formula::svSingleVectorRef)
3191 #ifdef ISNAN
3192 const formula::SingleVectorRefToken* pSVR =
3193 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
3194 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3195 #endif
3197 else if (pCur->GetType() == formula::svDouble)
3199 #ifdef ISNAN
3200 ss << " {\n";
3201 #endif
3203 #ifdef ISNAN
3204 if(ocPush==vSubArguments[0]->GetFormulaToken()->GetOpCode())
3206 ss << " if (isNan(";
3207 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3208 ss << "))\n";
3209 ss << " arg0 = 0;\n";
3210 ss << " else\n";
3211 ss << " arg0 = ";
3212 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3213 ss << " arg0 = floor(arg0);\n";
3214 ss << " if (arg0 < 0.0)\n";
3215 ss << " return 0.0;\n";
3216 ss << " else if (arg0 == 0.0)\n";
3217 ss << " return 1.0;\n";
3218 ss << " else if (arg0 <= 170.0)\n";
3219 ss << " {\n";
3220 ss << " double fTemp = arg0;\n";
3221 ss << " while (fTemp > 2.0)\n";
3222 ss << " {\n";
3223 ss << " fTemp = fTemp - 1;\n";
3224 ss << " arg0 = arg0 * fTemp;\n";
3225 ss << " }\n";
3226 ss << " }\n";
3227 ss << " else\n";
3228 ss << " return -DBL_MAX;\n";
3229 ss << " }\n";
3231 else
3233 ss << " arg0 = ";
3234 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3236 #else
3237 ss << " arg0 = ";
3238 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3239 #endif
3240 ss << " return arg0;\n";
3241 ss << "}";
3243 void OpQuotient::GenSlidingWindowFunction(std::stringstream &ss,
3244 const std::string sSymName, SubArguments &vSubArguments)
3246 ss << "\ndouble " << sSymName;
3247 ss << "_"<< BinFuncName() <<"(";
3248 for (unsigned i = 0; i < vSubArguments.size(); i++)
3250 if (i) ss << ",";
3251 vSubArguments[i]->GenSlidingWindowDecl(ss);
3253 ss << ") {\n";
3254 ss << " int gid0 = get_global_id(0);\n";
3255 ss << " double num1 = 1.0;\n";
3256 ss << " double num2 = 1.0;\n";
3257 #ifdef ISNAN
3258 ss << " if(isNan(";
3259 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << "))\n";
3260 ss << " num1 = 1.0;\n";
3261 ss << " else \n ";
3262 #endif
3263 ss << " num1 = " << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
3264 #ifdef ISNAN
3265 ss << " if(isNan(";
3266 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << "))\n";
3267 ss << " num2 = 1.0;\n";
3268 ss << " else \n ";
3269 #endif
3270 ss << " num2 = " << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
3271 ss << " return trunc(num1/num2);\n";
3272 ss << "}";
3274 void OpSeriesSum::GenSlidingWindowFunction(std::stringstream &ss,
3275 const std::string sSymName, SubArguments &vSubArguments)
3277 if( vSubArguments.size() != 4){return;}
3278 ss << "\ndouble " << sSymName;
3279 ss << "_"<< BinFuncName() <<"(";
3280 for (unsigned i = 0; i < vSubArguments.size(); i++)
3282 if (i)
3283 ss << ",";
3284 vSubArguments[i]->GenSlidingWindowDecl(ss);
3286 ss << ")\n";
3287 ss << "{\n";
3288 ss << " int gid0=get_global_id(0);\n";
3289 ss << " double var[3], coeff, res = 0.0f;\n";
3290 FormulaToken *tmpCur;
3291 for(int i = 0; i < 3; ++i)
3293 tmpCur = vSubArguments[i]->GetFormulaToken();
3294 assert(tmpCur);
3295 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
3297 if(tmpCur->GetType() == formula::svSingleVectorRef)
3299 const formula::SingleVectorRefToken* tmpCurDVR=
3300 dynamic_cast<
3301 const formula::SingleVectorRefToken *>(tmpCur);
3302 ss << " var["<<i<<"] = ";
3303 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3304 ss << ";\n";
3305 #ifdef ISNAN
3306 ss << " if(isNan(var["<<i<<"])||(gid0>=";
3307 ss << tmpCurDVR->GetArrayLength();
3308 ss << "))\n";
3309 ss << " var["<<i<<"] = 0;\n";
3310 #endif
3312 else if(tmpCur->GetType() == formula::svDouble)
3314 ss << " var["<<i<<"] = ";
3315 ss << tmpCur->GetDouble() << ";\n";
3318 else
3320 ss << " var["<<i<<"] = ";
3321 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3322 ss << ";\n";
3325 tmpCur = vSubArguments[3]->GetFormulaToken();
3326 assert(tmpCur);
3327 if(ocPush == vSubArguments[3]->GetFormulaToken()->GetOpCode())
3329 //TODO DoubleVector
3330 if (tmpCur->GetType() == formula::svDoubleVectorRef)
3332 const formula::DoubleVectorRefToken* pDVR =
3333 dynamic_cast<const formula::DoubleVectorRefToken *>(tmpCur);
3334 size_t nCurWindowSize = pDVR->GetRefRowSize();
3335 ss << " int j = 0;\n";
3336 ss << " for (int i = ";
3337 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
3339 #ifdef ISNAN
3340 ss << "gid0; i < " << pDVR->GetArrayLength();
3341 ss << " && i < " << nCurWindowSize << "; ++i)\n";
3342 ss << " {\n";
3343 #else
3344 ss << "gid0; i < "<< nCurWindowSize << "; ++i)\n";
3345 ss << " {\n";
3346 #endif
3348 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
3350 #ifdef ISNAN
3351 ss << "0; i < " << pDVR->GetArrayLength();
3352 ss << " && i < gid0+"<< nCurWindowSize << "; ++i)\n";
3353 ss << " {\n";
3354 #else
3355 ss << "0; i < gid0+"<< nCurWindowSize << "; ++i)\n";
3356 ss << " {\n";
3357 #endif
3359 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
3361 #ifdef ISNAN
3362 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3363 ss << " && i < "<< nCurWindowSize << "; ++i)\n";
3364 ss << " {\n";
3365 #else
3366 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
3367 ss << " {\n";
3368 #endif
3370 else
3372 #ifdef ISNAN
3373 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
3374 ss << " {\n";
3375 #else
3376 ss << "0; i < "<< nCurWindowSize << "; ++i)\n";
3377 ss << " {\n";
3378 #endif
3380 ss << " coeff = ";
3381 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3382 ss << ";\n";
3383 #ifdef ISNAN
3384 ss << " if (isNan(coeff))\n";
3385 ss << " continue;\n";
3386 #endif
3387 ss << " res = res + coeff * pow(var[0],";
3388 ss << " var[1] + j * var[2]);\n";
3389 ss << " ++j;\n";
3390 ss << " }\n";
3392 else if(tmpCur->GetType() == formula::svSingleVectorRef)
3394 const formula::SingleVectorRefToken* tmpCurDVR=
3395 dynamic_cast<
3396 const formula::SingleVectorRefToken *>(tmpCur);
3397 ss << " coeff = ";
3398 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3399 ss << ";\n";
3400 #ifdef ISNAN
3401 ss << " if(isNan(coeff)||(gid0>=";
3402 ss << tmpCurDVR->GetArrayLength();
3403 ss << "))\n";
3404 ss << " return 0;\n";
3405 #endif
3408 ss << " return res;\n";
3409 ss << "}";
3413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */