-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline64.h
63 lines (59 loc) · 1.55 KB
/
inline64.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
X86-64 inline functions for MSB(), LSB() and PopCnt(). Note
that these are 64 bit functions and they use 64 bit (quad-word)
X86-64 instructions.
*/
/* *INDENT-OFF* */
int static __inline__ MSB(long word)
{
long dummy, dummy2;
asm(" bsrq %1, %0 " "\n\t"
" jnz 1f " "\n\t"
" movq $64, %0 " "\n\t"
"1: " "\n\t"
: "=&r"(dummy), "=&r" (dummy2)
: "1"((long) (word))
: "cc");
return (dummy);
}
int static __inline__ LSB(long word)
{
long dummy, dummy2;
asm(" bsfq %1, %0 " "\n\t"
" jnz 1f " "\n\t"
" movq $64, %0 " "\n\t"
"1: " "\n\t"
: "=&r"(dummy), "=&r" (dummy2)
: "1"((long) (word))
: "cc");
return (dummy);
}
#if defined(POPCNT)
int static __inline__ PopCnt(long word)
{
long dummy, dummy2;
asm(" popcnt %1, %0 " "\n\t"
: "=&r"(dummy), "=&r" (dummy2)
: "1"((long) (word))
: "cc");
return (dummy);
}
#else
int static __inline__ PopCnt(long word)
{
long dummy, dummy2, dummy3;
asm(" xorq %0, %0 " "\n\t"
" testq %1, %1 " "\n\t"
" jz 2f " "\n\t"
"1: leaq -1(%1),%2 " "\n\t"
" incq %0 " "\n\t"
" andq %2, %1 " "\n\t"
" jnz 1b " "\n\t"
"2: " "\n\t"
: "=&r"(dummy), "=&r"(dummy2), "=&r"(dummy3)
: "1"((long) (word))
: "cc");
return (dummy);
}
#endif
/* *INDENT-ON* */