~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_bit.h

  • Committer: Monty Taylor
  • Date: 2009-10-06 19:37:52 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20091006193752-4dx2c8u35j4em79g
Removed more server_includes.h from headers.

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
 
1
20
/*
2
21
  Some useful bit functions
3
22
*/
4
23
 
5
 
C_MODE_START
 
24
#ifndef MYSYS_MY_BIT_H
 
25
#define MYSYS_MY_BIT_H
 
26
 
 
27
#ifdef __cplusplus
 
28
extern "C" {
 
29
#endif
6
30
 
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];
9
33
 
10
34
/*
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
13
37
*/
14
38
 
15
 
static inline uint my_bit_log2(uint32_t value)
 
39
static inline uint32_t my_bit_log2(uint32_t value)
16
40
{
17
 
  uint bit;
 
41
  uint32_t bit;
18
42
  for (bit=0 ; value > 1 ; value>>=1, bit++) ;
19
43
  return bit;
20
44
}
21
45
 
22
 
static inline uint my_count_bits(uint64_t v)
 
46
static inline uint32_t my_count_bits(uint64_t v)
23
47
{
24
 
#if SIZEOF_LONG_LONG > 4
25
48
  /* The following code is a bit faster on 16 bit machines than if we would
26
49
     only shift v */
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)]);
36
 
#else
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)]);
41
 
#endif
 
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)]);
42
59
}
43
60
 
44
 
static inline uint my_count_bits_ushort(ushort v)
 
61
static inline uint32_t my_count_bits_uint16(uint16_t v)
45
62
{
46
63
  return _my_bits_nbits[v];
47
64
}
97
114
     _my_bits_reverse_table[(key>>24)      ];
98
115
}
99
116
 
100
 
C_MODE_END
 
117
#ifdef __cplusplus
 
118
}
 
119
#endif
 
120
 
 
121
#endif /* MYSYS_MY_BIT_H */