tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sc / source / core / opencl / op_math.hxx
blob92f9701eefd092a657a06b98b0acea034e034289
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #pragma once
12 #include "opbase.hxx"
13 #include "utils.hxx"
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
40 public:
41 virtual std::string BinFuncName() const override { return "Cos"; }
42 virtual void GenerateCode( outputstream& ss ) const override;
45 class OpSec: public OpMathOneArgument
47 public:
48 virtual std::string BinFuncName() const override { return "Sec"; }
49 virtual void GenerateCode( outputstream& ss ) const override;
52 class OpSecH: public OpMathOneArgument
54 public:
55 virtual std::string BinFuncName() const override { return "SecH"; }
56 virtual void GenerateCode( outputstream& ss ) const override;
59 class OpMROUND: public OpMathTwoArguments
61 public:
62 virtual std::string BinFuncName() const override { return "MROUND"; }
63 virtual void GenerateCode( outputstream& ss ) const override;
66 class OpCsc: public OpMathOneArgument
68 public:
69 virtual std::string BinFuncName() const override { return "Csc"; }
70 virtual void GenerateCode( outputstream& ss ) const override;
73 class OpSumIfs final : public CheckVariables
75 public:
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; }
81 private:
82 bool mNeedReductionKernel;
85 class OpCosh: public OpMathOneArgument
87 public:
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
95 public:
96 virtual std::string BinFuncName() const override { return "Sinh"; }
97 virtual void GenerateCode( outputstream& ss ) const override;
100 class OpSin: public OpMathOneArgument
102 public:
103 virtual std::string BinFuncName() const override { return "Sin"; }
104 virtual void GenerateCode( outputstream& ss ) const override;
107 class OpAbs: public OpMathOneArgument
109 public:
110 virtual std::string BinFuncName() const override { return "ScAbs"; }
111 virtual void GenerateCode( outputstream& ss ) const override;
114 class OpArcCos: public OpMathOneArgument
116 public:
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
124 public:
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
132 public:
133 virtual std::string BinFuncName() const override { return "Tan"; }
134 virtual void GenerateCode( outputstream& ss ) const override;
137 class OpTanH: public OpMathOneArgument
139 public:
140 virtual std::string BinFuncName() const override { return "TanH"; }
141 virtual void GenerateCode( outputstream& ss ) const override;
144 class OpSqrt: public OpMathOneArgument
146 public:
147 virtual std::string BinFuncName() const override { return "Sqrt"; }
148 virtual void GenerateCode( outputstream& ss ) const override;
151 class OpArcCot : public OpMathOneArgument
153 public:
154 virtual std::string BinFuncName() const override { return "ScACot"; }
155 virtual void GenerateCode( outputstream& ss ) const override;
158 class OpArcCotHyp : public OpMathOneArgument
160 public:
161 virtual std::string BinFuncName() const override { return "ScACotH"; }
162 virtual void GenerateCode( outputstream& ss ) const override;
165 class OpArcSin : public OpMathOneArgument
167 public:
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
175 public:
176 virtual std::string BinFuncName() const override { return "ScASinH"; }
177 virtual void GenerateCode( outputstream& ss ) const override;
180 class OpArcTan2 : public OpMathTwoArguments
182 public:
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
190 public:
191 virtual std::string BinFuncName() const override { return "ScATan"; }
192 virtual void GenerateCode( outputstream& ss ) const override;
195 class OpArcTanH : public OpMathOneArgument
197 public:
198 virtual std::string BinFuncName() const override { return "ScATanH"; }
199 virtual void GenerateCode( outputstream& ss ) const override;
202 class OpBitAnd : public OpMathTwoArguments
204 public:
205 virtual std::string BinFuncName() const override { return "ScBitAnd"; }
206 virtual void GenerateCode( outputstream& ss ) const override;
209 class OpBitOr : public OpMathTwoArguments
211 public:
212 virtual std::string BinFuncName() const override { return "ScBitOr"; }
213 virtual void GenerateCode( outputstream& ss ) const override;
216 class OpBitXor : public OpMathTwoArguments
218 public:
219 virtual std::string BinFuncName() const override { return "ScBitXor"; }
220 virtual void GenerateCode( outputstream& ss ) const override;
223 class OpBitLshift : public OpMathTwoArguments
225 public:
226 virtual std::string BinFuncName() const override { return "ScBitLshift"; }
227 virtual void GenerateCode( outputstream& ss ) const override;
230 class OpBitRshift : public OpMathTwoArguments
232 public:
233 virtual std::string BinFuncName() const override { return "ScBitRshift"; }
234 virtual void GenerateCode( outputstream& ss ) const override;
237 class OpLn: public OpMathOneArgument
239 public:
240 virtual std::string BinFuncName() const override { return "Ln"; }
241 virtual void GenerateCode( outputstream& ss ) const override;
244 class OpRound: public Normal
246 public:
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
254 public:
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
261 public:
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
268 public:
269 virtual std::string BinFuncName() const override { return "Trunc"; }
271 class OpInt: public OpMathOneArgument
273 public:
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
281 public:
282 virtual std::string BinFuncName() const override { return "Radians"; }
283 virtual void GenerateCode( outputstream& ss ) const override;
286 class OpIsEven: public OpMathOneArgument
288 public:
289 virtual std::string BinFuncName() const override { return "IsEven"; }
290 virtual void GenerateCode( outputstream& ss ) const override;
293 class OpIsOdd: public OpMathOneArgument
295 public:
296 virtual std::string BinFuncName() const override { return "IsOdd"; }
297 virtual void GenerateCode( outputstream& ss ) const override;
300 class OpCot: public OpMathOneArgument
302 public:
303 virtual std::string BinFuncName() const override { return "Cot"; }
304 virtual void GenerateCode( outputstream& ss ) const override;
307 class OpSumSQ: public Normal
309 public:
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
319 public:
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
328 public:
329 virtual std::string BinFuncName() const override { return "Power"; }
330 virtual void GenerateCode( outputstream& ss ) const override;
333 class OpOdd: public OpMathOneArgument
335 public:
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
343 public:
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
351 public:
352 virtual std::string BinFuncName() const override { return "CscH"; }
353 virtual void GenerateCode( outputstream& ss ) const override;
356 class OpCeil:public Normal{
357 public:
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
366 public:
367 virtual std::string BinFuncName() const override { return "Exp"; }
368 virtual void GenerateCode( outputstream& ss ) const override;
371 class OpLog10: public OpMathOneArgument
373 public:
374 virtual std::string BinFuncName() const override { return "Log10"; }
375 virtual void GenerateCode( outputstream& ss ) const override;
378 class OpEven: public OpMathOneArgument
380 public:
381 virtual std::string BinFuncName() const override { return "Even"; }
382 virtual void GenerateCode( outputstream& ss ) const override;
385 class OpAverageIfs: public CheckVariables
387 public:
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
394 public:
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
401 public:
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
409 public:
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
418 public:
419 virtual std::string BinFuncName() const override { return "SqrtPi"; }
420 virtual void GenerateCode( outputstream& ss ) const override;
423 class OpCombinA: public OpMathTwoArguments
425 public:
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
433 public:
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
442 public:
443 virtual std::string BinFuncName() const override { return "ScCombin"; }
444 virtual void GenerateCode( outputstream& ss ) const override;
447 class OpAverageIf: public CheckVariables
449 public:
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
457 public:
458 virtual std::string BinFuncName() const override { return "Degrees"; }
459 virtual void GenerateCode( outputstream& ss ) const override;
462 class OpCountIf: public Normal
464 public:
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
471 public:
472 virtual std::string BinFuncName() const override { return "Fact"; }
473 virtual void GenerateCode( outputstream& ss ) const override;
476 class OpSeriesSum: public Normal
478 public:
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
486 public:
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
494 public:
495 virtual std::string BinFuncName() const override { return "Quotient"; }
496 virtual void GenerateCode( outputstream& ss ) const override;
499 class OpNegSub: public OpMathOneArgument
501 public:
502 virtual std::string BinFuncName() const override { return "NegSub"; }
503 virtual void GenerateCode( outputstream& ss ) const override;
506 class OpEqual : public Normal
508 public:
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
518 public:
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
528 public:
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
536 public:
537 virtual void GenerateCode( outputstream& ss ) const override;
538 virtual std::string BinFuncName() const override { return "lt"; }
541 class OpGreaterEqual : public OpMathTwoArguments
543 public:
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
551 public:
552 virtual void GenerateCode( outputstream& ss ) const override;
553 virtual std::string BinFuncName() const override { return "gt"; }
556 class SumOfProduct : public SlidingFunctionBase
558 public:
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
567 public:
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"; }
575 /// operator traits
576 class OpNop : public Reduction
578 public:
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
584 return lhs;
586 virtual std::string BinFuncName() const override { return "nop"; }
589 class OpSum : public Reduction
591 public:
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
597 outputstream ss;
598 ss << "fsum_approx((" << lhs << "),(" << rhs << "))";
599 return ss.str();
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
609 public:
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
623 public:
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
638 public:
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
650 if (argno == 1)
652 ss <<
653 "if (isnan(" << vSubArguments[argno]->GenSlidingWindowDeclRef() << ")) {\n"
654 " return CreateDoubleError(DivisionByZero);\n"
655 "}\n";
656 return true;
658 else if (argno == 0)
660 ss <<
661 "if (isnan(" << vSubArguments[argno]->GenSlidingWindowDeclRef() << ") &&\n"
662 " !(isnan(" << vSubArguments[1]->GenSlidingWindowDeclRef() << ") || " << vSubArguments[1]->GenSlidingWindowDeclRef() << " == 0)) {\n"
663 " return 0;\n"
664 "}\n";
666 return false;
673 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */