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
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
9
extern const char _my_bits_nbits[256];
33
10
extern const unsigned char _my_bits_reverse_table[256];
47
24
static inline uint32_t my_count_bits(uint64_t v)
26
#if SIZEOF_LONG_LONG > 4
49
27
/* The following code is a bit faster on 16 bit machines than if we would
51
29
uint32_t v2=(uint32_t) (v >> 32);
52
return (uint32_t) (unsigned char) (_my_bits_nbits[(unsigned char) v] +
30
return (uint) (unsigned char) (_my_bits_nbits[(unsigned char) v] +
53
31
_my_bits_nbits[(unsigned char) (v >> 8)] +
54
32
_my_bits_nbits[(unsigned char) (v >> 16)] +
55
33
_my_bits_nbits[(unsigned char) (v >> 24)] +
57
35
_my_bits_nbits[(unsigned char) (v2 >> 8)] +
58
36
_my_bits_nbits[(unsigned char) (v2 >> 16)] +
59
37
_my_bits_nbits[(unsigned char) (v2 >> 24)]);
39
return (uint) (unsigned char) (_my_bits_nbits[(unsigned char) v] +
40
_my_bits_nbits[(unsigned char) (v >> 8)] +
41
_my_bits_nbits[(unsigned char) (v >> 16)] +
42
_my_bits_nbits[(unsigned char) (v >> 24)]);
62
static inline uint32_t my_count_bits_uint16(uint16_t v)
46
static inline uint32_t my_count_bits_ushort(ushort v)
64
48
return _my_bits_nbits[v];
53
Next highest power of two
56
my_round_up_to_next_power()
60
Next or equal power of 2
64
Algorithm by Sean Anderson, according to:
65
http://graphics.stanford.edu/~seander/bithacks.html
66
(Orignal code public domain)
68
Comments shows how this works with 01100000000000000000000000001011
71
static inline uint32_t my_round_up_to_next_power(uint32_t v)
73
v--; /* 01100000000000000000000000001010 */
74
v|= v >> 1; /* 01110000000000000000000000001111 */
75
v|= v >> 2; /* 01111100000000000000000000001111 */
76
v|= v >> 4; /* 01111111110000000000000000001111 */
77
v|= v >> 8; /* 01111111111111111100000000001111 */
78
v|= v >> 16; /* 01111111111111111111111111111111 */
79
return v+1; /* 10000000000000000000000000000000 */
68
82
static inline uint32_t my_clear_highest_bit(uint32_t v)