Drizzled Public API Documentation

my_bit.h
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008, 2009 Sun Microsystems, Inc.
5  *
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.
9  *
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.
14  *
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
18  */
19 
20 /*
21  Some useful bit functions
22 */
23 
24 #pragma once
25 
26 namespace drizzled
27 {
28 namespace internal
29 {
30 
31 extern const char _my_bits_nbits[256];
32 extern const unsigned char _my_bits_reverse_table[256];
33 
34 /*
35  Find smallest X in 2^X >= value
36  This can be used to divide a number with value by doing a shift instead
37 */
38 
39 static inline uint32_t my_bit_log2(uint32_t value)
40 {
41  uint32_t bit;
42  for (bit=0 ; value > 1 ; value>>=1, bit++) ;
43  return bit;
44 }
45 
46 static inline uint32_t my_count_bits(uint64_t v)
47 {
48  /* The following code is a bit faster on 16 bit machines than if we would
49  only shift v */
50  uint32_t v2=(uint32_t) (v >> 32);
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)]);
59 }
60 
61 static inline uint32_t my_count_bits_uint16(uint16_t v)
62 {
63  return _my_bits_nbits[v];
64 }
65 
66 
67 static inline uint32_t my_clear_highest_bit(uint32_t v)
68 {
69  uint32_t w=v >> 1;
70  w|= w >> 1;
71  w|= w >> 2;
72  w|= w >> 4;
73  w|= w >> 8;
74  w|= w >> 16;
75  return v & w;
76 }
77 
78 static inline uint32_t my_reverse_bits(uint32_t key)
79 {
80  return
81  (_my_bits_reverse_table[ key & 255] << 24) |
82  (_my_bits_reverse_table[(key>> 8) & 255] << 16) |
83  (_my_bits_reverse_table[(key>>16) & 255] << 8) |
84  _my_bits_reverse_table[(key>>24) ];
85 }
86 
87 } /* namespace internal */
88 } /* namespace drizzled */
89