Implemented support for ARMv8 (64 bit arm)
[libcds.git] / cds / algo / int_algo.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_INT_ALGO_H
32 #define CDSLIB_INT_ALGO_H
33
34 #include <cds/algo/bitop.h>
35
36 namespace cds { namespace beans {
37 #if CDS_BUILD_BITS == 64
38     /// Returns largest previous integer for <tt>log2( n )</tt>
39     static inline size_t log2floor( size_t n )
40     {
41         return n ? cds::bitop::MSBnz( n ) : 0;
42     }
43
44     /// Returns smallest following integer for <tt>log2( n )</tt>
45     static inline size_t log2ceil( size_t n )
46     {
47         size_t i = log2floor( n );
48         return ( size_t( 1 ) << i ) < n ? i + 1 : i;
49     }
50
51     /// Returns largest previous power of 2 for \p n
52     /**
53         Examples:
54         \code
55         floor2(0) == 1   // !!!
56         floor2(1) == 1
57         floor2(2) == 2
58         floor2(3) == 2
59         floor2(4) == 4
60         floor2(15) == 8
61         floor2(16) == 16
62         floor2(17) == 16
63         \endcode
64     */
65     static inline size_t floor2( size_t n )
66     {
67         return size_t(1) << log2floor( n );
68     }
69
70     /// Returns smallest following power of 2 for \p n
71     /**
72         Examples:
73         \code
74         ceil2(0) == 1   // !!!
75         ceil2(1) == 1
76         ceil2(2) == 2
77         ceil2(3) == 4
78         ceil2(4) == 4
79         ceil2(15) == 16
80         ceil2(16) == 16
81         ceil2(17) == 32
82         \endcode
83     */
84     static inline size_t ceil2( size_t n )
85     {
86         return size_t(1) << log2ceil( n );
87     }
88
89     /// Checks if \p n is power of 2
90     CDS_CONSTEXPR static inline bool is_power2( size_t n ) CDS_NOEXCEPT
91     {
92         return (n & (n - 1)) == 0 && n;
93     }
94
95     /// Returns binary logarithm of \p n if \p n is power of two, otherwise returns 0
96     static inline size_t log2( size_t n )
97     {
98         return is_power2(n) ? log2floor(n) : 0;
99     }
100
101 #elif CDS_BUILD_BITS == 32
102     //@cond
103     // 64bit specializations
104
105 /// Returns largest previous integer for <tt>log2( n )</tt>
106     static inline uint64_t log2floor( uint64_t n )
107     {
108         return n ? cds::bitop::MSBnz( n ) : 0;
109     }
110
111 /// Returns smallest following integer for <tt>log2( n )</tt>
112     static inline uint64_t log2ceil( uint64_t n )
113     {
114         uint64_t i = log2floor( n );
115         return (uint64_t( 1 ) << i) < n ? i + 1 : i;
116     }
117
118 /// Returns largest previous power of 2 for \p n
119     /**
120         Examples:
121         \code
122         floor2(0) == 1   // !!!
123         floor2(1) == 1
124         floor2(2) == 2
125         floor2(3) == 2
126         floor2(4) == 4
127         floor2(15) == 8
128         floor2(16) == 16
129         floor2(17) == 16
130         \endcode
131     */
132     static inline uint64_t floor2( uint64_t n )
133     {
134         return uint64_t( 1 ) << log2floor( n );
135     }
136
137 /// Returns smallest following power of 2 for \p n
138     /**
139         Examples:
140         \code
141         ceil2(0) == 1   // !!!
142         ceil2(1) == 1
143         ceil2(2) == 2
144         ceil2(3) == 4
145         ceil2(4) == 4
146         ceil2(15) == 16
147         ceil2(16) == 16
148         ceil2(17) == 32
149         \endcode
150     */
151     static inline uint64_t ceil2( uint64_t n )
152     {
153         return uint64_t( 1 ) << log2ceil( n );
154     }
155
156 /// Checks if \p n is power of 2
157     CDS_CONSTEXPR static inline bool is_power2( uint64_t n ) CDS_NOEXCEPT
158     {
159         return (n & (n - 1)) == 0 && n;
160     }
161
162 /// Returns binary logarithm of \p n if \p n is power of two, otherwise returns 0
163     static inline uint64_t log2( uint64_t n )
164     {
165         return is_power2( n ) ? log2floor( n ) : 0;
166     }
167
168     //@endcond
169 #endif
170
171 }}   // namespace cds::beans
172
173 #endif  // #ifndef CDSLIB_INT_ALGO_H