sc/source/core/opencl/formulagroupcl.cxx       |   72 ++++++++++++-------------
 sc/source/core/opencl/op_financial.cxx         |   65 ++++++++--------------
 sc/source/core/opencl/op_financial.hxx         |   22 +++----
 sc/source/core/opencl/op_financial_helpers.hxx |   65 +++++++++++-----------
 sc/source/core/opencl/op_statistical.cxx       |   52 +++++++++---------
 5 files changed, 132 insertions(+), 144 deletions(-)

New commits:
commit 380b42bd1b8be39d774245f93651515b45df150b
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Tue Sep 20 18:08:31 2022 +0200
Commit:     Luboš Luňák <l.lu...@collabora.com>
CommitDate: Wed Sep 21 10:23:36 2022 +0200

    fix and enable some disabled opencl functions
    
    Change-Id: Ic7bd01edcc7297317b390567590821ce00e5ef22
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/140254
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>

diff --git a/sc/source/core/opencl/formulagroupcl.cxx 
b/sc/source/core/opencl/formulagroupcl.cxx
index 75485b47dc21..af81125e9c99 100644
--- a/sc/source/core/opencl/formulagroupcl.cxx
+++ b/sc/source/core/opencl/formulagroupcl.cxx
@@ -2523,10 +2523,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpLogNormDist>(), 
nResultSize));
                 break;
-            /*case ocGammaDist:
+            case ocGammaDist:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i], std::make_shared<OpGammaDist));
-                break;*/
+                         ft->Children[i], std::make_shared<OpGammaDist>(), 
nResultSize));
+                break;
             case ocLn:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpLn>(), 
nResultSize));
@@ -2551,10 +2551,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpVar>(), 
nResultSize));
                 break;
-            /*case ocChiDist:
+            case ocChiDist:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i],std::make_shared<OpChiDist));
-                break;*/
+                         ft->Children[i],std::make_shared<OpChiDist>(), 
nResultSize));
+                break;
             case ocPow:
             case ocPower:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
@@ -2564,26 +2564,26 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpOdd>(), 
nResultSize));
                 break;
-            /*case ocChiSqDist:
+            case ocChiSqDist:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i],std::make_shared<OpChiSqDist));
+                         ft->Children[i],std::make_shared<OpChiSqDist>(), 
nResultSize));
                 break;
             case ocChiSqInv:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i],std::make_shared<OpChiSqInv));
+                         ft->Children[i],std::make_shared<OpChiSqInv>(), 
nResultSize));
                 break;
             case ocGammaInv:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i], std::make_shared<OpGammaInv));
-                break;*/
+                         ft->Children[i], std::make_shared<OpGammaInv>(), 
nResultSize));
+                break;
             case ocFloor:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpFloor>(), 
nResultSize));
                 break;
-            /*case ocFInv:
+            case ocFInv:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i], std::make_shared<OpFInv));
-                break;*/
+                         ft->Children[i], std::make_shared<OpFInv>(), 
nResultSize));
+                break;
             case ocFTest:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpFTest>(), 
nResultSize));
@@ -2664,10 +2664,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpBitXor>(), 
nResultSize));
                 break;
-            /*case ocChiInv:
+            case ocChiInv:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i],std::make_shared<OpChiInv));
-                break;*/
+                         ft->Children[i],std::make_shared<OpChiInv>(), 
nResultSize));
+                break;
             case ocPoissonDist:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpPoisson>(), 
nResultSize));
@@ -2742,10 +2742,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpSumX2PY2>(), 
nResultSize));
                 break;
-            /*case ocBetaInv:
+            case ocBetaInv:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i],std::make_shared<OpBetainv));
-                 break;*/
+                         ft->Children[i],std::make_shared<OpBetainv>(), 
nResultSize));
+                 break;
             case ocTTest:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpTTest>(), 
nResultSize));
@@ -2754,10 +2754,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpTDist>(), 
nResultSize));
                 break;
