~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_bit.h

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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