Examples Makefile.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / normalize / normalize.cpp
1 #include <stdio.h>
2 #include <assert.h>
3 #include "tlib.hh"
4 #include "signals.hh"
5 #include "sigprint.hh"
6 #include "ppsig.hh"
7 #include "simplify.hh"
8 #include "normalize.hh"
9 #include "sigorderrules.hh"
10 #include <map>
11 #include <list>
12
13 #include "mterm.hh"
14 #include "aterm.hh"
15
16 #if 0
17 static void countAddTerm (map<Tree,Tree>& M, Tree t, bool invflag);
18 static void incTermCount (map<Tree,int>& M, Tree t, bool invflag);
19 static Tree buildPowTerm (Tree f, int q);
20 static Tree simplifyingReorganizingMul(Tree t1, Tree t2);
21 static Tree reorganizingMul(Tree k, Tree t);
22 static void factorizeAddTerm(map<Tree,Tree>& M);
23 #endif
24
25 #undef TRACE
26
27 /**
28 * Compute the Add-Normal form of a term t.
29 * \param t the term to be normalized
30 * \return the normalized term
31 */
32 Tree normalizeAddTerm(Tree t)
33 {
34 #ifdef TRACE
35 cerr << "START normalizeAddTerm : " << ppsig(t) << endl;
36 #endif
37
38 aterm A(t);
39 //cerr << "ATERM IS : " << A << endl;
40 mterm D = A.greatestDivisor();
41 while (D.isNotZero() && D.complexity() > 0) {
42 //cerr << "GREAT DIV : " << D << endl;
43 A = A.factorize(D);
44 D = A.greatestDivisor();
45 }
46 return A.normalizedTree();
47 }
48
49
50 /**
51 * Compute the normal form of a 1-sample delay term s'.
52 * The normalisation rules are :
53 * 0' -> 0 /// INACTIVATE dec07 bug recursion
54 * (k*s)' -> k*s'
55 * (s/k)' -> s'/k
56 * \param s the term to be delayed by 1 sample
57 * \return the normalized term
58 */
59 Tree normalizeDelay1Term(Tree s)
60 {
61 return normalizeFixedDelayTerm(s, tree(1));
62 }
63
64
65 /**
66 * Compute the normal form of a fixed delay term (s@d).
67 * The normalisation rules are :
68 * s@0 -> s
69 * 0@d -> 0
70 * (k*s)@d -> k*(s@d)
71 * (s/k)@d -> (s@d)/k
72 * (s@n)@m -> s@(n+m)
73 * Note that the same rules can't be applied to
74 * + et - becaue the value of the first d samples
75 * would be wrong. We could also add delays such that
76 * \param s the term to be delayed
77 * \param d the value of the delay
78 * \return the normalized term
79 */
80
81 Tree normalizeFixedDelayTerm(Tree s, Tree d)
82 {
83 Tree x, y, r;
84 int i;
85
86 if (isZero(d) && ! isProj(s, &i, r)) {
87
88 return s;
89
90 } else if (isZero(s)) {
91
92 return s;
93
94 } else if (isSigMul(s, x, y)) {
95
96 if (getSigOrder(x) < 2) {
97 return /*simplify*/(sigMul(x,normalizeFixedDelayTerm(y,d)));
98 } else if (getSigOrder(y) < 2) {
99 return /*simplify*/(sigMul(y,normalizeFixedDelayTerm(x,d)));
100 } else {
101 return sigFixDelay(s,d);
102 }
103
104 } else if (isSigDiv(s, x, y)) {
105
106 if (getSigOrder(y) < 2) {
107 return /*simplify*/(sigDiv(normalizeFixedDelayTerm(x,d),y));
108 } else {
109 return sigFixDelay(s,d);
110 }
111
112 } else if (isSigFixDelay(s, x, y)) {
113 // (x@n)@m = x@(n+m)
114 // return sigFixDelay(x,tree(tree2int(d)+tree2int(y)));
115 return normalizeFixedDelayTerm(x,simplify(sigAdd(d,y)));
116
117 } else {
118
119 return sigFixDelay(s,d);
120 }
121 }
122