MySQL 8.0.39
Source Code Documentation
my_bit.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2007, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef MY_BIT_INCLUDED
26#define MY_BIT_INCLUDED
27
28/**
29 @file include/my_bit.h
30 Some useful bit functions.
31*/
32
33#include <sys/types.h>
34
35#include "my_config.h"
36#include "my_inttypes.h"
37#include "my_macros.h"
38
39extern const char _my_bits_nbits[256];
40extern const uchar _my_bits_reverse_table[256];
41
42/*
43 Find smallest X in 2^X >= value
44 This can be used to divide a number with value by doing a shift instead
45*/
46
47static inline uint my_bit_log2(ulong value) {
48 uint bit;
49 for (bit = 0; value > 1; value >>= 1, bit++)
50 ;
51 return bit;
52}
53
54static inline uint my_count_bits(ulonglong v) {
55#if SIZEOF_LONG_LONG > 4
56 /* The following code is a bit faster on 16 bit machines than if we would
57 only shift v */
58 ulong v2 = (ulong)(v >> 32);
59 return (uint)(uchar)(
60 _my_bits_nbits[(uchar)v] + _my_bits_nbits[(uchar)(v >> 8)] +
61 _my_bits_nbits[(uchar)(v >> 16)] + _my_bits_nbits[(uchar)(v >> 24)] +
62 _my_bits_nbits[(uchar)(v2)] + _my_bits_nbits[(uchar)(v2 >> 8)] +
63 _my_bits_nbits[(uchar)(v2 >> 16)] + _my_bits_nbits[(uchar)(v2 >> 24)]);
64#else
65 return (uint)(uchar)(
66 _my_bits_nbits[(uchar)v] + _my_bits_nbits[(uchar)(v >> 8)] +
67 _my_bits_nbits[(uchar)(v >> 16)] + _my_bits_nbits[(uchar)(v >> 24)]);
68#endif
69}
70
72 return (uint)(uchar)(
73 _my_bits_nbits[(uchar)v] + _my_bits_nbits[(uchar)(v >> 8)] +
74 _my_bits_nbits[(uchar)(v >> 16)] + _my_bits_nbits[(uchar)(v >> 24)]);
75}
76
77/*
78 Next highest power of two
79
80 SYNOPSIS
81 my_round_up_to_next_power()
82 v Value to check
83
84 RETURN
85 Next or equal power of 2
86 Note: 0 will return 0
87
88 NOTES
89 Algorithm by Sean Anderson, according to:
90 http://graphics.stanford.edu/~seander/bithacks.html
91 (Original code public domain)
92
93 Comments shows how this works with 01100000000000000000000000001011
94*/
95
97 v--; /* 01100000000000000000000000001010 */
98 v |= v >> 1; /* 01110000000000000000000000001111 */
99 v |= v >> 2; /* 01111100000000000000000000001111 */
100 v |= v >> 4; /* 01111111110000000000000000001111 */
101 v |= v >> 8; /* 01111111111111111100000000001111 */
102 v |= v >> 16; /* 01111111111111111111111111111111 */
103 return v + 1; /* 10000000000000000000000000000000 */
104}
105
107 uint32 w = v >> 1;
108 w |= w >> 1;
109 w |= w >> 2;
110 w |= w >> 4;
111 w |= w >> 8;
112 w |= w >> 16;
113 return v & w;
114}
115
117 return (_my_bits_reverse_table[key & 255] << 24) |
118 (_my_bits_reverse_table[(key >> 8) & 255] << 16) |
119 (_my_bits_reverse_table[(key >> 16) & 255] << 8) |
121}
122
123/**
124 Determine if a single bit is set among some bits.
125 @tparam IntType an integer type
126 @param bits the bits to examine
127 @retval true if bits equals to a power of 2
128 @retval false otherwise
129*/
130
131template <typename IntType>
132constexpr bool is_single_bit(IntType bits) {
133 /*
134 Proof of correctness:
135 (1) is_single_bit(0)==false is left as an exercise to the reader.
136 (2) is_single_bit(1)==true is left as an exercise to the reader.
137 (3) is_single_bit(1<<(N+1))==true because the most significant set bit
138 in (bits - 1) would be 1<<N, and obviously (bits & (bits - 1)) == 0.
139 (4) In all other cases, is_single_bit(bits)==false.
140 In these cases, we must have multiple bits set, that is,
141 bits==m|1<<N such that N>=0 and m!=0 and the least significant
142 bit that is set in m is greater than 1<<N. In this case,
143 (bits-1)==m|((1<<N)-1), and (bits&(bits-1))==m, which we defined to be
144 nonzero. So, m==0 will not hold.
145
146 Note: The above proof (3),(4) is applicable also to the case where
147 IntType is signed using two's complement arithmetic, and the most
148 significant bit is set, or in other words, bits<0.
149 */
150 return bits != 0 && (bits & (bits - 1)) == 0;
151}
152
153#endif /* MY_BIT_INCLUDED */
static uint32 my_round_up_to_next_power(uint32 v)
Definition: my_bit.h:96
static uint my_bit_log2(ulong value)
Definition: my_bit.h:47
static uint my_count_bits(ulonglong v)
Definition: my_bit.h:54
static uint32 my_reverse_bits(uint32 key)
Definition: my_bit.h:116
const uchar _my_bits_reverse_table[256]
Definition: my_bit.cc:52
static uint my_count_bits_uint32(uint32 v)
Definition: my_bit.h:71
const char _my_bits_nbits[256]
Definition: my_bit.cc:35
constexpr bool is_single_bit(IntType bits)
Determine if a single bit is set among some bits.
Definition: my_bit.h:132
static uint32 my_clear_highest_bit(uint32 v)
Definition: my_bit.h:106
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
uint32_t uint32
Definition: my_inttypes.h:67
Some common macros.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
unsigned int uint
Definition: uca9-dump.cc:75