Replacing some integral typedefs with standard types
[libcds.git] / cds / compiler / vc / x86 / bitop.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_COMPILER_VC_X86_BITOP_H
4 #define __CDS_COMPILER_VC_X86_BITOP_H
5
6 #include <intrin.h>
7 #pragma intrinsic(_BitScanReverse)
8 #pragma intrinsic(_BitScanForward)
9
10 //@cond none
11 namespace cds {
12     namespace bitop { namespace platform { namespace vc { namespace x86 {
13         // MSB - return index (1..32) of most significant bit in nArg. If nArg == 0 return 0
14 #        define cds_bitop_msb32_DEFINED
15         static inline int msb32( uint32_t nArg )
16         {
17             unsigned long nIndex;
18             if ( _BitScanReverse( &nIndex, nArg ))
19                 return (int) nIndex + 1;
20             return 0;
21         }
22
23 #        define cds_bitop_msb32nz_DEFINED
24         static inline int msb32nz( uint32_t nArg )
25         {
26             assert( nArg != 0 );
27             unsigned long nIndex;
28             _BitScanReverse( &nIndex, nArg );
29             return (int) nIndex;
30         }
31
32         // LSB - return index (1..32) of least significant bit in nArg. If nArg == 0 return -1U
33 #        define cds_bitop_lsb32_DEFINED
34         static inline int lsb32( uint32_t nArg )
35         {
36             unsigned long nIndex;
37             if ( _BitScanForward( &nIndex, nArg ))
38                 return (int) nIndex + 1;
39             return 0;
40         }
41
42 #        define cds_bitop_lsb32nz_DEFINED
43         static inline int lsb32nz( uint32_t nArg )
44         {
45             assert( nArg != 0 );
46             unsigned long nIndex;
47             _BitScanForward( &nIndex, nArg );
48             return (int) nIndex;
49         }
50
51         // bswap - Reverses the byte order of a 32-bit word
52 #        define cds_bitop_bswap32_DEFINED
53         static inline uint32_t bswap32( uint32_t nArg )
54         {
55             __asm {
56                 mov    eax, nArg;
57                 bswap    eax;
58             }
59         }
60
61 #       define cds_bitop_complement32_DEFINED
62         static inline bool complement32( uint32_t * pArg, unsigned int nBit )
63         {
64             return _bittestandcomplement( reinterpret_cast<long *>( pArg ), nBit ) != 0;
65         }
66
67 #       define cds_bitop_complement64_DEFINED
68         static inline bool complement64( atomic64u_t * pArg, unsigned int nBit )
69         {
70             if ( nBit < 32 )
71                 return _bittestandcomplement( reinterpret_cast<long *>( pArg ), nBit ) != 0;
72             else
73                 return _bittestandcomplement( reinterpret_cast<long *>( pArg ) + 1, nBit - 32 ) != 0;
74         }
75
76     }} // namespace vc::x86
77
78     using namespace vc::x86;
79
80 }}}    // namespace cds::bitop::platform
81 //@endcond
82
83 #endif    // #ifndef __CDS_COMPILER_VC_X86_BITOP_H