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
2
21
Some useful bit functions
24
#ifndef MYSYS_MY_BIT_H
25
#define MYSYS_MY_BIT_H
7
31
extern const char _my_bits_nbits[256];
8
extern const uchar _my_bits_reverse_table[256];
32
extern const unsigned char _my_bits_reverse_table[256];
11
35
Find smallest X in 2^X >= value
12
36
This can be used to divide a number with value by doing a shift instead
15
static inline uint my_bit_log2(uint32_t value)
39
static inline uint32_t my_bit_log2(uint32_t value)
18
42
for (bit=0 ; value > 1 ; value>>=1, bit++) ;
22
static inline uint my_count_bits(uint64_t v)
46
static inline uint32_t my_count_bits(uint64_t v)
24
#if SIZEOF_LONG_LONG > 4
25
48
/* The following code is a bit faster on 16 bit machines than if we would
27
50
uint32_t v2=(uint32_t) (v >> 32);
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)]);
51
return (uint32_t) (unsigned char) (_my_bits_nbits[(unsigned char) v] +
52
_my_bits_nbits[(unsigned char) (v >> 8)] +
53
_my_bits_nbits[(unsigned char) (v >> 16)] +
54
_my_bits_nbits[(unsigned char) (v >> 24)] +
55
_my_bits_nbits[(unsigned char) (v2)] +
56
_my_bits_nbits[(unsigned char) (v2 >> 8)] +
57
_my_bits_nbits[(unsigned char) (v2 >> 16)] +
58
_my_bits_nbits[(unsigned char) (v2 >> 24)]);
44
static inline uint my_count_bits_ushort(ushort v)
61
static inline uint32_t my_count_bits_uint16(uint16_t v)
46
63
return _my_bits_nbits[v];