19 using Lucene.Net.Support;
21 namespace Lucene.Net.Util
34 public static int Pop(
long x)
51 x = (x & 0x3333333333333333L) + ((
Number.
URShift(x, 2)) & 0x3333333333333333L);
56 return ((
int) x) & 0x7F;
60 public static long Pop_array(
long[] A,
int wordOffset,
int numWords)
76 int n = wordOffset + numWords;
77 long tot = 0, tot8 = 0;
78 long ones = 0, twos = 0, fours = 0;
81 for (i = wordOffset; i <= n - 8; i += 8)
89 long twosA, twosB, foursA, foursB, eights;
93 long b = A[i], c = A[i + 1];
95 twosA = (ones & b) | (u & c);
100 long b = A[i + 2], c = A[i + 3];
102 twosB = (ones & b) | (u & c);
107 long u = twos ^ twosA;
108 foursA = (twos & twosA) | (u & twosB);
113 long b = A[i + 4], c = A[i + 5];
115 twosA = (ones & b) | (u & c);
120 long b = A[i + 6], c = A[i + 7];
122 twosB = (ones & b) | (u & c);
127 long u = twos ^ twosA;
128 foursB = (twos & twosA) | (u & twosB);
134 long u = fours ^ foursA;
135 eights = (fours & foursA) | (u & foursB);
149 long twosA, twosB, foursA, eights;
151 long b = A[i], c = A[i + 1];
153 twosA = (ones & b) | (u & c);
157 long b = A[i + 2], c = A[i + 3];
159 twosB = (ones & b) | (u & c);
163 long u = twos ^ twosA;
164 foursA = (twos & twosA) | (u & twosB);
167 eights = fours & foursA;
168 fours = fours ^ foursA;
176 long b = A[i], c = A[i + 1];
178 long twosA = (ones & b) | (u & c);
181 long foursA = twos & twosA;
184 long eights = fours & foursA;
185 fours = fours ^ foursA;
196 tot += (Pop(fours) << 2) + (Pop(twos) << 1) + Pop(ones) + (tot8 << 3);
204 public static long Pop_intersect(
long[] A,
long[] B,
int wordOffset,
int numWords)
207 int n = wordOffset + numWords;
208 long tot = 0, tot8 = 0;
209 long ones = 0, twos = 0, fours = 0;
212 for (i = wordOffset; i <= n - 8; i += 8)
214 long twosA, twosB, foursA, foursB, eights;
218 long b = (A[i] & B[i]), c = (A[i + 1] & B[i + 1]);
220 twosA = (ones & b) | (u & c);
225 long b = (A[i + 2] & B[i + 2]), c = (A[i + 3] & B[i + 3]);
227 twosB = (ones & b) | (u & c);
232 long u = twos ^ twosA;
233 foursA = (twos & twosA) | (u & twosB);
238 long b = (A[i + 4] & B[i + 4]), c = (A[i + 5] & B[i + 5]);
240 twosA = (ones & b) | (u & c);
245 long b = (A[i + 6] & B[i + 6]), c = (A[i + 7] & B[i + 7]);
247 twosB = (ones & b) | (u & c);
252 long u = twos ^ twosA;
253 foursB = (twos & twosA) | (u & twosB);
259 long u = fours ^ foursA;
260 eights = (fours & foursA) | (u & foursB);
269 long twosA, twosB, foursA, eights;
271 long b = (A[i] & B[i]), c = (A[i + 1] & B[i + 1]);
273 twosA = (ones & b) | (u & c);
277 long b = (A[i + 2] & B[i + 2]), c = (A[i + 3] & B[i + 3]);
279 twosB = (ones & b) | (u & c);
283 long u = twos ^ twosA;
284 foursA = (twos & twosA) | (u & twosB);
287 eights = fours & foursA;
288 fours = fours ^ foursA;
296 long b = (A[i] & B[i]), c = (A[i + 1] & B[i + 1]);
298 long twosA = (ones & b) | (u & c);
301 long foursA = twos & twosA;
304 long eights = fours & foursA;
305 fours = fours ^ foursA;
313 tot += Pop((A[i] & B[i]));
316 tot += (Pop(fours) << 2) + (Pop(twos) << 1) + Pop(ones) + (tot8 << 3);
324 public static long Pop_union(
long[] A,
long[] B,
int wordOffset,
int numWords)
327 int n = wordOffset + numWords;
328 long tot = 0, tot8 = 0;
329 long ones = 0, twos = 0, fours = 0;
332 for (i = wordOffset; i <= n - 8; i += 8)
340 long twosA, twosB, foursA, foursB, eights;
344 long b = (A[i] | B[i]), c = (A[i + 1] | B[i + 1]);
346 twosA = (ones & b) | (u & c);
351 long b = (A[i + 2] | B[i + 2]), c = (A[i + 3] | B[i + 3]);
353 twosB = (ones & b) | (u & c);
358 long u = twos ^ twosA;
359 foursA = (twos & twosA) | (u & twosB);
364 long b = (A[i + 4] | B[i + 4]), c = (A[i + 5] | B[i + 5]);
366 twosA = (ones & b) | (u & c);
371 long b = (A[i + 6] | B[i + 6]), c = (A[i + 7] | B[i + 7]);
373 twosB = (ones & b) | (u & c);
378 long u = twos ^ twosA;
379 foursB = (twos & twosA) | (u & twosB);
385 long u = fours ^ foursA;
386 eights = (fours & foursA) | (u & foursB);
395 long twosA, twosB, foursA, eights;
397 long b = (A[i] | B[i]), c = (A[i + 1] | B[i + 1]);
399 twosA = (ones & b) | (u & c);
403 long b = (A[i + 2] | B[i + 2]), c = (A[i + 3] | B[i + 3]);
405 twosB = (ones & b) | (u & c);
409 long u = twos ^ twosA;
410 foursA = (twos & twosA) | (u & twosB);
413 eights = fours & foursA;
414 fours = fours ^ foursA;
422 long b = (A[i] | B[i]), c = (A[i + 1] | B[i + 1]);
424 long twosA = (ones & b) | (u & c);
427 long foursA = twos & twosA;
430 long eights = fours & foursA;
431 fours = fours ^ foursA;
439 tot += Pop((A[i] | B[i]));
442 tot += (Pop(fours) << 2) + (Pop(twos) << 1) + Pop(ones) + (tot8 << 3);
450 public static long Pop_andnot(
long[] A,
long[] B,
int wordOffset,
int numWords)
453 int n = wordOffset + numWords;
454 long tot = 0, tot8 = 0;
455 long ones = 0, twos = 0, fours = 0;
458 for (i = wordOffset; i <= n - 8; i += 8)
466 long twosA, twosB, foursA, foursB, eights;
470 long b = (A[i] & ~ B[i]), c = (A[i + 1] & ~ B[i + 1]);
472 twosA = (ones & b) | (u & c);
477 long b = (A[i + 2] & ~ B[i + 2]), c = (A[i + 3] & ~ B[i + 3]);
479 twosB = (ones & b) | (u & c);
484 long u = twos ^ twosA;
485 foursA = (twos & twosA) | (u & twosB);
490 long b = (A[i + 4] & ~ B[i + 4]), c = (A[i + 5] & ~ B[i + 5]);
492 twosA = (ones & b) | (u & c);
497 long b = (A[i + 6] & ~ B[i + 6]), c = (A[i + 7] & ~ B[i + 7]);
499 twosB = (ones & b) | (u & c);
504 long u = twos ^ twosA;
505 foursB = (twos & twosA) | (u & twosB);
511 long u = fours ^ foursA;
512 eights = (fours & foursA) | (u & foursB);
521 long twosA, twosB, foursA, eights;
523 long b = (A[i] & ~ B[i]), c = (A[i + 1] & ~ B[i + 1]);
525 twosA = (ones & b) | (u & c);
529 long b = (A[i + 2] & ~ B[i + 2]), c = (A[i + 3] & ~ B[i + 3]);
531 twosB = (ones & b) | (u & c);
535 long u = twos ^ twosA;
536 foursA = (twos & twosA) | (u & twosB);
539 eights = fours & foursA;
540 fours = fours ^ foursA;
548 long b = (A[i] & ~ B[i]), c = (A[i + 1] & ~ B[i + 1]);
550 long twosA = (ones & b) | (u & c);
553 long foursA = twos & twosA;
556 long eights = fours & foursA;
557 fours = fours ^ foursA;
565 tot += Pop((A[i] & ~ B[i]));
568 tot += (Pop(fours) << 2) + (Pop(twos) << 1) + Pop(ones) + (tot8 << 3);
573 public static long Pop_xor(
long[] A,
long[] B,
int wordOffset,
int numWords)
575 int n = wordOffset + numWords;
576 long tot = 0, tot8 = 0;
577 long ones = 0, twos = 0, fours = 0;
580 for (i = wordOffset; i <= n - 8; i += 8)
588 long twosA, twosB, foursA, foursB, eights;
592 long b = (A[i] ^ B[i]), c = (A[i + 1] ^ B[i + 1]);
594 twosA = (ones & b) | (u & c);
599 long b = (A[i + 2] ^ B[i + 2]), c = (A[i + 3] ^ B[i + 3]);
601 twosB = (ones & b) | (u & c);
606 long u = twos ^ twosA;
607 foursA = (twos & twosA) | (u & twosB);
612 long b = (A[i + 4] ^ B[i + 4]), c = (A[i + 5] ^ B[i + 5]);
614 twosA = (ones & b) | (u & c);
619 long b = (A[i + 6] ^ B[i + 6]), c = (A[i + 7] ^ B[i + 7]);
621 twosB = (ones & b) | (u & c);
626 long u = twos ^ twosA;
627 foursB = (twos & twosA) | (u & twosB);
633 long u = fours ^ foursA;
634 eights = (fours & foursA) | (u & foursB);
643 long twosA, twosB, foursA, eights;
645 long b = (A[i] ^ B[i]), c = (A[i + 1] ^ B[i + 1]);
647 twosA = (ones & b) | (u & c);
651 long b = (A[i + 2] ^ B[i + 2]), c = (A[i + 3] ^ B[i + 3]);
653 twosB = (ones & b) | (u & c);
657 long u = twos ^ twosA;
658 foursA = (twos & twosA) | (u & twosB);
661 eights = fours & foursA;
662 fours = fours ^ foursA;
670 long b = (A[i] ^ B[i]), c = (A[i + 1] ^ B[i + 1]);
672 long twosA = (ones & b) | (u & c);
675 long foursA = twos & twosA;
678 long eights = fours & foursA;
679 fours = fours ^ foursA;
687 tot += Pop((A[i] ^ B[i]));
690 tot += (Pop(fours) << 2) + (Pop(twos) << 1) + Pop(ones) + (tot8 << 3);
708 public static readonly byte[] ntzTable =
new byte[]
710 8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1,
711 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0,
712 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2,
713 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0,
714 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1,
715 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0,
716 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
717 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0,
718 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1,
719 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0,
720 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,
726 public static int Ntz(
long val)
739 int lower = (int) val;
740 int lowByte = lower & 0xff;
742 return ntzTable[lowByte];
748 return ntzTable[lowByte] + 8;
751 return ntzTable[lowByte] + 16;
759 int upper = (int) (val >> 32);
760 lowByte = upper & 0xff;
762 return ntzTable[lowByte] + 32;
765 return ntzTable[lowByte] + 40;
768 return ntzTable[lowByte] + 48;
776 public static int Ntz(
int val)
782 int lowByte = val & 0xff;
784 return ntzTable[lowByte];
787 return ntzTable[lowByte] + 8;
790 return ntzTable[lowByte] + 16;
800 public static int Ntz2(
long x)
808 if ((y & 0x0000FFFF) == 0)
812 if ((y & 0x000000FF) == 0)
816 return (ntzTable[y & 0xff]) + n;
822 public static int Ntz3(
long x)
835 if ((y & 0x0000FFFF) == 0)
839 if ((y & 0x000000FF) == 0)
843 if ((y & 0x0000000F) == 0)
847 if ((y & 0x00000003) == 0)
856 public static bool IsPowerOfTwo(
int v)
858 return ((v & (v - 1)) == 0);
862 public static bool IsPowerOfTwo(
long v)
864 return ((v & (v - 1)) == 0);
868 public static int NextHighestPowerOfTwo(
int v)
881 public static long NextHighestPowerOfTwo(
long v)