MySQL 8.0.40
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)(_my_bits_nbits[(uchar)v] +
60 _my_bits_nbits[(uchar)(v >> 8)] +
61 _my_bits_nbits[(uchar)(v >> 16)] +
62 _my_bits_nbits[(uchar)(v >> 24)] +
63 _my_bits_nbits[(uchar)(v2)] +
64 _my_bits_nbits[(uchar)(v2 >> 8)] +
65 _my_bits_nbits[(uchar)(v2 >> 16)] +
66 _my_bits_nbits[(uchar)(v2 >> 24)]);
67#else
68 return (uint)(uchar)(_my_bits_nbits[(uchar)v] +
69 _my_bits_nbits[(uchar)(v >> 8)] +
70 _my_bits_nbits[(uchar)(v >> 16)] +
71 _my_bits_nbits[(uchar)(v >> 24)]);
72#endif
73}
74
76 return (uint)(uchar)(_my_bits_nbits[(uchar)v] +
77 _my_bits_nbits[(uchar)(v >> 8)] +
78 _my_bits_nbits[(uchar)(v >> 16)] +
79 _my_bits_nbits[(uchar)(v >> 24)]);
80}
81
82/*
83 Next highest power of two
84
85 SYNOPSIS
86 my_round_up_to_next_power()
87 v Value to check
88
89 RETURN
90 Next or equal power of 2
91 Note: 0 will return 0
92
93 NOTES
94 Algorithm by Sean Anderson, according to:
95 http://graphics.stanford.edu/~seander/bithacks.html
96 (Original code public domain)
97
98 Comments shows how this works with 01100000000000000000000000001011
99*/
100
102 v--; /* 01100000000000000000000000001010 */
103 v |= v >> 1; /* 01110000000000000000000000001111 */
104 v |= v >> 2; /* 01111100000000000000000000001111 */
105 v |= v >> 4; /* 01111111110000000000000000001111 */
106 v |= v >> 8; /* 01111111111111111100000000001111 */
107 v |= v >> 16; /* 01111111111111111111111111111111 */
108 return v + 1; /* 10000000000000000000000000000000 */
109}
110
112 uint32 w = v >> 1;
113 w |= w >> 1;
114 w |= w >> 2;
115 w |= w >> 4;
116 w |= w >> 8;
117 w |= w >> 16;
118 return v & w;
119}
120
122 return (_my_bits_reverse_table[key & 255] << 24) |
123 (_my_bits_reverse_table[(key >> 8) & 255] << 16) |
124 (_my_bits_reverse_table[(key >> 16) & 255] << 8) |
126}
127
128/**
129 Determine if a single bit is set among some bits.
130 @tparam IntType an integer type
131 @param bits the bits to examine
132 @retval true if bits equals to a power of 2
133 @retval false otherwise
134*/
135
136template <typename IntType>
137constexpr bool is_single_bit(IntType bits) {
138 /*
139 Proof of correctness:
140 (1) is_single_bit(0)==false is left as an exercise to the reader.
141 (2) is_single_bit(1)==true is left as an exercise to the reader.
142 (3) is_single_bit(1<<(N+1))==true because the most significant set bit
143 in (bits - 1) would be 1<<N, and obviously (bits & (bits - 1)) == 0.
144 (4) In all other cases, is_single_bit(bits)==false.
145 In these cases, we must have multiple bits set, that is,
146 bits==m|1<<N such that N>=0 and m!=0 and the least significant
147 bit that is set in m is greater than 1<<N. In this case,
148 (bits-1)==m|((1<<N)-1), and (bits&(bits-1))==m, which we defined to be
149 nonzero. So, m==0 will not hold.
150
151 Note: The above proof (3),(4) is applicable also to the case where
152 IntType is signed using two's complement arithmetic, and the most
153 significant bit is set, or in other words, bits<0.
154 */
155 return bits != 0 && (bits & (bits - 1)) == 0;
156}
157
158#endif /* MY_BIT_INCLUDED */
static uint32 my_round_up_to_next_power(uint32 v)
Definition: my_bit.h:101
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:121
const uchar _my_bits_reverse_table[256]
Definition: my_bit.cc:52
static uint my_count_bits_uint32(uint32 v)
Definition: my_bit.h:75
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:137
static uint32 my_clear_highest_bit(uint32 v)
Definition: my_bit.h:111
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