-            /*case ocTInv:
+            case ocTInv:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                         ft->Children[i], std::make_shared<OpTInv));
-                 break;*/
+                         ft->Children[i], std::make_shared<OpTInv>(), 
nResultSize));
+                 break;
             case ocSumXMY2:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpSumXMY2>(), 
nResultSize));
@@ -2915,10 +2915,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 {
                     mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i], std::make_shared<OpFvschedule>(), nResultSize));
                 }
-                /*else if ( pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getYield")
+                else if ( pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getYield")
                 {
-                    mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i], std::make_shared<OpYield));
-                }*/
+                    mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i], std::make_shared<OpYield>(), nResultSize));
+                }
                 else if (pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getYielddisc")
                 {
                     mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i], std::make_shared<OpYielddisc>(), nResultSize));
@@ -2969,16 +2969,16 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                     mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i],
                             std::make_shared<OpCoupnum>(), nResultSize));
                 }
-                /*else if pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getDuration"))
+                else if (pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getDuration")
                 {
                     mvSubArguments.push_back(
-                        SoPHelper(mCalcConfig, ts, ft->Children[i], 
std::make_shared<OpDuration_ADD));
-                }*/
-                /*else if (pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getAmordegrc")
+                        SoPHelper(mCalcConfig, ts, ft->Children[i], 
std::make_shared<OpDuration_ADD>(), nResultSize));
+                }
+                else if (pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getAmordegrc")
                 {
                     mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i],
-                            std::make_shared<OpAmordegrc, nResultSize));
-                }*/
+                            std::make_shared<OpAmordegrc>(), nResultSize));
+                }
                 else if (pChild->GetExternal() == 
"com.sun.star.sheet.addin.Analysis.getAmorlinc")
                 {
                     mvSubArguments.push_back(SoPHelper(mCalcConfig, ts, 
ft->Children[i],
diff --git a/sc/source/core/opencl/op_statistical.cxx 
b/sc/source/core/opencl/op_statistical.cxx
index 03e81c4dd034..1fbc80e2e00e 100644
--- a/sc/source/core/opencl/op_statistical.cxx
+++ b/sc/source/core/opencl/op_statistical.cxx
@@ -1010,12 +1010,16 @@ void OpPermut::GenSlidingWindowFunction(
     ss <<"    double tmp = 1 ;\n";
     GenerateArg( "inA", 0, vSubArguments, ss );
     GenerateArg( "inB", 1, vSubArguments, ss );
-    ss << "      for( int i =0; i<inB; i++)\n";
-    ss << "      {\n";
+    ss << "    inA = floor( inA );\n";
+    ss << "    inB = floor( inB );\n";
+    ss << "    if (inA < 0.0 || inB < 0.0 || inB > inA)\n";
+    ss << "        return CreateDoubleError(IllegalArgument);\n";
+    ss << "    for( int i = 0; i<inB; i++)\n";
+    ss << "    {\n";
     ss << "        tmp *= inA ;\n";
     ss << "        inA = inA - 1.0;\n";
-    ss << "      }\n";
-    ss << "      return tmp;\n";
+    ss << "    }\n";
+    ss << "    return tmp;\n";
     ss << "}\n";
 }
 void OpPermutationA::GenSlidingWindowFunction(
@@ -1029,11 +1033,11 @@ void OpPermutationA::GenSlidingWindowFunction(
     ss <<"    double tmp = 1.0;\n";
     GenerateArg( "inA", 0, vSubArguments, ss );
     GenerateArg( "inB", 1, vSubArguments, ss );
-    ss << " for(int i=0; i<inB; i++)\n";
-    ss << " {\n";
-    ss << "     tmp *= inA;\n";
-    ss << " }\n";
-    ss << "    return tmp;\n";
+    ss << "    inA = floor( inA );\n";
+    ss << "    inB = floor( inB );\n";
+    ss << "    if (inA < 0.0 || inB < 0.0)\n";
+    ss << "        return CreateDoubleError(IllegalArgument);\n";
+    ss << "    return pow(inA, inB);\n";
     ss << "}\n";
 }
 
@@ -1275,6 +1279,8 @@ void OpGammaDist::GenSlidingWindowFunction(outputstream 
&ss,
     GenerateArg( 1, vSubArguments, ss );
     GenerateArg( 2, vSubArguments, ss );
     GenerateArgWithDefault( "arg3", 3, 1, vSubArguments, ss );
+    ss << "    if(arg1 <= 0 || arg2 <= 0)\n";
+    ss << "        return CreateDoubleError(IllegalArgument);\n";
     ss << "    double tmp;\n";
     ss << "    if (arg3)\n";
     ss << "        tmp=GetGammaDist( arg0, arg1, arg2);\n";
@@ -1306,7 +1312,7 @@ void OpChiDist::GenSlidingWindowFunction(
     CHECK_PARAMETER_COUNT( 2, 2 );
     GenerateFunctionDeclaration( sSymName, vSubArguments, ss );
     ss << "{\n";
-    ss << "    double fx,fDF,tmp=0,tmp0=0,tmp1=0;\n";
+    ss << "    double fx,fDF,tmp=0;\n";
     ss << "    int gid0=get_global_id(0);\n";
     GenerateArg( "tmp0", 0, vSubArguments, ss );
     GenerateArg( "tmp1", 1, vSubArguments, ss );
@@ -1651,12 +1657,12 @@ void OpFInv::GenSlidingWindowFunction(outputstream &ss,
     ss << "{\n";
     ss << "    int gid0=get_global_id(0);\n";
     ss << "    double tmp;\n";
+    GenerateArg( 0, vSubArguments, ss );
     GenerateArg( 1, vSubArguments, ss );
     GenerateArg( 2, vSubArguments, ss );
-    GenerateArg( 3, vSubArguments, ss );
     ss << "    double fF2=floor(arg2);\n"
     "    double fF1=floor(arg1);\n"
-    "    if( arg0 <= 0 || arg1 < 1 || arg2 < 1 || arg1 >= 1.0e10 || arg2 >= 
1.0e10 || arg > 1 )\n"
+    "    if( arg0 <= 0 || arg1 < 1 || arg2 < 1 || arg1 >= 1.0e10 || arg2 >= 
1.0e10 || arg0 > 1 )\n"
     "        return CreateDoubleError(IllegalArgument);\n"
     "    double fAx=fF1*0.5;\n"
     "    double fBx=fF1;\n"
@@ -1938,6 +1944,8 @@ void OpPoisson::GenSlidingWindowFunction(
     GenerateArg( "lambda", 1, vSubArguments, ss );
     GenerateArgWithDefault( "bCumulative", 2, 1, vSubArguments, ss );
     ss << "    x = floor(x);\n";
+    ss << "    if (lambda <= 0.0 || x < 0.0)\n";
+    ss << "        return CreateDoubleError(IllegalArgument);\n";
     ss << "    if (!bCumulative)\n";
     ss << "    {\n";
     ss << "        if(lambda == 0.0)\n";
@@ -2085,23 +2093,17 @@ void OpBetainv::GenSlidingWindowFunction(
     GenerateArg( "tmp2", 2, vSubArguments, ss );
     GenerateArgWithDefault( "tmp3", 3, 0, vSubArguments, ss );
     GenerateArgWithDefault( "tmp4", 4, 1, vSubArguments, ss );
-    ss << "    if (tmp0 < 0.0 || tmp0 >= 1.0 ||";
-    ss << "tmp3 == tmp4 || tmp1 <= 0.0 || tmp2 <= 0.0)\n";
+    ss << "    if (tmp0 < 0.0 || tmp0 > 1.0 ||";
+    ss << "tmp3 >= tmp4 || tmp1 <= 0.0 || tmp2 <= 0.0)\n";
     ss << "    {\n";
     ss << "        return CreateDoubleError(IllegalArgument);\n";
     ss << "    }\n";
-    ss << "    if (tmp0 == 0.0)\n";
-    ss << "        return 0.0;\n";
-    ss << "    else\n";
-    ss << "    {";
-    ss << "        bool bConvError;";
-    ss << "        double fVal = lcl_IterateInverseBetaInv";
+    ss << "    bool bConvError;\n";
+    ss << "    double fVal = lcl_IterateInverseBetaInv";
     ss << "(tmp0, tmp1, tmp2, 0.0, 1.0, &bConvError);\n";
-    ss << "        if(bConvError)\n";
-    ss << "            return CreateDoubleError(NoConvergence);\n";
-    ss << "        else\n";
-    ss << "            return (tmp3 + fVal*(tmp4 - tmp3));\n";
-    ss << "    }";
+    ss << "    if(bConvError)\n";
+    ss << "        return CreateDoubleError(NoConvergence);\n";
+    ss << "    return (tmp3 + fVal*(tmp4 - tmp3));\n";
     ss << "}\n";
 }
 void OpDevSq::GenSlidingWindowFunction(outputstream& ss,
commit ab561d74375576efbc1629ae4f169c8e02f5fee0
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Tue Sep 20 18:08:27 2022 +0200
Commit:     Luboš Luňák <l.lu...@collabora.com>
CommitDate: Wed Sep 21 10:23:20 2022 +0200

    fix/simplify opencl DDB() and VDB()
    
    Change-Id: I7de072defa1f8a1020c0d974bd519e6d2f604381
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/140253
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>

diff --git a/sc/source/core/opencl/formulagroupcl.cxx 
b/sc/source/core/opencl/formulagroupcl.cxx
index 09fce6abb75a..75485b47dc21 100644
--- a/sc/source/core/opencl/formulagroupcl.cxx
+++ b/sc/source/core/opencl/formulagroupcl.cxx
@@ -2403,10 +2403,10 @@ 
DynamicKernelSoPArguments::DynamicKernelSoPArguments(const ScCalcConfig& config,
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpSumIfs>(), 
nResultSize));
                 break;
-                /*case ocVBD:
-                    mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
-                             ft->Children[i],std::make_shared<OpVDB));
-                     break;*/
+            case ocVBD:
+                mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
+                         ft->Children[i],std::make_shared<OpVDB>(), 
nResultSize));
+                 break;
             case ocKurt:
                 mvSubArguments.push_back(SoPHelper(mCalcConfig, ts,
                         ft->Children[i], std::make_shared<OpKurt>(), 
nResultSize));
diff --git a/sc/source/core/opencl/op_financial.cxx 
b/sc/source/core/opencl/op_financial.cxx
index c80f58cf72d1..67326251f9d0 100644
--- a/sc/source/core/opencl/op_financial.cxx
+++ b/sc/source/core/opencl/op_financial.cxx
@@ -17,6 +17,7 @@ using namespace formula;
 namespace sc::opencl {
 // Definitions of inline functions
 #include "op_financial_helpers.hxx"
+#include "op_math_helpers.hxx"
 
 void OpRRI::GenSlidingWindowFunction(
     outputstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
@@ -1699,6 +1700,13 @@ void OpTbillyield::GenSlidingWindowFunction(
     ss << "}\n";
 }
 
+void OpDDB::BinInlineFun(std::set<std::string>& decls,
+    std::set<std::string>& funs)
+{
+    decls.insert(ScGetDDBDecl);
+    funs.insert(ScGetDDB);
+}
+
 void OpDDB::GenSlidingWindowFunction(outputstream& ss,
             const std::string &sSymName, SubArguments& vSubArguments)
 {
@@ -1715,27 +1723,8 @@ void OpDDB::GenSlidingWindowFunction(outputstream& ss,
     ss << "    if (fCost < 0.0 || fSalvage < 0.0 || fFactor <= 0.0 || fSalvage 
> fCost\n";
     ss << "        || fPeriod < 1.0 || fPeriod > fLife)\n";
     ss << "        return CreateDoubleError(IllegalArgument);\n";
-    ss << "    double fRate, fOldValue, fNewValue;\n";
-    ss <<"    fRate = fFactor / fLife;\n";
-    ss <<"    if (fRate >= 1.0)\n";
-    ss <<"    {\n";
-    ss <<"        fRate = 1.0;\n";
-    ss <<"        if (fPeriod == 1.0)\n";
-    ss <<"            fOldValue = fCost;\n";
-    ss <<"        else\n";
-    ss <<"            fOldValue = 0.0;\n";
-    ss <<"    }\n";
-    ss <<"    else\n";
-    ss <<"        fOldValue = fCost * pow(1.0 - fRate, fPeriod - 1);\n";
-    ss <<"    fNewValue = fCost * pow(1.0 - fRate, fPeriod);\n";
-    ss <<"    if (fNewValue < fSalvage)\n";
-    ss <<"        tmp = fOldValue - fSalvage;\n";
-    ss <<"    else\n";
-    ss <<"        tmp = fOldValue - fNewValue;\n";
-    ss <<"    if (tmp < 0.0)\n";
-    ss <<"        tmp = 0.0;\n";
-    ss <<"    return tmp;\n";
-    ss <<"}";
+    ss << "   return ScGetDDB( fCost, fSalvage, fLife, fPeriod, fFactor );\n";
+    ss << "}\n";
 }
 
 void OpPV::GenSlidingWindowFunction(
@@ -1771,9 +1760,11 @@ void OpPV::GenSlidingWindowFunction(
 void OpVDB::BinInlineFun(std::set<std::string>& decls,
     std::set<std::string>& funs)
 {
-    decls.insert(ScGetDDBDecl);decls.insert(DblMinDecl);
+    decls.insert(is_representable_integerDecl);
+    decls.insert(ScGetDDBDecl);decls.insert(approx_equalDecl);
     decls.insert(ScInterVDBDecl);decls.insert(VDBImplementDecl);
-    funs.insert(ScGetDDB);funs.insert(DblMin);
+    funs.insert(is_representable_integer);
+    funs.insert(ScGetDDB);funs.insert(approx_equal);
     funs.insert(ScInterVDB);funs.insert(VDBImplement);
 }
 
@@ -1786,23 +1777,17 @@ void OpVDB::GenSlidingWindowFunction(
     ss << "    int gid0 = get_global_id(0);\n";
     ss << "    int singleIndex = gid0;\n";
     ss << "    double result = 0;\n";
-    GenTmpVariables(ss,vSubArguments);
-    CheckAllSubArgumentIsNan(ss,vSubArguments);
-    if(vSubArguments.size() <= 6)
-    {
-        ss << "    int tmp6  = 0;\n";
-    }
-    if(vSubArguments.size() == 5)
-    {
-        ss << "    double tmp5= 2.0;\n";
-    }
-    ss << "    if(tmp3 < 0 || tmp4<tmp3 || tmp4>tmp2 || tmp0<0 ||tmp1>tmp0";
-    ss << "|| tmp5 <=0)\n";
-    ss << "        result = -DBL_MAX;\n";
-    ss << "    else\n";
-    ss << "        result =";
-    ss << "VDBImplement(tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6);\n";
-    ss << "    return result;\n";
+    GenerateArg( "fCost", 0, vSubArguments, ss );
+    GenerateArg( "fSalvage", 1, vSubArguments, ss );
+    GenerateArg( "fLife", 2, vSubArguments, ss );
+    GenerateArg( "fStart", 3, vSubArguments, ss );
+    GenerateArg( "fEnd", 4, vSubArguments, ss );
+    GenerateArgWithDefault( "fFactor", 5, 2, vSubArguments, ss );
+    GenerateArgWithDefault( "fNoSwitch", 6, 0, vSubArguments, ss );
+    ss << "    if (fStart < 0.0 || fEnd < fStart || fEnd > fLife || fCost < 
0.0\n";
+    ss << "        || fSalvage > fCost || fFactor <= 0.0)\n";
+    ss << "        return CreateDoubleError(IllegalArgument);\n";
+    ss << "    return VDBImplement(fCost, fSalvage, fLife, fStart, fEnd, 
fFactor, fNoSwitch != 0);\n";
     ss << "}";
 }
 
diff --git a/sc/source/core/opencl/op_financial.hxx 
b/sc/source/core/opencl/op_financial.hxx
index 82c21b321d17..a071cc1617e6 100644
--- a/sc/source/core/opencl/op_financial.hxx
+++ b/sc/source/core/opencl/op_financial.hxx
@@ -454,8 +454,16 @@ class OpDDB:public Normal
 public:
     virtual void GenSlidingWindowFunction(outputstream &ss,
             const std::string &sSymName, SubArguments &vSubArguments) override;
-
-     virtual std::string BinFuncName() const override { return "DDB"; }
+    virtual std::string BinFuncName() const override { return "DDB"; }
+    virtual void BinInlineFun(std::set<std::string>& ,std::set<std::string>& ) 
override;
+};
+class OpVDB: public Normal
+{
+public:
+    virtual void GenSlidingWindowFunction(outputstream &ss,
+            const std::string &sSymName, SubArguments &vSubArguments) override;
+    virtual std::string BinFuncName() const override { return "VDB"; }
+    virtual void BinInlineFun(std::set<std::string>& ,std::set<std::string>& ) 
override;
 };
 class OpDB:public Normal
 {
@@ -550,16 +558,6 @@ public:
     virtual std::string BinFuncName() const override { return "PV"; }
 };
 
-class OpVDB: public CheckVariables
-{
-public:
-    virtual void GenSlidingWindowFunction(outputstream &ss,
-            const std::string &sSymName, SubArguments &vSubArguments) override;
-
-    virtual std::string BinFuncName() const override { return "VDB"; }
-    virtual void BinInlineFun(std::set<std::string>& ,std::set<std::string>& ) 
override;
-};
-
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/opencl/op_financial_helpers.hxx 
b/sc/source/core/opencl/op_financial_helpers.hxx
index 761cdeda62e0..6fdbb87716ee 100644
--- a/sc/source/core/opencl/op_financial_helpers.hxx
+++ b/sc/source/core/opencl/op_financial_helpers.hxx
@@ -1446,15 +1446,6 @@ const char ScGetDDB[] =
 "    return fDdb;\n"
 "}\n";
 
-const char DblMinDecl[] =
-"inline double DblMin( double a, double b );\n";
-
-const char DblMin[] =
-"inline double DblMin( double a, double b )\n"
-"{\n"
-"    return (a < b) ? a : b;\n"
-"}\n";
-
 const char ScInterVDBDecl[] =
 "double ScInterVDB(double fCost, double fSalvage, double fLife, double fLife1,"
 "double fPeriod, double fFactor);\n";
@@ -1511,45 +1502,57 @@ const char VDBImplement[] =
 "double VDBImplement(double fCost, double fSalvage, double fLife, double 
fStart"
 ", double fEnd, double fFactor, bool bNoSwitch)\n"
 "{\n"
-"    double result=0;\n"
 "    double fIntStart = floor(fStart);\n"
 "    double fIntEnd   = ceil(fEnd);\n"
-"    int nLoopStart = (int) fIntStart;\n"
-"    int nLoopEnd   = (int) fIntEnd;\n"
+"    long nLoopStart = (long)(fIntStart);\n"
+"    long nLoopEnd   = (long)(fIntEnd);\n"
+"\n"
+"    double fVdb = 0.0;\n"
 "    if (bNoSwitch)\n"
 "    {\n"
-"        for (int i = nLoopStart + 1; i <= nLoopEnd; i++)\n"
+"        for (long i = nLoopStart + 1; i <= nLoopEnd; i++)\n"
 "        {\n"
-"            double fTerm = ScGetDDB(fCost, fSalvage, fLife, (double) i, 
fFactor"
-");\n"
+"            double fTerm = ScGetDDB(fCost, fSalvage, fLife, i, fFactor);\n"
+"\n"
 "            if ( i == nLoopStart+1 )\n"
-"                fTerm *= ( DblMin( fEnd, fIntStart + 1.0 ) - fStart );\n"
+"                fTerm *= ( min( fEnd, fIntStart + 1.0 ) - fStart );\n"
 "            else if ( i == nLoopEnd )\n"
 "                fTerm *= ( fEnd + 1.0 - fIntEnd );\n"
-"            result += fTerm;\n"
+"\n"
+"            fVdb += fTerm;\n"
 "        }\n"
 "    }\n"
 "    else\n"
 "    {\n"
-"        double fLife1=fLife;\n"
-"        if(!isequal(fStart,floor(fStart)))\n"
+"        double fPart = 0.0;\n"
+"        if ( !approx_equal( fStart, fIntStart ) ||\n"
+"             !approx_equal( fEnd, fIntEnd ) )\n"
 "        {\n"
-"            if(fFactor>1)\n"
+"            if ( !approx_equal( fStart, fIntStart ) )\n"
 "            {\n"
-"                if(fStart>fLife/2 || isequal(fStart,fLife/2))\n"
-"                {\n"
-"                    double fPart=fStart-fLife/2;\n"
-"                    fStart=fLife/2;\n"
-"                    fEnd-=fPart;\n"
-"                    fLife1+=1;\n"
-"                }\n"
+"                double fTempIntEnd = fIntStart + 1.0;\n"
+"                double fTempValue = fCost -\n"
+"                    ScInterVDB( fCost, fSalvage, fLife, fLife, fIntStart, 
fFactor );\n"
+"                fPart += ( fStart - fIntStart ) *\n"
+"                    ScInterVDB( fTempValue, fSalvage, fLife, fLife - 
fIntStart,\n"
+"                    fTempIntEnd - fIntStart, fFactor);\n"
+"            }\n"
+"            if ( !approx_equal( fEnd, fIntEnd ) )\n"
+"            {\n"
+"                double fTempIntStart = fIntEnd - 1.0;\n"
+"                double fTempValue = fCost -\n"
+"                    ScInterVDB( fCost, fSalvage, fLife, fLife, fTempIntStart, 
fFactor );\n"
+"                fPart += ( fIntEnd - fEnd ) *\n"
+"                    ScInterVDB( fTempValue, fSalvage, fLife, fLife - 
fTempIntStart,\n"
+"                    fIntEnd - fTempIntStart, fFactor);\n"
 "            }\n"
 "        }\n"
-"        fCost-=ScInterVDB(fCost, fSalvage, fLife, fLife1, fStart, fFactor);\n"
-"        result=ScInterVDB(fCost, fSalvage, fLife, fLife-fStart, fEnd-fStart,"
-"fFactor);\n"
+"        fCost -= ScInterVDB( fCost, fSalvage, fLife, fLife, fIntStart, 
fFactor );\n"
+"        fVdb = ScInterVDB( fCost, fSalvage, fLife, fLife - fIntStart,\n"
+"            fIntEnd - fIntStart, fFactor);\n"
+"        fVdb -= fPart;\n"
 "    }\n"
-"    return result;\n"
+"    return fVdb;\n"
 "}\n";
 
 const char GetOddlpriceDecl[] =

Reply via email to