Replacing some integral typedefs with standard types
[libcds.git] / cds / compiler / gcc / amd64 / bitop.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_COMPILER_GCC_AMD64_BITOP_H
4 #define __CDS_COMPILER_GCC_AMD64_BITOP_H
5
6 //@cond none
7 namespace cds {
8     namespace bitop { namespace platform { namespace gcc { namespace amd64 {
9         // MSB - return index (1..32) of most significant bit in nArg. If nArg == 0 return 0
10 #        define cds_bitop_msb32_DEFINED
11         static inline int msb32( uint32_t nArg )
12         {
13             int        nRet;
14             __asm__ __volatile__ (
15                 "bsrl        %[nArg], %[nRet]     ;\n\t"
16                 "jnz        1f                    ;\n\t"
17                 "xorl        %[nRet], %[nRet]    ;\n\t"
18                 "subl        $1, %[nRet]         ;\n\t"
19                 "1:"
20                 "addl        $1, %[nRet]         ;\n\t"
21                 : [nRet] "=a" (nRet)
22                 : [nArg] "r" (nArg)
23                 : "cc"
24                 );
25             return nRet;
26         }
27
28 #        define cds_bitop_msb32nz_DEFINED
29         static inline int msb32nz( uint32_t nArg )
30         {
31             assert( nArg != 0 );
32             int        nRet;
33             __asm__ __volatile__ (
34                 "bsrl        %[nArg], %[nRet]    ;"
35                 : [nRet] "=a" (nRet)
36                 : [nArg] "r" (nArg)
37                 : "cc"
38                 );
39             return nRet;
40         }
41
42         // LSB - return index (0..31) of least significant bit in nArg. If nArg == 0 return -1U
43 #        define cds_bitop_lsb32_DEFINED
44         static inline int lsb32( uint32_t nArg )
45         {
46
47             int        nRet;
48             __asm__ __volatile__ (
49                 "bsfl        %[nArg], %[nRet]     ;"
50                 "jnz        1f        ;"
51                 "xorl        %[nRet], %[nRet]    ;"
52                 "subl        $1, %[nRet]         ;"
53                 "1:"
54                 "addl        $1, %[nRet]         ;"
55                 : [nRet] "=a" (nRet)
56                 : [nArg] "r" (nArg)
57                 : "cc"
58                 );
59             return nRet;
60
61         }
62
63         // LSB - return index (0..31) of least significant bit in nArg.
64         // Condition: nArg != 0
65 #        define cds_bitop_lsb32nz_DEFINED
66         static inline int lsb32nz( uint32_t nArg )
67         {
68             assert( nArg != 0 );
69             int        nRet;
70             __asm__ __volatile__ (
71                 "bsfl        %[nArg], %[nRet]    ;"
72                 : [nRet] "=a" (nRet)
73                 : [nArg] "r" (nArg)
74                 : "cc"
75                 );
76             return nRet;
77         }
78
79 #        define cds_bitop_msb64_DEFINED
80         static inline int msb64( atomic64u_unaligned nArg )
81         {
82             atomic64u_unaligned        nRet;
83             asm volatile (
84                 "bsrq        %[nArg], %[nRet]     ;\n\t"
85                 "jnz        1f                    ;\n\t"
86                 "xorq        %[nRet], %[nRet]    ;\n\t"
87                 "subq        $1, %[nRet]         ;\n\t"
88                 "1:"
89                 "addq        $1, %[nRet]         ;\n\t"
90                 : [nRet] "=a" (nRet)
91                 : [nArg] "r" (nArg)
92                 : "cc"
93                 );
94             return (int) nRet;
95         }
96
97 #        define cds_bitop_msb64nz_DEFINED
98         static inline int msb64nz( atomic64u_unaligned nArg )
99         {
100             assert( nArg != 0 );
101             atomic64u_unaligned        nRet;
102             __asm__ __volatile__ (
103                 "bsrq        %[nArg], %[nRet]    ;"
104                 : [nRet] "=a" (nRet)
105                 : [nArg] "r" (nArg)
106                 : "cc"
107                 );
108             return (int) nRet;
109         }
110
111         // LSB - return index (0..31) of least significant bit in nArg. If nArg == 0 return -1U
112 #        define cds_bitop_lsb64_DEFINED
113         static inline int lsb64( atomic64u_unaligned nArg )
114         {
115
116             atomic64u_unaligned        nRet;
117             __asm__ __volatile__ (
118                 "bsfq        %[nArg], %[nRet]     ;"
119                 "jnz        1f        ;"
120                 "xorq        %[nRet], %[nRet]    ;"
121                 "subq        $1, %[nRet]         ;"
122                 "1:"
123                 "addq        $1, %[nRet]         ;"
124                 : [nRet] "=a" (nRet)
125                 : [nArg] "r" (nArg)
126                 : "cc"
127                 );
128             return (int) nRet;
129
130         }
131
132         // LSB - return index (0..31) of least significant bit in nArg.
133         // Condition: nArg != 0
134 #        define cds_bitop_lsb64nz_DEFINED
135         static inline int lsb64nz( atomic64u_unaligned nArg )
136         {
137             assert( nArg != 0 );
138             atomic64u_unaligned        nRet;
139             __asm__ __volatile__ (
140                 "bsfq        %[nArg], %[nRet]    ;"
141                 : [nRet] "=a" (nRet)
142                 : [nArg] "r" (nArg)
143                 : "cc"
144                 );
145             return (int) nRet;
146         }
147
148
149     }} // namespace gcc::amd64
150
151     using namespace gcc::amd64;
152
153     }}}    // namespace cds::bitop::platform
154
155 //@endcond
156
157 #endif // #ifndef __CDS_COMPILER_GCC_AMD64_BITOP_H