LibreOffice Module sc (master)  1
op_financial.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #include "op_financial.hxx"
11 
12 #include <formula/vectortoken.hxx>
13 #include <sstream>
14 
15 using namespace formula;
16 
17 namespace sc::opencl {
18 // Definitions of inline functions
19 #include "opinlinefun_finacial.cxx"
20 
21 void RRI::GenSlidingWindowFunction(
22  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
23 {
24  ss << "\ndouble " << sSymName;
25  ss << "_"<< BinFuncName() <<"(";
26  for (size_t i = 0; i < vSubArguments.size(); i++)
27  {
28  if (i)
29  ss << ",";
30  vSubArguments[i]->GenSlidingWindowDecl(ss);
31  }
32  ss << ") {\n";
33  ss << " double tmp = " << GetBottom() <<";\n";
34  ss << " int gid0 = get_global_id(0);\n";
35  ss << " double fv;\n";
36  ss << " double pv;\n";
37  ss << " double nper;\n";
38  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
39  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
41 
42  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
43  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
45 
46  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
47  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
49 
50  ss<< " int buffer_nper_len = ";
51  ss<< tmpCurDVR0->GetArrayLength();
52  ss << ";\n";
53 
54  ss<< " int buffer_pv_len = ";
55  ss<< tmpCurDVR1->GetArrayLength();
56  ss << ";\n";
57 
58  ss<< " int buffer_fv_len = ";
59  ss<< tmpCurDVR2->GetArrayLength();
60  ss << ";\n";
61 
62  ss<<" if(gid0>=buffer_nper_len || isnan(";
63  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
64  ss<<"))\n";
65  ss<<" nper = 0;\n\telse \n";
66  ss<<" nper = ";
67  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
68  ss<<";\n";
69 
70  ss<<" if(gid0>=buffer_pv_len || isnan(";
71  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
72  ss<<"))\n";
73  ss<<" pv = 0;\n\telse \n";
74  ss<<" pv = ";
75  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
76  ss<<";\n";
77 
78  ss<<" if(gid0>=buffer_pv_len || isnan(";
79  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
80  ss<<"))\n";
81  ss<<" fv = 0;\n\telse \n";
82  ss<<" fv = ";
83  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
84  ss<<";\n";
85  ss << " tmp = pow(fv*pow(pv,-1),1.0*pow(nper,-1))-1;\n";
86  ss << " return tmp;\n";
87  ss << "}";
88 }
89 
90 void OpNominal::GenSlidingWindowFunction(
91  std::stringstream &ss, const std::string &sSymName, SubArguments &
92 vSubArguments)
93 {
94  ss << "\ndouble " << sSymName;
95  ss << "_"<< BinFuncName() <<"(";
96  for (size_t i = 0; i < vSubArguments.size(); i++)
97  {
98  if (i)
99  ss << ",";
100  vSubArguments[i]->GenSlidingWindowDecl(ss);
101  }
102  ss << ") {\n\t";
103  ss << "double tmp = 0;\n\t";
104  ss << "double temp = 0;\n\t";
105  ss << "int gid0 = get_global_id(0);\n\t";
106  ss << "double tmp0=0,tmp1=0;\n";
107  for (size_t i = 0; i < vSubArguments.size(); i++)
108  {
109  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
110  assert(pCur);
111  if (pCur->GetType() == formula::svSingleVectorRef)
112  {
113  const formula::SingleVectorRefToken* pSVR =
114  static_cast< const formula::SingleVectorRefToken* >(pCur);
115  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
116  }
117  else if (pCur->GetType() == formula::svDouble)
118  {
119  ss << "{\n";
120  }
121 
122  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
123  {
124  ss <<" temp="<<vSubArguments[i]->GenSlidingWindowDeclRef();
125  ss <<";\n";
126  ss <<" if (isnan(temp))\n";
127  ss <<" tmp"<<i<<"= 0;\n";
128  ss <<" else\n";
129  ss <<" tmp"<<i<<"=temp;\n";
130  ss <<" }\n";
131  }
132  else
133  {
134  ss <<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef(
135 );
136  ss <<";\n";
137  }
138  }
139  ss<<"if(tmp1==0)\n\t";
140  ss<<"\treturn 0;\n\t";
141  ss<<"tmp=pow( tmp1,-1);\n\t";
142  ss<<"tmp=( pow( tmp0+ 1.0, tmp ) - 1.0 ) *";
143  ss<<"tmp1;\n\t";
144  ss << "return tmp;\n";
145  ss << "}";
146 }
147 
148 void OpDollarde::GenSlidingWindowFunction(
149  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
150 {
151  ss << "\ndouble " << sSymName;
152  ss << "_"<< BinFuncName() <<"(";
153  for (size_t i = 0; i < vSubArguments.size(); i++)
154  {
155  if (i)
156  ss << ",";
157  vSubArguments[i]->GenSlidingWindowDecl(ss);
158  }
159  ss << ") {\n\t";
160  ss << "double tmp = " << GetBottom() <<";\n\t";
161  ss << "int gid0 = get_global_id(0);\n\t";
162  ss << "double fInt = " << GetBottom() <<";\n\t";
163  ss << "double dollar;\n\t";
164  ss << "double fFrac;\n\t";
165  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
166  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
167  formula::SingleVectorRefToken *>(tmpCur0);
168  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
169  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
170  formula::SingleVectorRefToken *>(tmpCur1);
171  ss<< "int buffer_dollar_len = ";
172  ss<< tmpCurDVR0->GetArrayLength();
173  ss << ";\n\t";
174  ss<< "int buffer_frac_len = ";
175  ss<< tmpCurDVR1->GetArrayLength();
176  ss << ";\n\t";
177  ss<<"if((gid0)>=buffer_dollar_len || isnan(";
178  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
179  ss<<"))\n\t\t";
180  ss<<"dollar = 0;\n\telse \n\t\t";
181  ss<<"dollar = ";
182  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
183  ss<<";\n\t";
184  ss<<"if((gid0)>=buffer_frac_len || isnan(";
185  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
186  ss<<"))\n\t\t";
187  ss<<"fFrac = 0;\n\telse \n\t\t";
188  ss<<"fFrac = (int)(";
189  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
190  ss<<");\n\t";
191  ss << "tmp = modf( dollar , &fInt );\n\t";
192  ss << "tmp /= fFrac;\n\t";
193  ss << "tmp *= pow( 10.0 , ceil( log10(fFrac ) ) );\n\t";
194  ss << "tmp += fInt;\t";
195  ss << "\n\treturn tmp;\n";
196  ss << "}";
197 }
198 
199 void OpDollarfr::GenSlidingWindowFunction(std::stringstream &ss,
200  const std::string &sSymName, SubArguments &vSubArguments)
201 {
202  ss << "\ndouble " << sSymName;
203  ss << "_"<< BinFuncName() <<"(";
204  for (size_t i = 0; i < vSubArguments.size(); i++)
205  {
206  if (i)
207  ss << ",";
208  vSubArguments[i]->GenSlidingWindowDecl(ss);
209  }
210  ss << ") {\n\t";
211  ss << "double tmp = " << GetBottom() <<";\n\t";
212  ss << "int gid0 = get_global_id(0);\n\t";
213  ss << "double fInt = " << GetBottom() <<";\n\t";
214  ss << "double dollar;\n\t";
215  ss << "double fFrac;\n\t";
216  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
217  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
218  formula::SingleVectorRefToken *>(tmpCur0);
219  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
220  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
221  formula::SingleVectorRefToken *>(tmpCur1);
222  ss<< "int buffer_dollar_len = ";
223  ss<< tmpCurDVR0->GetArrayLength();
224  ss << ";\n\t";
225  ss<< "int buffer_frac_len = ";
226  ss<< tmpCurDVR1->GetArrayLength();
227  ss << ";\n\t";
228  ss<<"if((gid0)>=buffer_dollar_len || isnan(";
229  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
230  ss<<"))\n\t\t";
231  ss<<"dollar = 0;\n\telse \n\t\t";
232  ss<<"dollar = ";
233  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
234  ss<<";\n\t";
235  ss<<"if((gid0)>=buffer_frac_len || isnan(";
236  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
237  ss<<"))\n\t\t";
238  ss<<"fFrac = 0;\n\telse \n\t\t";
239  ss<<"fFrac = (int)(";
240  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
241  ss<<");\n\t";
242  ss << "tmp = modf( dollar , &fInt );\n\t";
243  ss << "tmp *= fFrac;\n\t";
244  ss << "tmp *= pow( 10.0 , -ceil( log10(fFrac ) ) );\n\t";
245  ss << "tmp += fInt;\t";
246  ss << "\n\treturn tmp;\n";
247  ss << "}";
248 }
250 void OpDISC::BinInlineFun(std::set<std::string>& decls,
251  std::set<std::string>& funs)
252 {
253  decls.insert(GetYearFrac_newDecl);decls.insert(DaysToDate_newDecl);
254  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
255  funs.insert(GetYearFrac_new);funs.insert(DaysToDate_new);
256  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
257 }
258 
259 void OpDISC::GenSlidingWindowFunction(std::stringstream& ss,
260  const std::string &sSymName, SubArguments& vSubArguments)
261 {
262  ss << "\ndouble " << sSymName;
263  ss << "_" << BinFuncName() << "(";
264  for (size_t i = 0; i < vSubArguments.size(); i++)
265  {
266  if (i)
267  ss << ",";
268  vSubArguments[i]->GenSlidingWindowDecl(ss);
269  }
270  ss << ") {\n\t";
271  ss << " double tmp = " << GetBottom() << ";\n";
272  ss << " int gid0 = get_global_id(0);\n";
273  ss << " double arg0 = " << GetBottom() << ";\n";
274  ss << " double arg1 = " << GetBottom() << ";\n";
275  ss << " double arg2 = " << GetBottom() << ";\n";
276  ss << " double arg3 = " << GetBottom() << ";\n";
277  ss << " double arg4 = " << GetBottom() << ";\n";
278  for (size_t i = 0; i < vSubArguments.size(); i++)
279  {
280  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
281  assert(pCur);
283  {
284  const formula::SingleVectorRefToken* pSVR =
285  static_cast< const formula::SingleVectorRefToken* >(pCur);
286  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
287  }
288  else if (pCur->GetType() == formula::svDouble)
289  {
290  ss << " {\n";
291  }
292  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
293  {
294  ss << " if (isnan(";
295  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
296  ss << "))\n";
297  ss << " arg" << i << " = 0;\n";
298  ss << " else\n";
299  ss << " arg" << i << " = ";
300  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
301  ss << " }\n";
302  }
303  else
304  {
305  ss << " arg" << i << " = ";
306  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
307  }
308  }
309  ss << " int nNullDate = 693594;\n";
310  ss << " tmp = 1.0 - arg2 / arg3;\n";
311  ss << " tmp /=";
312  ss << " GetYearFrac_new(nNullDate, (int)arg0, (int)arg1, (int)arg4);\n";
313  ss << " return tmp;\n";
314  ss << "}";
315 }
316 
317 void OpINTRATE::BinInlineFun(std::set<std::string>& decls,
318  std::set<std::string>& funs)
319 {
320  decls.insert(GetYearDiff_newDecl);decls.insert(GetDiffDate_newDecl);
321  decls.insert(DaysToDate_newDecl);decls.insert(GetNullDateDecl);
322  decls.insert(DateToDaysDecl);decls.insert(DaysInMonthDecl);
323  decls.insert(IsLeapYearDecl);
324  funs.insert(GetYearDiff_new);funs.insert(GetDiffDate_new);
325  funs.insert(DaysToDate_new);funs.insert(GetNullDate);
326  funs.insert(DateToDays);funs.insert(DaysInMonth);
327  funs.insert(IsLeapYear);
328 }
330 void OpINTRATE::GenSlidingWindowFunction(std::stringstream& ss,
331  const std::string &sSymName, SubArguments& vSubArguments)
332 {
333  ss << "\ndouble " << sSymName;
334  ss << "_" << BinFuncName() << "(";
335  for (size_t i = 0; i < vSubArguments.size(); i++)
336  {
337  if (i)
338  ss << ",";
339  vSubArguments[i]->GenSlidingWindowDecl(ss);
340  }
341  ss << ") {\n";
342  ss << " double tmp = " << GetBottom() << ";\n";
343  ss << " int gid0 = get_global_id(0);\n";
344  ss << " double arg0 = " << GetBottom() << ";\n";
345  ss << " double arg1 = " << GetBottom() << ";\n";
346  ss << " double arg2 = " << GetBottom() << ";\n";
347  ss << " double arg3 = " << GetBottom() << ";\n";
348  ss << " double arg4 = " << GetBottom() << ";\n";
349  for (size_t i = 0; i < vSubArguments.size(); i++)
350  {
351  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
352  assert(pCur);
353  if (pCur->GetType() == formula::svSingleVectorRef)
354  {
355  const formula::SingleVectorRefToken* pSVR =
356  static_cast< const formula::SingleVectorRefToken* >(pCur);
357  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
358  }
359  else if (pCur->GetType() == formula::svDouble)
360  {
361  ss << " {\n";
362  }
363  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
364  {
365  ss << " if (isnan(";
366  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
367  ss << "))\n";
368  ss << " arg" << i << " = 0;\n";
369  ss << " else\n";
370  ss << " arg" << i << " = ";
371  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
372  ss << " }\n";
373  }
374  else
375  {
376  ss << " arg" << i << " = ";
377  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
378  }
379  }
380  ss << " int nNullDate = GetNullDate();\n";
381  ss << " tmp = ((arg3 / arg2) - 1) / GetYearDiff_new(nNullDate, (int)arg0,";
382  ss << " (int)arg1,(int)arg4);\n";
383  ss << " return tmp;\n";
384  ss << "}";
385 }
386 
387 void OpFV::BinInlineFun(std::set<std::string>& decls,
388  std::set<std::string>& funs)
389 {
390  decls.insert(GetFVDecl);
391  funs.insert(GetFV);
392 }
393 
394 void OpFV::GenSlidingWindowFunction(std::stringstream& ss,
395  const std::string &sSymName, SubArguments& vSubArguments)
396 {
397  ss << "\ndouble " << sSymName;
398  ss << "_"<< BinFuncName() <<"(";
399  for (size_t i = 0; i < vSubArguments.size(); i++)
400  {
401  if (i)
402  ss << ",";
403  vSubArguments[i]->GenSlidingWindowDecl(ss);
404  }
405  ss << ") {\n";
406  ss << " double tmp = " << GetBottom() << ";\n";
407  ss << " int gid0 = get_global_id(0);\n";
408  ss << " double arg0 = " << GetBottom() << ";\n";
409  ss << " double arg1 = " << GetBottom() << ";\n";
410  ss << " double arg2 = " << GetBottom() << ";\n";
411  ss << " double arg3 = " << GetBottom() << ";\n";
412  ss << " double arg4 = " << GetBottom() << ";\n";
413  unsigned j = vSubArguments.size();
414  while (j--)
415  {
416  FormulaToken* pCur = vSubArguments[j]->GetFormulaToken();
417  assert(pCur);
418  if(pCur->GetType() == formula::svSingleVectorRef)
419  {
420  const formula::SingleVectorRefToken* pSVR =
421  static_cast< const formula::SingleVectorRefToken* >(pCur);
422  ss << " if(gid0 >= " << pSVR->GetArrayLength() << " || isnan(";
423  ss << vSubArguments[j]->GenSlidingWindowDeclRef();
424  ss << "))\n";
425  ss << " arg" << j << " = " <<GetBottom() << ";\n";
426  ss << " else\n";
427  ss << " arg" << j << " = ";
428  ss << vSubArguments[j]->GenSlidingWindowDeclRef();
429  ss << ";\n";
430  }
431  }
432  ss << " tmp = GetFV(arg0, arg1, arg2, arg3, arg4);\n";
433  ss << " return tmp;\n";
434  ss << "}";
435 }
436 
437 void OpIPMT::BinInlineFun(std::set<std::string>& decls,
438  std::set<std::string>& funs)
439 {
440  decls.insert(GetFVDecl);
441  funs.insert(GetFV);
442 }
443 
444 void OpIPMT::GenSlidingWindowFunction(std::stringstream& ss,
445  const std::string &sSymName, SubArguments& vSubArguments)
446 {
447  ss << "\ndouble " << sSymName;
448  ss << "_"<< BinFuncName() <<"(";
449  for (size_t i = 0; i < vSubArguments.size(); i++)
450  {
451  if (i)
452  ss << ",";
453  vSubArguments[i]->GenSlidingWindowDecl(ss);
454  }
455  ss << ") {\n";
456  ss << " double tmp = " << GetBottom() << ";\n";
457  ss << " int gid0 = get_global_id(0);\n";
458  ss << " double arg0 = " << GetBottom() << ";\n";
459  ss << " double arg1 = " << GetBottom() << ";\n";
460  ss << " double arg2 = " << GetBottom() << ";\n";
461  ss << " double arg3 = " << GetBottom() << ";\n";
462  ss << " double arg4 = " << GetBottom() << ";\n";
463  ss << " double arg5 = " << GetBottom() << ";\n";
464  unsigned j = vSubArguments.size();
465  while (j--)
466  {
467  FormulaToken* pCur = vSubArguments[j]->GetFormulaToken();
468  assert(pCur);
470  {
471  const formula::SingleVectorRefToken* pSVR =
472  static_cast< const formula::SingleVectorRefToken* >(pCur);
473  ss << " if(gid0 >= " << pSVR->GetArrayLength() << " || isnan(";
474  ss << vSubArguments[j]->GenSlidingWindowDeclRef();
475  ss << "))\n";
476  ss << " arg" << j << " = " <<GetBottom() << ";\n";
477  ss << " else\n";
478  ss << " arg" << j << " = ";
479  ss << vSubArguments[j]->GenSlidingWindowDeclRef();
480  ss << ";\n";
481  }
482  }
483  ss << " double pmt ;\n";
484  ss << " if(arg0 == 0.0)\n";
485  ss << " return 0;\n";
486  ss << " double temp1 = 0;\n";
487  ss << " double abl = pow(1.0 + arg0, arg2);\n";
488  ss << " temp1 -= arg4;\n";
489  ss << " temp1 -= arg3 * abl;\n";
490  ss << " pmt = temp1 / (1.0 + arg0 * arg5) /";
491  ss << " ( (abl - 1.0) / arg0);\n";
492  ss << " double temp = pow( 1 + arg0, arg1 - 2);\n";
493  ss << " if(arg1 == 1.0)\n";
494  ss << " {\n";
495  ss << " if(arg5 > 0.0)\n";
496  ss << " tmp = 0.0;\n";
497  ss << " else\n";
498  ss << " tmp = -arg3;\n";
499  ss << " }\n";
500  ss << " else\n";
501  ss << " {\n";
502  ss << " if(arg5 > 0.0)\n";
503  ss << " tmp = GetFV(arg0, arg1 - 2.0, pmt, arg3, 1.0)";
504  ss << " - pmt;\n";
505  ss << " else\n";
506  ss << " tmp = GetFV(arg0, arg1 - 1.0, pmt, arg3, 0.0);\n";
507  ss << " }\n";
508  ss << " tmp = tmp * arg0;\n";
509  ss << " return tmp;\n";
510  ss << "}";
511 }
512 void OpISPMT::GenSlidingWindowFunction(std::stringstream& ss,
513  const std::string &sSymName, SubArguments& vSubArguments)
514 {
515  ss << "\ndouble " << sSymName;
516  ss << "_" << BinFuncName() <<"(";
517  for (size_t i = 0; i < vSubArguments.size(); i++)
518  {
519  if (i)
520  ss << ",";
521  vSubArguments[i]->GenSlidingWindowDecl(ss);
522  }
523  ss << ") {\n";
524  ss << " double tmp = " << GetBottom() << ";\n";
525  ss << " int gid0 = get_global_id(0);\n";
526  ss << " double arg0 = " << GetBottom() << ";\n";
527  ss << " double arg1 = " << GetBottom() << ";\n";
528  ss << " double arg2 = " << GetBottom() << ";\n";
529  ss << " double arg3 = " << GetBottom() << ";\n";
530  for (size_t i = 0; i < vSubArguments.size(); i++)
531  {
532  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
533  assert(pCur);
534  if (pCur->GetType() == formula::svSingleVectorRef)
535  {
536  const formula::SingleVectorRefToken* pSVR =
537  static_cast< const formula::SingleVectorRefToken* >(pCur);
538  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
539  }
540  else if (pCur->GetType() == formula::svDouble)
541  {
542  ss << " {\n";
543  }
544  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
545  {
546  ss << " if (isnan(";
547  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
548  ss << "))\n";
549  ss << " arg" << i << " = 0;\n";
550  ss << " else\n";
551  ss << " arg" << i << " = ";
552  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
553  ss << " }\n";
554  }
555  else
556  {
557  ss << " arg" << i << " = ";
558  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
559  }
560  }
561  ss << " tmp = arg3 * arg0 * ( arg1 - arg2) * pow(arg2, -1);\n";
562  ss << " return tmp;\n";
563  ss << "}";
564 }
565 
566 void OpPDuration::GenSlidingWindowFunction(std::stringstream& ss,
567  const std::string &sSymName, SubArguments& vSubArguments)
568 {
569  ss << "\ndouble " << sSymName;
570  ss << "_" << BinFuncName() <<"(";
571  for (size_t i = 0; i < vSubArguments.size(); i++)
572  {
573  if (i)
574  ss << ",";
575  vSubArguments[i]->GenSlidingWindowDecl(ss);
576  }
577  ss << ") {\n";
578  ss << " double tmp = " << GetBottom() << ";\n";
579  ss << " int gid0 = get_global_id(0);\n";
580  ss << " double arg0 = " << GetBottom() << ";\n";
581  ss << " double arg1 = " << GetBottom() << ";\n";
582  ss << " double arg2 = " << GetBottom() << ";\n";
583  for (size_t i = 0; i < vSubArguments.size(); i++)
584  {
585  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
586  assert(pCur);
587  if (pCur->GetType() == formula::svSingleVectorRef)
588  {
589  const formula::SingleVectorRefToken* pSVR =
590  static_cast< const formula::SingleVectorRefToken* >(pCur);
591  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
592  }
593  else if (pCur->GetType() == formula::svDouble)
594  {
595  ss << " {\n";
596  }
597  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
598  {
599  ss << " if (isnan(";
600  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
601  ss << "))\n";
602  ss << " arg" << i << " = 0;\n";
603  ss << " else\n";
604  ss << " arg" << i << " = ";
605  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
606  ss << " }\n";
607  }
608  else
609  {
610  ss << " arg" << i << " = ";
611  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
612  }
613  }
614  ss << " tmp = log(arg2 * pow( arg1,-1)) / log(arg0 + 1.0);\n";
615  ss << " return tmp;\n";
616  ss << "}";
617 }
618 
619 void OpDuration_ADD::BinInlineFun(std::set<std::string>& decls,
620  std::set<std::string>& funs)
621 {
622  decls.insert(GetDurationDecl);decls.insert(lcl_GetcoupnumDecl);
623  decls.insert(GetYearFracDecl);decls.insert(DaysToDateDecl);
624  decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
625  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
626  funs.insert(GetDuration);funs.insert(lcl_Getcoupnum);
627  funs.insert(GetYearFrac);funs.insert(DaysToDate);
628  funs.insert(GetNullDate);funs.insert(DateToDays);
629  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
630 }
631 
632 void OpDuration_ADD::GenSlidingWindowFunction(std::stringstream& ss,
633  const std::string &sSymName, SubArguments& vSubArguments)
634 {
635  ss << "\ndouble " << sSymName;
636  ss << "_"<< BinFuncName() <<"(";
637  for (size_t i = 0; i < vSubArguments.size(); i++)
638  {
639  if (i)
640  ss << ",";
641  vSubArguments[i]->GenSlidingWindowDecl(ss);
642  }
643  ss << ") {\n";
644  ss << " double tmp = " << GetBottom() << ";\n";
645  ss << " int gid0 = get_global_id(0);\n";
646  ss << " double arg0 = " << GetBottom() << ";\n";
647  ss << " double arg1 = " << GetBottom() << ";\n";
648  ss << " double arg2 = " << GetBottom() << ";\n";
649  ss << " double arg3 = " << GetBottom() << ";\n";
650  ss << " double arg4 = " << GetBottom() << ";\n";
651  ss << " double arg5 = " << GetBottom() << ";\n";
652  unsigned j = vSubArguments.size();
653  while (j--)
654  {
655  FormulaToken* pCur = vSubArguments[j]->GetFormulaToken();
656  assert(pCur);
657  if(pCur->GetType() == formula::svSingleVectorRef)
658  {
659  const formula::SingleVectorRefToken* pSVR =
660  static_cast< const formula::SingleVectorRefToken* >(pCur);
661  ss << " if(gid0 >= " << pSVR->GetArrayLength() << " || isnan(";
662  ss << vSubArguments[j]->GenSlidingWindowDeclRef();
663  ss << "))\n";
664  ss << " arg" << j << " = " <<GetBottom() << ";\n";
665  ss << " else\n";
666  ss << " arg" << j << " = ";
667  ss << vSubArguments[j]->GenSlidingWindowDeclRef();
668  ss << ";\n";
669  }
670  }
671  ss << " int nNullDate = GetNullDate();\n";
672  ss << " tmp = GetDuration( nNullDate, (int)arg0, (int)arg1, arg2,";
673  ss << " arg3, (int)arg4, (int)arg5);\n";
674  ss << " return tmp;\n";
675  ss << "}";
676 }
677 void OpMDuration::BinInlineFun(std::set<std::string>& decls,
678  std::set<std::string>& funs)
679 {
680  decls.insert(GetDuration_newDecl);decls.insert(lcl_Getcoupnum_newDecl);
681  decls.insert(addMonthsDecl);decls.insert(checklessthanDecl);
682  decls.insert(setDayDecl);decls.insert(ScaDateDecl);
683  decls.insert(GetYearFracDecl);decls.insert(DaysToDateDecl);
684  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
685  funs.insert(GetDuration_new);funs.insert(lcl_Getcoupnum_new);
686  funs.insert(addMonths);funs.insert(checklessthan);
687  funs.insert(setDay);funs.insert(ScaDate);
688  funs.insert(GetYearFrac);funs.insert(DaysToDate);
689  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
690 }
691 
692 void OpMDuration::GenSlidingWindowFunction(std::stringstream& ss,
693  const std::string &sSymName, SubArguments& vSubArguments)
694 {
695  ss << "\ndouble " << sSymName;
696  ss << "_"<< BinFuncName() <<"(";
697  for (size_t i = 0; i < vSubArguments.size(); i++)
698  {
699  if (i)
700  ss << ",";
701  vSubArguments[i]->GenSlidingWindowDecl(ss);
702  }
703  ss << ") {\n";
704  ss << " double tmp = " << GetBottom() << ";\n";
705  ss << " int gid0 = get_global_id(0);\n";
706  ss << " double arg0 = " << GetBottom() << ";\n";
707  ss << " double arg1 = " << GetBottom() << ";\n";
708  ss << " double arg2 = " << GetBottom() << ";\n";
709  ss << " double arg3 = " << GetBottom() << ";\n";
710  ss << " double arg4 = " << GetBottom() << ";\n";
711  ss << " double arg5 = " << GetBottom() << ";\n";
712  for (size_t i = 0; i < vSubArguments.size(); i++)
713  {
714  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
715  assert(pCur);
716  if (pCur->GetType() == formula::svSingleVectorRef)
717  {
718  const formula::SingleVectorRefToken* pSVR =
719  static_cast< const formula::SingleVectorRefToken* >(pCur);
720  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
721  }
722  else if (pCur->GetType() == formula::svDouble)
723  {
724  ss << " {\n";
725  }
726  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
727  {
728  ss << " if (isnan(";
729  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
730  ss << "))\n";
731  ss << " arg" << i << " = 0;\n";
732  ss << " else\n";
733  ss << " arg" << i << " = ";
734  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
735  ss << " }\n";
736  }
737  else
738  {
739  ss << " arg" << i << " = ";
740  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
741  }
742  }
743  ss << " int nNullDate = 693594;\n";
744  ss << " tmp = GetDuration_new( nNullDate, (int)arg0, (int)arg1, arg2,";
745  ss << " arg3, (int)arg4, (int)arg5);\n";
746  ss << " tmp = tmp * pow(1.0 + arg3 * pow((int)arg4, -1.0), -1);\n";
747  ss << " return tmp;\n";
748  ss << "}";
749 }
750 void Fvschedule::GenSlidingWindowFunction(
751  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
752 {
753  CHECK_PARAMETER_COUNT( 2, 2 );
754  FormulaToken* pCur = vSubArguments[1]->GetFormulaToken();
755  assert(pCur);
756  if(vSubArguments[0]->GetFormulaToken()->GetType() != formula::svDoubleVectorRef)
757  throw Unhandled( __FILE__, __LINE__ );
758  const formula::DoubleVectorRefToken* pCurDVR =
759  static_cast<const formula::DoubleVectorRefToken *>(pCur);
760  size_t nCurWindowSize = pCurDVR->GetRefRowSize();
761  ss << "\ndouble " << sSymName;
762  ss << "_"<< BinFuncName() <<"(";
763  for (size_t i = 0; i < vSubArguments.size(); i++)
764  {
765  if (i)
766  ss << ",";
767  vSubArguments[i]->GenSlidingWindowDecl(ss);
768  }
769  ss << ") {\n\t";
770  ss << "double tmp = 1.0;\n\t";
771  ss << "int gid0 = get_global_id(0);\n\t";
772  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
773  ss << ";\n\t";
774  ss << "if (isnan(arg0))\n\t\t";
775  ss << "arg0 = 0;\n\t";
776  ss << "double arg1;\n\t";
777  ss << "int arrayLength = " << pCurDVR->GetArrayLength() << ";\n\t";
778  ss << "for (int i = 0; i + gid0 < arrayLength &&";
779  ss << " i < " << nCurWindowSize << "; i++){\n\t\t";
780  ss << "arg1 = ";
781  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n\t\t\t";
782  ss << "if (isnan(arg1))\n\t\t\t\t";
783  ss << "arg1 = 0;\n\t\t\t";
784  ss << "tmp *= arg1 + 1.0;\n\t\t";
785  ss << "}\n\t";
786  ss << "return (double)tmp * arg0";
787  ss << ";\n}";
788 }
789 void Cumipmt::BinInlineFun(std::set<std::string>& decls,
790  std::set<std::string>& funs)
791 {
792  decls.insert(GetPMT_newDecl); decls.insert(GetFV_newDecl);
793  funs.insert(GetPMT_new);funs.insert(GetFV_new);
794 }
795 void Cumipmt::GenSlidingWindowFunction(
796  std::stringstream &ss, const std::string &sSymName, SubArguments &
797 vSubArguments)
798 {
799  ss << "\ndouble " << sSymName;
800  ss << "_"<< BinFuncName() <<"(";
801  for (size_t i = 0; i < vSubArguments.size(); i++)
802  {
803  if (i)
804  ss << ",";
805  vSubArguments[i]->GenSlidingWindowDecl(ss);
806  }
807  ss << ") {\n";
808  ss << " int gid0 = get_global_id(0);\n";
809  ss << " double fRate,fVal;\n";
810  ss << " int nStartPer,nEndPer,nNumPeriods,nPayType;\n";
811  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
812  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
813  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
814  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
815  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
816  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
817  if(tmpCur0->GetType() == formula::svSingleVectorRef)
818  {
819  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
820  formula::SingleVectorRefToken *>(tmpCur0);
821  ss <<" if(gid0 >= "<<tmpCurDVR0->GetArrayLength()<<" || isnan(";
822  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
823  ss <<"))\n";
824  ss <<" fRate = 0;\n else\n";
825  }
826  ss <<" fRate = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
827  ss <<";\n";
828  if(tmpCur1->GetType() == formula::svSingleVectorRef)
829  {
830  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
831  formula::SingleVectorRefToken *>(tmpCur1);
832  ss <<" if(gid0 >= "<<tmpCurDVR1->GetArrayLength()<<" || isnan(";
833  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
834  ss <<"))\n";
835  ss <<" nNumPeriods = 0;\n else\n";
836  }
837  ss <<" nNumPeriods = (int)";
838  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
839  ss <<";\n";
840  if(tmpCur2->GetType() == formula::svSingleVectorRef)
841  {
842  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
843  formula::SingleVectorRefToken *>(tmpCur2);
844  ss <<" if(gid0 >= "<<tmpCurDVR2->GetArrayLength()<<" || isnan(";
845  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
846  ss <<"))\n";
847  ss <<" fVal = 0;\n else\n";
848  }
849  ss <<" fVal = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
850  ss <<";\n";
851  if(tmpCur3->GetType() == formula::svSingleVectorRef)
852  {
853  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
854  formula::SingleVectorRefToken *>(tmpCur3);
855  ss <<" if(gid0 >= "<<tmpCurDVR3->GetArrayLength()<<" || isnan(";
856  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
857  ss <<"))\n";
858  ss <<" nStartPer = 0;\n else\n";
859  }
860  ss <<" nStartPer = (int)";
861  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
862  ss <<";\n";
863  if(tmpCur4->GetType() == formula::svSingleVectorRef)
864  {
865  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
866  formula::SingleVectorRefToken *>(tmpCur4);
867  ss <<" if(gid0 >= "<<tmpCurDVR4->GetArrayLength()<<" || isnan(";
868  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
869  ss <<"))\n";
870  ss <<" nEndPer = 0;\n else\n";
871  }
872  ss <<" nEndPer = (int)";
873  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
874  ss <<";\n";
876  if(tmpCur5->GetType() == formula::svSingleVectorRef)
877  {
878  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
879  formula::SingleVectorRefToken *>(tmpCur5);
880  ss <<" if(gid0 >= "<<tmpCurDVR5->GetArrayLength()<<" || isnan(";
881  ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
882  ss <<"))\n";
883  ss <<" nPayType = 0;\n else\n";
884  }
885  ss <<" nPayType = (int)"<<vSubArguments[5]->GenSlidingWindowDeclRef();
886  ss <<";\n";
887  ss <<" double fPmt;\n";
888  ss <<" fPmt = GetPMT_new( fRate, nNumPeriods, fVal, 0.0, nPayType );\n";
889  ss <<" double tmp = 0.0;\n";
890  ss <<" if( nStartPer == 1 )\n";
891  ss <<" {\n";
892  ss <<" if( nPayType <= 0 )\n";
893  ss <<" tmp = -fVal;\n";
894  ss <<" nStartPer++;\n";
895  ss <<" }\n";
896  ss <<" for( ; nStartPer<= nEndPer ; nStartPer++ )\n";
897  ss <<" {\n";
898  ss <<" if( nPayType > 0 )\n";
899  ss <<" tmp += GetFV_new( fRate, nStartPer - 2 , ";
900  ss <<"fPmt, fVal, 1 ) - fPmt;\n";
901  ss <<" else\n";
902  ss <<" tmp += GetFV_new( fRate, nStartPer - 1 , ";
903  ss <<"fPmt, fVal, 0 );\n";
904  ss <<" }\n";
905  ss <<" tmp *= fRate;\n";
906  ss <<" return tmp;\n";
907  ss <<"}";
908 }
909 
910 void IRR::GenSlidingWindowFunction(std::stringstream &ss,
911  const std::string &sSymName, SubArguments &vSubArguments)
912 {
913  ss << "\ndouble " << sSymName;
914  ss << "_" << BinFuncName() << "(";
915  for (size_t i = 0; i < vSubArguments.size(); i++)
916  {
917  if (i)
918  ss << ",";
919  vSubArguments[i]->GenSlidingWindowDecl(ss);
920  }
921  ss << ") {\n";
922  ss << " #define Epsilon 1.0E-7\n";
923  ss << " int gid0 = get_global_id(0);\n";
924  FormulaToken* pSur = vSubArguments[1]->GetFormulaToken();
925  assert(pSur);
926  ss << " double fEstimated = ";
927  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
928  ss << " double fEps = 1.0;\n";
929  ss << " double x = 0.0, xNew = 0.0, fNumerator = 0.0, fDenominator = 0.0;\n";
930  ss << " double nCount = 0.0;\n";
931  if (pSur->GetType() == formula::svSingleVectorRef)
932  {
933  const formula::SingleVectorRefToken* pSVR =
934  static_cast< const formula::SingleVectorRefToken* >(pSur);
935  ss << " if (gid0 >= " << pSVR->GetArrayLength() << ")\n";
936  ss << " fEstimated = 0.1;\n";
937  ss << " if (isnan(fEstimated))\n";
938  ss << " x = 0.1;\n";
939  ss << " else\n";
940  }
941  else if (pSur->GetType() == formula::svDouble)
942  {
943  ss << " if (isnan(fEstimated))\n";
944  ss << " x = 0.1;\n";
945  ss << " else\n";
946  }
947  ss << " x = fEstimated;\n";
948  ss << " unsigned short nItCount = 0;\n";
949  ss << " while (fEps > Epsilon && nItCount < 20){\n";
950  ss << " nCount = 0.0; fNumerator = 0.0; fDenominator = 0.0;\n";
951  ss << " double arg0, arg1;\n";
952  ss << " int i = 0;\n";
953  FormulaToken* pCur = vSubArguments[0]->GetFormulaToken();
954  assert(pCur);
956  static_cast<const formula::DoubleVectorRefToken* >(pCur);
957  size_t nCurWindowSize = pDVR->GetRefRowSize();
958  ss << " for ( ";
959  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
960  ss << "i = gid0; i < " << pDVR->GetArrayLength();
961  ss << " && i < " << nCurWindowSize << " /2*2; i++){\n";
962  ss << " arg0 = ";
963  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
964  ss << " i++;;\n";
965  ss << " arg1 = ";
966  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
967  ss << " if (!isnan(arg0)){\n";
968  ss << " fNumerator += arg0 / pow(1.0 + x, nCount);\n";
969  ss << " fDenominator+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
970  ss << " nCount += 1;\n";
971  ss << " }\n";
972  ss << " if (!isnan(arg1)){\n";
973  ss << " fNumerator += arg1 / pow(1.0 + x, nCount);\n";
974  ss << " fDenominator+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
975  ss << " nCount += 1;\n";
976  ss << " }\n";
977  ss << " }\n";
978  ss << "if(i < " << pDVR->GetArrayLength();
979  ss << " && i < " << nCurWindowSize << ") ;{\n";
980  }
981  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
982  ss << "; i < " << pDVR->GetArrayLength();
983  ss << " && i < (gid0+" << nCurWindowSize << " )/2*2; i++){\n";
984  ss << " arg0 = ";
985  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
986  ss << " if (!isnan(arg0)){\n";
987  ss << " fNumerator += arg0 / pow(1.0 + x, nCount);\n";
988  ss << " fDenominator+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
989  ss << " nCount += 1;\n";
990  ss << " }\n";
991  ss << " i++;\n";
992  ss << " arg1 = ";
993  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
994  ss << " if (!isnan(arg1)){\n";
995  ss << " fNumerator += arg1 / pow(1.0 + x, nCount);\n";
996  ss << " fDenominator+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
997  ss << " nCount+=1;\n";
998  ss << " }\n";
999  ss << " }\n";
1000  ss << " if(i < " << pDVR->GetArrayLength();
1001  ss << " && i < gid0+" << nCurWindowSize << "){\n";
1002  }
1003  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
1004  ss << " ; i + gid0 < " << pDVR->GetArrayLength();
1005  ss << " && i < " << nCurWindowSize << " /2*2; i++){\n";
1006  ss << " arg0 = ";
1007  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1008  ss << " i++;;\n";
1009  ss << " arg1 = ";
1010  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1011  ss << " if (!isnan(arg0)){\n";
1012  ss << " fNumerator += arg0 / pow(1.0 + x, nCount);\n";
1013  ss << " fDenominator+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
1014  ss << " nCount += 1;\n";
1015  ss << " }\n";
1016  ss << " if (!isnan(arg1)){\n";
1017  ss << " fNumerator += arg1 / pow(1.0 + x, nCount);\n";
1018  ss << " fDenominator+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
1019  ss << " nCount+=1;\n";
1020  ss << " }\n";
1021  ss << " }\n";
1022  ss << " if(i + gid0 < " << pDVR->GetArrayLength() << " &&";
1023  ss << " i < " << nCurWindowSize << "){\n";
1024 
1025  } else {
1026  ss << "; i < " << nCurWindowSize << " /2*2; i++){\n";
1027  ss << " arg0 = ";
1028  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1029  ss << " i++;;\n";
1030  ss << " arg1 = ";
1031  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1032  ss << " if (!isnan(arg0)){\n";
1033  ss << " fNumerator += arg0 / pow(1.0 + x, nCount);\n";
1034  ss << " fDenominator+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
1035  ss << " nCount += 1;\n";
1036  ss << " }\n";
1037  ss << " if (!isnan(arg1)){\n";
1038  ss << " fNumerator += arg1 / pow(1.0 + x, nCount);\n";
1039  ss << " fDenominator+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
1040  ss << " nCount+=1;\n";
1041  ss << " }\n";
1042  ss << " }\n";
1043  ss << "if(i<" << nCurWindowSize << "){\n";
1044 
1045  }
1046  ss << " arg0 = ";
1047  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1048  ss << " if (isnan(arg0))\n";
1049  ss << " continue;\n";
1050  ss << " fNumerator += arg0 / pow(1.0+x, nCount);\n";
1051  ss << " fDenominator += -nCount * arg0 / pow(1.0+x,nCount+1.0);\n";
1052  ss << " nCount+=1;\n";
1053  ss << " }\n";
1054  ss << " xNew = x - fNumerator / fDenominator;\n";
1055  ss << " fEps = fabs(xNew - x);\n";
1056  ss << " x = xNew;\n";
1057  ss << " nItCount++;\n }\n";
1058  ss << " if (fEstimated == 0.0 && fabs(x) < Epsilon)\n";
1059  ss << " x = 0.0;\n";
1060  ss << " if (fEps < Epsilon)\n";
1061  ss << " return x;\n";
1062  ss << " else\n";
1063  // FIXME: This is of course horribly wrong. 523 is the error code NoConvergence, and this should
1064  // be CreateDoubleError(523). Ditto for the other occurrences of 523 in the OpenCL code
1065  // generated in this file.
1066  ss << " return (double)523;\n";
1067  ss << "}";
1068 }
1069 
1070 void XNPV::GenSlidingWindowFunction(
1071  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1072 {
1073  FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
1074  assert(pCur);
1075  const formula::DoubleVectorRefToken* pCurDVR =
1076  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1077  size_t nCurWindowSize = pCurDVR->GetRefRowSize();
1078  ss << "\ndouble " << sSymName;
1079  ss << "_"<< BinFuncName() <<"( ";
1080  for (size_t i = 0; i < vSubArguments.size(); i++)
1081  {
1082  if (i)
1083  ss << ",";
1084  vSubArguments[i]->GenSlidingWindowDecl(ss);
1085  }
1086 
1087  ss << ") {\n\t";
1088  ss << "double result = 0.0;\n\t";
1089  ss << "int gid0 = get_global_id(0);\n\t";
1090  ss << "int i=0;\n\t";
1091  ss << "double date;\n\t";
1092  ss << "double value;\n\t";
1093  ss << "double rate;\n\t";
1094  ss << "double dateNull;\n\t";
1095  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1096  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1097  formula::SingleVectorRefToken *>(tmpCur0);
1098 
1099  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1100  const formula::DoubleVectorRefToken*tmpCurDVR1= static_cast<const
1101  formula::DoubleVectorRefToken *>(tmpCur1);
1102 
1103  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1104  const formula::DoubleVectorRefToken*tmpCurDVR2= static_cast<const
1105  formula::DoubleVectorRefToken *>(tmpCur2);
1106  ss<< "int buffer_rate_len = ";
1107  ss<< tmpCurDVR0->GetArrayLength();
1108  ss << ";\n\t";
1109  ss<< "int buffer_value_len = ";
1110  ss<< tmpCurDVR1->GetArrayLength();
1111  ss << ";\n\t";
1112  ss<< "int buffer_date_len = ";
1113  ss<< tmpCurDVR2->GetArrayLength();
1114  ss << ";\n\t";
1115  ss<<"if((gid0)>=buffer_date_len || isnan(";
1116  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1117  ss<<"))\n\t\t";
1118  ss<<"return NAN;\n\telse \n";
1119  ss<<"dateNull = ";
1120  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1121  ss<<";\n\t";
1122  ss<<"if((gid0)>=buffer_rate_len || isnan(";
1123  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1124  ss<<"))\n\t\t";
1125  ss<<"return NAN;\n\telse \n";
1126  ss<<"rate = ";
1127  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1128  ss<<";\n\t";
1129  ss<<"if(1 == buffer_date_len )\n";
1130  ss<<"return ";
1131  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1132  ss<<";\n\t";
1133  ss << "for (int i = ";
1134  if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
1135  {
1136  ss << "gid0; i < "<< nCurWindowSize <<"; i++)\n\t\t";
1137  }
1138  else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
1139  {
1140  ss << "0; i < gid0+"<< nCurWindowSize <<"; i++)\n\t\t";
1141  }
1142  else
1143  {
1144  ss << "0; i < "<< nCurWindowSize <<"; i++)\n\t\t";
1145  }
1146  ss << "{\n\t";
1147  if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
1148  {
1149  ss << "if((i+gid0)>=buffer_value_len || (i+gid0)>=buffer_date_len)\n\t\t";
1150  ss << "return result;\n\telse \n\t\t";
1151  }
1152  else
1153  {
1154  ss << "if(i>=buffer_value_len || i>=buffer_date_len)\n\t\t";
1155  ss << "return result;\n\telse \n\t\t";
1156  }
1157 
1158  ss << "value = ";
1159  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
1160  ss << ";\n";
1161  ss << " date = ";
1162  ss << vSubArguments[2]->GenSlidingWindowDeclRef(true);
1163  ss << ";\n";
1164  ss << "result += value/(pow((rate+1),(date-dateNull)/365));\n";
1165  ss << "}\n";
1166  ss << "return result;\n";
1167  ss << "}";
1168 }
1169 
1170  void PriceMat::BinInlineFun(std::set<std::string>& decls,
1171  std::set<std::string>& funs)
1172 {
1173  decls.insert(GetYearFracDecl);decls.insert(GetNullDateDecl);
1174  decls.insert(DateToDaysDecl);decls.insert(DaysToDateDecl);
1175  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
1176 
1177  funs.insert(GetYearFrac);funs.insert(GetNullDate);
1178  funs.insert(DateToDays);funs.insert(DaysToDate);
1179  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
1180 }
1181 void PriceMat::GenSlidingWindowFunction(
1182  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1183 {
1184  ss << "\ndouble " << sSymName;
1185  ss << "_"<< BinFuncName() <<"(";
1186  for (size_t i = 0; i < vSubArguments.size(); i++)
1187  {
1188  if (i)
1189  ss << ",";
1190  vSubArguments[i]->GenSlidingWindowDecl(ss);
1191  }
1192  ss << ") {\n\t";
1193  ss << "int gid0 = get_global_id(0);\n\t";
1194  ss << "double result=0;\n\t";
1195  ss<< "int nNullDate = GetNullDate( );\n\t";
1196  ss <<"int settle;\n\t";
1197  ss <<"int mat;\n\t";
1198  ss <<"int issue;\n\t";
1199  ss <<"double rate;\n\t";
1200  ss <<"double yield;\n\t";
1201  ss <<"int nBase;\n\t";
1202  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1203  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1204  formula::SingleVectorRefToken *>(tmpCur0);
1205  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1206  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
1207  formula::SingleVectorRefToken *>(tmpCur1);
1208  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1209  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1210  formula::SingleVectorRefToken *>(tmpCur2);
1211  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1212  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
1213  formula::SingleVectorRefToken *>(tmpCur3);
1214  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
1215  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
1216  formula::SingleVectorRefToken *>(tmpCur4);
1217  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
1218  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
1219  formula::SingleVectorRefToken *>(tmpCur5);
1220 
1221  ss<< "int buffer_settle_len = ";
1222  ss<< tmpCurDVR0->GetArrayLength();
1223  ss << ";\n\t";
1224  ss<< "int buffer_mat_len = ";
1225  ss<< tmpCurDVR1->GetArrayLength();
1226  ss << ";\n\t";
1227  ss<< "int buffer_issue_len = ";
1228  ss<< tmpCurDVR2->GetArrayLength();
1229  ss << ";\n\t";
1230  ss<< "int buffer_rate_len = ";
1231  ss<< tmpCurDVR3->GetArrayLength();
1232  ss << ";\n\t";
1233  ss<< "int buffer_yield_len = ";
1234  ss<< tmpCurDVR4->GetArrayLength();
1235  ss << ";\n\t";
1236  ss<< "int buffer_base_len = ";
1237  ss<< tmpCurDVR5->GetArrayLength();
1238  ss << ";\n\t";
1239  ss<<"if(gid0>=buffer_settle_len || isnan(";
1240  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1241  ss<<"))\n\t\t";
1242  ss<<"settle = 0;\n\telse \n\t\t";
1243  ss<<"settle = ";
1244  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1245  ss<<";\n\t";
1246  ss<<"if(gid0>=buffer_mat_len || isnan(";
1247  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1248  ss<<"))\n\t\t";
1249  ss<<"mat = 0;\n\telse \n\t\t";
1250  ss<<"mat = ";
1251  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1252  ss<<";\n\t";
1253  ss<<"if(gid0>=buffer_issue_len || isnan(";
1254  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1255  ss<<"))\n\t\t";
1256  ss<<"issue = 0;\n\telse \n\t\t";
1257  ss<<"issue = ";
1258  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1259  ss<<";\n\t";
1260  ss<<"if(gid0>=buffer_rate_len || isnan(";
1261  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1262  ss<<"))\n\t\t";
1263  ss<<"rate = 0;\n\telse \n\t\t";
1264  ss<<"rate = ";
1265  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1266  ss<<";\n\t";
1267  ss<<"if(gid0>=buffer_yield_len || isnan(";
1268  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
1269  ss<<"))\n\t\t";
1270  ss<<"yield = 0;\n\telse \n\t\t";
1271  ss<<"yield = ";
1272  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
1273  ss<<";\n\t";
1274  ss<<"if(gid0>=buffer_base_len || isnan(";
1275  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
1276  ss<<"))\n\t\t";
1277  ss<<"nBase = 0;\n\telse \n\t\t";
1278  ss<<"nBase = ";
1279  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
1280  ss<<";\n\t";
1281  ss<< "double fIssMat = GetYearFrac( nNullDate, issue, mat, nBase);\n";
1282  ss<<"double fIssSet = GetYearFrac( nNullDate, issue, settle,nBase);\n";
1283  ss<<"double fSetMat = GetYearFrac( nNullDate, settle, mat, nBase);\n";
1284  ss<<"result = 1.0 + fIssMat * rate;\n\t";
1285  ss<<"result /= 1.0 + fSetMat * yield;\n\t";
1286  ss<<"result -= fIssSet * rate;\n\t";
1287  ss<<"result*= 100.0;\n\t";
1288  ss<<"return result;\n\t";
1289  ss<<"}\n";
1290 }
1291 
1292 void OpSYD::GenSlidingWindowFunction(std::stringstream &ss,
1293  const std::string &sSymName, SubArguments &vSubArguments)
1294 {
1295  ss << "\ndouble " << sSymName;
1296  ss << "_"<< BinFuncName() <<"(";
1297  for (size_t i = 0; i < vSubArguments.size(); i++)
1298  {
1299  if (i)
1300  ss << ",";
1301  vSubArguments[i]->GenSlidingWindowDecl(ss);
1302  }
1303  ss << ") {\n";
1304  ss << " int gid0 = get_global_id(0);\n";
1305  ss << " double result=0;\n";
1306  ss << " double cost;\n";
1307  ss << " double salvage;\n";
1308  ss << " double life;\n";
1309  ss << " double period;\n";
1310  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1311  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1313 
1314  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1315  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
1316  formula::SingleVectorRefToken *>(tmpCur1);
1317 
1318  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1319  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1320  formula::SingleVectorRefToken *>(tmpCur2);
1321 
1322  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1323  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
1324  formula::SingleVectorRefToken *>(tmpCur3);
1325 
1326  ss << " int buffer_cost_len = ";
1327  ss << tmpCurDVR0->GetArrayLength();
1328  ss << ";\n";
1330  ss << " int buffer_salvage_len = ";
1331  ss << tmpCurDVR1->GetArrayLength();
1332  ss << ";\n";
1333 
1334  ss << " int buffer_life_len = ";
1335  ss << tmpCurDVR2->GetArrayLength();
1336  ss << ";\n";
1337  ss << " int buffer_period_len = ";
1338  ss << tmpCurDVR3->GetArrayLength();
1339  ss << ";\n";
1340 
1341  ss <<" if(gid0>=buffer_cost_len || isnan(";
1342  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1343  ss <<"))\n";
1344  ss <<" cost = 0;\n\telse \n";
1345  ss <<" cost = ";
1346  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1347  ss <<";\n";
1348  ss <<" if(gid0>=buffer_salvage_len || isnan(";
1349  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1350  ss <<"))\n";
1351  ss <<" salvage = 0;\n\telse \n";
1352  ss <<" salvage = ";
1353  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1354  ss <<";\n";
1355  ss <<" if(gid0>=buffer_life_len || isnan(";
1356  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1357  ss <<"))\n";
1358  ss <<" life = 0;\n\telse \n";
1359  ss <<" life = ";
1360  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1361  ss <<";\n";
1362  ss <<" if(gid0>=buffer_period_len || isnan(";
1363  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1364  ss <<"))\n";
1365  ss <<" period = 0;\n\telse \n";
1366  ss <<" period = ";
1367  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1368  ss <<";\n";
1369  ss <<" double tmpvalue = ((life*(life+1))*pow(2.0,-1));\n";
1370  ss <<" result = ((cost-salvage)*(life-period+1)";
1371  ss << "*pow(tmpvalue,-1));\n";
1372  ss <<" return result;\n";
1373  ss <<"}\n";
1374 }
1375 
1376 void MIRR::GenSlidingWindowFunction(
1377  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1378 {
1379  FormulaToken* pCur = vSubArguments[0]->GetFormulaToken();
1380  assert(pCur);
1381  const formula::DoubleVectorRefToken* pCurDVR =
1382  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1383  size_t nCurWindowSize = pCurDVR->GetRefRowSize();
1384  FormulaToken* pCur1 = vSubArguments[1]->GetFormulaToken();
1385  assert(pCur1);
1386  const formula::SingleVectorRefToken* pSVR1 =
1387  static_cast< const formula::SingleVectorRefToken* >(pCur1);
1388  assert(pSVR1);
1389  FormulaToken* pCur2 = vSubArguments[2]->GetFormulaToken();
1390  assert(pCur2);
1391  const formula::SingleVectorRefToken* pSVR2 =
1392  static_cast< const formula::SingleVectorRefToken* >(pCur2);
1393  assert(pSVR2);
1394 
1395  ss << "\ndouble " << sSymName;
1396  ss << "_"<< BinFuncName() <<"(";
1397  for (size_t i = 0; i < vSubArguments.size(); i++)
1398  {
1399  if (i)
1400  ss << ",";
1401  vSubArguments[i]->GenSlidingWindowDecl(ss);
1402  }
1403  ss << ") {\n\t";
1404  ss << "double tmp = " << GetBottom() <<";\n\t";
1405  ss << "int gid0 = get_global_id(0);\n\t";
1406  ss << "double arg0, arg1, arg2;\n\t";
1407  ss << "arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef();
1408  ss << ";\n\t";
1409  ss << "arg2 = " << vSubArguments[2]->GenSlidingWindowDeclRef();
1410  ss << ";\n\t";
1411  ss << "int argLen1 = " << pSVR1->GetArrayLength() << ";\n\t";
1412  ss << "int argLen2 = " << pSVR2->GetArrayLength() << ";\n\t";
1413  ss << "if (gid0 >= argLen1)\n\t\t";
1414  ss << "arg1 = 0.0;\n\t";
1415  ss << "if (gid0 >= argLen2)\n\t\t";
1416  ss << "arg2 = 0.0;\n\t";
1417  ss << "if (isnan(arg1))\n\t\t";
1418  ss << "arg1 = 0.0;\n\t";
1419  ss << "if (isnan(arg2))\n\t\t";
1420  ss << "arg2 = 0.0;\n\t";
1421  ss << "double invest = arg1 + 1.0;\n\t";
1422  ss << "double reinvest = arg2 + 1.0;\n\t";
1423  ss << "double NPV_invest = 0.0;\n\t";
1424  ss << "double Pow_invest = 1.0;\n\t";
1425  ss << "double NPV_reinvest = 0.0;\n\t";
1426  ss << "double Pow_reinvest = 1.0;\n\t";
1427  ss << "int nCount = 0;\n\t";
1428  ss << "int arrayLength = " << pCurDVR->GetArrayLength() << ";\n\t";
1429  ss << "for (int i = 0; i + gid0 < arrayLength &&";
1430  ss << " i < " << nCurWindowSize << "; i++){\n\t\t";
1431  ss << "arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1432  ss << ";\n\t\t";
1433  ss << "if (isnan(arg0))\n\t\t\t";
1434  ss << "continue;\n\t\t";
1435  ss << "if (arg0 > 0.0)\n\t\t\t";
1436  ss << "NPV_reinvest += arg0 * Pow_reinvest;\n\t\t";
1437  ss << "else if (arg0 < 0.0)\n\t\t\t";
1438  ss << "NPV_invest += arg0 * Pow_invest;\n\t\t";
1439  ss << "Pow_reinvest /= reinvest;\n\t\t";
1440  ss << "Pow_invest /= invest;\n\t\t";
1441  ss << "nCount++;\n\t";
1442  ss << "}\n\t";
1443  ss << "tmp = ";
1444  ss << "-NPV_reinvest /NPV_invest * pow(reinvest,(double)nCount-1);\n\t";
1445  ss << "tmp = pow(tmp, 1.0 / (nCount - 1)) - 1.0;\n\t";
1446  ss << "return (double)tmp;\n";
1447  ss << "}";
1448 }
1449 
1450 void OpEffective::GenSlidingWindowFunction(std::stringstream& ss,
1451  const std::string &sSymName, SubArguments& vSubArguments)
1452 {
1453  ss << "\ndouble " << sSymName;
1454  ss << "_" << BinFuncName() << "(";
1455  for (size_t i = 0; i < vSubArguments.size(); i++)
1456  {
1457  if (i)
1458  ss << ",";
1459  vSubArguments[i]->GenSlidingWindowDecl(ss);
1460  }
1461  ss << ") {\n";
1462  ss << " double tmp = " << GetBottom() <<";\n";
1463  ss << " int gid0 = get_global_id(0);\n\t";
1464  ss << " double arg0 = " << GetBottom() << ";\n";
1465  ss << " double arg1 = " << GetBottom() << ";\n";
1466  for (size_t i = 0; i < vSubArguments.size(); i++)
1467  {
1468  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
1469  assert(pCur);
1470  if (pCur->GetType() == formula::svSingleVectorRef)
1471  {
1472  const formula::SingleVectorRefToken* pSVR =
1473  static_cast< const formula::SingleVectorRefToken* >(pCur);
1474  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
1475  }
1476  else if (pCur->GetType() == formula::svDouble)
1477  {
1478  ss << " {\n";
1479  }
1480  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
1481  {
1482  ss << " if (isnan(";
1483  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1484  ss << "))\n";
1485  ss << " arg" << i << " = 0;\n";
1486  ss << " else\n";
1487  ss << " arg" << i << " = ";
1488  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1489  ss << " }\n";
1490  }
1491  else
1492  {
1493  ss << " arg" << i << " = ";
1494  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1495  }
1496  }
1497  ss << " tmp = pow(1.0 + arg0 * pow(arg1, -1), arg1)-1.0;\n";
1498  ss << " return tmp;\n";
1499  ss << "}";
1500 }
1501 
1502  void OpTbilleq::BinInlineFun(std::set<std::string>& decls,
1503  std::set<std::string>& funs)
1504 {
1505  decls.insert(GetDiffDate360_Decl);decls.insert(GetDiffDate360Decl);
1506  decls.insert(DateToDaysDecl);decls.insert(DaysToDate_LocalBarrierDecl);
1507  decls.insert(DaysInMonthDecl);decls.insert(GetNullDateDecl);
1508  decls.insert(IsLeapYearDecl);
1509  funs.insert(GetDiffDate360_);funs.insert(GetDiffDate360);
1510  funs.insert(DateToDays);funs.insert(DaysToDate_LocalBarrier);
1511  funs.insert(DaysInMonth);funs.insert(GetNullDate);
1512  funs.insert(IsLeapYear);
1513 }
1514 void OpTbilleq::GenSlidingWindowFunction(
1515  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1516 {
1517  ss << "\ndouble " << sSymName;
1518  ss << "_"<< BinFuncName() <<"(";
1519  for (size_t i = 0; i < vSubArguments.size(); i++)
1520  {
1521  if (i)
1522  ss << ",";
1523  vSubArguments[i]->GenSlidingWindowDecl(ss);
1524  }
1525  ss << ") {\n\t";
1526  ss << " int gid0 = get_global_id(0);\n";
1527  ss << "double tmp = 0;\n\t";
1528  ss << "double tmp000;\n\t";
1529  ss << "double tmp001;\n\t";
1530  ss << "double tmp002;\n\t";
1531 
1532  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1533  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1535 
1536  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1537  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
1539 
1540  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1541  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1542  formula::SingleVectorRefToken *>(tmpCur2);
1543 
1544  ss<< "int buffer_tmp000_len = ";
1545  ss<< tmpCurDVR0->GetArrayLength();
1546  ss << ";\n\t";
1547 
1548  ss<< "int buffer_tmp001_len = ";
1549  ss<< tmpCurDVR1->GetArrayLength();
1550  ss << ";\n\t";
1551 
1552  ss<< "int buffer_tmp002_len = ";
1553  ss<< tmpCurDVR2->GetArrayLength();
1554  ss << ";\n\t";
1555 
1556  ss<<"if(gid0>=buffer_tmp000_len || isnan(";
1557  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1558  ss<<"))\n\t\t";
1559  ss<<"tmp000 = 0;\n\telse \n\t\t";
1560  ss<<"tmp000 = ";
1561  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1562  ss<<";\n\t";
1563 
1564  ss<<"if(gid0>=buffer_tmp001_len || isnan(";
1565  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1566  ss<<"))\n\t\t";
1567  ss<<"tmp001 = 0;\n\telse \n\t\t";
1568  ss<<"tmp001 = ";
1569  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1570  ss<<";\n\t";
1571 
1572  ss<<"if(gid0>=buffer_tmp002_len || isnan(";
1573  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1574  ss<<"))\n\t\t";
1575  ss<<"tmp002 = 0;\n\telse \n\t\t";
1576  ss<<"tmp002 = ";
1577  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1578  ss<<";\n\t";
1579 
1580  ss<<"tmp001+=1.0;\n";
1581  ss<<"int nDiff =GetDiffDate360(GetNullDate(),tmp000,tmp001,true);\n";
1582  ss<<"tmp =( 365 * tmp002 ) / ( 360 - ( tmp002 * ( nDiff ) ) );\n";
1583  ss << "return tmp;\n";
1584  ss << "}";
1585 }
1586 void OpCumprinc::BinInlineFun(std::set<std::string>& decls,
1587  std::set<std::string>& funs)
1588 {
1589  decls.insert(GetPMT_newDecl); decls.insert(GetFV_newDecl);
1590  funs.insert(GetPMT_new);funs.insert(GetFV_new);
1591 }
1592 void OpCumprinc::GenSlidingWindowFunction(std::stringstream &ss,
1593  const std::string &sSymName, SubArguments &vSubArguments)
1594 {
1595  ss << "\ndouble " << sSymName;
1596  ss << "_"<< BinFuncName() <<"(";
1597  for (size_t i = 0; i < vSubArguments.size(); i++)
1598  {
1599  if (i)
1600  ss << ",";
1601  vSubArguments[i]->GenSlidingWindowDecl(ss);
1602  }
1603  ss << ") {\n";
1604  ss << " double tmp = " << GetBottom() <<";\n";
1605  ss << " int gid0 = get_global_id(0);\n";
1606  ss << " double fRate,fVal;\n";
1607  ss << " int nStartPer,nEndPer,nNumPeriods,nPayType;\n";
1608  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1609  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1610  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1611  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1612  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
1613  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
1614  if(tmpCur0->GetType() == formula::svSingleVectorRef)
1615  {
1616  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1617  formula::SingleVectorRefToken *>(tmpCur0);
1618  ss <<" if(gid0 >= "<<tmpCurDVR0->GetArrayLength()<<" || isnan(";
1619  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
1620  ss <<"))\n";
1621  ss <<" fRate = 0;\n else\n";
1622  }
1623  ss <<" fRate = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
1624  ss <<";\n";
1625  if(tmpCur1->GetType() == formula::svSingleVectorRef)
1626  {
1627  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
1628  formula::SingleVectorRefToken *>(tmpCur1);
1629  ss <<" if(gid0 >= "<<tmpCurDVR1->GetArrayLength()<<" || isnan(";
1630  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
1631  ss <<"))\n";
1632  ss <<" nNumPeriods = 0;\n else\n";
1633  }
1634  ss <<" nNumPeriods = (int)";
1635  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
1636  ss <<";\n";
1637  if(tmpCur2->GetType() == formula::svSingleVectorRef)
1638  {
1639  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1640  formula::SingleVectorRefToken *>(tmpCur2);
1641  ss <<" if(gid0 >= "<<tmpCurDVR2->GetArrayLength()<<" || isnan(";
1642  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
1643  ss <<"))\n";
1644  ss <<" fVal = 0;\n else\n";
1645  }
1646  ss <<" fVal = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
1647  ss <<";\n";
1648  if(tmpCur3->GetType() == formula::svSingleVectorRef)
1649  {
1650  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
1651  formula::SingleVectorRefToken *>(tmpCur3);
1652  ss <<" if(gid0 >= "<<tmpCurDVR3->GetArrayLength()<<" || isnan(";
1653  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
1654  ss <<"))\n";
1655  ss <<" nStartPer = 0;\n else\n";
1656  }
1657  ss <<" nStartPer = (int)";
1658  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
1659  ss <<";\n";
1660  if(tmpCur4->GetType() == formula::svSingleVectorRef)
1661  {
1662  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
1663  formula::SingleVectorRefToken *>(tmpCur4);
1664  ss <<" if(gid0 >= "<<tmpCurDVR4->GetArrayLength()<<" || isnan(";
1665  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
1666  ss <<"))\n";
1667  ss <<" nEndPer = 0;\n else\n";
1668  }
1669  ss <<" nEndPer = (int)";
1670  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
1671  ss <<";\n";
1672 
1673  if(tmpCur5->GetType() == formula::svSingleVectorRef)
1674  {
1675  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
1676  formula::SingleVectorRefToken *>(tmpCur5);
1677  ss <<" if(gid0 >= "<<tmpCurDVR5->GetArrayLength()<<" || isnan(";
1678  ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
1679  ss <<"))\n";
1680  ss <<" nPayType = 0;\n else\n";
1681  }
1682  ss <<" nPayType = (int)";
1683  ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
1684  ss <<";\n";
1685  ss <<" double fPmt;\n";
1686  ss <<" fPmt = GetPMT_new( fRate, nNumPeriods,fVal,0.0,nPayType );\n";
1687  ss <<" if(nStartPer == 1)\n";
1688  ss <<" {\n";
1689  ss <<" if( nPayType <= 0 )\n";
1690  ss <<" tmp = fPmt + fVal * fRate;\n";
1691  ss <<" else\n";
1692  ss <<" tmp = fPmt;\n";
1693  ss <<" nStartPer=nStartPer+1;\n";
1694  ss <<" }\n";
1695  ss <<" for( int i = nStartPer ; i <= nEndPer ; i++ )\n";
1696  ss <<" {\n";
1697  ss <<" if( nPayType > 0 )\n";
1698  ss <<" tmp += fPmt - ( GetFV_new( fRate,i - 2,";
1699  ss <<"fPmt,fVal,1)- fPmt ) * fRate;\n";
1700  ss <<" else\n";
1701  ss <<" tmp += fPmt - GetFV_new( fRate, i - 1,";
1702  ss <<"fPmt,fVal,0 ) * fRate;\n";
1703  ss <<" }\n";
1704  ss <<" return tmp;\n";
1705  ss <<"}";
1706 }
1707 void OpAccrint::BinInlineFun(std::set<std::string>& decls,
1708  std::set<std::string>& funs)
1709 {
1710  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
1711  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
1712  decls.insert(GetNullDateDecl); decls.insert(GetDiffDateDecl);
1713  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
1714  funs.insert(DaysToDate);funs.insert(DateToDays);
1715  funs.insert(GetNullDate);funs.insert(GetDiffDate);
1716 }
1717 void OpAccrint::GenSlidingWindowFunction(
1718  std::stringstream &ss, const std::string &sSymName,
1719  SubArguments &vSubArguments)
1720 {
1721  ss << "\ndouble " << sSymName;
1722  ss << "_"<< BinFuncName() <<"(";
1723  for (size_t i = 0; i < vSubArguments.size(); i++)
1724  {
1725  if (i)
1726  ss << ",";
1727  vSubArguments[i]->GenSlidingWindowDecl(ss);
1728  }
1729  ss << ") {\n";
1730  ss << " int gid0 = get_global_id(0);\n";
1731  ss << " double tmp = 0;\n";
1732  ss << " int nStartDate,nEndDate,mode,freq;\n";
1733  ss << " int nDays1stYear=0;\n";
1734  ss << " double fVal,fRate;\n";
1735  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
1736  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1737  formula::SingleVectorRefToken *>(tmpCur0);
1738  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
1739  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1740  formula::SingleVectorRefToken *>(tmpCur2);
1741  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
1742  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
1743  formula::SingleVectorRefToken *>(tmpCur3);
1744  FormulaToken* tmpCur4 = vSubArguments[4]->GetFormulaToken();
1745  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
1746  formula::SingleVectorRefToken *>(tmpCur4);
1747  FormulaToken* tmpCur5 = vSubArguments[5]->GetFormulaToken();
1748  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
1749  formula::SingleVectorRefToken *>(tmpCur5);
1750  FormulaToken* tmpCur6 = vSubArguments[6]->GetFormulaToken();
1751  const formula::SingleVectorRefToken*tmpCurDVR6= static_cast<const
1753  ss<< " int buffer_nIssue_len = ";
1754  ss<< tmpCurDVR0->GetArrayLength();
1755  ss<< ";\n";
1756  ss<< " int buffer_nSettle_len = ";
1757  ss<< tmpCurDVR2->GetArrayLength();
1758  ss<< ";\n";
1759  ss<< " int buffer_fRate_len = ";
1760  ss<< tmpCurDVR3->GetArrayLength();
1761  ss<< ";\n";
1762  ss<< " int buffer_fVal_len = ";
1763  ss<< tmpCurDVR4->GetArrayLength();
1764  ss<< ";\n";
1765  ss<< " int buffer_nFreq_len = ";
1766  ss<< tmpCurDVR5->GetArrayLength();
1767  ss<< ";\n";
1768  ss<< " int buffer_nMode_len = ";
1769  ss<< tmpCurDVR6->GetArrayLength();
1770  ss << ";\n";
1771  ss<<" if(gid0 >= buffer_nIssue_len || isnan(";
1772  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
1773  ss <<"))\n";
1774  ss <<" nStartDate = 0;\n else\n";
1775  ss <<" nStartDate=(int)";
1776  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
1777  ss <<";\n";
1778  ss <<" if(gid0 >= buffer_nSettle_len || isnan(";
1779  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
1780  ss <<"))\n";
1781  ss <<" nEndDate = 0;\n else\n";
1782  ss <<" nEndDate=(int)";
1783  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1784  ss << ";\n";
1785  ss <<" if(gid0 >= buffer_fRate_len || isnan(";
1786  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
1787  ss <<"))\n";
1788  ss <<" fRate = 0;\n else\n";
1789  ss <<" fRate=";
1790  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1791  ss <<";\n";
1792  ss <<" if(gid0 >= buffer_fVal_len || isnan(";
1793  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
1794  ss <<"))\n";
1795  ss <<" fVal = 0;\n else\n";
1796  ss <<" fVal=";
1797  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
1798  ss <<";\n";
1799  ss <<" if(gid0 >= buffer_nFreq_len || isnan(";
1800  ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
1801  ss <<"))\n";
1802  ss <<" freq = 0;\n else\n";
1803  ss <<" freq= (int)";
1804  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
1805  ss <<";\n";
1806  ss <<" if(gid0 >= buffer_nMode_len || isnan(";
1807  ss <<vSubArguments[6]->GenSlidingWindowDeclRef();
1808  ss <<"))\n";
1809  ss <<" mode = 0;\n else\n";
1810  ss <<" mode = (int)";
1811  ss << vSubArguments[6]->GenSlidingWindowDeclRef();
1812  ss <<";\n";
1813  ss <<" int nNullDate=GetNullDate();\n";
1814  ss <<" int nTotalDays = GetDiffDate(nNullDate,nStartDate,";
1815  ss <<"nEndDate, mode,&nDays1stYear);\n";
1816  ss <<" tmp = fVal*fRate*convert_double(nTotalDays)";
1817  ss <<"/convert_double(nDays1stYear);\n";
1818  ss <<" return tmp;\n";
1819  ss <<"}";
1820 }
1821 
1822 void OpAccrintm::BinInlineFun(std::set<std::string>& decls,
1823  std::set<std::string>& funs)
1824 {
1825  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
1826  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
1827  decls.insert(GetNullDateDecl); decls.insert(GetDiffDateDecl);
1828  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
1829  funs.insert(DaysToDate);funs.insert(DateToDays);
1830  funs.insert(GetNullDate);funs.insert(GetDiffDate);
1831 }
1832 void OpAccrintm::GenSlidingWindowFunction(
1833  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1834 {
1835  ss << "\ndouble " << sSymName;
1836  ss << "_"<< BinFuncName() <<"(";
1837  for (size_t i = 0; i < vSubArguments.size(); i++)
1838  {
1839  if (i)
1840  ss << ",";
1841  vSubArguments[i]->GenSlidingWindowDecl(ss);
1842  }
1843  ss << ") {\n\t";
1844  ss << "int gid0 = get_global_id(0);\n\t";
1845  ss << "double tmp = " << GetBottom() <<";\n\t";
1846  ss << "int nStartDate,nEndDate,mode;\n\t";
1847  ss << "double fRate,fVal;\n\t";
1848  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1849  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1850  formula::SingleVectorRefToken *>(tmpCur0);
1851  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1852  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
1853  formula::SingleVectorRefToken *>(tmpCur1);
1854 
1855  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1856  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1857  formula::SingleVectorRefToken *>(tmpCur2);
1859  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1860  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
1861  formula::SingleVectorRefToken *>(tmpCur3);
1863  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
1864  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
1865  formula::SingleVectorRefToken *>(tmpCur4);
1866  ss<< "int buffer_nIssue_len = ";
1867  ss<< tmpCurDVR0->GetArrayLength();
1868  ss << ";\n\t";
1869 
1870  ss<< "int buffer_nSettle_len = ";
1871  ss<< tmpCurDVR1->GetArrayLength();
1872  ss << ";\n\t";
1873 
1874  ss<< "int buffer_fRate_len = ";
1875  ss<< tmpCurDVR2->GetArrayLength();
1876  ss << ";\n\t";
1877 
1878  ss<< "int buffer_fVal_len = ";
1879  ss<< tmpCurDVR3->GetArrayLength();
1880  ss << ";\n\t";
1881 
1882  ss<< "int buffer_nMode_len = ";
1883  ss<< tmpCurDVR4->GetArrayLength();
1884  ss << ";\n\t";
1885  ss <<"if(gid0 >= buffer_nIssue_len || isnan(";
1886  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
1887  ss <<"))\n\t\t";
1888  ss <<"nStartDate = 0;\n\telse\n\t\t";
1889  ss << "nStartDate=(int)";
1890  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1891  ss <<";\n\t";
1892  ss <<"if(gid0 >= buffer_nSettle_len || isnan(";
1893  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
1894  ss <<"))\n\t\t";
1895  ss <<"nEndDate = 0;\n\telse\n\t\t";
1896  ss << "nEndDate=(int)";
1897  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1898  ss << ";\n\t";
1899 
1900  ss <<"if(gid0 >= buffer_fRate_len || isnan(";
1901  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
1902  ss <<"))\n\t\t";
1903  ss <<"fRate = 0;\n\telse\n\t\t";
1904  ss << "fRate=";
1905  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1906  ss <<";\n\t";
1907  ss <<"if(gid0 >= buffer_fVal_len || isnan(";
1908  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
1909  ss <<"))\n\t\t";
1910  ss <<"fVal = 0;\n\telse\n\t\t";
1911  ss << "fVal=";
1912  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1913  ss << ";\n\t";
1914  ss <<"if(gid0 >= buffer_nMode_len || isnan(";
1915  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
1916  ss <<"))\n\t\t";
1917  ss <<"mode = 0;\n\telse\n\t\t";
1918  ss << "mode = (int)";
1919  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
1920  ss << ";\n\t";
1921  ss <<"int nDays1stYear=0;\n\t";
1922  ss <<"int nNullDate=GetNullDate();\n\t";
1923  ss <<"int nTotalDays = GetDiffDate(nNullDate,nStartDate,";
1924  ss <<"nEndDate, mode,&nDays1stYear);\n\t";
1925  ss <<"tmp = fVal*fRate*convert_double(nTotalDays)";
1926  ss <<"/convert_double(nDays1stYear);\n\t";
1927  ss << "return tmp;\n";
1928  ss << "}";
1929 }
1930 
1931  void OpYield::BinInlineFun(std::set<std::string>& decls,
1932  std::set<std::string>& funs)
1933 {
1934  decls.insert(getYield_Decl);decls.insert(getPrice_Decl);
1935  decls.insert(coupnumDecl);decls.insert(coupdaysncDecl);
1936  decls.insert(coupdaybsDecl);decls.insert(coupdaysDecl);
1937  decls.insert(lcl_GetcoupnumDecl);decls.insert(lcl_GetcoupdaysDecl);
1938  decls.insert(lcl_GetcoupdaybsDecl);decls.insert(getDiffDecl);
1939  decls.insert(getDaysInYearRangeDecl);decls.insert(GetDaysInYearDecl);
1940  decls.insert(GetDaysInYearsDecl);decls.insert(getDaysInMonthRangeDecl);
1941  decls.insert(addMonthsDecl);decls.insert(ScaDateDecl);
1942  decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
1943  decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
1944  decls.insert(IsLeapYearDecl);
1945 
1946  funs.insert(getYield_);funs.insert(getPrice_);
1947  funs.insert(coupnum);funs.insert(coupdaysnc);
1948  funs.insert(coupdaybs);funs.insert(coupdays);
1949  funs.insert(lcl_Getcoupnum);funs.insert(lcl_Getcoupdays);
1950  funs.insert(lcl_Getcoupdaybs);funs.insert(getDiff);
1951  funs.insert(getDaysInYearRange);funs.insert(GetDaysInYear);
1952  funs.insert(GetDaysInYears);funs.insert(getDaysInMonthRange);
1953  funs.insert(addMonths);funs.insert(ScaDate);
1954  funs.insert(GetNullDate);funs.insert(DateToDays);
1955  funs.insert(DaysToDate);funs.insert(DaysInMonth);
1956  funs.insert(IsLeapYear);
1957 }
1958 
1959 void OpYield::GenSlidingWindowFunction(
1960  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
1961 {
1962  ss << "\ndouble " << sSymName;
1963  ss << "_"<< BinFuncName() <<"(";
1964  for (size_t i = 0; i < vSubArguments.size(); i++)
1965  {
1966  if (i)
1967  ss << ",";
1968  vSubArguments[i]->GenSlidingWindowDecl(ss);
1969  }
1970  ss << ") {\n\t";
1971  ss << "double tmp = 0;\n\t";
1972  ss << "int gid0 = get_global_id(0);\n\t";
1973  ss << "double tmp000;\n\t";
1974  ss << "double tmp001;\n\t";
1975  ss << "double tmp002;\n\t";
1976  ss << "double tmp003;\n\t";
1977  ss << "double tmp004;\n\t";
1978  ss << "double tmp005;\n\t";
1979  ss << "double tmp006;\n\t";
1980 
1981  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1982  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
1983  formula::SingleVectorRefToken *>(tmpCur0);
1984 
1985  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1986  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
1987  formula::SingleVectorRefToken *>(tmpCur1);
1988 
1989  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1990  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
1991  formula::SingleVectorRefToken *>(tmpCur2);
1992 
1993  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1994  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
1995  formula::SingleVectorRefToken *>(tmpCur3);
1996 
1997  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
1998  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
1999  formula::SingleVectorRefToken *>(tmpCur4);
2000 
2001  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
2002  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
2003  formula::SingleVectorRefToken *>(tmpCur5);
2004 
2005  FormulaToken *tmpCur6 = vSubArguments[6]->GetFormulaToken();
2006  const formula::SingleVectorRefToken*tmpCurDVR6= static_cast<const
2007  formula::SingleVectorRefToken *>(tmpCur6);
2008 
2009  ss<< "int buffer_tmp000_len = ";
2010  ss<< tmpCurDVR0->GetArrayLength();
2011  ss << ";\n\t";
2012 
2013  ss<< "int buffer_tmp001_len = ";
2014  ss<< tmpCurDVR1->GetArrayLength();
2015  ss << ";\n\t";
2016 
2017  ss<< "int buffer_tmp002_len = ";
2018  ss<< tmpCurDVR2->GetArrayLength();
2019  ss << ";\n\t";
2020 
2021  ss<< "int buffer_tmp003_len = ";
2022  ss<< tmpCurDVR3->GetArrayLength();
2023  ss << ";\n\t";
2024 
2025  ss<< "int buffer_tmp004_len = ";
2026  ss<< tmpCurDVR4->GetArrayLength();
2027  ss << ";\n\t";
2028 
2029  ss<< "int buffer_tmp005_len = ";
2030  ss<< tmpCurDVR5->GetArrayLength();
2031  ss << ";\n\t";
2032 
2033  ss<< "int buffer_tmp006_len = ";
2034  ss<< tmpCurDVR6->GetArrayLength();
2035  ss << ";\n\t";
2036 
2037  ss<<"if(gid0>=buffer_tmp000_len || isnan(";
2038  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2039  ss<<"))\n\t\t";
2040  ss<<"tmp000 = 0;\n\telse \n\t\t";
2041  ss<<"tmp000 = ";
2042  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2043  ss<<";\n\t";
2044 
2045  ss<<"if(gid0>=buffer_tmp001_len || isnan(";
2046  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2047  ss<<"))\n\t\t";
2048  ss<<"tmp001 = 0;\n\telse \n\t\t";
2049  ss<<"tmp001 = ";
2050  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2051  ss<<";\n\t";
2052 
2053  ss<<"if(gid0>=buffer_tmp002_len || isnan(";
2054  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2055  ss<<"))\n\t\t";
2056  ss<<"tmp002 = 0;\n\telse \n\t\t";
2057  ss<<"tmp002 = ";
2058  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2059  ss<<";\n\t";
2060 
2061  ss<<"if(gid0>=buffer_tmp003_len || isnan(";
2062  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2063  ss<<"))\n\t\t";
2064  ss<<"tmp003 = 0;\n\telse \n\t\t";
2065  ss<<"tmp003 = ";
2066  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2067  ss<<";\n\t";
2068 
2069  ss<<"if(gid0>=buffer_tmp004_len || isnan(";
2070  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2071  ss<<"))\n\t\t";
2072  ss<<"tmp004 = 0;\n\telse \n\t\t";
2073  ss<<"tmp004 = ";
2074  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2075  ss<<";\n\t";
2076 
2077  ss<<"if(gid0>=buffer_tmp005_len || isnan(";
2078  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2079  ss<<"))\n\t\t";
2080  ss<<"tmp005 = 0;\n\telse \n\t\t";
2081  ss<<"tmp005 = ";
2082  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2083  ss<<";\n\t";
2084 
2085  ss<<"if(gid0>=buffer_tmp006_len || isnan(";
2086  ss << vSubArguments[6]->GenSlidingWindowDeclRef();
2087  ss<<"))\n\t\t";
2088  ss<<"tmp006 = 0;\n\telse \n\t\t";
2089  ss<<"tmp006 = ";
2090  ss << vSubArguments[6]->GenSlidingWindowDeclRef();
2091  ss<<";\n\t";
2092 
2093  ss << "tmp = getYield_(";
2094  ss << "GetNullDate(),tmp000,tmp001,tmp002,tmp003,tmp004,tmp005,tmp006);\n\t ";
2095  ss << "return tmp;\n";
2096  ss << "}";
2097 }
2098 
2099 void OpSLN::GenSlidingWindowFunction(std::stringstream &ss,
2100  const std::string &sSymName, SubArguments &vSubArguments)
2101 {
2102  ss << "\ndouble " << sSymName;
2103  ss << "_"<< BinFuncName() <<"(";
2104  for (size_t i = 0; i < vSubArguments.size(); i++)
2105  {
2106  if (i)
2107  ss << ",";
2108  vSubArguments[i]->GenSlidingWindowDecl(ss);
2109  }
2110  ss << ") {\n";
2111  ss << " double tmp = 0;\n";
2112  ss << " int gid0 = get_global_id(0);\n";
2113  ss << " double cost;\n";
2114  ss << " double salvage;\n";
2115  ss << " double life;\n";
2116 
2117  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2118  const formula::SingleVectorRefToken*tmpCurDVR0=
2119  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
2120  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2121  const formula::SingleVectorRefToken*tmpCurDVR1=
2122  static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
2123  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
2124  const formula::SingleVectorRefToken*tmpCurDVR2=
2125  static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
2126  ss<< " int buffer_cost_len = ";
2127  ss<< tmpCurDVR0->GetArrayLength();
2128  ss << ";\n";
2129  ss<< " int buffer_salvage_len = ";
2130  ss<< tmpCurDVR1->GetArrayLength();
2131  ss << ";\n";
2132  ss<< " int buffer_life_len = ";
2133  ss<< tmpCurDVR2->GetArrayLength();
2134  ss << ";\n";
2135  ss<<" if(gid0>=buffer_cost_len || isnan(";
2136  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2137  ss<<"))\n";
2138  ss<<" cost = 0;\n\telse \n";
2139  ss<<" cost = ";
2140  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2141  ss<<";\n";
2142  ss<<" if(gid0>=buffer_salvage_len || isnan(";
2143  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2144  ss<<"))\n";
2145  ss<<" salvage = 0;\n\telse \n";
2146  ss<<" salvage = ";
2147  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2148  ss<<";\n";
2149  ss<<" if(gid0>=buffer_life_len || isnan(";
2150  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2151  ss<<"))\n";
2152  ss<<" life = 0;\n\telse \n";
2153  ss<<" life = ";
2154  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2155  ss<<";\n";
2156  ss << " tmp = (cost-salvage)*pow(life,-1);\n";
2157  ss << " return tmp;\n";
2158  ss << "}";
2159 }
2160 
2161  void OpYieldmat::BinInlineFun(std::set<std::string>& decls,
2162  std::set<std::string>& funs)
2163 {
2164  decls.insert(GetYearFrac_newDecl);decls.insert(GetNullDateDecl);
2165  decls.insert(DateToDaysDecl);decls.insert(DaysToDate_newDecl);
2166  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
2167  decls.insert(GetYieldmatDecl);
2168 
2169  funs.insert(GetYearFrac_new);funs.insert(GetNullDate);
2170  funs.insert(DateToDays);funs.insert(DaysToDate_new);
2171  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
2172  funs.insert(GetYieldmat);
2173 }
2174 
2175 void OpYieldmat::GenSlidingWindowFunction(
2176  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2177 {
2178  ss << "\ndouble " << sSymName;
2179  ss << "_"<< BinFuncName() <<"(";
2180  for (size_t i = 0; i < vSubArguments.size(); i++)
2181  {
2182  if (i)
2183  ss << ",";
2184  vSubArguments[i]->GenSlidingWindowDecl(ss);
2185  }
2186  ss << ") {\n\t";
2187  ss << "double tmp = 0;\n\t";
2188  ss << "int gid0 = get_global_id(0);\n\t";
2189  ss << "double tmp000;\n\t";
2190  ss << "double tmp001;\n\t";
2191  ss << "double tmp002;\n\t";
2192  ss << "double tmp003;\n\t";
2193  ss << "double tmp004;\n\t";
2194  ss << "double tmp005;\n\t";
2195 
2196  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2197  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
2198  formula::SingleVectorRefToken *>(tmpCur0);
2199 
2200  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2201  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
2202  formula::SingleVectorRefToken *>(tmpCur1);
2203 
2204  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
2205  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
2206  formula::SingleVectorRefToken *>(tmpCur2);
2207 
2208  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
2209  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
2210  formula::SingleVectorRefToken *>(tmpCur3);
2211 
2212  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
2213  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
2214  formula::SingleVectorRefToken *>(tmpCur4);
2215 
2216  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
2217  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
2218  formula::SingleVectorRefToken *>(tmpCur5);
2219 
2220  ss<< "int buffer_tmp000_len = ";
2221  ss<< tmpCurDVR0->GetArrayLength();
2222  ss << ";\n\t";
2223 
2224  ss<< "int buffer_tmp001_len = ";
2225  ss<< tmpCurDVR1->GetArrayLength();
2226  ss << ";\n\t";
2227 
2228  ss<< "int buffer_tmp002_len = ";
2229  ss<< tmpCurDVR2->GetArrayLength();
2230  ss << ";\n\t";
2231 
2232  ss<< "int buffer_tmp003_len = ";
2233  ss<< tmpCurDVR3->GetArrayLength();
2234  ss << ";\n\t";
2235 
2236  ss<< "int buffer_tmp004_len = ";
2237  ss<< tmpCurDVR4->GetArrayLength();
2238  ss << ";\n\t";
2239 
2240  ss<< "int buffer_tmp005_len = ";
2241  ss<< tmpCurDVR5->GetArrayLength();
2242  ss << ";\n\t";
2243 
2244  ss<<"if(gid0>=buffer_tmp000_len || isnan(";
2245  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2246  ss<<"))\n\t\t";
2247  ss<<"tmp000 = 0;\n\telse \n\t\t";
2248  ss<<"tmp000 = ";
2249  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2250  ss<<";\n\t";
2251 
2252  ss<<"if(gid0>=buffer_tmp001_len || isnan(";
2253  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2254  ss<<"))\n\t\t";
2255  ss<<"tmp001 = 0;\n\telse \n\t\t";
2256  ss<<"tmp001 = ";
2257  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2258  ss<<";\n\t";
2259 
2260  ss<<"if(gid0>=buffer_tmp002_len || isnan(";
2261  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2262  ss<<"))\n\t\t";
2263  ss<<"tmp002 = 0;\n\telse \n\t\t";
2264  ss<<"tmp002 = ";
2265  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2266  ss<<";\n\t";
2267 
2268  ss<<"if(gid0>=buffer_tmp003_len || isnan(";
2269  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2270  ss<<"))\n\t\t";
2271  ss<<"tmp003 = 0;\n\telse \n\t\t";
2272  ss<<"tmp003 = ";
2273  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2274  ss<<";\n\t";
2275 
2276  ss<<"if(gid0>=buffer_tmp004_len || isnan(";
2277  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2278  ss<<"))\n\t\t";
2279  ss<<"tmp004 = 0;\n\telse \n\t\t";
2280  ss<<"tmp004 = ";
2281  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2282  ss<<";\n\t";
2283 
2284  ss<<"if(gid0>=buffer_tmp005_len || isnan(";
2285  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2286  ss<<"))\n\t\t";
2287  ss<<"tmp005 = 0;\n\telse \n\t\t";
2288  ss<<"tmp005 = ";
2289  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2290  ss<<";\n\t";
2291 
2292  ss << "tmp = GetYieldmat(";
2293  ss<<"GetNullDate(),tmp000,tmp001,tmp002,tmp003,tmp004,tmp005);\n\t";
2294  ss << "return tmp;\n";
2295  ss << "}";
2296 }
2297 
2298 void OpPMT::GenSlidingWindowFunction(std::stringstream &ss,
2299  const std::string &sSymName, SubArguments &vSubArguments)
2300 {
2301  ss << "\ndouble " << sSymName;
2302  ss << "_"<< BinFuncName() <<"(";
2303  for (size_t i = 0; i < vSubArguments.size(); i++)
2304  {
2305  if (i)
2306  ss << ", ";
2307  vSubArguments[i]->GenSlidingWindowDecl(ss);
2308  }
2309  ss<<") {\n";
2310  ss<<" double tmp = 0;\n";
2311  ss<<" double temp=0.0;\n";
2312  ss<<" int gid0 = get_global_id(0);\n";
2313  ss<<" double tmp0=0,tmp1=0,tmp2=0;\n";
2314  ss<<" double tmp3=0,tmp4=0;\n";
2315  ss <<"\n ";
2316  //while (i-- > 1)
2317  for (size_t i = 0; i < vSubArguments.size(); i++)
2318  {
2319  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2320  assert(pCur);
2321  if (pCur->GetType() == formula::svSingleVectorRef)
2322  {
2323  const formula::SingleVectorRefToken* pSVR =
2324  static_cast< const formula::SingleVectorRefToken* >(pCur);
2325  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2326  }
2327  else if (pCur->GetType() == formula::svDouble)
2328  {
2329  ss << "{\n";
2330  }
2331 
2332  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2333  {
2334  ss <<" temp="<<vSubArguments[i]->GenSlidingWindowDeclRef();
2335  ss <<";\n";
2336  ss <<" if (isnan(temp))\n";
2337  ss <<" tmp"<<i<<"= 0;\n";
2338  ss <<" else\n";
2339  ss <<" tmp"<<i<<"=temp;\n";
2340  ss <<" }\n";
2341  }
2342  else
2343  {
2344  ss <<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
2345  ss <<";\n";
2346  }
2347  }
2348  ss<<" if(tmp0==0.0)\n";
2349  ss<<" return -(tmp2+tmp3)/tmp1;\n";
2350  ss<<" tmp-=tmp3;\n";
2351  ss<<" tmp=tmp-tmp2*pow(1.0+tmp0,tmp1);\n";
2352  ss<<" tmp=tmp*pow(( (1.0+tmp0*tmp4)* ";
2353  ss<<"( (pow(1.0+tmp0,tmp1)-1.0)/tmp0)),-1);\n";
2354  ss<<" return tmp;\n";
2355  ss<<"}";
2356 }
2357 
2358 void OpNPV::GenSlidingWindowFunction(std::stringstream &ss,
2359  const std::string &sSymName, SubArguments &vSubArguments)
2360 {
2361  ss << "\ndouble " << sSymName;
2362  ss << "_"<< BinFuncName() <<"(";
2363  for (size_t i = 0; i < vSubArguments.size(); i++)
2364  {
2365  if (i)
2366  ss << ", ";
2367  vSubArguments[i]->GenSlidingWindowDecl(ss);
2368  }
2369  ss << ") {\n";
2370  ss << " double tmp = 0.0;\n";
2371  ss << " int gid0 = get_global_id(0);\n";
2372  ss << " int nCount = 1;\n";
2373  ss << " double arg0=";
2374  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
2375  ss <<";\n";
2376  //while (i-- > 1)
2377  for (size_t i = 1; i < vSubArguments.size(); i++)
2378  {
2379  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2380  assert(pCur);
2381  if (pCur->GetType() == formula::svDoubleVectorRef)
2382  {
2383  const formula::DoubleVectorRefToken* pDVR =
2384  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2385  size_t nCurWindowSize = pDVR->GetRefRowSize();
2386  ss << " for (int i = ";
2387  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
2388  ss << "gid0; i < " << pDVR->GetArrayLength();
2389  ss << " && i < " << nCurWindowSize << "; i++){\n";
2390  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
2391  ss << "0; i < " << pDVR->GetArrayLength();
2392  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
2393  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
2394  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2395  ss << " && i < "<< nCurWindowSize << "; i++){\n";
2396  }
2397  else {
2398  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
2399  }
2400  }
2401  else if (pCur->GetType() == formula::svSingleVectorRef)
2402  {
2403  const formula::SingleVectorRefToken* pSVR =
2404  static_cast< const formula::SingleVectorRefToken* >(pCur);
2405  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2406  }
2407  else if (pCur->GetType() == formula::svDouble)
2408  {
2409  ss << "{\n";
2410  }
2411  else
2412  {
2413  ss << "nCount += 1;\n";
2414  }
2415  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2416  {
2417  ss << " double temp=";
2418  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2419  ss << ";\n";
2420  ss << " double temp1=1.0;";
2421  ss << " if (isnan(temp)){\n";
2422  ss << " tmp += 0;}\n";
2423  ss << " else{\n";
2424  ss << " for(int i=1;i<nCount;i+=2)\n";
2425  ss << " temp1*=pow(1.0f+ arg0 ,2);\n";
2426  ss << " if(nCount%2)\n";
2427  ss << " temp1*=1.0f+ arg0;\n";
2428  ss << " tmp +=temp/ temp1;\n";
2429  ss << " nCount += 1;\n";
2430  ss << " }\n";
2431  ss << " }\n";
2432  }
2433  else
2434  {
2435  ss << " double temp=";
2436  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2437  ss << ";\n";
2438  ss << " double temp1=1.0;";
2439  ss << " for(int i=1;i<nCount;i+=2)";
2440  ss << " temp1*=pow(1.0f+ arg0 ,2);\n";
2441  ss << " if(nCount%2)";
2442  ss << " temp1*=1.0f+ arg0;\n";
2443  ss << " tmp +=temp/ temp1;\n";
2444  ss << " nCount += 1;\n";
2445  }
2446  }
2447  ss << " return tmp;\n";
2448  ss << "}";
2449 }
2450 
2451  void OpPrice::BinInlineFun(std::set<std::string>& decls,
2452  std::set<std::string>& funs)
2453  {
2454  decls.insert(getPrice_new_Decl);
2455  decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
2456  decls.insert(DaysToDateDecl);
2457  decls.insert(DateToDaysDecl);
2458  decls.insert(ScaDateDecl);
2459  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
2460  decls.insert(addMonthsDecl);decls.insert(lcl_Getcoupnum_newDecl);
2461  decls.insert(coupnum_newDecl);
2462  decls.insert(DateToDays_newDecl);
2463  decls.insert(getDaysInMonthRangeDecl);
2464  decls.insert(GetDaysInYearsDecl); decls.insert(GetDaysInYearDecl);
2465  decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
2466  decls.insert(coupdaybs_newDecl);
2467  decls.insert(lcl_Getcoupdays_newDecl);
2468  decls.insert(lcl_Getcoupdaybs_newDecl);
2469  decls.insert(coupdays_newDecl);
2470  decls.insert(coupdaysnc_newDecl);
2471  funs.insert(IsLeapYear);funs.insert(DaysInMonth_new);
2472  funs.insert(DaysToDate);funs.insert(DateToDays_new);
2473  funs.insert(DateToDays);
2474  funs.insert(ScaDate);
2475  funs.insert(addMonths);funs.insert(getDaysInMonthRange);
2476  funs.insert(GetDaysInYears);funs.insert(GetDaysInYear);
2477  funs.insert(getDaysInYearRange);funs.insert(getDiff);
2478  funs.insert(setDay);funs.insert(checklessthan);
2479  funs.insert(lcl_Getcoupdaybs_new);
2480  funs.insert(coupdaybs_new);
2481  funs.insert(lcl_Getcoupdays_new);
2482  funs.insert(coupdaysnc_new);
2483  funs.insert(coupdays_new);
2484  funs.insert(setDay);funs.insert(checklessthan);
2485  funs.insert(lcl_Getcoupnum_new);
2486  funs.insert(coupnum_new);funs.insert(getPrice_new);
2487  }
2488 void OpPrice::GenSlidingWindowFunction(std::stringstream &ss,
2489  const std::string &sSymName, SubArguments &vSubArguments)
2490 {
2491  ss << "\ndouble " << sSymName;
2492  ss << "_"<< BinFuncName() <<"(";
2493  for (size_t i = 0; i < vSubArguments.size(); i++)
2494  {
2495  if (i)
2496  ss << ", ";
2497  vSubArguments[i]->GenSlidingWindowDecl(ss);
2498  }
2499  ss<<") {\n";
2500  ss<<" double tmp = 0;\n";
2501  ss<<" int gid0 = get_global_id(0);\n";
2502  ss<<" double tmp0=0;\n";
2503  ss<<" double tmp1=0;\n";
2504  ss<<" double tmp2=0;\n";
2505  ss<<" double tmp3=0;\n";
2506  ss<<" double tmp4=0,tmp5=0;\n";
2507  ss<<" double tmp6=0;\n";
2508  ss<<"\n";
2509  for (size_t i = 0; i < vSubArguments.size(); i++)
2510  {
2511  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2512  assert(pCur);
2513  if (pCur->GetType() == formula::svSingleVectorRef)
2514  {
2515  const formula::SingleVectorRefToken* pSVR =
2516  static_cast< const formula::SingleVectorRefToken* >(pCur);
2517  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2518  }
2519  else if (pCur->GetType() == formula::svDouble)
2520  {
2521  ss << "{\n";
2522  }
2523 
2524  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2525  {
2526  ss << " if (isnan(";
2527  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2528  ss << "))\n";
2529  ss << " tmp"<<i<<"= 0;\n";
2530  ss << " else\n";
2531  ss << " tmp"<<i<<"=";
2532  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2533  ss << ";\n }\n";
2534  }
2535  else
2536  {
2537  ss << " tmp"<<i<<"=";
2538  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2539  ss <<";\n";
2540  }
2541  }
2542  ss << " if(tmp4*tmp5 == 0) return NAN;\n";
2543  ss << " tmp = getPrice_(tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6);\n";
2544  ss << " return tmp;\n";
2545  ss << "}";
2546 }
2547 
2548 void OpOddlprice::BinInlineFun(std::set<std::string>& decls,
2549  std::set<std::string>& funs)
2550 {
2551  decls.insert(GetOddlpriceDecl);decls.insert(GetDiffDateDecl);
2552  decls.insert(GetYearDiffDecl);decls.insert(IsLeapYearDecl);
2553  decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
2554  decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
2555  decls.insert(GetYearFracDecl);
2556  funs.insert(GetOddlprice);funs.insert(GetDiffDate);
2557  funs.insert(GetYearDiff);funs.insert(IsLeapYear);
2558  funs.insert(GetNullDate);funs.insert(DaysInMonth);
2559  funs.insert(DaysToDate);funs.insert(DateToDays);
2560  funs.insert(GetYearFrac);
2561 }
2562 void OpOddlprice::GenSlidingWindowFunction(std::stringstream &ss,
2563  const std::string &sSymName, SubArguments &vSubArguments)
2564 {
2565  ss << "\ndouble " << sSymName;
2566  ss << "_"<< BinFuncName() <<"(";
2567  for (size_t i = 0; i < vSubArguments.size(); i++)
2568  {
2569  if (i)
2570  ss << ", ";
2571  vSubArguments[i]->GenSlidingWindowDecl(ss);
2572  }
2573  ss <<") {\n";
2574  ss <<" double tmp = 0;\n";
2575  ss <<" int gid0 = get_global_id(0);\n";
2576  ss <<" double tmp0=0;\n";
2577  ss <<" double tmp1=0;\n";
2578  ss <<" double tmp2=0;\n";
2579  ss <<" double tmp3=0;\n";
2580  ss <<" double tmp4=0;\n";
2581  ss <<" double tmp5=0;\n";
2582  ss <<" double tmp6=0;\n";
2583  ss <<" double tmp7=0;\n";
2584  ss <<" \n";
2585  for (size_t i = 0; i < vSubArguments.size(); i++)
2586  {
2587  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2588  assert(pCur);
2589  if (pCur->GetType() == formula::svDoubleVectorRef)
2590  {
2591  const formula::DoubleVectorRefToken* pDVR =
2592  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2593  size_t nCurWindowSize = pDVR->GetRefRowSize();
2594  ss << " for (int i = ";
2595  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
2596  ss << "gid0; i < " << pDVR->GetArrayLength();
2597  ss << " && i < " << nCurWindowSize << "; i++){\n";
2598  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
2599  ss << "0; i < " << pDVR->GetArrayLength();
2600  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
2601  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
2602  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2603  ss << " && i < "<< nCurWindowSize << "; i++){\n";
2604  }
2605  else {
2606  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
2607  }
2608  }
2609  else if (pCur->GetType() == formula::svSingleVectorRef)
2610  {
2611  const formula::SingleVectorRefToken* pSVR =
2612  static_cast< const formula::SingleVectorRefToken* >(pCur);
2613  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2614  }
2615  else if (pCur->GetType() == formula::svDouble)
2616  {
2617  ss << "{\n";
2618  }
2619 
2620  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2621  {
2622  ss << " if (isnan(";
2623  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2624  ss << "))\n";
2625  ss << " tmp"<<i<<"= 0;\n";
2626  ss << " else\n";
2627  ss << " tmp"<<i<<"=";
2628  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2629  ss << ";\n";
2630  ss <<" }\n";
2631  }
2632  else
2633  {
2634  ss << " tmp"<<i<<"=";
2635  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2636  ss <<";\n";
2637  }
2638  }
2639  ss <<" int nNullDate = GetNullDate();\n";
2640  ss <<" tmp = GetOddlprice(nNullDate,tmp0,tmp1,";
2641  ss <<"tmp2,tmp3,tmp4,tmp5,tmp6,tmp7);\n";
2642  ss <<" return tmp;\n";
2643  ss <<"}";
2644 }
2645 void OpOddlyield::BinInlineFun(std::set<std::string>& decls,
2646  std::set<std::string>& funs)
2647 {
2648  decls.insert(GetDiffDateDecl);decls.insert(DaysToDateDecl);
2649  decls.insert(GetYearDiffDecl);decls.insert(IsLeapYearDecl);
2650  decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
2651  decls.insert(DaysInMonthDecl);
2652  decls.insert(GetYearFracDecl);decls.insert(GetOddlyieldDecl);
2653  funs.insert(GetDiffDate);funs.insert(DaysToDate);
2654  funs.insert(GetYearDiff);funs.insert(IsLeapYear);
2655  funs.insert(GetNullDate);funs.insert(DaysInMonth);
2656  funs.insert(DateToDays);
2657  funs.insert(GetYearFrac);funs.insert(GetOddlyield);
2658 }
2659 void OpOddlyield::GenSlidingWindowFunction(std::stringstream &ss,
2660  const std::string &sSymName, SubArguments &vSubArguments)
2661 {
2662  ss << "\ndouble " << sSymName;
2663  ss << "_"<< BinFuncName() <<"(";
2664  for (size_t i = 0; i < vSubArguments.size(); i++)
2665  {
2666  if (i)
2667  ss << ", ";
2668  vSubArguments[i]->GenSlidingWindowDecl(ss);
2669  }
2670  ss <<") {\n";
2671  ss <<" double tmp = 0;\n";
2672  ss <<" int gid0 = get_global_id(0);\n";
2673  ss <<" double tmp0=0;\n";
2674  ss <<" double tmp1=0;\n";
2675  ss <<" double tmp2=0;\n";
2676  ss <<" double tmp3=0;\n";
2677  ss <<" double tmp4=0;\n";
2678  ss <<" double tmp5=0;\n";
2679  ss <<" double tmp6=0;\n";
2680  ss <<" double tmp7=0;\n";
2681  ss <<" \n";
2682  for (size_t i = 0; i < vSubArguments.size(); i++)
2683  {
2684  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2685  assert(pCur);
2686  if (pCur->GetType() == formula::svDoubleVectorRef)
2687  {
2688  const formula::DoubleVectorRefToken* pDVR =
2689  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2690  size_t nCurWindowSize = pDVR->GetRefRowSize();
2691  ss << " for (int i = ";
2692  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
2693  ss << "gid0; i < " << pDVR->GetArrayLength();
2694  ss << " && i < " << nCurWindowSize << "; i++){\n";
2695  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
2696  ss << "0; i < " << pDVR->GetArrayLength();
2697  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
2698  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
2699  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2700  ss << " && i < "<< nCurWindowSize << "; i++){\n";
2701  }
2702  else {
2703  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
2704  }
2705  }
2706  else if (pCur->GetType() == formula::svSingleVectorRef)
2707  {
2708  const formula::SingleVectorRefToken* pSVR =
2709  static_cast< const formula::SingleVectorRefToken* >(pCur);
2710  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2711  }
2712  else if (pCur->GetType() == formula::svDouble)
2713  {
2714  ss << "{\n";
2715  }
2716 
2717  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2718  {
2719  ss << " if (isnan(";
2720  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2721  ss << "))\n";
2722  ss << " tmp"<<i<<"= 0;\n";
2723  ss << " else\n";
2724  ss << " tmp"<<i<<"=";
2725  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2726  ss << ";\n";
2727  ss <<" }\n";
2728  }
2729  else
2730  {
2731  ss << " tmp"<<i<<"=";
2732  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2733  ss <<";\n";
2734  }
2735  }
2736  ss <<" int nNullDate = GetNullDate();\n";
2737  ss <<" tmp = GetOddlyield(nNullDate,tmp0,tmp1";
2738  ss <<",tmp2,tmp3,tmp4,tmp5,tmp6,tmp7);\n";
2739  ss <<" return tmp;\n";
2740  ss <<"}";
2741 }
2742 void OpPriceDisc::BinInlineFun(std::set<std::string>& decls,
2743  std::set<std::string>& funs)
2744 {
2745  decls.insert(GetYearDiffDecl);decls.insert(getDiffDecl);
2746  decls.insert(getDaysInYearRangeDecl);decls.insert(GetDaysInYearDecl);
2747  decls.insert(GetDaysInYearsDecl);decls.insert(getDaysInMonthRangeDecl);
2748  decls.insert(addMonthsDecl);decls.insert(ScaDateDecl);
2749  decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
2750  decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
2751  decls.insert(IsLeapYearDecl);decls.insert(GetDiffDateDecl);
2752  funs.insert(GetYearDiff);funs.insert(getDiff);
2753  funs.insert(getDaysInYearRange);funs.insert(GetDaysInYear);
2754  funs.insert(GetDaysInYears);funs.insert(getDaysInMonthRange);
2755  funs.insert(addMonths);funs.insert(ScaDate);
2756  funs.insert(GetNullDate);funs.insert(DateToDays);
2757  funs.insert(DaysToDate);funs.insert(DaysInMonth);
2758  funs.insert(IsLeapYear);funs.insert(GetDiffDate);
2759 }
2760 void OpPriceDisc::GenSlidingWindowFunction(std::stringstream &ss,
2761  const std::string &sSymName, SubArguments &vSubArguments)
2762 {
2763  ss << "\ndouble " << sSymName;
2764  ss << "_"<< BinFuncName() <<"(";
2765  for (size_t i = 0; i < vSubArguments.size(); i++)
2766  {
2767  if (i)
2768  ss << ", ";
2769  vSubArguments[i]->GenSlidingWindowDecl(ss);
2770  }
2771  ss << ") {\n";
2772  ss << " double tmp = 0;\n";
2773  ss << " int gid0 = get_global_id(0);\n";
2774  ss<<" double tmp0=0;\n";
2775  ss<<" double tmp1=0;\n";
2776  ss<<" double tmp2=0;\n";
2777  ss<<" double tmp3=0;\n";
2778  ss<<" double tmp4=0;\n";
2779  ss <<" \n";
2780  for (size_t i = 0; i < vSubArguments.size(); i++)
2781  {
2782  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2783  assert(pCur);
2784  if (pCur->GetType() == formula::svSingleVectorRef)
2785  {
2786  const formula::SingleVectorRefToken* pSVR =
2787  static_cast< const formula::SingleVectorRefToken* >(pCur);
2788  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2789  }
2790  else if (pCur->GetType() == formula::svDouble)
2791  {
2792  ss << "{\n";
2793  }
2794 
2795  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2796  {
2797  ss << " if (isnan(";
2798  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2799  ss << "))\n";
2800  ss << " tmp"<<i<<"= 0;\n";
2801  ss << " else\n";
2802  ss << " tmp"<<i<<"=";
2803  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2804  ss << ";\n";
2805  ss <<" }\n";
2806  }
2807  else
2808  {
2809  ss << " tmp"<<i<<"=";
2810  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2811  ss <<";\n";
2812  }
2813  }
2814  ss <<" int nNullDate = GetNullDate();\n";
2815  ss <<" tmp=tmp3* ( 1.0 -tmp2*GetYearDiff( nNullDate, ";
2816  ss <<"tmp0,tmp1,tmp4));\n";
2817  ss <<" return tmp;\n";
2818  ss <<"}";
2819 }
2820 void OpNper::GenSlidingWindowFunction(std::stringstream &ss,
2821  const std::string &sSymName, SubArguments &vSubArguments)
2822  {
2823  ss << "\ndouble " << sSymName;
2824  ss << "_"<< BinFuncName() <<"(";
2825  for (size_t i = 0; i < vSubArguments.size(); i++)
2826  {
2827  if (i)
2828  ss << ", ";
2829  vSubArguments[i]->GenSlidingWindowDecl(ss);
2830  }
2831  ss << ") {\n";
2832  ss << " double tmp = 0;\n";
2833  ss << " int gid0 = get_global_id(0);\n";
2834  ss <<" double tmp0=0;\n";
2835  ss <<" double tmp1=0;\n";
2836  ss <<" double tmp2=0;\n";
2837  ss <<" double tmp3=0;\n";
2838  ss <<" double tmp4=0;\n";
2839 
2840  for (size_t i = 0; i < vSubArguments.size(); i++)
2841  {
2842  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2843  assert(pCur);
2844  if (pCur->GetType() == formula::svSingleVectorRef)
2845  {
2846  const formula::SingleVectorRefToken* pSVR =
2847  static_cast< const formula::SingleVectorRefToken* >(pCur);
2848  ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2849  }
2850  else if (pCur->GetType() == formula::svDouble)
2851  {
2852  ss << "{\n";
2853  }
2854 
2855  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2856  {
2857  ss << " if (isnan(";
2858  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2859  ss << "))\n";
2860  ss << " tmp"<<i<<"= 0;\n";
2861  ss << " else\n";
2862  ss << " tmp"<<i<<"=";
2863  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2864  ss << ";\n";
2865  ss <<" }\n";
2866  }
2867  else
2868  {
2869  ss << " tmp"<<i<<"=";
2870  ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
2871  ss <<";\n";
2872  }
2873  }
2874  ss <<" if (tmp0 == 0.0)\n";
2875  ss <<" tmp=(-1*(tmp2 + tmp3)/tmp1);\n";
2876  ss <<" else if (tmp4 > 0.0)\n";
2877  ss <<" tmp=log(-1*(tmp0*tmp3-tmp1*(1.0+tmp0))*";
2878  ss <<"pow((tmp0*tmp2+tmp1*(1.0+tmp0)),-1))/log(1.0+tmp0);\n";
2879  ss <<" else\n";
2880  ss <<" tmp=log(-1*(tmp0*tmp3-tmp1)*pow(tmp0*tmp2+tmp1,-1))";
2881  ss <<"/log(1.0+tmp0);\n";
2882  ss <<" return tmp;\n";
2883  ss <<"}";
2884  }
2885 
2886 void OpPPMT::BinInlineFun(std::set<std::string>& decls,
2887  std::set<std::string>& funs)
2888 {
2889  decls.insert(GetFVDecl);
2890  funs.insert(GetFV);
2891 }
2892 
2893 void OpPPMT::GenSlidingWindowFunction(std::stringstream &ss,
2894  const std::string &sSymName, SubArguments &vSubArguments)
2895 {
2896  ss << "\ndouble " << sSymName;
2897  ss << "_"<< BinFuncName() <<"(";
2898  for (size_t i = 0; i < vSubArguments.size(); i++)
2899  {
2900  if (i)
2901  ss << ", ";
2902  vSubArguments[i]->GenSlidingWindowDecl(ss);
2903  }
2904  ss<<") {\n";
2905  ss<<" double tmp = 0;\n";
2906  ss<<" int gid0 = get_global_id(0);\n";
2907  ss<<" double arg=0;\n";
2908  ss<<" double tmp0=0;\n";
2909  ss<<" double tmp1=0;\n";
2910  ss<<" double tmp2=0;\n";
2911  ss<<" double tmp3=0;\n";
2912  ss<<" double tmp4=0,tmp5=0;\n";
2913  ss <<"\n ";
2914  //while (i-- > 1)
2915  for (size_t i = 0; i < vSubArguments.size(); i++)
2916  {
2917  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2918  assert(pCur);
2919  if (pCur->GetType() == formula::svSingleVectorRef)
2920  {
2921  const formula::SingleVectorRefToken* pSVR =
2922  static_cast< const formula::SingleVectorRefToken* >(pCur);
2923  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2924  }
2925  else if (pCur->GetType() == formula::svDouble)
2926  {
2927  ss << "{\n";
2928  }
2929 
2930  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2931  {
2932  ss << " arg=";
2933  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2934  ss << ";\n";
2935  ss << " if (isnan(arg))\n";
2936  ss << " tmp"<<i<<"= 0;\n";
2937  ss << " else\n";
2938  ss << " tmp"<<i<<"=arg;\n";
2939  ss << " }\n";
2940  }
2941  else
2942  {
2943  ss<<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
2944  ss<<";\n";
2945  }
2946  }
2947  ss<<" double pmt=0 ;\n";
2948  ss<<" if(tmp0==0.0)\n";
2949  ss<<" return -(tmp3+tmp4)/tmp2;\n";
2950  ss<<" pmt=pmt-tmp4-tmp3*pow(1.0+tmp0,tmp2);\n";
2951  ss<<" pmt=pmt*pow(( (1.0+tmp0*tmp5)* ";
2952  ss<<"( (pow(1.0+tmp0,tmp2)-1.0)/tmp0)),-1);\n";
2953  ss<<" double temp = pow( 1+tmp0,tmp1-2);\n";
2954  ss<<" double re;\n";
2955  ss<<" if(tmp1==1.0){\n";
2956  ss<<" if(tmp5>0.0)\n";
2957  ss<<" re=0.0;\n";
2958  ss<<" else\n";
2959  ss<<" re=-tmp3;\n";
2960  ss<<" }\n";
2961  ss<<" else\n";
2962  ss<<" {\n";
2963  ss<<" if(tmp5>0.0)\n ";
2964  ss<<" re=GetFV(tmp0, tmp1-2.0, pmt, tmp3, 1.0) - pmt;\n";
2965  ss<<" else\n";
2966  ss<<" re=GetFV(tmp0, tmp1-1.0, pmt, tmp3, 0.0);\n";
2967  ss<<" }\n ";
2968  ss<<" re = re * tmp0;\n";
2969  ss<<" tmp = pmt - re;\n";
2970  ss<<" return tmp;\n";
2971  ss<<"}";
2972 }
2973 
2974 void OpCoupdaybs::BinInlineFun(std::set<std::string>& decls,
2975  std::set<std::string>& funs)
2976 {
2977  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
2978  decls.insert(DaysToDateDecl); decls.insert(DateToDays_newDecl);
2979  decls.insert(GetNullDate_newDecl); decls.insert(ScaDateDecl);
2980  decls.insert(addMonthsDecl); decls.insert(getDaysInMonthRangeDecl);
2981  decls.insert(GetDaysInYearsDecl);
2982  decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
2983  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
2984  decls.insert(lcl_Getcoupdaybs_newDecl);
2985  decls.insert(coupdaybs_newDecl);
2986  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
2987  funs.insert(DaysToDate);funs.insert(DateToDays_new);
2988  funs.insert(GetNullDate_new);funs.insert(ScaDate);
2989  funs.insert(addMonths);funs.insert(getDaysInMonthRange);
2990  funs.insert(GetDaysInYears);
2991  funs.insert(getDaysInYearRange);funs.insert(getDiff);
2992  funs.insert(setDay);funs.insert(checklessthan);
2993  funs.insert(lcl_Getcoupdaybs_new);
2994  funs.insert(coupdaybs_new);
2995 }
2996 void OpCoupdaybs::GenSlidingWindowFunction(
2997  std::stringstream &ss, const std::string &sSymName, SubArguments &
2998 vSubArguments)
2999 {
3000  ss << "\ndouble " << sSymName;
3001  ss << "_"<< BinFuncName() <<"(";
3002  for (size_t i = 0; i < vSubArguments.size(); i++)
3003  {
3004  if (i)
3005  ss << ",";
3006  vSubArguments[i]->GenSlidingWindowDecl(ss);
3007  }
3008  ss << ") {\n";
3009  ss << " double tmp = 0;\n";
3010  ss << " int gid0 = get_global_id(0);\n";
3011  ss << " int nSettle,nMat,nFreq,nBase;\n";
3012  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3013  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3014  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3015  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3016  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3017  {
3018  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3019  formula::SingleVectorRefToken *>(tmpCur0);
3020  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3021  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3022  ss <<" nSettle = 0;\n else\n";
3023  }
3024  ss <<" nSettle=(int)";
3025  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3026  ss <<";\n";
3027  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3028  {
3029  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3030  formula::SingleVectorRefToken *>(tmpCur1);
3031  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3032  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3033  ss <<" nMat = 0;\n else\n";
3034  }
3035  ss <<" nMat=(int)";
3036  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3037  ss <<";\n";
3038  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3039  {
3040  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3041  formula::SingleVectorRefToken *>(tmpCur2);
3042  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3043  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3044  ss <<" nFreq = 0;\n else\n";
3045  }
3046  ss << " nFreq=(int)";
3047  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3048  ss <<";\n";
3049  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3050  {
3051  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3052  formula::SingleVectorRefToken *>(tmpCur3);
3053  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3054  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3055  ss <<" nBase = 0;\n else\n";
3056  }
3057  ss << " nBase=(int)";
3058  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3059  ss << ";\n";
3060  ss <<" tmp = coupdaybs_new(nSettle,nMat,nFreq,nBase);\n";
3061  ss <<" return tmp;\n";
3062  ss <<"}";
3063 }
3064 
3065 void OpCoupdays::BinInlineFun(std::set<std::string>& decls,
3066  std::set<std::string>& funs)
3067 {
3068  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3069  decls.insert(DaysToDateDecl); decls.insert(DateToDays_newDecl);
3070  decls.insert(GetNullDate_newDecl); decls.insert(ScaDateDecl);
3071  decls.insert(addMonthsDecl); decls.insert(getDaysInMonthRangeDecl);
3072  decls.insert(GetDaysInYearsDecl); decls.insert(GetDaysInYearDecl);
3073  decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
3074  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3075  decls.insert(lcl_Getcoupdays_newDecl);
3076  decls.insert(coupdays_newDecl);
3077  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3078  funs.insert(DaysToDate);funs.insert(DateToDays_new);
3079  funs.insert(GetNullDate_new);funs.insert(ScaDate);
3080  funs.insert(addMonths);funs.insert(getDaysInMonthRange);
3081  funs.insert(GetDaysInYears);funs.insert(GetDaysInYear);
3082  funs.insert(getDaysInYearRange);funs.insert(getDiff);
3083  funs.insert(lcl_Getcoupdays_new);
3084  funs.insert(setDay);funs.insert(checklessthan);
3085  funs.insert(coupdays_new);
3086 }
3087 void OpCoupdays::GenSlidingWindowFunction(
3088  std::stringstream &ss, const std::string &sSymName, SubArguments &
3089 vSubArguments)
3090 {
3091  ss << "\ndouble " << sSymName;
3092  ss << "_"<< BinFuncName() <<"(";
3093  for (size_t i = 0; i < vSubArguments.size(); i++)
3094  {
3095  if (i)
3096  ss << ",";
3097  vSubArguments[i]->GenSlidingWindowDecl(ss);
3098  }
3099  ss << ") {\n";
3100  ss << " double tmp = 0;\n";
3101  ss << " int gid0 = get_global_id(0);\n";
3102  ss << " int nSettle,nMat,nFreq,nBase;\n";
3103  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3104  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3105  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3106  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3107  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3108  {
3109  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3110  formula::SingleVectorRefToken *>(tmpCur0);
3111  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3112  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3113  ss <<" nSettle = 0;\n else\n";
3114  }
3115  ss <<" nSettle=(int)";
3116  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3117  ss <<";\n";
3118  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3119  {
3120  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3121  formula::SingleVectorRefToken *>(tmpCur1);
3122  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3123  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3124  ss <<" nMat = 0;\n else\n";
3125  }
3126  ss <<" nMat=(int)";
3127  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3128  ss <<";\n";
3129  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3130  {
3131  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3132  formula::SingleVectorRefToken *>(tmpCur2);
3133  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3134  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3135  ss <<" nFreq = 0;\n else\n";
3136  }
3137  ss << " nFreq=(int)";
3138  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3139  ss <<";\n";
3140  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3141  {
3142  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3143  formula::SingleVectorRefToken *>(tmpCur3);
3144  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3145  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3146  ss <<" nBase = 0;\n else\n";
3147  }
3148  ss << " nBase=(int)";
3149  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3150  ss << ";\n";
3151  ss <<" tmp = coupdays_new(nSettle,nMat,nFreq,nBase);\n";
3152  ss <<" return tmp;\n";
3153  ss << "}";
3154 }
3155 void OpCouppcd::BinInlineFun(std::set<std::string>& decls,
3156  std::set<std::string>& funs)
3157 {
3158  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3159  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3160  decls.insert(GetNullDateDecl);
3161  decls.insert(ScaDateDecl);
3162  decls.insert(addMonthsDecl);
3163  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3164  decls.insert(lcl_GetCouppcdDecl);
3165  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3166  funs.insert(DaysToDate);funs.insert(DateToDays);
3167  funs.insert(GetNullDate);
3168  funs.insert(ScaDate);
3169  funs.insert(addMonths);
3170  funs.insert(setDay);funs.insert(checklessthan);
3171  funs.insert(lcl_GetCouppcd);
3172 }
3173 void OpCouppcd::GenSlidingWindowFunction(
3174  std::stringstream &ss, const std::string &sSymName,
3175  SubArguments &vSubArguments)
3176 {
3177  ss << "\ndouble " << sSymName;
3178  ss << "_"<< BinFuncName() <<"(";
3179  for (size_t i = 0; i < vSubArguments.size(); i++)
3180  {
3181  if (i)
3182  ss << ",";
3183  vSubArguments[i]->GenSlidingWindowDecl(ss);
3184  }
3185  ss << ") {\n";
3186  ss << " double tmp = 0;\n";
3187  ss << " int gid0 = get_global_id(0);\n";
3188  ss << " int nSettle,nMat,nFreq,nBase;\n";
3189  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3190  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3191  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3192  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3193  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3194  {
3195  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3196  formula::SingleVectorRefToken *>(tmpCur0);
3197  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3198  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3199  ss <<" nSettle = 0;\n else\n";
3200  }
3201  ss <<" nSettle=(int)";
3202  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3203  ss <<";\n";
3204  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3205  {
3206  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3207  formula::SingleVectorRefToken *>(tmpCur1);
3208  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3209  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3210  ss <<" nMat = 0;\n else\n";
3211  }
3212  ss <<" nMat=(int)";
3213  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3214  ss <<";\n";
3215  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3216  {
3217  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3218  formula::SingleVectorRefToken *>(tmpCur2);
3219  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3220  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3221  ss <<" nFreq = 0;\n else\n";
3222  }
3223  ss << " nFreq=(int)";
3224  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3225  ss <<";\n";
3226  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3227  {
3228  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3229  formula::SingleVectorRefToken *>(tmpCur3);
3230  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3231  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3232  ss <<" nBase = 0;\n else\n";
3233  }
3234  ss << " nBase=(int)";
3235  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3236  ss << ";\n";
3237  ss <<" int nNullDate=693594;\n";
3238  ss <<" tmp = lcl_GetCouppcd(nNullDate,nSettle,nMat,nFreq,nBase);\n";
3239  ss <<" return tmp;\n";
3240  ss <<"}";
3241 }
3242 void OpCoupncd::BinInlineFun(std::set<std::string>& decls,
3243  std::set<std::string>& funs)
3244 {
3245  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3246  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3247  decls.insert(GetNullDateDecl);
3248  decls.insert(ScaDateDecl);
3249  decls.insert(addMonthsDecl);
3250  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3251  decls.insert(lcl_GetCoupncdDecl);
3252  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3253  funs.insert(DaysToDate);funs.insert(DateToDays);
3254  funs.insert(GetNullDate);
3255  funs.insert(ScaDate);
3256  funs.insert(addMonths);
3257  funs.insert(setDay);funs.insert(checklessthan);
3258  funs.insert(lcl_GetCoupncd);
3259 }
3260 void OpCoupncd::GenSlidingWindowFunction(
3261  std::stringstream &ss, const std::string &sSymName, SubArguments &
3262 vSubArguments)
3263 {
3264  ss << "\ndouble " << sSymName;
3265  ss << "_"<< BinFuncName() <<"(";
3266  for (size_t i = 0; i < vSubArguments.size(); i++)
3267  {
3268  if (i)
3269  ss << ",";
3270  vSubArguments[i]->GenSlidingWindowDecl(ss);
3271  }
3272  ss << ") {\n";
3273  ss << " double tmp = 0;\n";
3274  ss << " int gid0 = get_global_id(0);\n";
3275  ss << " int nSettle,nMat,nFreq,nBase;\n";
3276  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3277  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3278  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3279  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3280  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3281  {
3282  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3283  formula::SingleVectorRefToken *>(tmpCur0);
3284  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3285  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3286  ss <<" nSettle = 0;\n else\n";
3287  }
3288  ss <<" nSettle=(int)";
3289  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3290  ss <<";\n";
3291  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3292  {
3293  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3294  formula::SingleVectorRefToken *>(tmpCur1);
3295  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3296  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3297  ss <<" nMat = 0;\n else\n";
3298  }
3299  ss <<" nMat=(int)";
3300  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3301  ss <<";\n";
3302  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3303  {
3304  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3305  formula::SingleVectorRefToken *>(tmpCur2);
3306  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3307  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3308  ss <<" nFreq = 0;\n else\n";
3309  }
3310  ss << " nFreq=(int)";
3311  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3312  ss <<";\n";
3313  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3314  {
3315  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3316  formula::SingleVectorRefToken *>(tmpCur3);
3317  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3318  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3319  ss <<" nBase = 0;\n else\n";
3320  }
3321  ss << " nBase=(int)";
3322  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3323  ss << ";\n";
3324  ss <<" int nNullDate=693594;\n";
3325  ss <<" tmp = lcl_GetCoupncd(nNullDate,nSettle,nMat,nFreq,nBase);\n";
3326  ss <<" return tmp;\n";
3327  ss <<"}";
3328 }
3329 
3330 void OpCoupdaysnc::BinInlineFun(std::set<std::string>& decls,
3331  std::set<std::string>& funs)
3332 {
3333  decls.insert(IsLeapYearDecl); decls.insert(DaysInMonth_newDecl);
3334  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3335  decls.insert(DateToDays_newDecl);
3336  decls.insert(ScaDateDecl);
3337  decls.insert(addMonthsDecl); decls.insert(getDaysInMonthRangeDecl);
3338  decls.insert(GetDaysInYearsDecl); decls.insert(GetDaysInYearDecl);
3339  decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
3340  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3341  decls.insert(coupdaybs_newDecl);
3342  decls.insert(lcl_Getcoupdays_newDecl);
3343  decls.insert(lcl_Getcoupdaybs_newDecl);
3344  decls.insert(coupdays_newDecl);
3345  decls.insert(coupdaysnc_newDecl);
3346  funs.insert(IsLeapYear);funs.insert(DaysInMonth_new);
3347  funs.insert(DaysToDate);funs.insert(DateToDays_new);
3348  funs.insert(DateToDays);
3349  funs.insert(ScaDate);
3350  funs.insert(addMonths);funs.insert(getDaysInMonthRange);
3351  funs.insert(GetDaysInYears);funs.insert(GetDaysInYear);
3352  funs.insert(getDaysInYearRange);funs.insert(getDiff);
3353  funs.insert(setDay);funs.insert(checklessthan);
3354  funs.insert(lcl_Getcoupdaybs_new);
3355  funs.insert(coupdaybs_new);
3356  funs.insert(lcl_Getcoupdays_new);
3357  funs.insert(coupdaysnc_new);
3358  funs.insert(coupdays_new);
3359 }
3360 void OpCoupdaysnc::GenSlidingWindowFunction(
3361  std::stringstream &ss, const std::string &sSymName,
3362  SubArguments &vSubArguments)
3363 {
3364  ss << "\ndouble " << sSymName;
3365  ss << "_"<< BinFuncName() <<"(";
3366  for (size_t i = 0; i < vSubArguments.size(); i++)
3367  {
3368  if (i)
3369  ss << ",";
3370  vSubArguments[i]->GenSlidingWindowDecl(ss);
3371  }
3372  ss << ") {\n";
3373  ss << " double tmp = 0;\n";
3374  ss << " int gid0 = get_global_id(0);\n";
3375  ss << " int nSettle,nMat,nFreq,nBase;\n";
3376  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3377  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3378  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3379  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3380  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3381  {
3382  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3383  formula::SingleVectorRefToken *>(tmpCur0);
3384  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3385  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3386  ss <<" nSettle = 0;\n else\n";
3387  }
3388  ss <<" nSettle=(int)";
3389  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3390  ss <<";\n";
3391  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3392  {
3393  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3394  formula::SingleVectorRefToken *>(tmpCur1);
3395  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3396  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3397  ss <<" nMat = 0;\n else\n";
3398  }
3399  ss <<" nMat=(int)";
3400  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3401  ss <<";\n";
3402  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3403  {
3404  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3405  formula::SingleVectorRefToken *>(tmpCur2);
3406  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3407  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3408  ss <<" nFreq = 0;\n else\n";
3409  }
3410  ss << " nFreq=(int)";
3411  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3412  ss <<";\n";
3413  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3414  {
3415  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3416  formula::SingleVectorRefToken *>(tmpCur3);
3417  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3418  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3419  ss <<" nBase = 0;\n else\n";
3420  }
3421  ss << " nBase=(int)";
3422  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3423  ss << ";\n";
3424  ss <<" tmp = coupdaysnc_new(nSettle,nMat,nFreq,nBase);\n";
3425  ss <<" return tmp;\n";
3426  ss << "}";
3427 }
3428 
3429 void OpCoupnum::BinInlineFun(std::set<std::string>& decls,
3430  std::set<std::string>& funs)
3431 {
3432  decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
3433  decls.insert(DaysToDateDecl);
3434  decls.insert(DateToDaysDecl);
3435  decls.insert(ScaDateDecl);
3436  decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3437  decls.insert(addMonthsDecl);decls.insert(lcl_Getcoupnum_newDecl);
3438  decls.insert(coupnum_newDecl);
3439  funs.insert(IsLeapYear);funs.insert(DaysInMonth_new);
3440  funs.insert(DaysToDate);
3441  funs.insert(DateToDays);
3442  funs.insert(ScaDate);
3443  funs.insert(setDay);funs.insert(checklessthan);
3444  funs.insert(addMonths);funs.insert(lcl_Getcoupnum_new);
3445  funs.insert(coupnum_new);
3446 }
3447 void OpCoupnum::GenSlidingWindowFunction(std::stringstream &ss,
3448  const std::string &sSymName, SubArguments &vSubArguments)
3449 {
3450  ss << "\ndouble " << sSymName;
3451  ss << "_"<< BinFuncName() <<"(";
3452  for (size_t i = 0; i < vSubArguments.size(); i++)
3453  {
3454  if (i)
3455  ss << ",";
3456  vSubArguments[i]->GenSlidingWindowDecl(ss);
3457  }
3458  ss << ") {\n";
3459  ss << " double tmp = 0;\n";
3460  ss << " int gid0 = get_global_id(0);\n";
3461  ss << " int nSettle,nMat,nFreq,nBase;\n";
3462  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3463  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3464  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3465  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3466  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3467  {
3468  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3469  formula::SingleVectorRefToken *>(tmpCur0);
3470  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3471  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3472  ss <<" nSettle = 0;\n else\n";
3473  }
3474  ss <<" nSettle=(int)";
3475  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3476  ss <<";\n";
3477  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3478  {
3479  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3480  formula::SingleVectorRefToken *>(tmpCur1);
3481  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3482  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3483  ss <<" nMat = 0;\n else\n";
3484  }
3485  ss <<" nMat=(int)";
3486  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3487  ss <<";\n";
3488  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3489  {
3490  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3491  formula::SingleVectorRefToken *>(tmpCur2);
3492  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3493  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3494  ss <<" nFreq = 0;\n else\n";
3495  }
3496  ss << " nFreq=(int)";
3497  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3498  ss <<";\n";
3499  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3500  {
3501  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3502  formula::SingleVectorRefToken *>(tmpCur3);
3503  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3504  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3505  ss <<" nBase = 0;\n else\n";
3506  }
3507  ss << " nBase=(int)";
3508  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3509  ss << ";\n";
3510  ss <<" tmp = coupnum_new(nSettle,nMat,nFreq,nBase);\n";
3511  ss <<" return tmp;\n";
3512  ss << "}";
3513 }
3514 void OpAmordegrc::BinInlineFun(std::set<std::string>& decls,
3515  std::set<std::string>& funs)
3516 {
3517  decls.insert(nCorrValDecl); decls.insert(RoundDecl);
3518  decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
3519  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3520  decls.insert(GetNullDateDecl); decls.insert(GetYearFracDecl);
3521  funs.insert(Round);
3522  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3523  funs.insert(DaysToDate);funs.insert(DateToDays);
3524  funs.insert(GetNullDate);funs.insert(GetYearFrac);
3525 }
3526 void OpAmordegrc::GenSlidingWindowFunction(std::stringstream &ss,
3527  const std::string &sSymName, SubArguments &vSubArguments)
3528 {
3529  ss << "\ndouble " << sSymName;
3530  ss << "_"<< BinFuncName() <<"(";
3531  for (size_t i = 0; i < vSubArguments.size(); i++)
3532  {
3533  if (i)
3534  ss << ",";
3535  vSubArguments[i]->GenSlidingWindowDecl(ss);
3536  }
3537  ss << ") {\n ";
3538  ss << "int gid0 = get_global_id(0);\n";
3539  ss << " double tmp = " << GetBottom() <<";\n";
3540  ss << " double fCost,fRestVal,fPer,fRate;\n";
3541  ss << " int nDate,nFirstPer,nBase;\n";
3542  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
3543  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
3544  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
3545  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
3546  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
3547  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
3548  FormulaToken *tmpCur6 = vSubArguments.size() < 7 ? nullptr : vSubArguments[6]->GetFormulaToken();
3549  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3550  {
3551  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3552  formula::SingleVectorRefToken *>(tmpCur0);
3553  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3554  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3555  ss <<" fCost = 0;\n else\n";
3556  }
3557  ss << " fCost=";
3558  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3559  ss <<";\n";
3560  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3561  {
3562  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3563  formula::SingleVectorRefToken *>(tmpCur1);
3564  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3565  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3566  ss <<" nDate = 0;\n else\n";
3567  }
3568  ss << " nDate=(int)";
3569  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3570  ss << ";\n";
3571  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3572  {
3573  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3574  formula::SingleVectorRefToken *>(tmpCur2);
3575  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3576  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3577  ss <<" nFirstPer = 0;\n else\n";
3578  }
3579  ss << " nFirstPer=(int)";
3580  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3581  ss <<";\n";
3582  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3583  {
3584  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3585  formula::SingleVectorRefToken *>(tmpCur3);
3586  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3587  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3588  ss <<" fRestVal = 0;\n else\n";
3589  }
3590  ss << " fRestVal=";
3591  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3592  ss << ";\n";
3593  if(tmpCur4->GetType() == formula::svSingleVectorRef)
3594  {
3595  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
3596  formula::SingleVectorRefToken *>(tmpCur4);
3597  ss <<" if(isnan(" <<vSubArguments[4]->GenSlidingWindowDeclRef();
3598  ss <<")||(gid0 >="<<tmpCurDVR4->GetArrayLength()<<"))\n";
3599  ss <<" fPer = 0;\n else\n";
3600  }
3601  ss << " fPer = ";
3602  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
3603  ss <<";\n";
3604  if(tmpCur5->GetType() == formula::svSingleVectorRef)
3605  {
3606  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
3607  formula::SingleVectorRefToken *>(tmpCur5);
3608  ss <<" if(isnan(" <<vSubArguments[5]->GenSlidingWindowDeclRef();
3609  ss <<")||(gid0 >="<<tmpCurDVR5->GetArrayLength()<<"))\n";
3610  ss <<" fRate = 0;\n else\n";
3611  }
3612  ss << " fRate=";
3613  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
3614  ss << ";\n";
3615  if(tmpCur6 == nullptr)
3616  {
3617  ss << " nBase = 0;\n";
3618  }
3619  else
3620  {
3621  if(tmpCur6->GetType() == formula::svSingleVectorRef)
3622  {
3623  const formula::SingleVectorRefToken*tmpCurDVR6=
3624  static_cast<const formula::SingleVectorRefToken *>(tmpCur6);
3625  ss <<" if(isnan(" <<vSubArguments[6]->GenSlidingWindowDeclRef();
3626  ss <<")||(gid0 >="<<tmpCurDVR6->GetArrayLength()<<"))\n";
3627  ss <<" nBase = 0;\n else\n";
3628  }
3629  ss << " nBase = (int)";
3630  ss << vSubArguments[6]->GenSlidingWindowDeclRef();
3631  ss << ";\n";
3632  }
3633  ss <<" uint nPer = convert_int( fPer );\n";
3634  ss <<" double fUsePer = 1.0 *pow( fRate,-1);\n";
3635  ss <<" double fAmorCoeff;\n";
3636  ss <<" if( fUsePer < 3.0 )\n";
3637  ss <<" fAmorCoeff = 1.0;\n";
3638  ss <<" else if( fUsePer < 5.0 )\n";
3639  ss <<" fAmorCoeff = 1.5;\n";
3640  ss <<" else if( fUsePer <= 6.0 )\n";
3641  ss <<" fAmorCoeff = 2.0;\n";
3642  ss <<" else\n";
3643  ss <<" fAmorCoeff = 2.5;\n";
3644  ss <<" fRate *= fAmorCoeff;\n";
3645  ss <<" tmp = Round( GetYearFrac( 693594,";
3646  ss <<"nDate, nFirstPer, nBase ) * fRate * fCost);\n";
3647  ss <<" fCost = fCost-tmp;\n";
3648  ss <<" double fRest = fCost - fRestVal;\n";
3649  ss <<" for( uint n = 0 ; n < nPer ; n++ )\n";
3650  ss <<" {\n";
3651  ss <<" tmp = Round( fRate * fCost);\n";
3652  ss <<" fRest -= tmp;\n";
3653  ss <<" if( fRest < 0.0 )\n";
3654  ss <<" {\n";
3655  ss <<" switch( nPer - n )\n";
3656  ss <<" {\n";
3657  ss <<" case 0:\n";
3658  ss <<" case 1:\n";
3659  ss <<" tmp = Round( fCost * 0.5);\n";
3660  ss <<" default:\n";
3661  ss <<" tmp = 0.0;\n";
3662  ss <<" }\n";
3663  ss <<" }\n";
3664  ss <<" fCost -= tmp;\n";
3665  ss <<" }\n";
3666  ss <<" return tmp;\n";
3667  ss <<"}";
3668 }
3669 void OpAmorlinc::BinInlineFun(std::set<std::string>& decls,
3670  std::set<std::string>& funs)
3671 {
3672  decls.insert(nCorrValDecl); decls.insert(RoundDecl);
3673  decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
3674  decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3675  decls.insert(GetYearFracDecl);
3676  funs.insert(Round);
3677  funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3678  funs.insert(DaysToDate);funs.insert(DateToDays);
3679  funs.insert(GetYearFrac);
3680 }
3681 void OpAmorlinc::GenSlidingWindowFunction(std::stringstream &ss,
3682  const std::string &sSymName, SubArguments &vSubArguments)
3683 {
3684  ss << "\ndouble " << sSymName;
3685  ss << "_"<< BinFuncName() <<"(";
3686  for (size_t i = 0; i < vSubArguments.size(); i++)
3687  {
3688  if (i)
3689  ss << ",";
3690  vSubArguments[i]->GenSlidingWindowDecl(ss);
3691  }
3692  ss << ") {\n";
3693  ss << " int gid0 = get_global_id(0);\n";
3694  ss << " double tmp = 0;\n";
3695  ss << " double fCost,fRestVal,fPer,fRate;\n";
3696  ss << " int nDate,nFirstPer,nBase;\n";
3697  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
3698  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
3699  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
3700  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
3701  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
3702  FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
3703  FormulaToken *tmpCur6 = vSubArguments.size() < 7 ? nullptr : vSubArguments[6]->GetFormulaToken();
3704  if(tmpCur0->GetType() == formula::svSingleVectorRef)
3705  {
3706  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3707  formula::SingleVectorRefToken *>(tmpCur0);
3708  ss <<" if(isnan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3709  ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3710  ss <<" fCost = 0;\n else\n";
3711  }
3712  ss << " fCost=";
3713  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3714  ss <<";\n";
3715  if(tmpCur1->GetType() == formula::svSingleVectorRef)
3716  {
3717  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3718  formula::SingleVectorRefToken *>(tmpCur1);
3719  ss <<" if(isnan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3720  ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3721  ss <<" nDate = 0;\n else\n";
3722  }
3723  ss << " nDate=(int)";
3724  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3725  ss << ";\n";
3726  if(tmpCur2->GetType() == formula::svSingleVectorRef)
3727  {
3728  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3729  formula::SingleVectorRefToken *>(tmpCur2);
3730  ss <<" if(isnan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3731  ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3732  ss <<" nFirstPer = 0;\n else\n";
3733  }
3734  ss << " nFirstPer=(int)";
3735  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3736  ss <<";\n";
3737  if(tmpCur3->GetType() == formula::svSingleVectorRef)
3738  {
3739  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3740  formula::SingleVectorRefToken *>(tmpCur3);
3741  ss <<" if(isnan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3742  ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3743  ss <<" fRestVal = 0;\n else\n";
3744  }
3745  ss << " fRestVal=";
3746  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3747  ss << ";\n";
3748  if(tmpCur4->GetType() == formula::svSingleVectorRef)
3749  {
3750  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
3751  formula::SingleVectorRefToken *>(tmpCur4);
3752  ss <<" if(isnan(" <<vSubArguments[4]->GenSlidingWindowDeclRef();
3753  ss <<")||(gid0 >="<<tmpCurDVR4->GetArrayLength()<<"))\n";
3754  ss <<" fPer = 0;\n else\n";
3755  }
3756  ss << " fPer = ";
3757  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
3758  ss <<";\n";
3759  if(tmpCur5->GetType() == formula::svSingleVectorRef)
3760  {
3761  const formula::SingleVectorRefToken*tmpCurDVR5= static_cast<const
3762  formula::SingleVectorRefToken *>(tmpCur5);
3763  ss <<" if(isnan(" <<vSubArguments[5]->GenSlidingWindowDeclRef();
3764  ss <<")||(gid0 >="<<tmpCurDVR5->GetArrayLength()<<"))\n";
3765  ss <<" fRate = 0;\n else\n";
3766  }
3767  ss << " fRate=";
3768  ss << vSubArguments[5]->GenSlidingWindowDeclRef();
3769  ss << ";\n";
3770  if(tmpCur6 == nullptr)
3771  {
3772  ss << " nBase = 0;\n";
3773  }
3774  else
3775  {
3776  if(tmpCur6->GetType() == formula::svSingleVectorRef)
3777  {
3778  const formula::SingleVectorRefToken*tmpCurDVR6=
3779  static_cast<const formula::SingleVectorRefToken *>(tmpCur6);
3780  ss <<" if(isnan(" <<vSubArguments[6]->GenSlidingWindowDeclRef();
3781  ss <<")||(gid0 >="<<tmpCurDVR6->GetArrayLength()<<"))\n";
3782  ss <<" nBase = 0;\n else\n";
3783  }
3784  ss << " nBase = (int)";
3785  ss << vSubArguments[6]->GenSlidingWindowDeclRef();
3786  ss << ";\n";
3787  }
3788  ss <<" int nPer = convert_int( fPer );\n";
3789  ss <<" double fOneRate = fCost * fRate;\n";
3790  ss <<" double fCostDelta = fCost - fRestVal;\n";
3791  ss <<" double f0Rate = GetYearFrac( 693594,";
3792  ss <<"nDate, nFirstPer, nBase )* fRate * fCost;\n";
3793  ss <<" int nNumOfFullPeriods = (int)";
3794  ss <<"( ( fCost - fRestVal - f0Rate) *pow(fOneRate,-1) );\n";
3795  ss <<" if( nPer == 0 )\n";
3796  ss <<" tmp = f0Rate;\n";
3797  ss <<" else if( nPer <= nNumOfFullPeriods )\n";
3798  ss <<" tmp = fOneRate;\n";
3799  ss <<" else if( nPer == nNumOfFullPeriods + 1 )\n";
3800  ss <<" tmp = fCostDelta - fOneRate * nNumOfFullPeriods - f0Rate;\n";
3801  ss <<" else\n";
3802  ss <<" tmp = 0.0;\n";
3803  ss <<" return tmp;\n";
3804  ss <<"}";
3805 }
3806 void OpReceived::BinInlineFun(std::set<std::string>& decls,
3807  std::set<std::string>& funs)
3808 {
3809  decls.insert(GetYearDiffDecl);decls.insert(GetDiffDateDecl);
3810  decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
3811  decls.insert(GetNullDateDecl);decls.insert(IsLeapYearDecl);
3812  decls.insert(DateToDaysDecl);
3813  funs.insert(GetDiffDate);funs.insert(DaysToDate);
3814  funs.insert(DaysInMonth);funs.insert(GetNullDate);
3815  funs.insert(DateToDays);funs.insert(IsLeapYear);
3816  funs.insert(GetYearDiff);
3817 }
3818 
3819 void OpReceived::GenSlidingWindowFunction(std::stringstream &ss,
3820  const std::string &sSymName, SubArguments &vSubArguments)
3821 {
3822  ss << "\ndouble " << sSymName;
3823  ss << "_"<< BinFuncName() <<"(";
3824  for (size_t i = 0; i < vSubArguments.size(); i++)
3825  {
3826  if (i)
3827  ss << ",";
3828  vSubArguments[i]->GenSlidingWindowDecl(ss);
3829  }
3830  ss << ") {\n";
3831  ss << " double tmp = " << GetBottom() <<";\n";
3832  ss << " int gid0 = get_global_id(0);\n";
3833  ss << " int nSettle, nMat;\n";
3834  ss << " double fInvest,fDisc;\n";
3835  ss << " int rOB;\n";
3836  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
3837  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3838  formula::SingleVectorRefToken *>(tmpCur0);
3839  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
3840  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3841  formula::SingleVectorRefToken *>(tmpCur1);
3842  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
3843  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3844  formula::SingleVectorRefToken *>(tmpCur2);
3845  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
3846  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3847  formula::SingleVectorRefToken *>(tmpCur3);
3848  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
3849  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
3850  formula::SingleVectorRefToken *>(tmpCur4);
3851  ss<< " int buffer_settle_len = ";
3852  ss<< tmpCurDVR0->GetArrayLength();
3853  ss << ";\n";
3854  ss<< " int buffer_mat_len = ";
3855  ss<< tmpCurDVR1->GetArrayLength();
3856  ss << ";\n";
3857  ss<< " int buffer_invest_len = ";
3858  ss<< tmpCurDVR2->GetArrayLength();
3859  ss << ";\n";
3860  ss<< " int buffer_disc_len = ";
3861  ss<< tmpCurDVR3->GetArrayLength();
3862  ss << ";\n";
3863  ss<< " int buffer_rob_len = ";
3864  ss<< tmpCurDVR4->GetArrayLength();
3865  ss << ";\n";
3866  ss <<" if(gid0 >= buffer_settle_len || isnan(";
3867  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
3868  ss <<"))\n";
3869  ss <<" nSettle = 0;\n\telse\n";
3870  ss <<" nSettle = (int)"<<vSubArguments[0]->GenSlidingWindowDeclRef();
3871  ss <<";\n";
3872  ss <<" if(gid0 >= buffer_mat_len || isnan(";
3873  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
3874  ss <<"))\n";
3875  ss <<" nMat = 0;\n\telse\n";
3876  ss <<" nMat = (int)";
3877  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
3878  ss <<";\n";
3879  ss <<" if(gid0 >= buffer_invest_len || isnan(";
3880  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
3881  ss <<"))\n";
3882  ss <<" fInvest = 0;\n\telse\n";
3883  ss <<" fInvest = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
3884  ss <<";\n";
3885  ss <<" if(gid0 >= buffer_disc_len || isnan(";
3886  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
3887  ss <<"))\n";
3888  ss <<" fDisc = 0;\n\telse\n";
3889  ss <<" fDisc = "<<vSubArguments[3]->GenSlidingWindowDeclRef();
3890  ss <<";\n";
3891  ss <<" if(gid0 >= buffer_rob_len || isnan(";
3892  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
3893  ss <<"))\n";
3894  ss <<" rOB = 0;\n\telse\n";
3895  ss <<" rOB = (int)"<<vSubArguments[4]->GenSlidingWindowDeclRef();
3896  ss <<";\n";
3897  ss << " double tmpvalue = (1.0-(fDisc";
3898  ss <<" * GetYearDiff( GetNullDate()";
3899  ss <<",nSettle,nMat,rOB)));\n";
3900  ss << " tmp = fInvest*pow(tmpvalue,-1);\n";
3901  ss << " return tmp;\n";
3902  ss << "}";
3903 }
3904 
3905  void OpYielddisc::BinInlineFun(std::set<std::string>& decls,
3906  std::set<std::string>& funs)
3907 {
3908  decls.insert(GetYearFracDecl);decls.insert(GetNullDateDecl);
3909  decls.insert(DateToDaysDecl);decls.insert(DaysToDateDecl);
3910  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
3911 
3912  funs.insert(GetYearFrac);funs.insert(GetNullDate);
3913  funs.insert(DateToDays);funs.insert(DaysToDate);
3914  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
3915 }
3916 void OpYielddisc::GenSlidingWindowFunction(
3917  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3918 {
3919  CHECK_PARAMETER_COUNT(5,5);
3920  ss << "\ndouble " << sSymName;
3921  ss << "_"<< BinFuncName() <<"(";
3922  for (size_t i = 0; i < vSubArguments.size(); i++)
3923  {
3924  if (i)
3925  ss << ",";
3926  vSubArguments[i]->GenSlidingWindowDecl(ss);
3927  }
3928  ss << ") {\n\t";
3929  ss << "double tmp = 0;\n\t";
3930  ss << "int gid0 = get_global_id(0);\n\t";
3931  ss << "double tmp000;\n\t";
3932  ss << "double tmp001;\n\t";
3933  ss << "double tmp002;\n\t";
3934  ss << "double tmp003;\n\t";
3935  ss << "double tmp004;\n\t";
3936 
3937  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
3938  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
3939  formula::SingleVectorRefToken *>(tmpCur0);
3940 
3941  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
3942  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
3943  formula::SingleVectorRefToken *>(tmpCur1);
3944 
3945  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
3946  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
3947  formula::SingleVectorRefToken *>(tmpCur2);
3948 
3949  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
3950  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
3951  formula::SingleVectorRefToken *>(tmpCur3);
3952 
3953  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
3954  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
3955  formula::SingleVectorRefToken *>(tmpCur4);
3956 
3957  ss<< "int buffer_tmp000_len = ";
3958  ss<< tmpCurDVR0->GetArrayLength();
3959  ss << ";\n\t";
3960 
3961  ss<< "int buffer_tmp001_len = ";
3962  ss<< tmpCurDVR1->GetArrayLength();
3963  ss << ";\n\t";
3964 
3965  ss<< "int buffer_tmp002_len = ";
3966  ss<< tmpCurDVR2->GetArrayLength();
3967  ss << ";\n\t";
3968 
3969  ss<< "int buffer_tmp003_len = ";
3970  ss<< tmpCurDVR3->GetArrayLength();
3971  ss << ";\n\t";
3972 
3973  ss<< "int buffer_tmp004_len = ";
3974  ss<< tmpCurDVR4->GetArrayLength();
3975  ss << ";\n\t";
3976 
3977  ss<<"if(gid0>=buffer_tmp000_len || isnan(";
3978  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3979  ss<<"))\n\t\t";
3980  ss<<"tmp000 = 0;\n\telse \n\t\t";
3981  ss<<"tmp000 = ";
3982  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3983  ss<<";\n\t";
3984 
3985  ss<<"if(gid0>=buffer_tmp001_len || isnan(";
3986  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3987  ss<<"))\n\t\t";
3988  ss<<"tmp001 = 0;\n\telse \n\t\t";
3989  ss<<"tmp001 = ";
3990  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3991  ss<<";\n\t";
3992 
3993  ss<<"if(gid0>=buffer_tmp002_len || isnan(";
3994  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3995  ss<<"))\n\t\t";
3996  ss<<"tmp002 = 0;\n\telse \n\t\t";
3997  ss<<"tmp002 = ";
3998  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3999  ss<<";\n\t";
4000 
4001  ss<<"if(gid0>=buffer_tmp003_len || isnan(";
4002  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4003  ss<<"))\n\t\t";
4004  ss<<"tmp003 = 0;\n\telse \n\t\t";
4005  ss<<"tmp003 = ";
4006  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4007  ss<<";\n\t";
4008 
4009  ss<<"if(gid0>=buffer_tmp004_len || isnan(";
4010  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4011  ss<<"))\n\t\t";
4012  ss<<"tmp004 = 0;\n\telse \n\t\t";
4013  ss<<"tmp004 = ";
4014  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4015  ss<<";\n\t";
4016 
4017  ss<< "if(tmp002 <= 0 || tmp003 <= 0 || tmp000 >= tmp001 )\n";
4018  ss<< " return CreateDoubleError(IllegalArgument);\n";
4019  ss<< "tmp = (tmp003/tmp002)-1;\n\t";
4020  ss << "tmp /= GetYearFrac( GetNullDate(),tmp000,tmp001,tmp004);\n\t";
4021  ss << "return tmp;\n";
4022  ss << "}";
4023 }
4024 
4025  void OpTbillprice::BinInlineFun(std::set<std::string>& decls,
4026  std::set<std::string>& funs)
4027 {
4028  decls.insert(GetYearFracDecl);
4029  decls.insert(DateToDaysDecl);decls.insert(DaysToDateDecl);
4030  decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
4031 
4032  funs.insert(GetYearFrac);
4033  funs.insert(DateToDays);funs.insert(DaysToDate);
4034  funs.insert(DaysInMonth);funs.insert(IsLeapYear);
4035 }
4036 
4037 void OpTbillprice::GenSlidingWindowFunction(
4038  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
4039 {
4040  ss << "\ndouble " << sSymName;
4041  ss << "_"<< BinFuncName() <<"(";
4042  for (size_t i = 0; i < vSubArguments.size(); i++)
4043  {
4044  if (i)
4045  ss << ",";
4046  vSubArguments[i]->GenSlidingWindowDecl(ss);
4047  }
4048  ss << ") {\n";
4049  ss << " int gid0 = get_global_id(0);\n";
4050  ss << " double tmp = 0;\n";
4051 
4052  ss << " int singleIndex = gid0;\n";
4053  ss << " int doubleIndex = gid0;\n";
4054  ss << " int i = gid0;\n";
4055  GenTmpVariables(ss,vSubArguments);
4056  CheckAllSubArgumentIsNan(ss,vSubArguments);
4057 
4058  ss << " tmp1+=1.0;\n";
4059  ss << " double fFraction =GetYearFrac(693594,tmp0,tmp1,0);\n";
4060  ss << " tmp = 100.0 * ( 1.0 - tmp2 * fFraction );\n";
4061  ss << " return tmp;\n";
4062  ss << "}\n";
4063 }
4064  void RATE::BinInlineFun(std::set<std::string>& decls,
4065  std::set<std::string>& funs)
4066 {
4067  decls.insert(nCorrValDecl);
4068  decls.insert(SCdEpsilonDecl);decls.insert(RoundDecl);
4069  funs.insert(Round);
4070 }
4071 
4072 void RATE::GenSlidingWindowFunction(
4073  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
4074 {
4075  ss << "\ndouble " << sSymName;
4076  ss << "_"<< BinFuncName() <<"(";
4077  for (size_t i = 0; i < vSubArguments.size(); i++)
4078  {
4079  if (i)
4080  ss << ",";
4081  vSubArguments[i]->GenSlidingWindowDecl(ss);
4082  }
4083 
4084  FormulaToken* pCur = vSubArguments[5]->GetFormulaToken();
4085  assert(pCur);
4086  const formula::SingleVectorRefToken* pSVR =
4087  static_cast< const formula::SingleVectorRefToken* >(pCur);
4088  assert(pSVR);
4089  ss << ") {\n";
4090  ss << " double result;\n";
4091  ss << " int gid0 = get_global_id(0);\n";
4092  ss << " bool bValid = true, bFound = false;\n";
4093  ss << " double fX, fXnew, fTerm, fTermDerivation;\n";
4094  ss << " double fGeoSeries, fGeoSeriesDerivation;\n";
4095  ss << " int nIterationsMax = 150;\n";
4096  ss << " int nCount = 0;\n";
4097  ss << " double fEpsilonSmall = 1.0E-14;\n";
4098  ss << " double arg0, arg1, arg2, arg3, arg4, arg5;\n";
4099  ss << " arg0=" << vSubArguments[0]->GenSlidingWindowDeclRef()<<";\n";
4100  ss << " arg1=" << vSubArguments[1]->GenSlidingWindowDeclRef()<<";\n";
4101  ss << " arg2=" << vSubArguments[2]->GenSlidingWindowDeclRef()<<";\n";
4102  ss << " arg3=" << vSubArguments[3]->GenSlidingWindowDeclRef()<<";\n";
4103  ss << " arg4=" << vSubArguments[4]->GenSlidingWindowDeclRef()<<";\n";
4104  ss << " arg5=" << vSubArguments[5]->GenSlidingWindowDeclRef()<<";\n";
4105  ss << " int guessLen = " << pSVR->GetArrayLength() << ";\n";
4106  ss << " if (isnan(arg0) || isnan(arg1) || isnan(arg2)){\n";
4107  ss << " result = 523;\n";
4108  ss << " return result;\n";
4109  ss << " }\n";
4110  ss << " if (isnan(arg3))\n";
4111  ss << " arg3 = 0.0;\n";
4112  ss << " if (isnan(arg4))\n";
4113  ss << " arg4 = 0.0;\n";
4114  ss << " if (isnan(arg5))\n";
4115  ss << " arg5 = 0.1;\n";
4116  ss << " if (gid0 >= guessLen)\n";
4117  ss << " arg5 = 0.1;\n";
4118  ss << " arg3 = arg3 - arg1 * arg4;\n";
4119  ss << " arg2 = arg2 + arg1 * arg4;\n";
4120  ss << " if (arg0 == Round(arg0)){\n";
4121  ss << " fX = arg5;\n";
4122  ss << " double fPowN, fPowNminus1;\n";
4123  ss << " while (!bFound && nCount < nIterationsMax)\n";
4124  ss << " {\n";
4125  ss << " fPowNminus1 = pow( 1.0+fX, arg0-1.0);\n";
4126  ss << " fPowN = fPowNminus1 * (1.0+fX);\n";
4127  ss << " if (fX == 0.0)\n";
4128  ss << " {\n";
4129  ss << " fGeoSeries = arg0;\n";
4130  ss << " fGeoSeriesDerivation = arg0 * (arg0-1.0)";
4131  ss << "*pow(2.0,-1);\n";
4132  ss << " }\n";
4133  ss << " else\n";
4134  ss << " {";
4135  ss << " fGeoSeries = (fPowN-1.0)*pow(fX,-1);\n";
4136  ss << " fGeoSeriesDerivation =";
4137  ss << " arg0 * fPowNminus1 * pow( fX , -1) - fGeoSeries * pow(fX, -1);\n";
4138  ss << " }\n";
4139  ss << " fTerm = arg3 + arg2 *fPowN+ arg1 * fGeoSeries;\n";
4140  ss << " fTermDerivation = arg2 * arg0 * fPowNminus1 +";
4141  ss << "arg1 * fGeoSeriesDerivation;\n";
4142  ss << " if (fabs(fTerm) < fEpsilonSmall)\n";
4143  ss << " bFound = true;\n";
4144  ss << " else\n";
4145  ss << " {\n";
4146  ss << " if (fTermDerivation == 0.0)\n";
4147  ss << " fXnew = fX + 1.1 * SCdEpsilon;\n";
4148  ss << " else\n";
4149  ss << " fXnew = fX - fTerm ";
4150  ss << "*pow( fTermDerivation,-1);\n";
4151  ss << " nCount++;\n";
4152  ss << " bFound = (fabs(fXnew - fX) < SCdEpsilon);\n";
4153  ss << " fX = fXnew;\n";
4154  ss << " }\n";
4155  ss << " }\n";
4156  ss << " }\n";
4157  ss << " else\n";
4158  ss << " {";
4159  ss << " fX = (arg5 < -1.0) ? -1.0 : arg5;\n";
4160  ss << " while (bValid && !bFound && nCount < nIterationsMax)\n";
4161  ss << " {\n";
4162  ss << " if (fX == 0.0){\n";
4163  ss << " fGeoSeries = arg0;\n";
4164  ss << " fGeoSeriesDerivation = arg0 * ";
4165  ss << "(arg0-1.0)* pow(2.0,-1);\n";
4166  ss << " }else{\n";
4167  ss << " fGeoSeries = (pow( 1.0+fX, arg0) - 1.0)";
4168  ss << " *pow( fX,-1);\n";
4169  ss << " fGeoSeriesDerivation =";
4170  ss << " arg0 * pow(1.0+fX,arg0-1.0) *pow(fX,-1)";
4171  ss << " - fGeoSeries *pow( fX,-1);\n";
4172  ss << " }\n";
4173  ss << " fTerm = arg3 + arg2 *pow(1.0+fX, arg0)";
4174  ss << "+ arg1 * fGeoSeries;\n";
4175  ss << " fTermDerivation =";
4176  ss << "arg2*arg0*pow(1.0+fX,arg0-1.0)";
4177  ss << "+arg1*fGeoSeriesDerivation;\n";
4178  ss << " if (fabs(fTerm) < fEpsilonSmall)\n";
4179  ss << " bFound = true;\n";
4180  ss << " else{\n";
4181  ss << " if (fTermDerivation == 0.0)\n";
4182  ss << " fXnew = fX + 1.1 * SCdEpsilon;\n";
4183  ss << " else\n";
4184  ss << " fXnew = fX - fTerm ";
4185  ss << "*pow( fTermDerivation,-1);\n";
4186  ss << " nCount++;\n";
4187  ss << " bFound = (fabs(fXnew - fX) < SCdEpsilon);\n";
4188  ss << " fX = fXnew;\n";
4189  ss << " bValid = (fX >= -1.0);\n";
4190  ss << " }\n";
4191  ss << " }\n";
4192  ss << " }\n";
4193  ss << " if (bValid && bFound)\n";
4194  ss << " result = fX;\n";
4195  ss << " else\n";
4196  ss << " result = 523;\n";
4197  ss << " return result;\n";
4198  ss << "}";
4199 }
4200 
4201  void OpTbillyield::BinInlineFun(std::set<std::string>& decls,
4202  std::set<std::string>& funs)
4203 {
4204  decls.insert(GetDiffDate360Decl);decls.insert(IsLeapYearDecl);
4205  decls.insert(DateToDaysDecl);decls.insert(DaysToDate_LocalBarrierDecl);
4206  decls.insert(DaysInMonthDecl);decls.insert(GetNullDateDecl);
4207  decls.insert(GetDiffDate360_Decl);
4208  funs.insert(GetDiffDate360);funs.insert(DateToDays);
4209  funs.insert(DaysToDate_LocalBarrier);funs.insert(IsLeapYear);
4210  funs.insert(DaysInMonth);funs.insert(GetNullDate);
4211  funs.insert(GetDiffDate360_);
4212 
4213 }
4214 
4215 void OpTbillyield::GenSlidingWindowFunction(
4216  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
4217 {
4218  ss << "\ndouble " << sSymName;
4219  ss << "_"<< BinFuncName() <<"(";
4220  for (size_t i = 0; i < vSubArguments.size(); i++)
4221  {
4222  if (i)
4223  ss << ",";
4224  vSubArguments[i]->GenSlidingWindowDecl(ss);
4225  }
4226  ss << ") {\n\t";
4227  ss << "int gid0 = get_global_id(0);\n\t";
4228  ss << "double tmp = 0;\n\t";
4229  ss << "double tmp000;\n\t";
4230  ss << "double tmp001;\n\t";
4231  ss << "double tmp002;\n\t";
4232 
4233  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4234  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
4235  formula::SingleVectorRefToken *>(tmpCur0);
4236 
4237  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4238  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
4239  formula::SingleVectorRefToken *>(tmpCur1);
4240 
4241  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4242  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
4243  formula::SingleVectorRefToken *>(tmpCur2);
4244 
4245  ss<< "int buffer_tmp000_len = ";
4246  ss<< tmpCurDVR0->GetArrayLength();
4247  ss << ";\n\t";
4248 
4249  ss<< "int buffer_tmp001_len = ";
4250  ss<< tmpCurDVR1->GetArrayLength();
4251  ss << ";\n\t";
4252 
4253  ss<< "int buffer_tmp002_len = ";
4254  ss<< tmpCurDVR2->GetArrayLength();
4255  ss << ";\n\t";
4256 
4257  ss<<"if(gid0>=buffer_tmp000_len || isnan(";
4258  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4259  ss<<"))\n\t\t";
4260  ss<<"tmp000 = 0;\n\telse \n\t\t";
4261  ss<<"tmp000 = ";
4262  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4263  ss<<";\n\t";
4264 
4265  ss<<"if(gid0>=buffer_tmp001_len || isnan(";
4266  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4267  ss<<"))\n\t\t";
4268  ss<<"tmp001 = 0;\n\telse \n\t\t";
4269  ss<<"tmp001 = ";
4270  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4271  ss<<";\n\t";
4272 
4273  ss<<"if(gid0>=buffer_tmp002_len || isnan(";
4274  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4275  ss<<"))\n\t\t";
4276  ss<<"tmp002 = 0;\n\telse \n\t\t";
4277  ss<<"tmp002 = ";
4278  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4279  ss<<";\n\t";
4280  ss <<" int nDiff=GetDiffDate360(GetNullDate(),tmp000,tmp001,true);\n";
4281  ss <<" nDiff++;\n";
4282  ss <<" tmp=100.0;\n";
4283  ss <<" tmp = tmp *pow( tmp002,-1);\n";
4284  ss <<" tmp = tmp - 1.0;\n";
4285  ss <<" tmp = tmp * pow( nDiff,-1.0 );\n";
4286  ss <<" tmp = tmp * 360.0;\n";
4287  ss <<" return tmp;\n";
4288  ss << "}\n";
4289 }
4290 void OpDDB::GenSlidingWindowFunction(std::stringstream& ss,
4291  const std::string &sSymName, SubArguments& vSubArguments)
4292 {
4293  ss << "\ndouble " << sSymName;
4294  ss << "_"<< BinFuncName() <<"(";
4295  for (size_t i = 0; i < vSubArguments.size(); i++)
4296  {
4297  if (i)
4298  ss << ",";
4299  vSubArguments[i]->GenSlidingWindowDecl(ss);
4300  }
4301  ss << ") {\n";
4302  ss << " int gid0 = get_global_id(0);\n";
4303  ss << " double tmp = 0;\n";
4304  ss << " double fCost, fSalvage, fLife, fPeriod, fFactor;\n";
4305  ss << " double fRate, fOldValue, fNewValue;\n";
4306 
4307  FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
4308  FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
4309  FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
4310  FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
4311  FormulaToken* tmpCur4 = vSubArguments[4]->GetFormulaToken();
4312  if(tmpCur0->GetType() == formula::svSingleVectorRef)
4313  {
4314  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
4315  formula::SingleVectorRefToken *>(tmpCur0);
4316  ss <<" if(gid0 >= "<<tmpCurDVR0->GetArrayLength()<<" || isnan(";
4317  ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
4318  ss <<"))\n";
4319  ss <<" fCost = 0;\n else\n";
4320  }
4321  ss <<" fCost = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
4322  ss <<";\n";
4323  if(tmpCur1->GetType() == formula::svSingleVectorRef)
4324  {
4325  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
4326  formula::SingleVectorRefToken *>(tmpCur1);
4327  ss <<" if(gid0 >= "<<tmpCurDVR1->GetArrayLength()<<" || isnan(";
4328  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
4329  ss <<"))\n";
4330  ss <<" fSalvage = 0;\n else\n";
4331  }
4332  ss <<" fSalvage = ";
4333  ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
4334  ss <<";\n";
4335  if(tmpCur2->GetType() == formula::svSingleVectorRef)
4336  {
4337  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
4338  formula::SingleVectorRefToken *>(tmpCur2);
4339  ss <<" if(gid0 >= "<<tmpCurDVR2->GetArrayLength()<<" || isnan(";
4340  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
4341  ss <<"))\n";
4342  ss <<" fLife = 0;\n else\n";
4343  }
4344  ss <<" fLife = ";
4345  ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
4346  ss <<";\n";
4347  if(tmpCur3->GetType() == formula::svSingleVectorRef)
4348  {
4349  const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
4350  formula::SingleVectorRefToken *>(tmpCur3);
4351  ss <<" if(gid0 >= "<<tmpCurDVR3->GetArrayLength()<<" || isnan(";
4352  ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
4353  ss <<"))\n";
4354  ss <<" fPeriod = 0;\n else\n";
4355  }
4356  ss <<" fPeriod = "<<vSubArguments[3]->GenSlidingWindowDeclRef();
4357  ss <<";\n";
4358  if(tmpCur4->GetType() == formula::svSingleVectorRef)
4359  {
4360  const formula::SingleVectorRefToken*tmpCurDVR4= static_cast<const
4361  formula::SingleVectorRefToken *>(tmpCur4);
4362  ss <<" if(gid0 >= "<<tmpCurDVR4->GetArrayLength()<<" || isnan(";
4363  ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
4364  ss <<"))\n";
4365  ss <<" fFactor = 0;\n else\n";
4366  }
4367  ss <<" fFactor = "<<vSubArguments[4]->GenSlidingWindowDeclRef();
4368  ss <<";\n";
4369  ss <<" fRate = fFactor * pow(fLife,-1);\n";
4370  ss <<" if (fRate >= 1.0)\n";
4371  ss <<" {\n";
4372  ss <<" fRate = 1.0;\n";
4373  ss <<" if (fPeriod == 1.0)\n";
4374  ss <<" fOldValue = fCost;\n";
4375  ss <<" else\n";
4376  ss <<" fOldValue = 0.0;\n";
4377  ss <<" }\n";
4378  ss <<" else\n";
4379  ss <<" fOldValue = fCost * pow(1.0 - fRate, fPeriod - 1);\n";
4380  ss <<" fNewValue = fCost * pow(1.0 - fRate, fPeriod);\n";
4381  ss <<" if (fNewValue < fSalvage)\n";
4382  ss <<" tmp = fOldValue - fSalvage;\n";
4383  ss <<" else\n";
4384  ss <<" tmp = fOldValue - fNewValue;\n";
4385  ss <<" if (tmp < 0.0)\n";
4386  ss <<" tmp = 0.0;\n";
4387  ss <<" return tmp;\n";
4388  ss <<"}";
4389 }
4390 void OpPV::GenSlidingWindowFunction(
4391  std::stringstream &ss, const std::string &sSymName, SubArguments &
4392 vSubArguments)
4393 {
4394  ss << "\ndouble " << sSymName;
4395  ss << "_"<< BinFuncName() <<"(";
4396  for (size_t i = 0; i < vSubArguments.size(); i++)
4397  {
4398  if (i)
4399  ss << ",";
4400  vSubArguments[i]->GenSlidingWindowDecl(ss);
4401  }
4402  ss << ") {\n";
4403  ss << " double result = 0;\n";
4404  ss << " int gid0 = get_global_id(0);\n";
4405  ss << " double rate;\n";
4406  ss << " double nper;\n";
4407  ss << " double pmt;\n";
4408  ss << " double fv;\n";
4409  ss << " double type;\n";
4410 
4411  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4412  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
4413  formula::SingleVectorRefToken *>(tmpCur0);
4414 
4415  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4416  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
4417  formula::SingleVectorRefToken *>(tmpCur1);
4418 
4419  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4420  const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
4421  formula::SingleVectorRefToken *>(tmpCur2);
4422 
4423  if(vSubArguments.size()>3)
4424  {
4425  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
4426  const formula::SingleVectorRefToken* tmpCurDVR3= static_cast<const formula::SingleVectorRefToken *>(
4427 tmpCur3);
4428  ss<< " int buffer_fv_len = ";
4429  ss<< tmpCurDVR3->GetArrayLength();
4430  ss << ";\n";
4431  }
4432 
4433  if(vSubArguments.size()>4)
4434  {
4435  FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
4436  const formula::SingleVectorRefToken* tmpCurDVR4= static_cast<const formula::SingleVectorRefToken *>(
4437 tmpCur4);
4438  ss<< " int buffer_type_len = ";
4439  ss<< tmpCurDVR4->GetArrayLength();
4440  ss << ";\n";
4441  }
4442 
4443  ss<< " int buffer_rate_len = ";
4444  ss<< tmpCurDVR0->GetArrayLength();
4445  ss << ";\n";
4446 
4447  ss<< " int buffer_nper_len = ";
4448  ss<< tmpCurDVR1->GetArrayLength();
4449  ss << ";\n";
4450 
4451  ss<< " int buffer_pmt_len = ";
4452  ss<< tmpCurDVR2->GetArrayLength();
4453  ss << ";\n";
4454 
4455  ss<<" if(gid0>=buffer_rate_len || isnan(";
4456  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4457  ss<<"))\n";
4458  ss<<" rate = 0;\n else \n";
4459  ss<<" rate = ";
4460  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4461  ss<<";\n";
4462 
4463  ss<<" if(gid0>=buffer_nper_len || isnan(";
4464  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4465  ss<<"))\n";
4466  ss<<" nper = 0;\n else \n";
4467  ss<<" nper = ";
4468  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4469  ss<<";\n";
4470 
4471  ss<<" if(gid0>=buffer_pmt_len || isnan(";
4472  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4473  ss<<"))\n";
4474  ss<<" pmt = 0;\n else \n";
4475  ss<<" pmt = ";
4476  ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4477  ss<<";\n";
4478 
4479  if(vSubArguments.size()>3)
4480  {
4481  ss<<" if(gid0>=buffer_fv_len || isnan(";
4482  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4483  ss<<"))\n";
4484  ss<<" fv = 0;\n else \n";
4485  ss<<" fv = ";
4486  ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4487  ss<<";\n";
4488  }else
4489  {
4490  ss<<" fv = 0;\n";
4491  }
4492 
4493  if(vSubArguments.size()>4)
4494  {
4495  ss<<" if(gid0>=buffer_type_len || isnan(";
4496  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4497  ss<<"))\n";
4498  ss<<" type = 0;\n else \n";
4499  ss<<" type = ";
4500  ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4501  ss<<";\n";
4502  }else
4503  {
4504  ss<<" type = 0;\n";
4505  }
4506  ss << " if(rate == 0)\n";
4507  ss << " result=fv+pmt*nper;\n";
4508  ss << " else if(type > 0)\n";
4509  ss << " result=(fv*pow(1+rate,-nper))+";
4510  ss << "(pmt*(1-pow(1+rate,-nper+1))*pow(rate,-1))+pmt;\n";
4511  ss << " else\n";
4512  ss << " result=(fv*pow(1+rate,-nper))+";
4513  ss << "(pmt*(1-pow(1+rate,-nper))*pow(rate,-1));\n";
4514  ss << " return -result;\n";
4515  ss << "}";
4516 }
4517  void OpVDB::BinInlineFun(std::set<std::string>& decls,
4518  std::set<std::string>& funs)