1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 namespace sc::opencl
{
17 /// Implements functions in the form of FUNC(x), e.g. COS().
18 /// The function should take one simple argument (i.e. no ranges).
19 class OpMathOneArgument
: public Normal
21 virtual void GenSlidingWindowFunction(outputstream
&ss
,
22 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
23 /// This writes out OpenCL code returning the computed value, the argument is "arg0".
24 virtual void GenerateCode( outputstream
& ss
) const = 0;
27 /// Implements functions in the form of FUNC(x1, x2), e.g. ATAN2().
28 /// The function should take exactly two simple arguments (i.e. no ranges).
29 class OpMathTwoArguments
: public Normal
31 virtual void GenSlidingWindowFunction(outputstream
&ss
,
32 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
33 /// This writes out OpenCL code returning the computed value, the arguments are "arg0" and "arg1".
34 virtual void GenerateCode( outputstream
& ss
) const = 0;
38 class OpCos
: public OpMathOneArgument
41 virtual std::string
BinFuncName() const override
{ return "Cos"; }
42 virtual void GenerateCode( outputstream
& ss
) const override
;
45 class OpSec
: public OpMathOneArgument
48 virtual std::string
BinFuncName() const override
{ return "Sec"; }
49 virtual void GenerateCode( outputstream
& ss
) const override
;
52 class OpSecH
: public OpMathOneArgument
55 virtual std::string
BinFuncName() const override
{ return "SecH"; }
56 virtual void GenerateCode( outputstream
& ss
) const override
;
59 class OpMROUND
: public OpMathTwoArguments
62 virtual std::string
BinFuncName() const override
{ return "MROUND"; }
63 virtual void GenerateCode( outputstream
& ss
) const override
;
66 class OpCsc
: public OpMathOneArgument
69 virtual std::string
BinFuncName() const override
{ return "Csc"; }
70 virtual void GenerateCode( outputstream
& ss
) const override
;
73 class OpSumIfs final
: public CheckVariables
76 OpSumIfs(): CheckVariables(), mNeedReductionKernel(false) {}
77 virtual void GenSlidingWindowFunction(outputstream
&ss
,
78 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
79 virtual std::string
BinFuncName() const override
{ return "SumIfs"; }
80 bool NeedReductionKernel() const { return mNeedReductionKernel
; }
82 bool mNeedReductionKernel
;
85 class OpCosh
: public OpMathOneArgument
88 virtual std::string
BinFuncName() const override
{ return "Cosh"; }
89 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>& ) override
;
90 virtual void GenerateCode( outputstream
& ss
) const override
;
93 class OpSinh
: public OpMathOneArgument
96 virtual std::string
BinFuncName() const override
{ return "Sinh"; }
97 virtual void GenerateCode( outputstream
& ss
) const override
;
100 class OpSin
: public OpMathOneArgument
103 virtual std::string
BinFuncName() const override
{ return "Sin"; }
104 virtual void GenerateCode( outputstream
& ss
) const override
;
107 class OpAbs
: public OpMathOneArgument
110 virtual std::string
BinFuncName() const override
{ return "ScAbs"; }
111 virtual void GenerateCode( outputstream
& ss
) const override
;
114 class OpArcCos
: public OpMathOneArgument
117 virtual std::string
BinFuncName() const override
{ return "ScACos"; }
118 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>&) override
;
119 virtual void GenerateCode( outputstream
& ss
) const override
;
122 class OpArcCosHyp
: public OpMathOneArgument
125 virtual std::string
GetBottom() override
{ return "1.0"; }
126 virtual std::string
BinFuncName() const override
{ return "ScACosH"; }
127 virtual void GenerateCode( outputstream
& ss
) const override
;
130 class OpTan
: public OpMathOneArgument
133 virtual std::string
BinFuncName() const override
{ return "Tan"; }
134 virtual void GenerateCode( outputstream
& ss
) const override
;
137 class OpTanH
: public OpMathOneArgument
140 virtual std::string
BinFuncName() const override
{ return "TanH"; }
141 virtual void GenerateCode( outputstream
& ss
) const override
;
144 class OpSqrt
: public OpMathOneArgument
147 virtual std::string
BinFuncName() const override
{ return "Sqrt"; }
148 virtual void GenerateCode( outputstream
& ss
) const override
;
151 class OpArcCot
: public OpMathOneArgument
154 virtual std::string
BinFuncName() const override
{ return "ScACot"; }
155 virtual void GenerateCode( outputstream
& ss
) const override
;
158 class OpArcCotHyp
: public OpMathOneArgument
161 virtual std::string
BinFuncName() const override
{ return "ScACotH"; }
162 virtual void GenerateCode( outputstream
& ss
) const override
;
165 class OpArcSin
: public OpMathOneArgument
168 virtual std::string
BinFuncName() const override
{ return "ScASin"; }
169 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>&) override
;
170 virtual void GenerateCode( outputstream
& ss
) const override
;
173 class OpArcSinHyp
: public OpMathOneArgument
176 virtual std::string
BinFuncName() const override
{ return "ScASinH"; }
177 virtual void GenerateCode( outputstream
& ss
) const override
;
180 class OpArcTan2
: public OpMathTwoArguments
183 virtual std::string
BinFuncName() const override
{ return "ScATan2"; }
184 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>&) override
;
185 virtual void GenerateCode( outputstream
& ss
) const override
;
188 class OpArcTan
: public OpMathOneArgument
191 virtual std::string
BinFuncName() const override
{ return "ScATan"; }
192 virtual void GenerateCode( outputstream
& ss
) const override
;
195 class OpArcTanH
: public OpMathOneArgument
198 virtual std::string
BinFuncName() const override
{ return "ScATanH"; }
199 virtual void GenerateCode( outputstream
& ss
) const override
;
202 class OpBitAnd
: public OpMathTwoArguments
205 virtual std::string
BinFuncName() const override
{ return "ScBitAnd"; }
206 virtual void GenerateCode( outputstream
& ss
) const override
;
209 class OpBitOr
: public OpMathTwoArguments
212 virtual std::string
BinFuncName() const override
{ return "ScBitOr"; }
213 virtual void GenerateCode( outputstream
& ss
) const override
;
216 class OpBitXor
: public OpMathTwoArguments
219 virtual std::string
BinFuncName() const override
{ return "ScBitXor"; }
220 virtual void GenerateCode( outputstream
& ss
) const override
;
223 class OpBitLshift
: public OpMathTwoArguments
226 virtual std::string
BinFuncName() const override
{ return "ScBitLshift"; }
227 virtual void GenerateCode( outputstream
& ss
) const override
;
230 class OpBitRshift
: public OpMathTwoArguments
233 virtual std::string
BinFuncName() const override
{ return "ScBitRshift"; }
234 virtual void GenerateCode( outputstream
& ss
) const override
;
237 class OpLn
: public OpMathOneArgument
240 virtual std::string
BinFuncName() const override
{ return "Ln"; }
241 virtual void GenerateCode( outputstream
& ss
) const override
;
244 class OpRound
: public Normal
247 virtual void GenSlidingWindowFunction(outputstream
&ss
,
248 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
249 virtual std::string
BinFuncName() const override
{ return "Round"; }
250 virtual void BinInlineFun(std::set
<std::string
>& , std::set
<std::string
>& ) override
;
252 class OpRoundUp
: public Normal
255 virtual void GenSlidingWindowFunction(outputstream
&ss
,
256 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
257 virtual std::string
BinFuncName() const override
{ return "RoundUp"; }
259 class OpRoundDown
: public Normal
262 virtual void GenSlidingWindowFunction(outputstream
&ss
,
263 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
264 virtual std::string
BinFuncName() const override
{ return "RoundDown"; }
266 class OpTrunc
: public OpRoundDown
269 virtual std::string
BinFuncName() const override
{ return "Trunc"; }
271 class OpInt
: public OpMathOneArgument
274 virtual std::string
BinFuncName() const override
{ return "Int"; }
275 virtual void GenerateCode( outputstream
& ss
) const override
;
276 virtual void BinInlineFun(std::set
<std::string
>& , std::set
<std::string
>& ) override
;
279 class OpRadians
: public OpMathOneArgument
282 virtual std::string
BinFuncName() const override
{ return "Radians"; }
283 virtual void GenerateCode( outputstream
& ss
) const override
;
286 class OpIsEven
: public OpMathOneArgument
289 virtual std::string
BinFuncName() const override
{ return "IsEven"; }
290 virtual void GenerateCode( outputstream
& ss
) const override
;
293 class OpIsOdd
: public OpMathOneArgument
296 virtual std::string
BinFuncName() const override
{ return "IsOdd"; }
297 virtual void GenerateCode( outputstream
& ss
) const override
;
300 class OpCot
: public OpMathOneArgument
303 virtual std::string
BinFuncName() const override
{ return "Cot"; }
304 virtual void GenerateCode( outputstream
& ss
) const override
;
307 class OpSumSQ
: public Normal
310 virtual void GenSlidingWindowFunction(outputstream
&ss
,
311 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
313 virtual std::string
BinFuncName() const override
{ return "SumSQ"; }
314 virtual bool canHandleMultiVector() const override
{ return true; }
317 class OpCoth
: public OpMathOneArgument
320 virtual void BinInlineFun(std::set
<std::string
>& ,
321 std::set
<std::string
>& ) override
;
322 virtual std::string
BinFuncName() const override
{ return "Coth"; }
323 virtual void GenerateCode( outputstream
& ss
) const override
;
326 class OpPower
: public OpMathTwoArguments
329 virtual std::string
BinFuncName() const override
{ return "Power"; }
330 virtual void GenerateCode( outputstream
& ss
) const override
;
333 class OpOdd
: public OpMathOneArgument
336 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>&) override
;
337 virtual std::string
BinFuncName() const override
{ return "Odd"; }
338 virtual void GenerateCode( outputstream
& ss
) const override
;
341 class OpFloor
: public Normal
344 virtual void GenSlidingWindowFunction(outputstream
&ss
,
345 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
346 virtual std::string
BinFuncName() const override
{ return "Floor"; }
349 class OpCscH
: public OpMathOneArgument
352 virtual std::string
BinFuncName() const override
{ return "CscH"; }
353 virtual void GenerateCode( outputstream
& ss
) const override
;
356 class OpCeil
:public Normal
{
358 virtual void GenSlidingWindowFunction(outputstream
&ss
,
359 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
360 virtual std::string
GetBottom() override
{ return "0.0"; }
361 virtual std::string
BinFuncName() const override
{ return "ScCeil"; }
364 class OpExp
: public OpMathOneArgument
367 virtual std::string
BinFuncName() const override
{ return "Exp"; }
368 virtual void GenerateCode( outputstream
& ss
) const override
;
371 class OpLog10
: public OpMathOneArgument
374 virtual std::string
BinFuncName() const override
{ return "Log10"; }
375 virtual void GenerateCode( outputstream
& ss
) const override
;
378 class OpEven
: public OpMathOneArgument
381 virtual std::string
BinFuncName() const override
{ return "Even"; }
382 virtual void GenerateCode( outputstream
& ss
) const override
;
385 class OpAverageIfs
: public CheckVariables
388 virtual void GenSlidingWindowFunction(outputstream
&ss
,
389 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
390 virtual std::string
BinFuncName() const override
{ return "AverageIfs"; }
392 class OpCountIfs
: public CheckVariables
395 virtual void GenSlidingWindowFunction(outputstream
&ss
,
396 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
397 virtual std::string
BinFuncName() const override
{ return "CountIfs"; }
399 class OpMod
: public OpMathTwoArguments
402 virtual std::string
BinFuncName() const override
{ return "Mod"; }
403 virtual void GenerateCode( outputstream
& ss
) const override
;
404 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>& ) override
;
407 class OpProduct
: public Normal
410 virtual void GenSlidingWindowFunction(outputstream
&ss
,
411 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
412 virtual std::string
BinFuncName() const override
{ return "Product"; }
413 virtual bool canHandleMultiVector() const override
{ return true; }
416 class OpSqrtPi
: public OpMathOneArgument
419 virtual std::string
BinFuncName() const override
{ return "SqrtPi"; }
420 virtual void GenerateCode( outputstream
& ss
) const override
;
423 class OpCombinA
: public OpMathTwoArguments
426 virtual std::string
BinFuncName() const override
{ return "Combina"; }
427 virtual void BinInlineFun(std::set
<std::string
>& ,std::set
<std::string
>& ) override
;
428 virtual void GenerateCode( outputstream
& ss
) const override
;
431 class OpLog
: public Normal
434 virtual void GenSlidingWindowFunction(outputstream
&ss
,
435 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
437 virtual std::string
BinFuncName() const override
{ return "Log"; }
440 class OpCombin
: public OpMathTwoArguments
443 virtual std::string
BinFuncName() const override
{ return "ScCombin"; }
444 virtual void GenerateCode( outputstream
& ss
) const override
;
447 class OpAverageIf
: public CheckVariables
450 virtual void GenSlidingWindowFunction(outputstream
&ss
,
451 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
453 virtual std::string
BinFuncName() const override
{ return "AverageIf"; }
455 class OpDeg
: public OpMathOneArgument
458 virtual std::string
BinFuncName() const override
{ return "Degrees"; }
459 virtual void GenerateCode( outputstream
& ss
) const override
;
462 class OpCountIf
: public Normal
465 virtual void GenSlidingWindowFunction(outputstream
&ss
,
466 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
467 virtual std::string
BinFuncName() const override
{ return "Countif"; }
469 class OpFact
: public OpMathOneArgument
472 virtual std::string
BinFuncName() const override
{ return "Fact"; }
473 virtual void GenerateCode( outputstream
& ss
) const override
;
476 class OpSeriesSum
: public Normal
479 virtual void GenSlidingWindowFunction(outputstream
&ss
,
480 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
481 virtual std::string
BinFuncName() const override
{ return "SeriesSum"; }
482 virtual bool canHandleMultiVector() const override
{ return true; }
484 class OpSumIf
: public Normal
487 virtual void GenSlidingWindowFunction(outputstream
&ss
,
488 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
489 virtual std::string
BinFuncName() const override
{ return "SumIf"; }
492 class OpQuotient
: public OpMathTwoArguments
495 virtual std::string
BinFuncName() const override
{ return "Quotient"; }
496 virtual void GenerateCode( outputstream
& ss
) const override
;
499 class OpNegSub
: public OpMathOneArgument
502 virtual std::string
BinFuncName() const override
{ return "NegSub"; }
503 virtual void GenerateCode( outputstream
& ss
) const override
;
506 class OpEqual
: public Normal
509 virtual void GenSlidingWindowFunction(outputstream
&ss
,
510 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
511 virtual std::string
BinFuncName() const override
{ return "eq"; }
512 virtual void BinInlineFun(std::set
<std::string
>& , std::set
<std::string
>& ) override
;
513 virtual bool takeString() const override
{ return true; }
516 class OpNotEqual
: public Normal
519 virtual void GenSlidingWindowFunction(outputstream
&ss
,
520 const std::string
&sSymName
, SubArguments
&vSubArguments
) override
;
521 virtual std::string
BinFuncName() const override
{ return "neq"; }
522 virtual void BinInlineFun(std::set
<std::string
>& , std::set
<std::string
>& ) override
;
523 virtual bool takeString() const override
{ return true; }
526 class OpLessEqual
: public OpMathTwoArguments
529 virtual void GenerateCode( outputstream
& ss
) const override
;
530 virtual std::string
BinFuncName() const override
{ return "le"; }
531 virtual void BinInlineFun(std::set
<std::string
>& , std::set
<std::string
>& ) override
;
534 class OpLess
: public OpMathTwoArguments
537 virtual void GenerateCode( outputstream
& ss
) const override
;
538 virtual std::string
BinFuncName() const override
{ return "lt"; }
541 class OpGreaterEqual
: public OpMathTwoArguments
544 virtual void GenerateCode( outputstream
& ss
) const override
;
545 virtual std::string
BinFuncName() const override
{ return "ge"; }
546 virtual void BinInlineFun(std::set
<std::string
>& , std::set
<std::string
>& ) override
;
549 class OpGreater
: public OpMathTwoArguments
552 virtual void GenerateCode( outputstream
& ss
) const override
;
553 virtual std::string
BinFuncName() const override
{ return "gt"; }
556 class SumOfProduct
: public SlidingFunctionBase
559 virtual void GenSlidingWindowFunction( outputstream
& ss
,
560 const std::string
& sSymName
, SubArguments
& vSubArguments
) override
;
561 virtual bool takeString() const override
{ return false; }
562 virtual bool takeNumeric() const override
{ return true; }
565 class OpSumProduct
: public SumOfProduct
568 virtual std::string
Gen2( const std::string
& lhs
, const std::string
& rhs
) const override
570 return lhs
+ "*" + rhs
;
572 virtual std::string
BinFuncName() const override
{ return "fsop"; }
576 class OpNop
: public Reduction
579 explicit OpNop(int nResultSize
) : Reduction(nResultSize
) {}
581 virtual std::string
GetBottom() override
{ return "0"; }
582 virtual std::string
Gen2( const std::string
& lhs
, const std::string
& ) const override
586 virtual std::string
BinFuncName() const override
{ return "nop"; }
589 class OpSum
: public Reduction
592 explicit OpSum(int nResultSize
) : Reduction(nResultSize
) {}
594 virtual std::string
GetBottom() override
{ return "0"; }
595 virtual std::string
Gen2( const std::string
& lhs
, const std::string
& rhs
) const override
598 ss
<< "fsum_approx((" << lhs
<< "),(" << rhs
<< "))";
601 virtual void BinInlineFun(std::set
<std::string
>& decls
,std::set
<std::string
>& funs
) override
;
602 virtual std::string
BinFuncName() const override
{ return "fsum"; }
603 // All arguments are simply summed, so it doesn't matter if SvDoubleVector is split.
604 virtual bool canHandleMultiVector() const override
{ return true; }
607 class OpSub
: public Reduction
610 explicit OpSub(int nResultSize
) : Reduction(nResultSize
) {}
612 virtual std::string
GetBottom() override
{ return "0"; }
613 virtual std::string
Gen2( const std::string
& lhs
, const std::string
& rhs
) const override
615 return "fsub_approx(" + lhs
+ "," + rhs
+ ")";
617 virtual void BinInlineFun(std::set
<std::string
>& decls
,std::set
<std::string
>& funs
) override
;
618 virtual std::string
BinFuncName() const override
{ return "fsub"; }
621 class OpMul
: public Reduction
624 explicit OpMul(int nResultSize
) : Reduction(nResultSize
) {}
626 virtual std::string
GetBottom() override
{ return "1"; }
627 virtual std::string
Gen2( const std::string
& lhs
, const std::string
& rhs
) const override
629 return lhs
+ "*" + rhs
;
631 virtual std::string
BinFuncName() const override
{ return "fmul"; }
632 virtual bool ZeroReturnZero() override
{ return true; }
635 /// Technically not a reduction, but fits the framework.
636 class OpDiv
: public Reduction
639 explicit OpDiv(int nResultSize
) : Reduction(nResultSize
) {}
641 virtual std::string
GetBottom() override
{ return "1.0"; }
642 virtual std::string
Gen2( const std::string
& lhs
, const std::string
& rhs
) const override
644 return "(" + rhs
+ "==0 ? CreateDoubleError(DivisionByZero) : (" + lhs
+ "/" + rhs
+ ") )";
646 virtual std::string
BinFuncName() const override
{ return "fdiv"; }
648 virtual bool HandleNaNArgument( outputstream
& ss
, unsigned argno
, SubArguments
& vSubArguments
) const override
653 "if (isnan(" << vSubArguments
[argno
]->GenSlidingWindowDeclRef() << ")) {\n"
654 " return CreateDoubleError(DivisionByZero);\n"
661 "if (isnan(" << vSubArguments
[argno
]->GenSlidingWindowDeclRef() << ") &&\n"
662 " !(isnan(" << vSubArguments
[1]->GenSlidingWindowDeclRef() << ") || " << vSubArguments
[1]->GenSlidingWindowDeclRef() << " == 0)) {\n"
673 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */