1717#ifndef MY_BIT_INCLUDED
1818#define MY_BIT_INCLUDED
1919
20+ #ifdef __cplusplus
21+ extern "C" {
22+ #else
23+ #define constexpr
24+ #endif
25+
2026/*
2127 Some useful bit functions
2228*/
2329
24- C_MODE_START
25-
26- extern const uchar _my_bits_reverse_table [256 ];
30+ extern const unsigned char _my_bits_reverse_table [256 ];
2731
2832
2933/*
@@ -43,42 +47,42 @@ extern const uchar _my_bits_reverse_table[256];
4347 Let's return 0 for the input 0, for the code simplicity.
4448 See the 000x branch. It covers both (1<<0) and 0.
4549*/
46- static inline CONSTEXPR uint my_bit_log2_hex_digit (uint8 value )
50+ static inline constexpr unsigned int my_bit_log2_hex_digit (unsigned char value )
4751{
4852 return value & 0x0C ? /*1100*/ (value & 0x08 ? /*1000*/ 3 : /*0100*/ 2 ) :
4953 /*0010*/ (value & 0x02 ? /*0010*/ 1 : /*000x*/ 0 );
5054}
51- static inline CONSTEXPR uint my_bit_log2_uint8 (uint8 value )
55+ static inline constexpr unsigned int my_bit_log2_uint8 (unsigned char value )
5256{
53- return value & 0xF0 ? my_bit_log2_hex_digit ((uint8 ) (value >> 4 )) + 4 :
57+ return value & 0xF0 ? my_bit_log2_hex_digit ((unsigned char ) (value >> 4 )) + 4 :
5458 my_bit_log2_hex_digit (value );
5559}
56- static inline CONSTEXPR uint my_bit_log2_uint16 (uint16 value )
60+ static inline constexpr unsigned int my_bit_log2_uint16 (unsigned short value )
5761{
58- return value & 0xFF00 ? my_bit_log2_uint8 ((uint8 ) (value >> 8 )) + 8 :
59- my_bit_log2_uint8 ((uint8 ) value );
62+ return value & 0xFF00 ? my_bit_log2_uint8 ((unsigned char ) (value >> 8 )) + 8 :
63+ my_bit_log2_uint8 ((unsigned char ) value );
6064}
61- static inline CONSTEXPR uint my_bit_log2_uint32 (uint32 value )
65+ static inline constexpr unsigned int my_bit_log2_uint32 (unsigned int value )
6266{
6367 return value & 0xFFFF0000UL ?
64- my_bit_log2_uint16 ((uint16 ) (value >> 16 )) + 16 :
65- my_bit_log2_uint16 ((uint16 ) value );
68+ my_bit_log2_uint16 ((unsigned short ) (value >> 16 )) + 16 :
69+ my_bit_log2_uint16 ((unsigned short ) value );
6670}
67- static inline CONSTEXPR uint my_bit_log2_uint64 (ulonglong value )
71+ static inline constexpr unsigned int my_bit_log2_uint64 (unsigned long long int value )
6872{
6973 return value & 0xFFFFFFFF00000000ULL ?
70- my_bit_log2_uint32 ((uint32 ) (value >> 32 )) + 32 :
71- my_bit_log2_uint32 ((uint32 ) value );
74+ my_bit_log2_uint32 ((unsigned int ) (value >> 32 )) + 32 :
75+ my_bit_log2_uint32 ((unsigned int ) value );
7276}
73- static inline CONSTEXPR uint my_bit_log2_size_t (size_t value )
77+ static inline constexpr unsigned int my_bit_log2_size_t (size_t value )
7478{
7579#ifdef __cplusplus
76- static_assert (sizeof (size_t ) <= sizeof (ulonglong ),
77- "size_t <= ulonglong is an assumption that needs to be fixed "
80+ static_assert (sizeof (size_t ) <= sizeof (unsigned long long int ),
81+ "size_t <= unsigned long long int is an assumption that needs to be fixed "
7882 "for this architecture. Please create an issue on "
7983 "https://jira.mariadb.org" );
8084#endif
81- return my_bit_log2_uint64 ((ulonglong ) value );
85+ return my_bit_log2_uint64 ((unsigned long long int ) value );
8286}
8387
8488
@@ -91,17 +95,17 @@ Count bits in 32bit integer
9195
9296 (Original code public domain).
9397*/
94- static inline uint my_count_bits_uint32 (uint32 v )
98+ static inline unsigned int my_count_bits_uint32 (unsigned int v )
9599{
96100 v = v - ((v >> 1 ) & 0x55555555 );
97101 v = (v & 0x33333333 ) + ((v >> 2 ) & 0x33333333 );
98102 return (((v + (v >> 4 )) & 0xF0F0F0F ) * 0x1010101 ) >> 24 ;
99103}
100104
101105
102- static inline uint my_count_bits (ulonglong x )
106+ static inline unsigned int my_count_bits (unsigned long long int x )
103107{
104- return my_count_bits_uint32 ((uint32 )x ) + my_count_bits_uint32 ((uint32 )(x >> 32 ));
108+ return my_count_bits_uint32 ((unsigned int )x ) + my_count_bits_uint32 ((unsigned int )(x >> 32 ));
105109}
106110
107111
@@ -126,7 +130,7 @@ static inline uint my_count_bits(ulonglong x)
126130 Comments shows how this works with 01100000000000000000000000001011
127131*/
128132
129- static inline uint32 my_round_up_to_next_power (uint32 v )
133+ static inline unsigned int my_round_up_to_next_power (unsigned int v )
130134{
131135 v -- ; /* 01100000000000000000000000001010 */
132136 v |= v >> 1 ; /* 01110000000000000000000000001111 */
@@ -137,9 +141,9 @@ static inline uint32 my_round_up_to_next_power(uint32 v)
137141 return v + 1 ; /* 10000000000000000000000000000000 */
138142}
139143
140- static inline uint32 my_clear_highest_bit (uint32 v )
144+ static inline unsigned int my_clear_highest_bit (unsigned int v )
141145{
142- uint32 w = v >> 1 ;
146+ unsigned int w = v >> 1 ;
143147 w |= w >> 1 ;
144148 w |= w >> 2 ;
145149 w |= w >> 4 ;
@@ -148,34 +152,34 @@ static inline uint32 my_clear_highest_bit(uint32 v)
148152 return v & w ;
149153}
150154
151- static inline uint32 my_reverse_bits (uint32 key )
155+ static inline unsigned int my_reverse_bits (unsigned int key )
152156{
153157 return
154- ((uint32 )_my_bits_reverse_table [ key & 255 ] << 24 ) |
155- ((uint32 )_my_bits_reverse_table [(key >> 8 ) & 255 ] << 16 ) |
156- ((uint32 )_my_bits_reverse_table [(key >>16 ) & 255 ] << 8 ) |
157- (uint32 )_my_bits_reverse_table [(key >>24 ) ];
158+ ((unsigned int )_my_bits_reverse_table [ key & 255 ] << 24 ) |
159+ ((unsigned int )_my_bits_reverse_table [(key >> 8 ) & 255 ] << 16 ) |
160+ ((unsigned int )_my_bits_reverse_table [(key >>16 ) & 255 ] << 8 ) |
161+ (unsigned int )_my_bits_reverse_table [(key >>24 ) ];
158162}
159163
160164/*
161165 a number with the n lowest bits set
162166 an overflow-safe version of (1 << n) - 1
163167*/
164- static inline uint64 my_set_bits (int n )
168+ static inline unsigned long long int my_set_bits (int n )
165169{
166170 return (((1ULL << (n - 1 )) - 1 ) << 1 ) | 1 ;
167171}
168172
169173/* Create a mask of the significant bits for the last byte (1,3,7,..255) */
170- static inline uchar last_byte_mask (uint bits )
174+ static inline unsigned char last_byte_mask (unsigned int bits )
171175{
172176 /* Get the number of used bits-1 (0..7) in the last byte */
173177 unsigned int const used = (bits - 1U ) & 7U ;
174178 /* Return bitmask for the significant bits */
175- return (uchar ) ((2U << used ) - 1 );
179+ return (unsigned char ) ((2U << used ) - 1 );
176180}
177181
178- static inline uint my_bits_in_bytes (uint n )
182+ static inline unsigned int my_bits_in_bytes (unsigned int n )
179183{
180184 return ((n + 7 ) / 8 );
181185}
@@ -188,7 +192,7 @@ static inline uint my_bits_in_bytes(uint n)
188192 Find the position of the first(least significant) bit set in
189193 the argument. Returns 64 if the argument was 0.
190194*/
191- static inline uint my_find_first_bit (ulonglong n )
195+ static inline unsigned int my_find_first_bit (unsigned long long int n )
192196{
193197 if (!n )
194198 return 64 ;
@@ -197,9 +201,9 @@ static inline uint my_find_first_bit(ulonglong n)
197201#elif defined(_MSC_VER )
198202#if defined(_M_IX86 )
199203 unsigned long bit ;
200- if ( _BitScanForward (& bit , (uint )n ))
204+ if ( _BitScanForward (& bit , (unsigned int )n ))
201205 return bit ;
202- _BitScanForward (& bit , (uint )(n >>32 ));
206+ _BitScanForward (& bit , (unsigned int )(n >>32 ));
203207 return bit + 32 ;
204208#else
205209 unsigned long bit ;
@@ -208,18 +212,23 @@ static inline uint my_find_first_bit(ulonglong n)
208212#endif
209213#else
210214 /* Generic case */
211- uint shift = 0 ;
212- static const uchar last_bit [16 ] = { 32 , 0 , 1 , 0 ,
215+ unsigned int shift = 0 ;
216+ static const unsigned char last_bit [16 ] = { 32 , 0 , 1 , 0 ,
213217 2 , 0 , 1 , 0 ,
214218 3 , 0 , 1 , 0 ,
215219 2 , 0 , 1 , 0 };
216- uint bit ;
220+ unsigned int bit ;
217221 while ((bit = last_bit [(n >> shift ) & 0xF ]) == 32 )
218222 shift += 4 ;
219223 return shift + bit ;
220224#endif
221225}
222- C_MODE_END
226+
227+ #ifdef __cplusplus
228+ }
229+ #else
230+ #undef constexpr
231+ #endif
223232
224233/*
225234The helper function my_nlz(x) calculates the number of leading zeros
@@ -289,4 +298,4 @@ inline unsigned int my_nlz (unsigned long long x)
289298}
290299#endif
291300
292- #endif /* MY_BIT_INCLUDED */
301+ #endif /* MY_BIT_INCLUDED */
0 commit comments