~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to include/my_bit.h

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
1
/*
21
2
  Some useful bit functions
22
3
*/
23
4
 
24
 
#ifndef DRIZZLED_INTERNAL_MY_BIT_H
25
 
#define DRIZZLED_INTERNAL_MY_BIT_H
26
 
 
27
 
#ifdef __cplusplus
28
 
extern "C" {
29
 
#endif
 
5
C_MODE_START
 
6
#ifdef HAVE_INLINE
30
7
 
31
8
extern const char _my_bits_nbits[256];
32
 
extern const unsigned char _my_bits_reverse_table[256];
 
9
extern const uchar _my_bits_reverse_table[256];
33
10
 
34
11
/*
35
12
  Find smallest X in 2^X >= value
36
13
  This can be used to divide a number with value by doing a shift instead
37
14
*/
38
15
 
39
 
static inline uint32_t my_bit_log2(uint32_t value)
 
16
STATIC_INLINE uint my_bit_log2(ulong value)
40
17
{
41
 
  uint32_t bit;
 
18
  uint bit;
42
19
  for (bit=0 ; value > 1 ; value>>=1, bit++) ;
43
20
  return bit;
44
21
}
45
22
 
46
 
static inline uint32_t my_count_bits(uint64_t v)
 
23
STATIC_INLINE uint my_count_bits(ulonglong v)
47
24
{
 
25
#if SIZEOF_LONG_LONG > 4
48
26
  /* The following code is a bit faster on 16 bit machines than if we would
49
27
     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)]);
 
28
  ulong v2=(ulong) (v >> 32);
 
29
  return (uint) (uchar) (_my_bits_nbits[(uchar)  v] +
 
30
                         _my_bits_nbits[(uchar) (v >> 8)] +
 
31
                         _my_bits_nbits[(uchar) (v >> 16)] +
 
32
                         _my_bits_nbits[(uchar) (v >> 24)] +
 
33
                         _my_bits_nbits[(uchar) (v2)] +
 
34
                         _my_bits_nbits[(uchar) (v2 >> 8)] +
 
35
                         _my_bits_nbits[(uchar) (v2 >> 16)] +
 
36
                         _my_bits_nbits[(uchar) (v2 >> 24)]);
 
37
#else
 
38
  return (uint) (uchar) (_my_bits_nbits[(uchar)  v] +
 
39
                         _my_bits_nbits[(uchar) (v >> 8)] +
 
40
                         _my_bits_nbits[(uchar) (v >> 16)] +
 
41
                         _my_bits_nbits[(uchar) (v >> 24)]);
 
42
#endif
59
43
}
60
44
 
61
 
static inline uint32_t my_count_bits_uint16(uint16_t v)
 
45
STATIC_INLINE uint my_count_bits_ushort(ushort v)
62
46
{
63
47
  return _my_bits_nbits[v];
64
48
}
83
67
    Comments shows how this works with 01100000000000000000000000001011
84
68
*/
85
69
 
86
 
static inline uint32_t my_round_up_to_next_power(uint32_t v)
 
70
STATIC_INLINE uint32 my_round_up_to_next_power(uint32 v)
87
71
{
88
72
  v--;                  /* 01100000000000000000000000001010 */
89
73
  v|= v >> 1;           /* 01110000000000000000000000001111 */
94
78
  return v+1;           /* 10000000000000000000000000000000 */
95
79
}
96
80
 
97
 
static inline uint32_t my_clear_highest_bit(uint32_t v)
 
81
STATIC_INLINE uint32 my_clear_highest_bit(uint32 v)
98
82
{
99
 
  uint32_t w=v >> 1;
 
83
  uint32 w=v >> 1;
100
84
  w|= w >> 1;
101
85
  w|= w >> 2;
102
86
  w|= w >> 4;
105
89
  return v & w;
106
90
}
107
91
 
108
 
static inline uint32_t my_reverse_bits(uint32_t key)
 
92
STATIC_INLINE uint32 my_reverse_bits(uint32 key)
109
93
{
110
94
  return
111
95
    (_my_bits_reverse_table[ key      & 255] << 24) |
114
98
     _my_bits_reverse_table[(key>>24)      ];
115
99
}
116
100
 
117
 
#ifdef __cplusplus
118
 
}
119
 
#endif
120
 
 
121
 
#endif /* DRIZZLED_INTERNAL_MY_BIT_H */
 
101
#else  /* HAVE_INLINE */
 
102
extern uint my_bit_log2(ulong value);
 
103
extern uint32 my_round_up_to_next_power(uint32 v);
 
104
uint32 my_clear_highest_bit(uint32 v);
 
105
uint32 my_reverse_bits(uint32 key);
 
106
extern uint my_count_bits(ulonglong v);
 
107
extern uint my_count_bits_ushort(ushort v);
 
108
#endif /* HAVE_INLINE */
 
109
C_MODE_END