summaryrefslogtreecommitdiff
path: root/tb/sim/control_flow.c
blob: 3bcf643d06637a124abe79c2864ee5143b8efd1c (plain)
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
56
int __attribute__((noinline)) sgn(int x)
{
	return x > 0 ? 1 : x < 0 ? -1 : 0;
}

int reset(int a, int b)
{
	int c;
	do
	{
		c = sgn(a) + sgn(b);
		if(!c)
		{
			goto l1;
		}

		switch(c)
		{
			case -1:
				a = b;
				__attribute__((fallthrough));

			case 0:
				b = a;
				break;

			case 1:
				goto l2;

			default:
				a = (a >> 7) ^ ((unsigned)a << 10);
				b = b + (b << 12 | (unsigned)b >> 17);
				continue;
		}

		if(sgn(b) <= 0)
		{
			a = ~b;
		}

		if(sgn(a) > 0)
		{
			b = 1 - a;
		}
	} while(a > b);

	c = a + b;
	return c;

l1:
	a = (b << 16 | (unsigned)a >> 16) ^ c;

l2:
	a = a << 2 | (unsigned)a >> 30;
	return a;
}