FAUST compiler  0.9.9.6b8
absprim.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 AbsPrim : public xtended
9 {
10 
11  public:
12 
13  AbsPrim() : xtended("abs") {}
14 
15  virtual unsigned int arity () { return 1; }
16 
17  virtual bool needCache () { return true; }
18 
19  virtual Type infereSigType (const vector<Type>& types)
20  {
21  assert (types.size() == arity());
22  Type t = types[0];
23  return castInterval(t, abs(t->getInterval()));
24  return t;
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 args[0];
33  }
34 
35 
36  virtual Tree computeSigOutput (const vector<Tree>& args)
37  {
38  double f; int i;
39 
40  assert (args.size() == arity());
41 
42  if (isDouble(args[0]->node(),&f)) {
43  return tree(fabs(f));
44 
45  } else if (isInt(args[0]->node(),&i)) {
46  return tree(abs(i));
47 
48  } else {
49  return tree(symbol(), args[0]);
50  }
51  }
52 
53  virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
54  {
55  assert (args.size() == arity());
56  assert (types.size() == arity());
57 
58  Type t = infereSigType(types);
59  if (t->nature() == kReal) {
60  return subst("fabs$1($0)", args[0], isuffix());
61  } else {
62  return subst("abs($0)", args[0]);
63  }
64  }
65 
66  virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
67  {
68  assert (args.size() == arity());
69  assert (types.size() == arity());
70 
71  Type t = infereSigType(types);
72  return subst("\\left\\lvert{$0}\\right\\rvert", args[0]);
73  }
74 };
75 
76 
78 
79 
AbsPrim()
Definition: absprim.cpp:13
interval abs(const interval &x)
Definition: interval.hh:226
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
Definition: lateq.hh:56
virtual unsigned int arity()
Definition: absprim.cpp:15
virtual int infereSigOrder(const vector< int > &args)
Definition: absprim.cpp:29
xtended * gAbsPrim
Definition: absprim.cpp:77
string subst(const string &model, const vector< string > &args)
Text substitution.
Definition: Text.cpp:47
Definition: klass.hh:55
virtual void sigVisit(Tree sig, sigvisitor *visitor)
Definition: absprim.cpp:27
bool isDouble(const Node &n)
Definition: node.hh:143
virtual string generateCode(Klass *klass, const vector< string > &args, const vector< Type > &types)
Definition: absprim.cpp:53
Definition: sigtype.hh:54
Sym symbol()
Definition: xtended.hh:24
virtual bool needCache()
Definition: absprim.cpp:17
Tree tree(const Node &n)
Definition: tree.hh:186
Type castInterval(Type t, const interval &i)
Definition: sigtype.hh:278
const char * isuffix()
suffix for math functions
Definition: floats.cpp:43
virtual Type infereSigType(const vector< Type > &types)
Definition: absprim.cpp:19
API to the typing system of signals.
bool isInt(const Node &n)
Definition: node.hh:126
virtual string generateLateq(Lateq *lateq, const vector< string > &args, const vector< Type > &types)
Definition: absprim.cpp:66
virtual Tree computeSigOutput(const vector< Tree > &args)
Definition: absprim.cpp:36