The implementation of unrolled popcount below yields a wrong result, I managed to observer that only elements b[0]
and b[2]
are counted while b[1]
and b[3]
are not.
#include <stdio.h>
int count_multiple_bits(unsigned long long *b, int size) {
unsigned long long *d = b;
int c;
__asm__("LD2 {v0.D, v1.D}[0], [%1], #16 \n\t"
"LD2 {v0.D, v1.D}[1], [%1] \n\t"
"CNT v0.16b, v0.16b \n\t"
"CNT v1.16b, v1.16b \n\t"
"UADDLV h2, v0.16b \n\t"
"UADDLV h2, v1.16b \n\t"
"UMOV %0, v2.d[0] \n\t"
: "+r"(c)
: "r"(d) : "v0", "v1", "v2");
return c;
}
int main(int argc, const char *argv[]) {
unsigned long long bits[] = { -1ull, -1ull, -1ull, -1ull };
printf("Test: %i\n", count_multiple_bits(bits, 4));
return 0;
}
This one which counts 2 elements at a time works fine:
int count_multiple_bits(unsigned long long *b, int size) {
unsigned long long *d = b;
int c;
__asm__("LD1 {v0.D}[0], [%1], #8 \n\t"
"LD1 {v0.D}[1], [%1] \n\t"
"CNT v0.16b, v0.16b \n\t"
"UADDLV h1, v0.16b \n\t"
"UMOV %0, v1.d[0] \n\t"
: "+r"(c)
: "r"(d) : "v0", "v1");
return c;
}
With all else being equal, I'd guess that the loads are wrong, here's the layout I presume:
v0.D[0] = b[0]
v1.D[0] = b[1]
v0.D[1] = b[2]
v1.D[1] = b[3]
My bad.
The problem was in UADDLV
which clears the destination register, here's final version that sums arbitrary length input:
int count_multiple_bits(unsigned long long *b, unsigned int size) {
unsigned long long *d = b;
unsigned int masked = 0, i = 0;
int c = 0;
masked = size & ~3;
for (; i < masked; i += 4)
__asm__("LD1 {v0.2D, v1.2D}[0], [%1], #32 \n\t"
"CNT v0.16b, v0.16b \n\t"
"CNT v1.16b, v1.16b \n\t"
"UADDLV h2, v0.16b \n\t"
"UADDLV h3, v1.16b \n\t"
"ADD d2, d3, d2 \n\t"
"UMOV x0, v2.d[0] \n\t"
"ADD %0, x0, %0 \n\t"
: "+r"(c), "+r"(d)
:: "x0", "v0", "v1", "v2", "v3");
masked = size & 3;
for (i = 0; i < masked; ++i)
__asm__("LD1 {v0.D}[0], [%1], #8 \n\t"
"CNT v0.8b, v0.8b \n\t"
"UADDLV h1, v0.8b \n\t"
"UMOV x0, v1.d[0] \n\t"
"ADD %0, x0, %0 \n\t"
: "+r"(c), "+r"(d)
: : "x0", "v0", "v1");
return c;
}
Works like a charm :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.