JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
popcnt/gcc | Reply
""
GCC has a function called __builtin_popcount which does precisely this. However, unlike __builtin_ctz, it does not translate into a hardware instruction (at least on x86). Instead, it uses a table-based method similar to the one described above for bit searches.
""

If you use -march=native on a Nehalem or better with a recent gcc, it works fine:


rokicki@stoa:~$ cat bc.c
int f(int v) {
return __builtin_popcount(v) ;
}
rokicki@stoa:~$ gcc -O -march=native -S bc.c
rokicki@stoa:~$ cat bc.s
.file "bc.c"
.text
.globl f
.type f, @function
f:
.LFB0:
.cfi_startproc
popcntl %edi, %eax
ret
.cfi_endproc
.LFE0:
.size f, .-f
.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
.section .note.GNU-stack,"",@progbits
RSS