FAUST compiler  0.9.9.6b8
maxprim.cpp
Go to the documentation of this file.
1 #include "xtended.hh"
2 #include "Text.hh"
3 #include <math.h>
4 #include "sigtyperules.hh"
5 
6 #include "floats.hh"
7 
8 class MaxPrim : public xtended
9 {
10 
11  public:
12 
13  MaxPrim() : xtended("max") {}
14 
15  virtual unsigned int arity () { return 2; }
16 
17  virtual bool needCache () { return true; }
18 
19  virtual Type infereSigType (const vector<Type>& types)
20  {
21  assert (types.size() == arity());
22  interval i = types[0]->getInterval();
23  interval j = types[1]->getInterval();
24  return castInterval(types[0]|types[1], max(i,j));
25  }
26 
27  virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
28 
29  virtual int infereSigOrder (const vector<int>& args)
30  {
31  assert (args.size() == arity());
32  return max(args[0], args[1]);
33  }
34 
35 
36  virtual Tree computeSigOutput (const vector<Tree>& args)
37  {
38  double f,g; int i,j;
39 
40  assert (args.size() == arity());
41 
42  if (isDouble(args[0]->node(),&f)) {
43 
44  if (isDouble(args[1]->node(), &g)) {
45  return tree(max(f, g));
46  } else if (isInt(args[1]->node(),&j)) {
47  return tree(max(f, double(j)));
48  } else {
49  return tree(symbol(), args[0], args[1]);
50  }
51 
52  } else if (isInt(args[0]->node(),&i)) {
53 
54  if (isDouble(args[1]->node(), &g)) {
55  return tree(max(double(i), g));
56  } else if (isInt(args[1]->node(),&j)) {
57  return tree(max(i, j));
58  } else {
59  return tree(symbol(), args[0], args[1]);
60  }
61 
62  } else {
63 
64  return tree(symbol(), args[0], args[1]);
65  }
66  }
67 
68  virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
69  {
70  assert (args.size() == arity());
71  assert (types.size() == arity());
72 
73  // generates code compatible with overloaded max
74  int n0 = types[0]->nature();
75  int n1 = types[1]->nature();
76  if (n0==kReal) {
77  if (n1==kReal) {
78  // both are floats, no need to cast
79  return subst("max($0, $1)", args[0], args[1]);
80  } else {
81  assert(n1==kInt); // second argument is not float, cast it to float
82  return subst("max($0, $2$1)", args[0], args[1], icast());
83  }
84  } else if (n1==kReal) {
85  assert(n0==kInt); // first not float but second is, cast first to float
86  return subst("max($2$0, $1)", args[0], args[1], icast());
87  } else {
88  assert(n0==kInt); assert(n1==kInt); // both are integers, check for booleans
89  int b0 = types[0]->boolean();
90  int b1 = types[1]->boolean();
91  if (b0==kNum) {
92  if (b1==kNum) {
93  // both are integers, no need to cast
94  return subst("max($0, $1)", args[0], args[1]);
95  } else {
96  assert(b1==kBool); // second is boolean, cast to int
97  return subst("max($0, (int)$1)", args[0], args[1]);
98  }
99  } else if (b1==kNum) {
100  assert(b0==kBool); // first is boolean, cast to int
101  return subst("max((int)$0, $1)", args[0], args[1], icast());
102  } else {
103  assert(b0==kBool); assert(b1==kBool); // both are booleans, no need to cast
104  return subst("max((int)$0, (int)$1)", args[0], args[1]);
105  }
106  }
107  }
108 
109  virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
110  {
111  assert (args.size() == arity());
112  assert (types.size() == arity());
113 
114  Type t = infereSigType(types);
115  return subst("\\max\\left( $0, $1 \\right)", args[0], args[1]);
116  }
117 
118 };
119 
120 
122 
123 
virtual Tree computeSigOutput(const vector< Tree > &args)
Definition: maxprim.cpp:36
virtual string generateCode(Klass *klass, const vector< string > &args, const vector< Type > &types)
Definition: maxprim.cpp:68
Definition: sigtype.hh:54
virtual void sigVisit(Tree sig, sigvisitor *visitor)
Definition: maxprim.cpp:27
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
MaxPrim()
Definition: maxprim.cpp:13
Definition: lateq.hh:56
virtual string generateLateq(Lateq *lateq, const vector< string > &args, const vector< Type > &types)
Definition: maxprim.cpp:109
const char * icast()
Definition: floats.cpp:47
virtual int infereSigOrder(const vector< int > &args)
Definition: maxprim.cpp:29
Definition: sigtype.hh:55
double max(double x, double y)
Definition: interval.hh:60
virtual Type infereSigType(const vector< Type > &types)
Definition: maxprim.cpp:19
string subst(const string &model, const vector< string > &args)
Text substitution.
Definition: Text.cpp:47
Definition: sigtype.hh:55
Definition: klass.hh:55
bool isDouble(const Node &n)
Definition: node.hh:143
virtual bool needCache()
Definition: maxprim.cpp:17
Definition: sigtype.hh:54
Sym symbol()
Definition: xtended.hh:24
virtual unsigned int arity()
Definition: maxprim.cpp:15
Tree tree(const Node &n)
Definition: tree.hh:186
Type castInterval(Type t, const interval &i)
Definition: sigtype.hh:278
API to the typing system of signals.
bool isInt(const Node &n)
Definition: node.hh:126
xtended * gMaxPrim
Definition: maxprim.cpp:121