40aa579011cb2e98d52adbaa1e6c46f89427ead4
[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, and it adds up the totals of each bit set to 1 in each bit-position,
10 of an array of input alues.
11
12 ```
13 // Copyright (c) 2020 Robert Clausecker <fuz@fuz.su>
14 // count8 reference implementation for tests. Do not alter.
15 func count8safe(counts *[8]int, buf []uint8) {
16 for i := range buf {
17 for j := 0; j < 8; j++ {
18 counts[j] += int(buf[i] >> j & 1)
19 }
20 }
21 }
22 ```
23
24 <img src="/openpower/sv/cookbook/popcount.svg " alt="pospopcnt" width="100%" />
25
26 [[!tag svp64_cookbook ]]