~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_bit.h

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

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, 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
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
 
namespace drizzled
28
 
{
29
 
namespace internal
30
 
{
 
5
C_MODE_START
31
6
 
32
7
extern const char _my_bits_nbits[256];
33
 
extern const unsigned char _my_bits_reverse_table[256];
 
8
extern const uchar _my_bits_reverse_table[256];
34
9
 
35
10
/*
36
11
  Find smallest X in 2^X >= value
37
12
  This can be used to divide a number with value by doing a shift instead
38
13
*/
39
14
 
40
 
static inline uint32_t my_bit_log2(uint32_t value)
 
15
static inline uint my_bit_log2(uint32_t value)
41
16
{
42
 
  uint32_t bit;
 
17
  uint bit;
43
18
  for (bit=0 ; value > 1 ; value>>=1, bit++) ;
44
19
  return bit;
45
20
}
46
21
 
47
 
static inline uint32_t my_count_bits(uint64_t v)
 
22
static inline uint my_count_bits(uint64_t v)
48
23
{
 
24
#if SIZEOF_LONG_LONG > 4
49
25
  /* The following code is a bit faster on 16 bit machines than if we would
50
26
     only shift v */
51
27
  uint32_t v2=(uint32_t) (v >> 32);
52
 
  return (uint32_t) (unsigned char) (_my_bits_nbits[(unsigned char)  v] +
53
 
                         _my_bits_nbits[(unsigned char) (v >> 8)] +
54
 
                         _my_bits_nbits[(unsigned char) (v >> 16)] +
55
 
                         _my_bits_nbits[(unsigned char) (v >> 24)] +
56
 
                         _my_bits_nbits[(unsigned char) (v2)] +
57
 
                         _my_bits_nbits[(unsigned char) (v2 >> 8)] +
58
 
                         _my_bits_nbits[(unsigned char) (v2 >> 16)] +
59
 
                         _my_bits_nbits[(unsigned char) (v2 >> 24)]);
 
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
60
42
}
61
43
 
62
 
static inline uint32_t my_count_bits_uint16(uint16_t v)
 
44
static inline uint my_count_bits_ushort(ushort v)
63
45
{
64
46
  return _my_bits_nbits[v];
65
47
}
66
48
 
67
49
 
 
50
/*
 
51
  Next highest power of two
 
52
 
 
53
  SYNOPSIS
 
54
    my_round_up_to_next_power()
 
55
    v           Value to check
 
56
 
 
57
  RETURN
 
58
    Next or equal power of 2
 
59
    Note: 0 will return 0
 
60
 
 
61
  NOTES
 
62
    Algorithm by Sean Anderson, according to:
 
63
    http://graphics.stanford.edu/~seander/bithacks.html
 
64
    (Orignal code public domain)
 
65
 
 
66
    Comments shows how this works with 01100000000000000000000000001011
 
67
*/
 
68
 
 
69
static inline uint32_t my_round_up_to_next_power(uint32_t v)
 
70
{
 
71
  v--;                  /* 01100000000000000000000000001010 */
 
72
  v|= v >> 1;           /* 01110000000000000000000000001111 */
 
73
  v|= v >> 2;           /* 01111100000000000000000000001111 */
 
74
  v|= v >> 4;           /* 01111111110000000000000000001111 */
 
75
  v|= v >> 8;           /* 01111111111111111100000000001111 */
 
76
  v|= v >> 16;          /* 01111111111111111111111111111111 */
 
77
  return v+1;           /* 10000000000000000000000000000000 */
 
78
}
 
79
 
68
80
static inline uint32_t my_clear_highest_bit(uint32_t v)
69
81
{
70
82
  uint32_t w=v >> 1;
85
97
     _my_bits_reverse_table[(key>>24)      ];
86
98
}
87
99
 
88
 
} /* namespace internal */
89
 
} /* namespace drizzled */
90
 
 
91
 
#endif /* DRIZZLED_INTERNAL_MY_BIT_H */
 
100
C_MODE_END