codex
Loading...
Searching...
No Matches
popcount.h
Go to the documentation of this file.
1#include <limits.h>
2#include <stdint.h>
3#include <stdio.h>
4
5#include <immintrin.h>
6
7typedef uint32_t u32;
8
9static inline u32 U32_BIT = sizeof(u32) * CHAR_BIT;
10
12 return (u32)__builtin_popcount(n);
13}
14
16 return (u32)_mm_popcnt_u32(n);
17}
18
20 __asm__("popcnt %0, %1\n" : "+r" (n) : "r" (n));
21 return n;
22}
23
26 "popcount3: popcnt %eax, %edi\n"
27 "ret\n");
28
30 u32 ret = 0, mask = 1;
31 do
32 ret += !!(n & mask);
33 while(mask <<= 1);
34 return ret;
35}
36
38 u32 ret = 0;
39 for(u32 c = U32_BIT; c--; n >>= 1)
40 ret += n & 1;
41 return ret;
42}
43
45 u32 ret = 0;
46 for(; n; n >>= 1)
47 ret += n & 1;
48 return ret;
49}
50
52 u32 ret = 0;
53 while(n)
54 __asm__(
55 "shr %1, 1\n"
56 "adc %0, 0\n"
57 : "+r" (ret), "+r" (n));
58 return ret;
59}
60
63 "popcount8:\n"
64 " xor rax, rax\n"
65 ".L0:\n"
66 " shr %edi, 1\n"
67 " jz .L1\n"
68 " adc %eax, 0\n"
69 " jmp .L0\n"
70 ".L1:\n"
71 " adc %eax, 0\n"
72 " ret\n");
73
75 u32 ret = 0;
76 for(; n; n &= n - 1)
77 ++ret;
78 return ret;
79}
80
82 u32 ret = 0;
83 for(; n; n = _blsr_u32(n))
84 ++ret;
85 return ret;
86}
u32 popcount6(u32 n)
Definition popcount.h:44
u32 popcount7(u32 n)
Definition popcount.h:51
u32 popcount10(u32 n)
Definition popcount.h:81
u32 popcount4(u32 n)
Definition popcount.h:29
__asm__("popcount3: popcnt %eax, %edi\n" "ret\n")
u32 popcount8(u32 n)
u32 popcount1(u32 n)
Definition popcount.h:15
u32 popcount0(u32 n)
Definition popcount.h:11
u32 popcount5(u32 n)
Definition popcount.h:37
u32 popcount3(u32 n)
u32 popcount2(u32 n)
Definition popcount.h:19
static u32 U32_BIT
Definition popcount.h:9
u32 popcount9(u32 n)
Definition popcount.h:74
uint32_t u32
Definition popcount.h:7
Definition mult_inh.c:27