ee143d6f890c59d58c2da36c301a5575c2e3a4f9
[libcds.git] / cds / compiler / vc / amd64 / bitop.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_COMPILER_VC_AMD64_BITOP_H
4 #define __CDS_COMPILER_VC_AMD64_BITOP_H
5
6 #if _MSC_VER == 1500
7     /*
8         VC 2008 bug:
9             math.h(136) : warning C4985: 'ceil': attributes not present on previous declaration.
10             intrin.h(142) : see declaration of 'ceil'
11
12         See http://connect.microsoft.com/VisualStudio/feedback/details/381422/warning-of-attributes-not-present-on-previous-declaration-on-ceil-using-both-math-h-and-intrin-h
13     */
14 #   pragma warning(push)
15 #   pragma warning(disable: 4985)
16 #   include <intrin.h>
17 #   pragma warning(pop)
18 #else
19 #   include <intrin.h>
20 #endif
21
22 #pragma intrinsic(_BitScanReverse)
23 #pragma intrinsic(_BitScanForward)
24 #pragma intrinsic(_BitScanReverse64)
25 #pragma intrinsic(_BitScanForward64)
26
27 //@cond none
28 namespace cds {
29     namespace bitop { namespace platform { namespace vc { namespace amd64 {
30
31         // MSB - return index (1..32) of most significant bit in nArg. If nArg == 0 return 0
32 #        define cds_bitop_msb32_DEFINED
33         static inline int msb32( atomic32u_t nArg )
34         {
35             unsigned long nIndex;
36             if ( _BitScanReverse( &nIndex, nArg ))
37                 return (int) nIndex + 1;
38             return 0;
39         }
40
41 #        define cds_bitop_msb32nz_DEFINED
42         static inline int msb32nz( atomic32u_t nArg )
43         {
44             assert( nArg != 0 );
45             unsigned long nIndex;
46             _BitScanReverse( &nIndex, nArg );
47             return (int) nIndex;
48         }
49
50         // LSB - return index (1..32) of least significant bit in nArg. If nArg == 0 return -1U
51 #        define cds_bitop_lsb32_DEFINED
52         static inline int lsb32( atomic32u_t nArg )
53         {
54             unsigned long nIndex;
55             if ( _BitScanForward( &nIndex, nArg ))
56                 return (int) nIndex + 1;
57             return 0;
58         }
59
60 #        define cds_bitop_lsb32nz_DEFINED
61         static inline int lsb32nz( atomic32u_t nArg )
62         {
63             assert( nArg != 0 );
64             unsigned long nIndex;
65             _BitScanForward( &nIndex, nArg );
66             return (int) nIndex;
67         }
68
69
70 #        define cds_bitop_msb64_DEFINED
71         static inline int msb64( atomic64u_unaligned nArg )
72         {
73             unsigned long nIndex;
74             if ( _BitScanReverse64( &nIndex, nArg ))
75                 return (int) nIndex + 1;
76             return 0;
77         }
78
79 #        define cds_bitop_msb64nz_DEFINED
80         static inline int msb64nz( atomic64u_unaligned nArg )
81         {
82             assert( nArg != 0 );
83             unsigned long nIndex;
84             _BitScanReverse64( &nIndex, nArg );
85             return (int) nIndex;
86         }
87
88 #        define cds_bitop_lsb64_DEFINED
89         static inline int lsb64( atomic64u_unaligned nArg )
90         {
91             unsigned long nIndex;
92             if ( _BitScanForward64( &nIndex, nArg ))
93                 return (int) nIndex + 1;
94             return 0;
95         }
96
97 #        define cds_bitop_lsb64nz_DEFINED
98         static inline int lsb64nz( atomic64u_unaligned nArg )
99         {
100             assert( nArg != 0 );
101             unsigned long nIndex;
102             _BitScanForward64( &nIndex, nArg );
103             return (int) nIndex;
104         }
105
106 #       define cds_bitop_complement32_DEFINED
107         static inline bool complement32( atomic32u_t * pArg, unsigned int nBit )
108         {
109             return _bittestandcomplement( reinterpret_cast<long *>( pArg ), nBit ) != 0;
110         }
111
112 #       define cds_bitop_complement64_DEFINED
113         static inline bool complement64( atomic64u_t * pArg, unsigned int nBit )
114         {
115             return _bittestandcomplement64( reinterpret_cast<__int64 *>( pArg ), nBit ) != 0;
116         }
117
118
119     }} // namespace vc::amd64
120
121     using namespace vc::amd64;
122
123 }}}    // namespace cds::bitop::platform
124 //@endcond
125
126 #endif    // #ifndef __CDS_COMPILER_VC_AMD64_BITOP_H