Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / extended / absprim.cpp
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
77 xtended* gAbsPrim = new AbsPrim();
78
79