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
7
typedef
uint32_t
u32
;
8
9
static
inline
u32
U32_BIT
=
sizeof
(
u32
) * CHAR_BIT;
10
11
u32
popcount0
(
u32
n
) {
12
return
(
u32
)__builtin_popcount(
n
);
13
}
14
15
u32
popcount1
(
u32
n
) {
16
return
(
u32
)_mm_popcnt_u32(
n
);
17
}
18
19
u32
popcount2
(
u32
n
) {
20
__asm__
(
"popcnt %0, %1\n"
:
"+r"
(
n
) :
"r"
(
n
));
21
return
n
;
22
}
23
24
u32
popcount3
(
u32
n
);
25
__asm__
(
26
"popcount3: popcnt %eax, %edi\n"
27
"ret\n"
);
28
29
u32
popcount4
(
u32
n
) {
30
u32
ret = 0, mask = 1;
31
do
32
ret += !!(
n
& mask);
33
while
(mask <<= 1);
34
return
ret;
35
}
36
37
u32
popcount5
(
u32
n
) {
38
u32
ret = 0;
39
for
(
u32
c
=
U32_BIT
;
c
--;
n
>>= 1)
40
ret +=
n
& 1;
41
return
ret;
42
}
43
44
u32
popcount6
(
u32
n
) {
45
u32
ret = 0;
46
for
(;
n
;
n
>>= 1)
47
ret +=
n
& 1;
48
return
ret;
49
}
50
51
u32
popcount7
(
u32
n
) {
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
61
u32
popcount8
(
u32
n
);
62
__asm__
(
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
74
u32
popcount9
(
u32
n
) {
75
u32
ret = 0;
76
for
(;
n
;
n
&=
n
- 1)
77
++ret;
78
return
ret;
79
}
80
81
u32
popcount10
(
u32
n
) {
82
u32
ret = 0;
83
for
(;
n
;
n
= _blsr_u32(
n
))
84
++ret;
85
return
ret;
86
}
node::n
@ n
popcount6
u32 popcount6(u32 n)
Definition
popcount.h:44
popcount7
u32 popcount7(u32 n)
Definition
popcount.h:51
popcount10
u32 popcount10(u32 n)
Definition
popcount.h:81
popcount4
u32 popcount4(u32 n)
Definition
popcount.h:29
__asm__
__asm__("popcount3: popcnt %eax, %edi\n" "ret\n")
popcount8
u32 popcount8(u32 n)
popcount1
u32 popcount1(u32 n)
Definition
popcount.h:15
popcount0
u32 popcount0(u32 n)
Definition
popcount.h:11
popcount5
u32 popcount5(u32 n)
Definition
popcount.h:37
popcount3
u32 popcount3(u32 n)
popcount2
u32 popcount2(u32 n)
Definition
popcount.h:19
U32_BIT
static u32 U32_BIT
Definition
popcount.h:9
popcount9
u32 popcount9(u32 n)
Definition
popcount.h:74
u32
uint32_t u32
Definition
popcount.h:7
c
Definition
mult_inh.c:27
tmp
popcount
popcount.h
Generated by
1.11.0