1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "InterpKernelFunction.hxx"
22 #include "InterpKernelValue.hxx"
26 using namespace INTERP_KERNEL;
28 const char IdentityFunction::REPR[]="Id";
30 const char PositiveFunction::REPR[]="+";
32 const char NegateFunction::REPR[]="-";
34 const char CosFunction::REPR[]="cos";
36 const char SinFunction::REPR[]="sin";
38 const char TanFunction::REPR[]="tan";
40 const char SqrtFunction::REPR[]="sqrt";
42 const char AbsFunction::REPR[]="abs";
44 const char PlusFunction::REPR[]="+";
46 const char MinusFunction::REPR[]="-";
48 const char MultFunction::REPR[]="*";
50 const char DivFunction::REPR[]="/";
52 const char PowFunction::REPR[]="^";
54 const char ExpFunction::REPR[]="exp";
56 const char LnFunction::REPR[]="ln";
58 const char LogFunction::REPR[]="log";
60 const char Log10Function::REPR[]="log10";
62 const char MaxFunction::REPR[]="max";
64 const char MinFunction::REPR[]="min";
66 const char GreaterThanFunction::REPR[]=">";
68 const char LowerThanFunction::REPR[]="<";
70 const char IfFunction::REPR[]="if";
72 Function *FunctionsFactory::buildFuncFromString(const char *type, int nbOfParams) throw(INTERP_KERNEL::Exception)
77 return buildUnaryFuncFromString(type);
79 return buildBinaryFuncFromString(type);
81 return buildTernaryFuncFromString(type);
83 throw INTERP_KERNEL::Exception("Invalid number of params detected : limited to 2 !");
87 Function *FunctionsFactory::buildUnaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
89 std::string tmp(type);
91 return new IdentityFunction;
92 if(tmp==CosFunction::REPR)
93 return new CosFunction;
94 if(tmp==SinFunction::REPR)
95 return new SinFunction;
96 if(tmp==TanFunction::REPR)
97 return new TanFunction;
98 if(tmp==SqrtFunction::REPR)
99 return new SqrtFunction;
100 if(tmp==AbsFunction::REPR)
101 return new AbsFunction;
102 if(tmp==PositiveFunction::REPR)
103 return new PositiveFunction;
104 if(tmp==NegateFunction::REPR)
105 return new NegateFunction;
106 if(tmp==ExpFunction::REPR)
107 return new ExpFunction;
108 if(tmp==LnFunction::REPR)
109 return new LnFunction;
110 if(tmp==LogFunction::REPR)
111 return new LogFunction;
112 if(tmp==Log10Function::REPR)
113 return new Log10Function;
115 std::string msg("Invalid unary function detected : \"");
116 msg+=type; msg+="\"";
117 throw INTERP_KERNEL::Exception(msg.c_str());
120 Function *FunctionsFactory::buildBinaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
122 std::string tmp(type);
123 if(tmp==PositiveFunction::REPR)
124 return new PlusFunction;
125 if(tmp==NegateFunction::REPR)
126 return new MinusFunction;
127 if(tmp==MultFunction::REPR)
128 return new MultFunction;
129 if(tmp==DivFunction::REPR)
130 return new DivFunction;
131 if(tmp==PowFunction::REPR)
132 return new PowFunction;
133 if(tmp==MaxFunction::REPR)
134 return new MaxFunction;
135 if(tmp==MinFunction::REPR)
136 return new MinFunction;
137 if(tmp==GreaterThanFunction::REPR)
138 return new GreaterThanFunction;
139 if(tmp==LowerThanFunction::REPR)
140 return new LowerThanFunction;
141 std::string msg("Invalid binary function detected : \"");
142 msg+=type; msg+="\"";
143 throw INTERP_KERNEL::Exception(msg.c_str());
146 Function *FunctionsFactory::buildTernaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
148 std::string tmp(type);
149 if(tmp==IfFunction::REPR)
150 return new IfFunction();
151 std::string msg("Invalid ternary function detected : \"");
152 msg+=type; msg+="\"";
153 throw INTERP_KERNEL::Exception(msg.c_str());
156 Function *FunctionsFactory::buildBinaryFuncFromString(char type) throw(INTERP_KERNEL::Exception)
158 char tmp[2]; tmp[0]=type; tmp[1]='\0';
159 return buildBinaryFuncFromString(tmp);
162 Function::~Function()
166 IdentityFunction::~IdentityFunction()
170 void IdentityFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
174 void IdentityFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
178 const char *IdentityFunction::getRepr() const
183 bool IdentityFunction::isACall() const
188 PositiveFunction::~PositiveFunction()
192 int UnaryFunction::getNbInputParams() const
197 void PositiveFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
201 void PositiveFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
205 const char *PositiveFunction::getRepr() const
210 bool PositiveFunction::isACall() const
215 NegateFunction::~NegateFunction()
219 void NegateFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
221 Value *val=stack.back();
225 void NegateFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
227 asmb.push_back("fchs");
230 const char *NegateFunction::getRepr() const
235 bool NegateFunction::isACall() const
240 CosFunction::~CosFunction()
244 void CosFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
246 Value *val=stack.back();
250 void CosFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
252 asmb.push_back("fcos");
255 const char *CosFunction::getRepr() const
260 bool CosFunction::isACall() const
265 SinFunction::~SinFunction()
269 void SinFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
271 Value *val=stack.back();
275 void SinFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
277 asmb.push_back("fsin");
280 const char *SinFunction::getRepr() const
285 bool SinFunction::isACall() const
290 TanFunction::~TanFunction()
294 void TanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
296 Value *val=stack.back();
300 void TanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
302 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
305 const char *TanFunction::getRepr() const
310 bool TanFunction::isACall() const
315 SqrtFunction::~SqrtFunction()
319 void SqrtFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
321 Value *val=stack.back();
325 void SqrtFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
327 asmb.push_back("fsqrt");
330 const char *SqrtFunction::getRepr() const
335 bool SqrtFunction::isACall() const
340 AbsFunction::~AbsFunction()
344 void AbsFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
346 Value *val=stack.back();
350 void AbsFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
352 asmb.push_back("fabs");
355 const char *AbsFunction::getRepr() const
360 bool AbsFunction::isACall() const
365 void ExpFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
367 Value *val=stack.back();
371 void ExpFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
373 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
376 const char *ExpFunction::getRepr() const
381 bool ExpFunction::isACall() const
386 LnFunction::~LnFunction()
390 void LnFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
392 Value *val=stack.back();
396 void LnFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
398 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
401 const char *LnFunction::getRepr() const
406 bool LnFunction::isACall() const
411 LogFunction::~LogFunction()
415 void LogFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
417 Value *val=stack.back();
421 void LogFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
423 throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
426 const char *LogFunction::getRepr() const
431 bool LogFunction::isACall() const
436 Log10Function::~Log10Function()
440 void Log10Function::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
442 Value *val=stack.back();
446 void Log10Function::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
448 throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
451 const char *Log10Function::getRepr() const
456 bool Log10Function::isACall() const
461 int BinaryFunction::getNbInputParams() const
466 PlusFunction::~PlusFunction()
470 void PlusFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
472 Value *val1=stack.back();
474 Value *& val2=stack.back();
478 val3=val1->plus(val2);
480 catch(INTERP_KERNEL::Exception& e)
490 void PlusFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
492 asmb.push_back("faddp st1");
495 const char *PlusFunction::getRepr() const
500 bool PlusFunction::isACall() const
505 MinusFunction::~MinusFunction()
509 void MinusFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
511 Value *val1=stack.back();
513 Value *& val2=stack.back();
517 val3=val1->minus(val2);
519 catch(INTERP_KERNEL::Exception& e)
529 void MinusFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
531 asmb.push_back("fsubp st1");
534 const char *MinusFunction::getRepr() const
539 bool MinusFunction::isACall() const
544 MultFunction::~MultFunction()
548 void MultFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
550 Value *val1=stack.back();
552 Value *& val2=stack.back();
553 Value *val3=val1->mult(val2);
559 void MultFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
561 asmb.push_back("fmulp st1");
564 const char *MultFunction::getRepr() const
569 bool MultFunction::isACall() const
574 DivFunction::~DivFunction()
578 void DivFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
580 Value *val1=stack.back();
582 Value *& val2=stack.back();
586 val3=val1->div(val2);
588 catch(INTERP_KERNEL::Exception& e)
598 void DivFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
600 asmb.push_back("fdivp st1");
603 const char *DivFunction::getRepr() const
608 bool DivFunction::isACall() const
613 PowFunction::~PowFunction()
617 void PowFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
619 Value *val1=stack.back();
621 Value *& val2=stack.back();
625 val3=val1->pow(val2);
627 catch(INTERP_KERNEL::Exception& e)
637 void PowFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
639 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
642 const char *PowFunction::getRepr() const
647 bool PowFunction::isACall() const
652 ExpFunction::~ExpFunction()
656 MaxFunction::~MaxFunction()
660 void MaxFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
662 Value *val1=stack.back();
664 Value *& val2=stack.back();
668 val3=val1->max(val2);
670 catch(INTERP_KERNEL::Exception& e)
680 void MaxFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
682 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
685 const char *MaxFunction::getRepr() const
690 bool MaxFunction::isACall() const
695 MinFunction::~MinFunction()
699 void MinFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
701 Value *val1=stack.back();
703 Value *& val2=stack.back();
707 val3=val1->min(val2);
709 catch(INTERP_KERNEL::Exception& e)
719 void MinFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
721 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
724 const char *MinFunction::getRepr() const
729 bool MinFunction::isACall() const
734 GreaterThanFunction::~GreaterThanFunction()
738 void GreaterThanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
740 Value *val1=stack.back();
742 Value *& val2=stack.back();
746 val3=val1->greaterThan(val2);
748 catch(INTERP_KERNEL::Exception& e)
758 void GreaterThanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
760 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
763 const char *GreaterThanFunction::getRepr() const
768 bool GreaterThanFunction::isACall() const
773 LowerThanFunction::~LowerThanFunction()
777 void LowerThanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
779 Value *val1=stack.back();
781 Value *& val2=stack.back();
785 val3=val1->lowerThan(val2);
787 catch(INTERP_KERNEL::Exception& e)
797 void LowerThanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
799 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
802 const char *LowerThanFunction::getRepr() const
807 bool LowerThanFunction::isACall() const
812 int TernaryFunction::getNbInputParams() const
817 IfFunction::~IfFunction()
821 void IfFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
823 Value *val1=stack.back();
825 Value *val2=stack.back();
827 Value *&val3=stack.back();
831 val4=val1->ifFunc(val2,val3);
833 catch(INTERP_KERNEL::Exception& e)
845 void IfFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
847 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
850 const char *IfFunction::getRepr() const
855 bool IfFunction::isACall() const