1717#ifndef MY_BIT_INCLUDED
1818#define MY_BIT_INCLUDED
1919
20+ /* C vs C++ */
21+ #ifdef __cplusplus
22+ #define MY_BIT_CONSTEXPR constexpr
23+ #else
24+ #define MY_BIT_CONSTEXPR
25+ #endif /* __cplusplus */
26+
27+ /* C VS C++ */
28+ #ifdef __cplusplus
29+ #define MY_BIT_C_MODE_START extern "C" {
30+ #define MY_BIT_C_MODE_END }
31+ #else
32+ #define MY_BIT_C_MODE_START
33+ #define MY_BIT_C_MODE_END
34+ #endif
35+
2036/*
2137 Some useful bit functions
2238*/
2339
24- C_MODE_START
40+ MY_BIT_C_MODE_START
2541
26- extern const uchar _my_bits_reverse_table [256 ];
42+ extern const unsigned char _my_bits_reverse_table [256 ];
2743
2844
2945/*
@@ -43,42 +59,42 @@ extern const uchar _my_bits_reverse_table[256];
4359 Let's return 0 for the input 0, for the code simplicity.
4460 See the 000x branch. It covers both (1<<0) and 0.
4561*/
46- static inline CONSTEXPR uint my_bit_log2_hex_digit (uint8 value )
62+ static inline MY_BIT_CONSTEXPR unsigned int my_bit_log2_hex_digit (unsigned char value )
4763{
4864 return value & 0x0C ? /*1100*/ (value & 0x08 ? /*1000*/ 3 : /*0100*/ 2 ) :
4965 /*0010*/ (value & 0x02 ? /*0010*/ 1 : /*000x*/ 0 );
5066}
51- static inline CONSTEXPR uint my_bit_log2_uint8 (uint8 value )
67+ static inline MY_BIT_CONSTEXPR unsigned int my_bit_log2_uint8 (unsigned char value )
5268{
53- return value & 0xF0 ? my_bit_log2_hex_digit ((uint8 ) (value >> 4 )) + 4 :
69+ return value & 0xF0 ? my_bit_log2_hex_digit ((unsigned char ) (value >> 4 )) + 4 :
5470 my_bit_log2_hex_digit (value );
5571}
56- static inline CONSTEXPR uint my_bit_log2_uint16 (uint16 value )
72+ static inline MY_BIT_CONSTEXPR unsigned int my_bit_log2_uint16 (unsigned short value )
5773{
58- return value & 0xFF00 ? my_bit_log2_uint8 ((uint8 ) (value >> 8 )) + 8 :
59- my_bit_log2_uint8 ((uint8 ) value );
74+ return value & 0xFF00 ? my_bit_log2_uint8 ((unsigned char ) (value >> 8 )) + 8 :
75+ my_bit_log2_uint8 ((unsigned char ) value );
6076}
61- static inline CONSTEXPR uint my_bit_log2_uint32 (uint32 value )
77+ static inline MY_BIT_CONSTEXPR unsigned int my_bit_log2_uint32 (unsigned int value )
6278{
6379 return value & 0xFFFF0000UL ?
64- my_bit_log2_uint16 ((uint16 ) (value >> 16 )) + 16 :
65- my_bit_log2_uint16 ((uint16 ) value );
80+ my_bit_log2_uint16 ((unsigned short ) (value >> 16 )) + 16 :
81+ my_bit_log2_uint16 ((unsigned short ) value );
6682}
67- static inline CONSTEXPR uint my_bit_log2_uint64 (ulonglong value )
83+ static inline MY_BIT_CONSTEXPR unsigned int my_bit_log2_uint64 (unsigned long long int value )
6884{
6985 return value & 0xFFFFFFFF00000000ULL ?
70- my_bit_log2_uint32 ((uint32 ) (value >> 32 )) + 32 :
71- my_bit_log2_uint32 ((uint32 ) value );
86+ my_bit_log2_uint32 ((unsigned int ) (value >> 32 )) + 32 :
87+ my_bit_log2_uint32 ((unsigned int ) value );
7288}
73- static inline CONSTEXPR uint my_bit_log2_size_t (size_t value )
89+ static inline MY_BIT_CONSTEXPR unsigned int my_bit_log2_size_t (size_t value )
7490{
7591#ifdef __cplusplus
76- static_assert (sizeof (size_t ) <= sizeof (ulonglong ),
77- "size_t <= ulonglong is an assumption that needs to be fixed "
92+ static_assert (sizeof (size_t ) <= sizeof (unsigned long long int ),
93+ "size_t <= unsigned long long int is an assumption that needs to be fixed "
7894 "for this architecture. Please create an issue on "
7995 "https://jira.mariadb.org" );
8096#endif
81- return my_bit_log2_uint64 ((ulonglong ) value );
97+ return my_bit_log2_uint64 ((unsigned long long int ) value );
8298}
8399
84100
@@ -91,17 +107,17 @@ Count bits in 32bit integer
91107
92108 (Original code public domain).
93109*/
94- static inline uint my_count_bits_uint32 (uint32 v )
110+ static inline unsigned int my_count_bits_uint32 (unsigned int v )
95111{
96112 v = v - ((v >> 1 ) & 0x55555555 );
97113 v = (v & 0x33333333 ) + ((v >> 2 ) & 0x33333333 );
98114 return (((v + (v >> 4 )) & 0xF0F0F0F ) * 0x1010101 ) >> 24 ;
99115}
100116
101117
102- static inline uint my_count_bits (ulonglong x )
118+ static inline unsigned int my_count_bits (unsigned long long int x )
103119{
104- return my_count_bits_uint32 ((uint32 )x ) + my_count_bits_uint32 ((uint32 )(x >> 32 ));
120+ return my_count_bits_uint32 ((unsigned int )x ) + my_count_bits_uint32 ((unsigned int )(x >> 32 ));
105121}
106122
107123
@@ -126,7 +142,7 @@ static inline uint my_count_bits(ulonglong x)
126142 Comments shows how this works with 01100000000000000000000000001011
127143*/
128144
129- static inline uint32 my_round_up_to_next_power (uint32 v )
145+ static inline unsigned int my_round_up_to_next_power (unsigned int v )
130146{
131147 v -- ; /* 01100000000000000000000000001010 */
132148 v |= v >> 1 ; /* 01110000000000000000000000001111 */
@@ -137,9 +153,9 @@ static inline uint32 my_round_up_to_next_power(uint32 v)
137153 return v + 1 ; /* 10000000000000000000000000000000 */
138154}
139155
140- static inline uint32 my_clear_highest_bit (uint32 v )
156+ static inline unsigned int my_clear_highest_bit (unsigned int v )
141157{
142- uint32 w = v >> 1 ;
158+ unsigned int w = v >> 1 ;
143159 w |= w >> 1 ;
144160 w |= w >> 2 ;
145161 w |= w >> 4 ;
@@ -148,34 +164,34 @@ static inline uint32 my_clear_highest_bit(uint32 v)
148164 return v & w ;
149165}
150166
151- static inline uint32 my_reverse_bits (uint32 key )
167+ static inline unsigned int my_reverse_bits (unsigned int key )
152168{
153169 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 ) ];
170+ ((unsigned int )_my_bits_reverse_table [ key & 255 ] << 24 ) |
171+ ((unsigned int )_my_bits_reverse_table [(key >> 8 ) & 255 ] << 16 ) |
172+ ((unsigned int )_my_bits_reverse_table [(key >>16 ) & 255 ] << 8 ) |
173+ (unsigned int )_my_bits_reverse_table [(key >>24 ) ];
158174}
159175
160176/*
161177 a number with the n lowest bits set
162178 an overflow-safe version of (1 << n) - 1
163179*/
164- static inline uint64 my_set_bits (int n )
180+ static inline unsigned long long int my_set_bits (int n )
165181{
166182 return (((1ULL << (n - 1 )) - 1 ) << 1 ) | 1 ;
167183}
168184
169185/* Create a mask of the significant bits for the last byte (1,3,7,..255) */
170- static inline uchar last_byte_mask (uint bits )
186+ static inline unsigned char last_byte_mask (unsigned int bits )
171187{
172188 /* Get the number of used bits-1 (0..7) in the last byte */
173189 unsigned int const used = (bits - 1U ) & 7U ;
174190 /* Return bitmask for the significant bits */
175- return (uchar ) ((2U << used ) - 1 );
191+ return (unsigned char ) ((2U << used ) - 1 );
176192}
177193
178- static inline uint my_bits_in_bytes (uint n )
194+ static inline unsigned int my_bits_in_bytes (unsigned int n )
179195{
180196 return ((n + 7 ) / 8 );
181197}
@@ -188,7 +204,7 @@ static inline uint my_bits_in_bytes(uint n)
188204 Find the position of the first(least significant) bit set in
189205 the argument. Returns 64 if the argument was 0.
190206*/
191- static inline uint my_find_first_bit (ulonglong n )
207+ static inline unsigned int my_find_first_bit (unsigned long long int n )
192208{
193209 if (!n )
194210 return 64 ;
@@ -197,9 +213,9 @@ static inline uint my_find_first_bit(ulonglong n)
197213#elif defined(_MSC_VER )
198214#if defined(_M_IX86 )
199215 unsigned long bit ;
200- if ( _BitScanForward (& bit , (uint )n ))
216+ if ( _BitScanForward (& bit , (unsigned int )n ))
201217 return bit ;
202- _BitScanForward (& bit , (uint )(n >>32 ));
218+ _BitScanForward (& bit , (unsigned int )(n >>32 ));
203219 return bit + 32 ;
204220#else
205221 unsigned long bit ;
@@ -208,18 +224,18 @@ static inline uint my_find_first_bit(ulonglong n)
208224#endif
209225#else
210226 /* Generic case */
211- uint shift = 0 ;
212- static const uchar last_bit [16 ] = { 32 , 0 , 1 , 0 ,
227+ unsigned int shift = 0 ;
228+ static const unsigned char last_bit [16 ] = { 32 , 0 , 1 , 0 ,
213229 2 , 0 , 1 , 0 ,
214230 3 , 0 , 1 , 0 ,
215231 2 , 0 , 1 , 0 };
216- uint bit ;
232+ unsigned int bit ;
217233 while ((bit = last_bit [(n >> shift ) & 0xF ]) == 32 )
218234 shift += 4 ;
219235 return shift + bit ;
220236#endif
221237}
222- C_MODE_END
238+ MY_BIT_C_MODE_END
223239
224240/*
225241The helper function my_nlz(x) calculates the number of leading zeros
0 commit comments