File: loopDetector.cpp

package info (click to toggle)
faust 0.9.9.4b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,164 kB
  • ctags: 6,050
  • sloc: cpp: 25,384; xml: 4,102; makefile: 648; yacc: 372; ruby: 247; lex: 161; sh: 50
file content (55 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "loopDetector.hh"
#include "ppbox.hh"
 
void loopDetector::listPossibleCycles (vector<int>& v)
{
	//cerr << "list possible cycles" << endl;
	Tree t = get(0);
	for (int i=1; i<=(fBuffersize/2); i++) {
		if (t == get(i)) { 
			//cout << "possible cycle at " << i << endl;
			v.push_back(i); 
		}
	}
}
 
bool loopDetector::detect (Tree t)
{
	//cerr << "detect " << t << endl;
	fPhase++;
	fBuffer[fPhase%fBuffersize] = t;
	if ((fPhase%fCheckperiod) == 0) {
		// list possible cycles
		vector<int> vc;
		listPossibleCycles(vc);
	
		// check each possible cycle
		//for (int i = vc.size(); i > 0;) {
		for (unsigned int i = 0; i < vc.size(); i++) {
			//i--;
			if (isCycle(vc[i])) {
				cerr 	<< "ERROR : the Faust compiler has detected an endless cycle of "
						<< vc[i]  
						<< " evaluations. Last evaluated expression : "
						<< fPhase << endl;
				exit(1);
				return true;
			}
		}		
	}
	return false;
}


bool loopDetector::isCycle(int period)
{
	//cerr << "check cycle " << period << endl;
	int n = fBuffersize/period; // number of periods
	for (int i=0; i<period; i++) {
		Tree x = get(i);
		for (int p=1; p<n; p++) {
			if (x != get(i+p*period)) return false;
		}
	}
	return true;
}