summary refs log tree commit diff
path: root/lisc/tools/slot.c
blob: ead0034481b6b6a8a760cbf083864288a2d0d184 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*% clang -g -Wall -o # %
 *
 * This is a test program for the slot
 * routine in isel.c, it's a tricky beast
 * so when you modify it you can use this
 * test program to debug your changes.
 *
 * Please make sure it stays in sync.
 */
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define VARL 1

enum { N = 3 };
static int sa[N] = {0, 0, 2};

static int
slot(int sz, int al)
{
	int j, k, s, l, a, ret;

	a = 1 << al;
	l = sz;

	if (l > a) {
		/* for large slots, we just
		 * tack them on the next max
		 * alignment slot available
		 * todo, could sophisticate
		 */
		l = (l + a-1) & ~(a-1);
		s = sa[N-1] + l;
		ret = s;
		for (j=0, k=1; j<N; j++, k*=2) {
			l = (l + k-1) & ~(k-1);
			sa[j] = sa[N-1] + l;
		}
	} else {
		j = al;
		s = sa[j] + a;
		ret = s;
	Shift:
		if (j < N-1 && s < sa[j+1])
			/* ........-----------...
			 * ^       ^          ^
			 * sa[j]  sa[j]+a    sa[j+1]
			 *
			 * we have to skip to the
			 * next large whole
			 */
			s = sa[j+1];

		for (k=0; k<=j; k++)
			/* move all smaller holes
			 * that we contain with us
			 */
			if (sa[k] == sa[j])
				sa[k] = s;

		if (j < N-1 && s > sa[j+1]) {
			/* we were in a bigger hole,
			 * it needs to shift further
			 */
			s = sa[++j] + (a *= 2);
			goto Shift;
		}
	}
	return ret;
}

enum { S = 300 };

int
main(int ac, char *av[])
{
	char stk[S] = {0}, buf[4] = {0};
	unsigned seed;
	int i, a, l, s, itr;
	int ret;
	FILE *rnd;

	if (ac < 2) {
		rnd = fopen("/dev/urandom", "r");
		fread(buf, 4, 1, rnd);
		seed = *(unsigned *)buf;
		printf("seed: %u", seed);
		fclose(rnd);
	} else
		seed = atol(av[1]);
	srand(seed);

	for (itr=1;;itr++) {
		if ((itr-1) % 4 == 0)
			printf("\n");
		do
			a = rand() % 4;
		while (a >= N);
		if ((float)rand()/RAND_MAX < 0.1 && VARL) {
			l = rand() % (S/20);
			printf("[(%02d) %02d %d] ", itr, l, a);
		} else {
			l = 1 << a;
			printf("[(%02d) xx %d] ", itr, a);
		}
		s = slot(l, a);
		if (s > S)
			break;
		if ((s+2) % (1 << a) != 0) {
			printf("... FAIL (%d align)\n", s);
			ret = 1;
			goto end;
		}
		for (i=0; i<l; i++) {
			s--;
			assert(s >= 0);
			if (stk[s]) {
				printf("... FAIL (%d)\n", s);
				ret = 1;
				goto end;
			}
			stk[s] = itr;
		}
	}

	for (s=0, i=0; i<S; i++)
		if (!stk[i])
			s++;
	printf("... OK (%d)\n", s);
	ret = 0;
end:
	printf("\n");
	for (i=0; i<S; i++)
		printf("%02d ", stk[i]);
	printf("\n\n");
	for (i=0; i<N; i++)
		printf("sa[%d] = %d\n", i, sa[i]);
	exit(ret);
}