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
20 #include "InterpKernelFunction.hxx"
21 #include "InterpKernelValue.hxx"
25 using namespace INTERP_KERNEL;
27 const char IdentityFunction::REPR[]="Id";
29 const char PositiveFunction::REPR[]="+";
31 const char NegateFunction::REPR[]="-";
33 const char CosFunction::REPR[]="cos";
35 const char SinFunction::REPR[]="sin";
37 const char TanFunction::REPR[]="tan";
39 const char SqrtFunction::REPR[]="sqrt";
41 const char AbsFunction::REPR[]="abs";
43 const char PlusFunction::REPR[]="+";
45 const char MinusFunction::REPR[]="-";
47 const char MultFunction::REPR[]="*";
49 const char DivFunction::REPR[]="/";
51 const char PowFunction::REPR[]="^";
53 const char ExpFunction::REPR[]="exp";
55 const char LnFunction::REPR[]="ln";
57 const char LogFunction::REPR[]="log";
59 const char Log10Function::REPR[]="log10";
61 const char MaxFunction::REPR[]="max";
63 const char MinFunction::REPR[]="min";
65 const char GreaterThanFunction::REPR[]=">";
67 const char LowerThanFunction::REPR[]="<";
69 const char IfFunction::REPR[]="if";
71 Function *FunctionsFactory::buildFuncFromString(const char *type, int nbOfParams) throw(INTERP_KERNEL::Exception)
76 return buildUnaryFuncFromString(type);
78 return buildBinaryFuncFromString(type);
80 return buildTernaryFuncFromString(type);
82 throw INTERP_KERNEL::Exception("Invalid number of params detected : limited to 2 !");
86 Function *FunctionsFactory::buildUnaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
88 std::string tmp(type);
90 return new IdentityFunction;
91 if(tmp==CosFunction::REPR)
92 return new CosFunction;
93 if(tmp==SinFunction::REPR)
94 return new SinFunction;
95 if(tmp==TanFunction::REPR)
96 return new TanFunction;
97 if(tmp==SqrtFunction::REPR)
98 return new SqrtFunction;
99 if(tmp==AbsFunction::REPR)
100 return new AbsFunction;
101 if(tmp==PositiveFunction::REPR)
102 return new PositiveFunction;
103 if(tmp==NegateFunction::REPR)
104 return new NegateFunction;
105 if(tmp==ExpFunction::REPR)
106 return new ExpFunction;
107 if(tmp==LnFunction::REPR)
108 return new LnFunction;
109 if(tmp==LogFunction::REPR)
110 return new LogFunction;
111 if(tmp==Log10Function::REPR)
112 return new Log10Function;
114 std::string msg("Invalid unary function detected : \"");
115 msg+=type; msg+="\"";
116 throw INTERP_KERNEL::Exception(msg.c_str());
119 Function *FunctionsFactory::buildBinaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
121 std::string tmp(type);
122 if(tmp==PositiveFunction::REPR)
123 return new PlusFunction;
124 if(tmp==NegateFunction::REPR)
125 return new MinusFunction;
126 if(tmp==MultFunction::REPR)
127 return new MultFunction;
128 if(tmp==DivFunction::REPR)
129 return new DivFunction;
130 if(tmp==PowFunction::REPR)
131 return new PowFunction;
132 if(tmp==MaxFunction::REPR)
133 return new MaxFunction;
134 if(tmp==MinFunction::REPR)
135 return new MinFunction;
136 if(tmp==GreaterThanFunction::REPR)
137 return new GreaterThanFunction;
138 if(tmp==LowerThanFunction::REPR)
139 return new LowerThanFunction;
140 std::string msg("Invalid binary function detected : \"");
141 msg+=type; msg+="\"";
142 throw INTERP_KERNEL::Exception(msg.c_str());
145 Function *FunctionsFactory::buildTernaryFuncFromString(const char *type) throw(INTERP_KERNEL::Exception)
147 std::string tmp(type);
148 if(tmp==IfFunction::REPR)
149 return new IfFunction();
150 std::string msg("Invalid ternary function detected : \"");
151 msg+=type; msg+="\"";
152 throw INTERP_KERNEL::Exception(msg.c_str());
155 Function *FunctionsFactory::buildBinaryFuncFromString(char type) throw(INTERP_KERNEL::Exception)
157 char tmp[2]; tmp[0]=type; tmp[1]='\0';
158 return buildBinaryFuncFromString(tmp);
161 Function::~Function()
165 IdentityFunction::~IdentityFunction()
169 void IdentityFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
173 void IdentityFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
177 const char *IdentityFunction::getRepr() const
182 bool IdentityFunction::isACall() const
187 PositiveFunction::~PositiveFunction()
191 int UnaryFunction::getNbInputParams() const
196 void PositiveFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
200 void PositiveFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
204 const char *PositiveFunction::getRepr() const
209 bool PositiveFunction::isACall() const
214 NegateFunction::~NegateFunction()
218 void NegateFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
220 Value *val=stack.back();
224 void NegateFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
226 asmb.push_back("fchs");
229 const char *NegateFunction::getRepr() const
234 bool NegateFunction::isACall() const
239 CosFunction::~CosFunction()
243 void CosFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
245 Value *val=stack.back();
249 void CosFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
251 asmb.push_back("fcos");
254 const char *CosFunction::getRepr() const
259 bool CosFunction::isACall() const
264 SinFunction::~SinFunction()
268 void SinFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
270 Value *val=stack.back();
274 void SinFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
276 asmb.push_back("fsin");
279 const char *SinFunction::getRepr() const
284 bool SinFunction::isACall() const
289 TanFunction::~TanFunction()
293 void TanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
295 Value *val=stack.back();
299 void TanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
301 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
304 const char *TanFunction::getRepr() const
309 bool TanFunction::isACall() const
314 SqrtFunction::~SqrtFunction()
318 void SqrtFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
320 Value *val=stack.back();
324 void SqrtFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
326 asmb.push_back("fsqrt");
329 const char *SqrtFunction::getRepr() const
334 bool SqrtFunction::isACall() const
339 AbsFunction::~AbsFunction()
343 void AbsFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
345 Value *val=stack.back();
349 void AbsFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
351 asmb.push_back("fabs");
354 const char *AbsFunction::getRepr() const
359 bool AbsFunction::isACall() const
364 void ExpFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
366 Value *val=stack.back();
370 void ExpFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
372 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
375 const char *ExpFunction::getRepr() const
380 bool ExpFunction::isACall() const
385 LnFunction::~LnFunction()
389 void LnFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
391 Value *val=stack.back();
395 void LnFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
397 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
400 const char *LnFunction::getRepr() const
405 bool LnFunction::isACall() const
410 LogFunction::~LogFunction()
414 void LogFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
416 Value *val=stack.back();
420 void LogFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
422 throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
425 const char *LogFunction::getRepr() const
430 bool LogFunction::isACall() const
435 Log10Function::~Log10Function()
439 void Log10Function::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
441 Value *val=stack.back();
445 void Log10Function::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
447 throw INTERP_KERNEL::Exception("Assembly for log Not implemented yet !");
450 const char *Log10Function::getRepr() const
455 bool Log10Function::isACall() const
460 int BinaryFunction::getNbInputParams() const
465 PlusFunction::~PlusFunction()
469 void PlusFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
471 Value *val1=stack.back();
473 Value *& val2=stack.back();
477 val3=val1->plus(val2);
479 catch(INTERP_KERNEL::Exception& e)
489 void PlusFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
491 asmb.push_back("faddp st1");
494 const char *PlusFunction::getRepr() const
499 bool PlusFunction::isACall() const
504 MinusFunction::~MinusFunction()
508 void MinusFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
510 Value *val1=stack.back();
512 Value *& val2=stack.back();
516 val3=val1->minus(val2);
518 catch(INTERP_KERNEL::Exception& e)
528 void MinusFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
530 asmb.push_back("fsubp st1");
533 const char *MinusFunction::getRepr() const
538 bool MinusFunction::isACall() const
543 MultFunction::~MultFunction()
547 void MultFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
549 Value *val1=stack.back();
551 Value *& val2=stack.back();
552 Value *val3=val1->mult(val2);
558 void MultFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
560 asmb.push_back("fmulp st1");
563 const char *MultFunction::getRepr() const
568 bool MultFunction::isACall() const
573 DivFunction::~DivFunction()
577 void DivFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
579 Value *val1=stack.back();
581 Value *& val2=stack.back();
585 val3=val1->div(val2);
587 catch(INTERP_KERNEL::Exception& e)
597 void DivFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
599 asmb.push_back("fdivp st1");
602 const char *DivFunction::getRepr() const
607 bool DivFunction::isACall() const
612 PowFunction::~PowFunction()
616 void PowFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
618 Value *val1=stack.back();
620 Value *& val2=stack.back();
624 val3=val1->pow(val2);
626 catch(INTERP_KERNEL::Exception& e)
636 void PowFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
638 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
641 const char *PowFunction::getRepr() const
646 bool PowFunction::isACall() const
651 ExpFunction::~ExpFunction()
655 MaxFunction::~MaxFunction()
659 void MaxFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
661 Value *val1=stack.back();
663 Value *& val2=stack.back();
667 val3=val1->max(val2);
669 catch(INTERP_KERNEL::Exception& e)
679 void MaxFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
681 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
684 const char *MaxFunction::getRepr() const
689 bool MaxFunction::isACall() const
694 MinFunction::~MinFunction()
698 void MinFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
700 Value *val1=stack.back();
702 Value *& val2=stack.back();
706 val3=val1->min(val2);
708 catch(INTERP_KERNEL::Exception& e)
718 void MinFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
720 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
723 const char *MinFunction::getRepr() const
728 bool MinFunction::isACall() const
733 GreaterThanFunction::~GreaterThanFunction()
737 void GreaterThanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
739 Value *val1=stack.back();
741 Value *& val2=stack.back();
745 val3=val1->greaterThan(val2);
747 catch(INTERP_KERNEL::Exception& e)
757 void GreaterThanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
759 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
762 const char *GreaterThanFunction::getRepr() const
767 bool GreaterThanFunction::isACall() const
772 LowerThanFunction::~LowerThanFunction()
776 void LowerThanFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
778 Value *val1=stack.back();
780 Value *& val2=stack.back();
784 val3=val1->lowerThan(val2);
786 catch(INTERP_KERNEL::Exception& e)
796 void LowerThanFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
798 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
801 const char *LowerThanFunction::getRepr() const
806 bool LowerThanFunction::isACall() const
811 int TernaryFunction::getNbInputParams() const
816 IfFunction::~IfFunction()
820 void IfFunction::operate(std::vector<Value *>& stack) const throw(INTERP_KERNEL::Exception)
822 Value *val1=stack.back();
824 Value *val2=stack.back();
826 Value *&val3=stack.back();
830 val4=val1->ifFunc(val2,val3);
832 catch(INTERP_KERNEL::Exception& e)
844 void IfFunction::operateX86(std::vector<std::string>& asmb) const throw(INTERP_KERNEL::Exception)
846 throw INTERP_KERNEL::Exception("Assembly Not implemented yet !");
849 const char *IfFunction::getRepr() const
854 bool IfFunction::isACall() const