-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.c
More file actions
110 lines (86 loc) · 2.46 KB
/
Util.c
File metadata and controls
110 lines (86 loc) · 2.46 KB
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
#include "Util.h"
void util_setup(void) {
char r, f, i, width, tlbr, bltr;
rf_to_tlbr_start = (int*) malloc(64*sizeof(int));
rf_to_bltr_start = (int*) malloc(64*sizeof(int));
tlbr_to_rf = (int*) malloc(64*sizeof(int));
bltr_to_rf = (int*) malloc(64*sizeof(int));
rf_to_tlbr = (int*) malloc(64*sizeof(int));
rf_to_bltr = (int*) malloc(64*sizeof(int));
rf_to_tlbr_width = (int*) malloc(64*sizeof(int));
rf_to_bltr_width = (int*) malloc(64*sizeof(int));
/* TL_BR */
/* top left of board, including diagnol */
i = 0;
for(width=1; width<=8; width++) {
for(f=0; f<width; f++) {
r = 7 - (width - f - 1);
/* this number is where the diagnol starts */
tlbr = width*(width-1)/2;
rf_to_tlbr_width[(int) r*8+f] = width;
rf_to_tlbr_start[(int) r*8+f] = (int) tlbr;
tlbr_to_rf[(int) i] = (int) r*8+f;
rf_to_tlbr[(int) r*8+f] = i;
i++;
}
}
/* bottom right of board, excluding diagnol */
for(width=7; width>=0; width--) {
for(f=(8-width); f<8; f++) {
r = f - (8-width);
/* this number is where the diagnol starts */
tlbr = 64 - (width*(width-1)/2 + width);
rf_to_tlbr_width[(int) r*8+f] = width;
rf_to_tlbr_start[(int) r*8+f] = (int) tlbr;
tlbr_to_rf[(int) i] = (int) r*8+f;
rf_to_tlbr[(int) r*8+f] = i;
i++;
}
}
/* BL_TR */
/* bottom left of board, including diagnol */
i = 0;
for(width=1; width<=8; width++) {
for(f=width-1; f>=0; f--) {
r = width - f - 1;
/* this number is where the diagnol starts */
bltr = width*(width-1)/2;
rf_to_bltr_width[(int) r*8+f] = width;
rf_to_bltr_start[(int) r*8+f] = (int) bltr;
bltr_to_rf[(int) i] = (int) r*8+f;
rf_to_bltr[(int) r*8+f] = i;
i++;
}
}
/* top right of board, excluding diagnol */
for(width=7; width>=0; width--) {
for(f=7; f>=8-width; f--) {
r = 8 - (width - (7-f));
/* this number is where the diagnol starts */
bltr = 64 - (width*(width-1)/2 + width);
rf_to_bltr_width[(int) r*8+f] = width;
rf_to_bltr_start[(int) r*8+f] = (int) bltr;
bltr_to_rf[(int) i] = (int) r*8+f;
rf_to_bltr[(int) r*8+f] = i;
i++;
}
}
}
void util_cleanup(void) {
free(tlbr_to_rf);
free(bltr_to_rf);
free(rf_to_tlbr);
free(rf_to_bltr);
free(rf_to_tlbr_start);
free(rf_to_bltr_start);
free(rf_to_tlbr_width);
free(rf_to_bltr_width);
}
/* this will get replaced with ASM */
int MSB(long bits, int offset) {
int i;
for(i=(63-offset); i>=0; i--)
if(bits & (1L << i))
return i;
return -1;
}