~drizzle-trunk/drizzle/development

1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
4
 *  Copyright (C) 2008, 2009 Sun Microsystems
1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
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 by brian
clean slate
20
/*
21
  Some useful bit functions
22
*/
23
1122.2.10 by Monty Taylor
Fixed all of the include guards.
24
#ifndef MYSYS_MY_BIT_H
25
#define MYSYS_MY_BIT_H
26
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
27
#ifdef __cplusplus
28
extern "C" {
29
#endif
1 by brian
clean slate
30
31
extern const char _my_bits_nbits[256];
481 by Brian Aker
Remove all of uchar.
32
extern const unsigned char _my_bits_reverse_table[256];
1 by brian
clean slate
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
482 by Brian Aker
Remove uint.
39
static inline uint32_t my_bit_log2(uint32_t value)
1 by brian
clean slate
40
{
482 by Brian Aker
Remove uint.
41
  uint32_t bit;
1 by brian
clean slate
42
  for (bit=0 ; value > 1 ; value>>=1, bit++) ;
43
  return bit;
44
}
45
482 by Brian Aker
Remove uint.
46
static inline uint32_t my_count_bits(uint64_t v)
1 by brian
clean slate
47
{
48
  /* The following code is a bit faster on 16 bit machines than if we would
49
     only shift v */
298 by Brian Aker
ulong conversion.
50
  uint32_t v2=(uint32_t) (v >> 32);
895 by Brian Aker
Completion (?) of uint conversion.
51
  return (uint32_t) (unsigned char) (_my_bits_nbits[(unsigned char)  v] +
481 by Brian Aker
Remove all of uchar.
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)]);
1 by brian
clean slate
59
}
60
481.1.12 by Monty Taylor
Removed ushort references.
61
static inline uint32_t my_count_bits_uint16(uint16_t v)
1 by brian
clean slate
62
{
63
  return _my_bits_nbits[v];
64
}
65
66
67
/*
68
  Next highest power of two
69
70
  SYNOPSIS
71
    my_round_up_to_next_power()
72
    v		Value to check
73
74
  RETURN
75
    Next or equal power of 2
76
    Note: 0 will return 0
77
78
  NOTES
79
    Algorithm by Sean Anderson, according to:
80
    http://graphics.stanford.edu/~seander/bithacks.html
81
    (Orignal code public domain)
82
83
    Comments shows how this works with 01100000000000000000000000001011
84
*/
85
205 by Brian Aker
uint32 -> uin32_t
86
static inline uint32_t my_round_up_to_next_power(uint32_t v)
1 by brian
clean slate
87
{
88
  v--;			/* 01100000000000000000000000001010 */
89
  v|= v >> 1;		/* 01110000000000000000000000001111 */
90
  v|= v >> 2;		/* 01111100000000000000000000001111 */
91
  v|= v >> 4;		/* 01111111110000000000000000001111 */
92
  v|= v >> 8;		/* 01111111111111111100000000001111 */
93
  v|= v >> 16;		/* 01111111111111111111111111111111 */
94
  return v+1;		/* 10000000000000000000000000000000 */
95
}
96
205 by Brian Aker
uint32 -> uin32_t
97
static inline uint32_t my_clear_highest_bit(uint32_t v)
1 by brian
clean slate
98
{
205 by Brian Aker
uint32 -> uin32_t
99
  uint32_t w=v >> 1;
1 by brian
clean slate
100
  w|= w >> 1;
101
  w|= w >> 2;
102
  w|= w >> 4;
103
  w|= w >> 8;
104
  w|= w >> 16;
105
  return v & w;
106
}
107
205 by Brian Aker
uint32 -> uin32_t
108
static inline uint32_t my_reverse_bits(uint32_t key)
1 by brian
clean slate
109
{
110
  return
111
    (_my_bits_reverse_table[ key      & 255] << 24) |
112
    (_my_bits_reverse_table[(key>> 8) & 255] << 16) |
113
    (_my_bits_reverse_table[(key>>16) & 255] <<  8) |
114
     _my_bits_reverse_table[(key>>24)      ];
115
}
116
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
117
#ifdef __cplusplus
118
}
119
#endif
120
1122.2.10 by Monty Taylor
Fixed all of the include guards.
121
#endif /* MYSYS_MY_BIT_H */