15dd12b3edb6a580c44bcd4f1c08c59cd9cf7431
[libcds.git] / cds / compiler / vc / amd64 / bitop.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_COMPILER_VC_AMD64_BITOP_H
32 #define CDSLIB_COMPILER_VC_AMD64_BITOP_H
33
34 #if _MSC_VER == 1500
35     /*
36         VC 2008 bug:
37             math.h(136) : warning C4985: 'ceil': attributes not present on previous declaration.
38             intrin.h(142) : see declaration of 'ceil'
39
40         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
41     */
42 #   pragma warning(push)
43 #   pragma warning(disable: 4985)
44 #   include <intrin.h>
45 #   pragma warning(pop)
46 #else
47 #   include <intrin.h>
48 #endif
49
50 #pragma intrinsic(_BitScanReverse)
51 #pragma intrinsic(_BitScanForward)
52 #pragma intrinsic(_BitScanReverse64)
53 #pragma intrinsic(_BitScanForward64)
54
55 //@cond none
56 namespace cds {
57     namespace bitop { namespace platform { namespace vc { namespace amd64 {
58
59         // MSB - return index (1..32) of most significant bit in nArg. If nArg == 0 return 0
60 #        define cds_bitop_msb32_DEFINED
61         static inline int msb32( uint32_t nArg )
62         {
63             unsigned long nIndex;
64             if ( _BitScanReverse( &nIndex, nArg ))
65                 return (int) nIndex + 1;
66             return 0;
67         }
68
69 #        define cds_bitop_msb32nz_DEFINED
70         static inline int msb32nz( uint32_t nArg )
71         {
72             assert( nArg != 0 );
73             unsigned long nIndex;
74             _BitScanReverse( &nIndex, nArg );
75             return (int) nIndex;
76         }
77
78         // LSB - return index (1..32) of least significant bit in nArg. If nArg == 0 return -1U
79 #        define cds_bitop_lsb32_DEFINED
80         static inline int lsb32( uint32_t nArg )
81         {
82             unsigned long nIndex;
83             if ( _BitScanForward( &nIndex, nArg ))
84                 return (int) nIndex + 1;
85             return 0;
86         }
87
88 #        define cds_bitop_lsb32nz_DEFINED
89         static inline int lsb32nz( uint32_t nArg )
90         {
91             assert( nArg != 0 );
92             unsigned long nIndex;
93             _BitScanForward( &nIndex, nArg );
94             return (int) nIndex;
95         }
96
97
98 #        define cds_bitop_msb64_DEFINED
99         static inline int msb64( uint64_t nArg )
100         {
101             unsigned long nIndex;
102             if ( _BitScanReverse64( &nIndex, nArg ))
103                 return (int) nIndex + 1;
104             return 0;
105         }
106
107 #        define cds_bitop_msb64nz_DEFINED
108         static inline int msb64nz( uint64_t nArg )
109         {
110             assert( nArg != 0 );
111             unsigned long nIndex;
112             _BitScanReverse64( &nIndex, nArg );
113             return (int) nIndex;
114         }
115
116 #        define cds_bitop_lsb64_DEFINED
117         static inline int lsb64( uint64_t nArg )
118         {
119             unsigned long nIndex;
120             if ( _BitScanForward64( &nIndex, nArg ))
121                 return (int) nIndex + 1;
122             return 0;
123         }
124
125 #        define cds_bitop_lsb64nz_DEFINED
126         static inline int lsb64nz( uint64_t nArg )
127         {
128             assert( nArg != 0 );
129             unsigned long nIndex;
130             _BitScanForward64( &nIndex, nArg );
131             return (int) nIndex;
132         }
133
134 #       define cds_bitop_complement32_DEFINED
135         static inline bool complement32( uint32_t * pArg, unsigned int nBit )
136         {
137             return _bittestandcomplement( reinterpret_cast<long *>( pArg ), nBit ) != 0;
138         }
139
140 #       define cds_bitop_complement64_DEFINED
141         static inline bool complement64( uint64_t * pArg, unsigned int nBit )
142         {
143             return _bittestandcomplement64( reinterpret_cast<__int64 *>( pArg ), nBit ) != 0;
144         }
145
146
147     }} // namespace vc::amd64
148
149     using namespace vc::amd64;
150
151 }}}    // namespace cds::bitop::platform
152 //@endcond
153
154 #endif    // #ifndef CDSLIB_COMPILER_VC_AMD64_BITOP_H