Skip to content
Navigation Menu
{{ message }}
forked from alexander-io/Bitmap-Engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWAHQuery.c
More file actions
422 lines (381 loc) · 12.1 KB
/
Copy pathWAHQuery.c
File metadata and controls
422 lines (381 loc) · 12.1 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <stdio.h>
#include <stdlib.h>
#include "Core.h"
/*
* ANDs two columns together (col0 AND col1) and saves into ret arg
* sz0 and sz1 are sizes of the columns we're ANDing
*/
int AND_WAH(word_32 *ret, word_32 *col0, int sz0, word_32 *col1, int sz1){
int c0;//the word number we're scanning from col0
int c1;//the word number we're scanning from col1
int d;//the spot we're saving into the result
c0 = c1 = 1;//track which word we're looking at
d=0;//start saving into the first spot
word_32 w0 = col0[c0++];//get the first word from first col
word_32 w1 = col1[c1++];//get the first word from second col
//get each of their word type SegUtil.h type definitions
int t0 = getType(w0,WORD_LENGTH);
int t1 = getType(w1,WORD_LENGTH);
while(c0<=sz0 && c1<=sz1){
word_32 toAdd;//this is the result word we're creating from w0 and w1
if(t0 < ZERO_RUN && t1 < ZERO_RUN){//two literals
toAdd = litANDlitWAH(w0,w1);
//update both
w0 = col0[c0++];
t0 = getType(w0,WORD_LENGTH);
w1 = col1[c1++];
t1 = getType(w1,WORD_LENGTH);
}
else if(t0 < ZERO_RUN || t1 < ZERO_RUN){//one literal, one fill
if(t0 < ZERO_RUN){//w0 is the literal
toAdd = fillANDlitWAH(&w1,&t1,w0);
w0 = col0[c0++];//update the literal
t0 = getType(w0,WORD_LENGTH);
}
else{//w1 is the literal
toAdd = fillANDlitWAH(&w0,&t0,w1);
w1 = col1[c1++];//update the literal
t1 = getType(w1,WORD_LENGTH);
}
}
else{//two fills
if ((w0 << 2) < (w1 << 2)){//w0 is smaller
toAdd = fillANDfillWAH(w0,t0,&w1,&t1);
w0 = col0[c0++];//update the smaller fill
t0 = getType(w0,WORD_LENGTH);
}
else if ((w0 << 2) > (w1 << 2)){//w1 is smaller
toAdd = fillANDfillWAH(w1,t1,&w0,&t0);
w1 = col1[c1++];//update the smaller fill
t1 = getType(w1,WORD_LENGTH);
}
else{//special case, equal fills (can be treated as literals)
toAdd = litANDlitWAH(w0,w1);
//update both
w0 = col0[c0++];
t0 = getType(w0,WORD_LENGTH);
w1 = col1[c1++];
t1 = getType(w1,WORD_LENGTH);
}
}
if(d>=1){//if this isn't the first word, append it to the end of the resulting column we're building
appendWAH(ret,toAdd,&d);
}
else{//special case --> first word (can't append because first word is wordLength)
ret[++d] = toAdd;//just add it
}
}
return d+1;//the number of words we just wrote
}
/*
* ORs two columns together (col0 AND col1) and saves into ret arg
* sz0 and sz1 are sizes of the columns we're ORing
*/
int OR_WAH(word_32 *ret,word_32 *col0,int sz0, word_32 *col1, int sz1){
int c0=1;//track which word in col0 we're looki98 ng at
int c1=1;//track which word in col1 we're looking at
int d=0;//track which spot in the resulting array we're saving into
printf("\nin OR_WAH\n");
//get the first word from each column
word_32 w0 = col0[c0++];
printf("\n\tpre word_32 w1 = col1[c1++], in OR_WAH\n");
printf("\n\tc0:%d\t\tc1:%d\n\n", c0, c1);
// printf("\nc1:%d\n\n", c1);
word_32 w1 = col1[c1++];
printf("\n\tpost word_32 w1 = col1[c1++], in OR_WAH\n");
//and figure out their types (see type definition in SegUtil.h)
int t0 = getType(w0,WORD_LENGTH);
int t1 = getType(w1,WORD_LENGTH);
while(c0<=sz0 && c1<=sz1){
word_32 toAdd;//this is the resulting word from ORing w0 and w1
if(t0 < ZERO_RUN && t1 < ZERO_RUN){//two literals
toAdd = litORlitWAH(w0,w1);
//update both
w0 = col0[c0++];
t0 = getType(w0,WORD_LENGTH);
w1 = col1[c1++];
t1 = getType(w1,WORD_LENGTH);
}
else if(t0 < ZERO_RUN || t1 < ZERO_RUN){//one literal, one fill
if(t0 < ZERO_RUN){//w0 is the literal
toAdd = fillORlitWAH(&w1,&t1,w0);
w0 = col0[c0++];//update the literal
t0 = getType(w0,WORD_LENGTH);
}
else{//w1 is the literal
toAdd = fillORlitWAH(&w0,&t0,w1);
w1 = col1[c1++];//update the literal
t1 = getType(w1,WORD_LENGTH);
}
}
else{//two fills
if ((w0 << 2) < (w1 << 2)){//w0 is smaller
toAdd = fillORfillWAH(w0,t0,&w1,&t1);
w0 = col0[c0++];//update the smaller fill
t0 = getType(w0,WORD_LENGTH);
}
else if ((w0 << 2) > (w1 << 2)){//w1 is smaller
toAdd = fillORfillWAH(w1,t1,&w0,&t0);
w1 = col1[c1++];//update the smaller
t1 = getType(w1,WORD_LENGTH);
}
else{//special case, equal fills (can be treated as literals)
toAdd = litORlitWAH(w0,w1);
//update both
w0 = col0[c0++];
t0 = getType(w0,WORD_LENGTH);
w1 = col1[c1++];
t1 = getType(w1,WORD_LENGTH);
}
}
if(d>=1){//if we're in the middle of the columns, just append
appendWAH(ret,toAdd,&d);
}
else{//if we're on the first word, can't append (first word is wordLength)
ret[++d] = toAdd;
}
}
return d+1;//the total number of words result now has
}
/*
* Adds the wordToAdd to the end (d=last added position) of the addTo sequence
* wordToAdd will be consolidated into position if possible and if not (or the leftover) will go into d+1
*/
void appendWAH(word_32 *addTo, word_32 wordToAdd, int *d){
int prevT = getType(addTo[*d],WORD_LENGTH);//type of the last added
if(prevT==LITERAL){//there's no way to consolidate
addTo[++(*d)] = wordToAdd;
return;
}
int addT = getType(wordToAdd,WORD_LENGTH);//type of the one we're adding
if(prevT==ZERO_RUN){
if(addT == ZERO_RUN){//both zero runs so we might be able to consolidate if there's enough room
word_32 minCheck = getZeroFill(BASE_LEN)-2;//helps to check the stopping condition (as long as there are still fills left)
word_32 maxCheck = getMaxZeroFill(BASE_LEN);
while(wordToAdd>minCheck && addTo[(*d)]<maxCheck){
addTo[(*d)]++;
wordToAdd--;
}
if(wordToAdd==minCheck){
return;//successfully added everything to previous
}
else{//stopped because ran out of space
if(wordToAdd==minCheck+1){//there was exactly one left so switch to literal before adding
wordToAdd = 0;
}
addTo[++(*d)] = wordToAdd;
return;
}
}
else if(addT == ZERO_LIT){//we can probably just add this one lit to the previous run
if(addTo[(*d)]<getMaxZeroFill(BASE_LEN)){//not maxed out yet, so just add it
addTo[(*d)]++;
}
else{//maxed out so can't consolidate
addTo[++(*d)] = wordToAdd;//save into the next spot
return;
}
}
else{//can't consolidate
addTo[++(*d)] = wordToAdd;
return;
}
}
else if(prevT==ZERO_LIT){
if(addT==ZERO_LIT){//consolidate two literals into one fill
addTo[(*d)] = getZeroFill(BASE_LEN);
return;
}
else if(addT==ZERO_RUN){
if(wordToAdd<getMaxZeroFill(BASE_LEN)){//not maxed out yet, so add it
addTo[(*d)] = wordToAdd+1;
return;
}
else{//maxed out
addTo[(*d)+1] = addTo[(*d)];
addTo[(*d)++] = wordToAdd;
return;
}
}
else{//can't consolidate
addTo[++(*d)] = wordToAdd;
return;
}
}
else if(prevT==ONE_RUN){
if(addT == ONE_RUN){//both run of ones so might be able to consolidate
word_32 minCheck = getOneFill(BASE_LEN)-2;//helps to check the stopping condition (as long as there are still fills left)
word_32 maxCheck = getMaxOneFill(BASE_LEN);
while(wordToAdd>minCheck && addTo[(*d)]<maxCheck){
addTo[(*d)]++;
wordToAdd--;
}
if(wordToAdd==minCheck){
return;//successfully added everything to previous
}
else{//stopped because ran out of space
if(wordToAdd==minCheck+1){//there was exactly one left so switch to literal before adding
wordToAdd = getZeroFill(BASE_LEN)-3;
}
addTo[++(*d)] = wordToAdd;
return;
}
}
else if(addT == ONE_LIT){
if(addTo[(*d)]<getMaxOneFill(BASE_LEN)){//previous isn't maxed out so just add it
addTo[(*d)]++;
return;
}
else{//maxed out so can't consolidate
addTo[++(*d)] = wordToAdd;
return;
}
}
else{
addTo[++(*d)] = wordToAdd;
return;
}
}
else{//prev is lit of ones
if(addT==ONE_LIT){
addTo[(*d)] = getOneFill(BASE_LEN);
return;
}
else if(addT==ONE_RUN){
if(wordToAdd<getMaxOneFill(BASE_LEN)){//not maxed out
addTo[(*d)] = wordToAdd+1;
}
else{//maxed out so can't consolidate
addTo[(*d)+1] = addTo[(*d)];
addTo[(*d)++] = wordToAdd;
return;
}
}
else{//can't consolidate
addTo[++(*d)] = wordToAdd;
return;
}
}
}
/* Performs an OR on 2 fills
* Updates the larger fill for the future
* Returns the resulting word
*/
word_32 fillORfillWAH(word_32 smallFill, int smallT, word_32 *bigFill, int *bigT){
word_32 ret;
word_32 sub = ((smallFill<<2)>>2);//what we are going to subtract from the larger
if(smallT == ZERO_RUN && *bigT == ZERO_RUN){//if both 0 runs, must return small run of 0s
ret = smallFill;
}
else{
ret = getOneFill(BASE_LEN)-2+sub;//build run of 1s of length sub (number of runs in smallFill)
}
*bigFill -= sub;//subtract that from the larger
//check to see if we subtracted too much
if(((*bigFill<<2)>>2)==1){//need to change to literal
if(*bigT==ZERO_RUN){
*bigFill = 0;//literal run of zeros
*bigT = ZERO_LIT;
}
else{//update the larger fill for the future
*bigFill = getZeroFill(BASE_LEN)-3;//literal run of ones
*bigT = ONE_LIT;
}
}
return ret;
}
/*
* Performs an AND on 2 fills
* Updates the larger of the fills for the future
* Returns the resulting word
*/
word_32 fillANDfillWAH(word_32 smallFill, int smallT, word_32 *bigFill, int *bigT){
word_32 ret;
word_32 sub = ((smallFill<<2)>>2);//what we are going to subtract from the larger (number of runs the smallFill represents)
if(smallT == ONE_RUN && *bigT == ONE_RUN){//the only way to return run of 1s is if both are 1s
ret = smallFill;
}
else{//otherwise we're returning a run of zeros of length smallFill was
ret = getZeroFill(BASE_LEN)-2+sub;//build run of 0s of length sub (number of runs in smallFill)
}
*bigFill -= sub;
if(((*bigFill<<2)>>2)==1){
if(*bigT==ZERO_RUN){
*bigFill = 0;//literal run of zeros
*bigT = ZERO_LIT;
}
else{
*bigFill = getZeroFill(BASE_LEN) -3;//literal run of ones
*bigT = ONE_LIT;
}
}
return ret;
}
/*
* Performs an OR on a fill and a literal word
* Updates the fill for the future
* Returns the resulting word
*/
word_32 fillORlitWAH(word_32 *fill, int *fillT, word_32 lit){
word_32 ret;//the word to be returned
if(*fillT == ONE_RUN){//whatever we or with a run of 1s will be 1 so just return lit of ones (later)
ret = getZeroFill(BASE_LEN) -3;//literal run of 1s
}
else{//otherwise we have a run of 1s so the result will be whatever the literal is
ret = lit;
}
if(((*fill<<2)>>2)==2){//we need to turn it into a literal
if(*fillT == ZERO_RUN){
*fill = 0;//literal run of 0s
*fillT = ZERO_LIT;
}
else{
*fill = getZeroFill(BASE_LEN)-3;//literal run of ones
*fillT = ONE_LIT;
}
}
else{//otherwise we can decrement without a problem
*fill -= 1;
}
return ret;
}
/*
* Performs an AND on a fill and a literal
* Updates the fill for the future
* Returns the resulting word
*/
word_32 fillANDlitWAH(word_32 *fill, int *fillT, word_32 lit){
word_32 ret;
if(*fillT == ZERO_RUN){//whatever we and with a run of 0s will be 0 so just return 0 (later)
ret = 0;
}
else{//otherwise we have a run of 1s so the result will be whatever the literal is
ret = lit;
}
if(((*fill<<2)>>2)==2){//we need to turn it into a literal
if(*fillT == ZERO_RUN){
*fill = 0;//literal run of 0s
*fillT = ZERO_LIT;
}
else{
*fill = getZeroFill(BASE_LEN)-3;//literal run of ones
*fillT = ONE_LIT;
}
}
else{//otherwise we can decrement without a problem
*fill -= 1;
}
return ret;
}
/*
* Performs an OR between 2 literals and returns the resulting word
*/
word_32 litORlitWAH(word_32 lit1, word_32 lit2){
return lit1 | lit2;//just or them together
}
/*
* Performs an AND between 2 literals and returns the resulting word
*/
word_32 litANDlitWAH(word_32 lit1, word_32 lit2){
return lit1 & lit2;//just and them together
}
You can’t perform that action at this time.
