00001 #include "xtended.hh"
00002 #include "Text.hh"
00003 #include <math.h>
00004
00005
00006 class SqrtPrim : public xtended
00007 {
00008
00009 public:
00010
00011 SqrtPrim() : xtended("sqrt") {}
00012
00013 virtual unsigned int arity () { return 1; }
00014
00015 virtual bool needCache () { return true; }
00016
00017 virtual Type infereSigType (const vector<Type>& args)
00018 {
00019 assert (args.size() == 1);
00020 Type t = args[0];
00021 interval i = t->getInterval();
00022 if (i.valid && i.lo >=0) {
00023 return castInterval(floatCast(t), interval(sqrt(i.lo), sqrt(i.hi)));
00024 } else {
00025 return castInterval(floatCast(t), interval());
00026 }
00027 }
00028
00029 virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
00030
00031 virtual int infereSigOrder (const vector<int>& args) {
00032 return args[0];
00033 }
00034
00035
00036 virtual Tree computeSigOutput (const vector<Tree>& args) {
00037
00038 num n;
00039 if (isNum(args[0],n)) {
00040 return tree(sqrtf(float(n)));
00041 } else {
00042 return tree(symbol(), args[0]);
00043 }
00044 }
00045
00046 virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
00047 {
00048 return subst("sqrtf($0)", args[0]);
00049 }
00050
00051 };
00052
00053
00054 xtended* gSqrtPrim = new SqrtPrim();
00055
00056