FAUST compiler  0.9.9.6b8
absprim.cpp
Go to the documentation of this file.
00001 #include "xtended.hh"
00002 #include "Text.hh"
00003 #include <math.h>
00004 #include "sigtyperules.hh"
00005 
00006 #include "floats.hh"
00007 
00008 class AbsPrim : public xtended
00009 {
00010 
00011  public:
00012 
00013     AbsPrim() : xtended("abs") {}
00014 
00015     virtual unsigned int    arity () { return 1; }
00016 
00017     virtual bool    needCache ()    { return true; }
00018 
00019     virtual Type    infereSigType (const vector<Type>& types)
00020     {
00021         assert (types.size() == arity());
00022         Type t = types[0];
00023         return castInterval(t, abs(t->getInterval()));
00024         return t;
00025     }
00026 
00027     virtual void    sigVisit (Tree sig, sigvisitor* visitor) {}
00028 
00029     virtual int infereSigOrder (const vector<int>& args)
00030     {
00031         assert (args.size() == arity());
00032         return args[0];
00033     }
00034 
00035 
00036     virtual Tree    computeSigOutput (const vector<Tree>& args)
00037     {
00038         double f; int i;
00039 
00040         assert (args.size() == arity());
00041 
00042         if (isDouble(args[0]->node(),&f)) {
00043             return tree(fabs(f));
00044 
00045         } else if (isInt(args[0]->node(),&i)) {
00046             return tree(abs(i));
00047 
00048         } else {
00049             return tree(symbol(), args[0]);
00050         }
00051     }
00052 
00053     virtual string  generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
00054     {
00055         assert (args.size() == arity());
00056         assert (types.size() == arity());
00057 
00058         Type t = infereSigType(types);
00059         if (t->nature() == kReal) {
00060             return subst("fabs$1($0)", args[0], isuffix());
00061         } else {
00062             return subst("abs($0)", args[0]);
00063         }
00064     }
00065     
00066     virtual string  generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
00067     {
00068         assert (args.size() == arity());
00069         assert (types.size() == arity());
00070         
00071         Type t = infereSigType(types);
00072         return subst("\\left\\lvert{$0}\\right\\rvert", args[0]);
00073     }
00074 };
00075 
00076 
00077 xtended* gAbsPrim = new AbsPrim();
00078 
00079