FAUST compiler  0.9.9.6b8
powprim.cpp
Go to the documentation of this file.
00001 #include "xtended.hh"
00002 #include "Text.hh"
00003 #include <math.h>
00004 
00005 #include "floats.hh"
00006 
00007 class PowPrim : public xtended
00008 {
00009 
00010  public:
00011  
00012     PowPrim() : xtended("powf") {}
00013     
00014     virtual unsigned int arity () { return 2; }
00015     
00016     virtual bool    needCache ()    { return true; }
00017     
00018     virtual Type    infereSigType (const vector<Type>& args)
00019     {
00020         assert (args.size() == arity());
00021         //return castInterval(floatCast(args[0]|args[1]), interval()); // temporary !!!
00022         return castInterval(args[0]|args[1], interval()); // temporary !!!
00023     }
00024     
00025     virtual void    sigVisit (Tree sig, sigvisitor* visitor) {} 
00026     
00027     virtual int infereSigOrder (const vector<int>& args) {
00028         assert (args.size() == arity());
00029         return max(args[0], args[1]);
00030     }
00031 
00032     
00033     virtual Tree    computeSigOutput (const vector<Tree>& args) {
00034         num n,m;
00035         assert (args.size() == arity());
00036         if (isNum(args[0],n) & isNum(args[1],m)) {
00037             return tree(pow(double(n), double(m)));
00038         } else {
00039             return tree(symbol(), args[0], args[1]);
00040         }
00041     }
00042         
00043     virtual string  generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
00044     {
00045         assert (args.size() == arity());
00046         assert (types.size() == arity());
00047 
00048         if (types[1]->nature() == kInt) {
00049             klass->rememberNeedPowerDef();
00050             return subst("faustpower<$1>($0)", args[0], args[1]);
00051         } else {
00052             return subst("pow$2($0,$1)", args[0], args[1], isuffix());
00053         }
00054     }
00055     
00056     virtual string  generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
00057     {
00058         assert (args.size() == arity());
00059         assert (types.size() == arity());
00060         
00061         return subst("{$0}^{$1}", args[0], args[1]);
00062     }
00063 
00064     // power is now used as an infix binary operator, we return true to
00065     // indicate that we want ^(n) to be equivalent to _^n
00066     virtual bool    isSpecialInfix()    { return true; }
00067 
00068     
00069 };
00070 
00071 
00072 xtended* gPowPrim = new PowPrim();
00073 
00074