53767c25eaba94fda824f019c6b98a52bae08dfc
[libreriscv.git] / openpower / sv / cookbook / pospopcnt.mdwn
1 # Positional popcount SVP64
2
3 * <https://bugs.libre-soc.org/show_bug.cgi?id=672>
4 * <https://github.com/clausecker/pospop/blob/master/countsse2_amd64.s>
5
6 Positional popcount in optimised assembler is typically done on SIMD ISAs in
7 around 500 lines. Power ISA thanks to `bpermd` can be much more efficient:
8 with SVP64 even more so. The reference implementation showing the concept
9 is below.
10
11 ```
12 // Copyright (c) 2020 Robert Clausecker <fuz@fuz.su>
13 // count8 reference implementation for tests. Do not alter.
14 func count8safe(counts *[8]int, buf []uint8) {
15 for i := range buf {
16 for j := 0; j < 8; j++ {
17 counts[j] += int(buf[i] >> j & 1)
18 }
19 }
20 }
21 ```
22 Array popcount is just standard popcount function ([[!wikipedia Hamming weight]]) on an array of values whereas positional popcount adds up the totals of each bit set to 1 in each bit-position, of an array of input values.
23
24 <img src="/openpower/sv/cookbook/popcount.svg " alt="pospopcnt" width="70%" />
25
26 [[!tag svp64_cookbook ]]