FAUST compiler  0.9.9.6b8
powprim.cpp
Go to the documentation of this file.
1 #include "xtended.hh"
2 #include "Text.hh"
3 #include <math.h>
4 
5 #include "floats.hh"
6 
7 class PowPrim : public xtended
8 {
9 
10  public:
11 
12  PowPrim() : xtended("pow") {}
13 
14  virtual unsigned int arity () { return 2; }
15 
16  virtual bool needCache () { return true; }
17 
18  virtual Type infereSigType (const vector<Type>& args)
19  {
20  assert (args.size() == arity());
21  //return castInterval(floatCast(args[0]|args[1]), interval()); // temporary !!!
22  //return castInterval(args[0]|args[1], interval()); // temporary !!!
23 
24  interval i = args[0]->getInterval();
25  interval j = args[1]->getInterval();
26  return castInterval(args[0]|args[1], pow(i,j));
27 
28  }
29 
30  virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
31 
32  virtual int infereSigOrder (const vector<int>& args) {
33  assert (args.size() == arity());
34  return max(args[0], args[1]);
35  }
36 
37 
38  virtual Tree computeSigOutput (const vector<Tree>& args) {
39  num n,m;
40  assert (args.size() == arity());
41  if (isNum(args[0],n) & isNum(args[1],m)) {
42  return tree(pow(double(n), double(m)));
43  } else {
44  return tree(symbol(), args[0], args[1]);
45  }
46  }
47 
48  virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
49  {
50  assert (args.size() == arity());
51  assert (types.size() == arity());
52 
53  if ((types[1]->nature() == kInt) && (types[1]->variability() == kKonst) && (types[1]->computability() == kComp)) {
54  klass->rememberNeedPowerDef();
55  return subst("faustpower<$1>($0)", args[0], args[1]);
56  } else {
57  return subst("pow$2($0,$1)", args[0], args[1], isuffix());
58  }
59  }
60 
61  virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
62  {
63  assert (args.size() == arity());
64  assert (types.size() == arity());
65 
66  return subst("{$0}^{$1}", args[0], args[1]);
67  }
68 
69  // power is now used as an infix binary operator, we return true to
70  // indicate that we want ^(n) to be equivalent to _^n
71  virtual bool isSpecialInfix() { return true; }
72 
73 
74 };
75 
76 
78 
79 
Definition: num.hh:58
interval pow(const interval &x, const interval &y)
Definition: interval.hh:213
virtual string generateCode(Klass *klass, const vector< string > &args, const vector< Type > &types)
Definition: powprim.cpp:48
bool isNum(Tree a)
Definition: signals.hh:187
Definition: sigtype.hh:54
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Definition: lateq.hh:56
Definition: sigtype.hh:57
PowPrim()
Definition: powprim.cpp:12
double max(double x, double y)
Definition: interval.hh:60
virtual bool isSpecialInfix()
generaly false, but true for binary op # such that #(x) == _::x
Definition: powprim.cpp:71
string subst(const string &model, const vector< string > &args)
Text substitution.
Definition: Text.cpp:47
Definition: klass.hh:55
virtual Tree computeSigOutput(const vector< Tree > &args)
Definition: powprim.cpp:38
virtual Type infereSigType(const vector< Type > &args)
Definition: powprim.cpp:18
virtual bool needCache()
Definition: powprim.cpp:16
virtual void sigVisit(Tree sig, sigvisitor *visitor)
Definition: powprim.cpp:30
Sym symbol()
Definition: xtended.hh:24
virtual int infereSigOrder(const vector< int > &args)
Definition: powprim.cpp:32
void rememberNeedPowerDef()
Definition: klass.hh:141
Tree tree(const Node &n)
Definition: tree.hh:186
virtual unsigned int arity()
Definition: powprim.cpp:14
Type castInterval(Type t, const interval &i)
Definition: sigtype.hh:278
const char * isuffix()
suffix for math functions
Definition: floats.cpp:43
xtended * gPowPrim
Definition: powprim.cpp:77
virtual string generateLateq(Lateq *lateq, const vector< string > &args, const vector< Type > &types)
Definition: powprim.cpp:61