Generate populationCount const at compile time (#7583)

This commit is contained in:
Oscar Nihlgård 2018-04-16 21:52:27 +02:00 • committed by Andreas Rumpf
commit c4d7cc3da0

View file

@ -72,24 +72,25 @@ proc bitSetContains(x, y: TBitSet): bool =
result = true result = true
# Number of set bits for all values of int8 # Number of set bits for all values of int8
const populationCount: array[low(int8)..high(int8), int8] = [ const populationCount: array[low(int8)..high(int8), int8] = block:
1.int8, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, var arr: array[low(int8)..high(int8), int8]
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, proc countSetBits(x: int8): int8 =
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, return
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, ( x and 0b00000001'i8) +
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, ((x and 0b00000010'i8) shr 1) +
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, ((x and 0b00000100'i8) shr 2) +
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, ((x and 0b00001000'i8) shr 3) +
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, ((x and 0b00010000'i8) shr 4) +
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, ((x and 0b00100000'i8) shr 5) +
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, ((x and 0b01000000'i8) shr 6) +
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, ((x and 0b10000000'i8) shr 7)
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, for it in low(int8)..high(int8):
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7 arr[it] = countSetBits(it)
]
arr
proc bitSetCard(x: TBitSet): BiggestInt = proc bitSetCard(x: TBitSet): BiggestInt =
for it in x: for it in x: