| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #include <stdio.h>
- #include <stdlib.h>
- static __attribute__((pure)) int popcount(int v)
- {
- if (__builtin_expect ( v != 0, 1)) {
- int c = 0;
- while (v) {
- if (v & 1)
- ++c;
- v>>=1;
- }
- return c;
- }
- return 0;
- }
- static __attribute__((pure)) int ctz(int v)
- {
- if (__builtin_expect ( v != 0, 1)) {
- int c = 0;
- while (1) {
- if((v & 1))
- break;
- ++c;
- v>>=1;
- }
- return c;
- }
- return sizeof v * 8;
- }
- #define TEST(f0, f1, x) { int r0 = f0(x); int r1=f1(x); \
- printf("[%-3s] C ==> %d vs ASM ==> %d.\n", (r0==r1) ? "OK":"BUG", r0, r1) ; }
- int main(int argc, char *argv[])
- {
- int v;
- if (argc != 2)
- return 1;
- v = strtol(argv[1], NULL, 16);
- printf("Pop count: ");
- TEST(popcount, __builtin_popcount, v)
- printf("CTZ : ");
- TEST(ctz, __builtin_ctz, v)
- return 0;
- }
|