1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008, 2009 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
2
Some useful bit functions
24
#ifndef DRIZZLED_INTERNAL_MY_BIT_H
25
#define DRIZZLED_INTERNAL_MY_BIT_H
32
7
extern const char _my_bits_nbits[256];
33
extern const unsigned char _my_bits_reverse_table[256];
8
extern const uchar _my_bits_reverse_table[256];
36
11
Find smallest X in 2^X >= value
37
12
This can be used to divide a number with value by doing a shift instead
40
static inline uint32_t my_bit_log2(uint32_t value)
15
static inline uint my_bit_log2(uint32_t value)
43
18
for (bit=0 ; value > 1 ; value>>=1, bit++) ;
47
static inline uint32_t my_count_bits(uint64_t v)
22
static inline uint my_count_bits(uint64_t v)
24
#if SIZEOF_LONG_LONG > 4
49
25
/* The following code is a bit faster on 16 bit machines than if we would
51
27
uint32_t v2=(uint32_t) (v >> 32);
52
return (uint32_t) (unsigned char) (_my_bits_nbits[(unsigned char) v] +
53
_my_bits_nbits[(unsigned char) (v >> 8)] +
54
_my_bits_nbits[(unsigned char) (v >> 16)] +
55
_my_bits_nbits[(unsigned char) (v >> 24)] +
56
_my_bits_nbits[(unsigned char) (v2)] +
57
_my_bits_nbits[(unsigned char) (v2 >> 8)] +
58
_my_bits_nbits[(unsigned char) (v2 >> 16)] +
59
_my_bits_nbits[(unsigned char) (v2 >> 24)]);
28
return (uint) (uchar) (_my_bits_nbits[(uchar) v] +
29
_my_bits_nbits[(uchar) (v >> 8)] +
30
_my_bits_nbits[(uchar) (v >> 16)] +
31
_my_bits_nbits[(uchar) (v >> 24)] +
32
_my_bits_nbits[(uchar) (v2)] +
33
_my_bits_nbits[(uchar) (v2 >> 8)] +
34
_my_bits_nbits[(uchar) (v2 >> 16)] +
35
_my_bits_nbits[(uchar) (v2 >> 24)]);
37
return (uint) (uchar) (_my_bits_nbits[(uchar) v] +
38
_my_bits_nbits[(uchar) (v >> 8)] +
39
_my_bits_nbits[(uchar) (v >> 16)] +
40
_my_bits_nbits[(uchar) (v >> 24)]);
62
static inline uint32_t my_count_bits_uint16(uint16_t v)
44
static inline uint my_count_bits_ushort(ushort v)
64
46
return _my_bits_nbits[v];
51
Next highest power of two
54
my_round_up_to_next_power()
58
Next or equal power of 2
62
Algorithm by Sean Anderson, according to:
63
http://graphics.stanford.edu/~seander/bithacks.html
64
(Orignal code public domain)
66
Comments shows how this works with 01100000000000000000000000001011
69
static inline uint32_t my_round_up_to_next_power(uint32_t v)
71
v--; /* 01100000000000000000000000001010 */
72
v|= v >> 1; /* 01110000000000000000000000001111 */
73
v|= v >> 2; /* 01111100000000000000000000001111 */
74
v|= v >> 4; /* 01111111110000000000000000001111 */
75
v|= v >> 8; /* 01111111111111111100000000001111 */
76
v|= v >> 16; /* 01111111111111111111111111111111 */
77
return v+1; /* 10000000000000000000000000000000 */
68
80
static inline uint32_t my_clear_highest_bit(uint32_t v)