~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to include/my_bit.h

  • Committer: Monty Taylor
  • Date: 2008-07-22 05:48:51 UTC
  • mto: (202.1.3 toru)
  • mto: This revision was merged to the branch mainline in revision 204.
  • Revision ID: monty@inaugust.com-20080722054851-airxt73370725p7x
Re-enabled optimizations for the normal build, and added back the --with-debug option to turn them off. 

Show diffs side-by-side

added added

removed removed

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