~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/*
2
  Some useful bit functions
3
*/
4
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
5
#ifdef __cplusplus
6
extern "C" {
7
#endif
1 by brian
clean slate
8
9
extern const char _my_bits_nbits[256];
481 by Brian Aker
Remove all of uchar.
10
extern const unsigned char _my_bits_reverse_table[256];
1 by brian
clean slate
11
12
/*
13
  Find smallest X in 2^X >= value
14
  This can be used to divide a number with value by doing a shift instead
15
*/
16
482 by Brian Aker
Remove uint.
17
static inline uint32_t my_bit_log2(uint32_t value)
1 by brian
clean slate
18
{
482 by Brian Aker
Remove uint.
19
  uint32_t bit;
1 by brian
clean slate
20
  for (bit=0 ; value > 1 ; value>>=1, bit++) ;
21
  return bit;
22
}
23
482 by Brian Aker
Remove uint.
24
static inline uint32_t my_count_bits(uint64_t v)
1 by brian
clean slate
25
{
26
  /* The following code is a bit faster on 16 bit machines than if we would
27
     only shift v */
298 by Brian Aker
ulong conversion.
28
  uint32_t v2=(uint32_t) (v >> 32);
481 by Brian Aker
Remove all of uchar.
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)]);
1 by brian
clean slate
37
}
38
481.1.12 by Monty Taylor
Removed ushort references.
39
static inline uint32_t my_count_bits_uint16(uint16_t v)
1 by brian
clean slate
40
{
41
  return _my_bits_nbits[v];
42
}
43
44
45
/*
46
  Next highest power of two
47
48
  SYNOPSIS
49
    my_round_up_to_next_power()
50
    v		Value to check
51
52
  RETURN
53
    Next or equal power of 2
54
    Note: 0 will return 0
55
56
  NOTES
57
    Algorithm by Sean Anderson, according to:
58
    http://graphics.stanford.edu/~seander/bithacks.html
59
    (Orignal code public domain)
60
61
    Comments shows how this works with 01100000000000000000000000001011
62
*/
63
205 by Brian Aker
uint32 -> uin32_t
64
static inline uint32_t my_round_up_to_next_power(uint32_t v)
1 by brian
clean slate
65
{
66
  v--;			/* 01100000000000000000000000001010 */
67
  v|= v >> 1;		/* 01110000000000000000000000001111 */
68
  v|= v >> 2;		/* 01111100000000000000000000001111 */
69
  v|= v >> 4;		/* 01111111110000000000000000001111 */
70
  v|= v >> 8;		/* 01111111111111111100000000001111 */
71
  v|= v >> 16;		/* 01111111111111111111111111111111 */
72
  return v+1;		/* 10000000000000000000000000000000 */
73
}
74
205 by Brian Aker
uint32 -> uin32_t
75
static inline uint32_t my_clear_highest_bit(uint32_t v)
1 by brian
clean slate
76
{
205 by Brian Aker
uint32 -> uin32_t
77
  uint32_t w=v >> 1;
1 by brian
clean slate
78
  w|= w >> 1;
79
  w|= w >> 2;
80
  w|= w >> 4;
81
  w|= w >> 8;
82
  w|= w >> 16;
83
  return v & w;
84
}
85
205 by Brian Aker
uint32 -> uin32_t
86
static inline uint32_t my_reverse_bits(uint32_t key)
1 by brian
clean slate
87
{
88
  return
89
    (_my_bits_reverse_table[ key      & 255] << 24) |
90
    (_my_bits_reverse_table[(key>> 8) & 255] << 16) |
91
    (_my_bits_reverse_table[(key>>16) & 255] <<  8) |
92
     _my_bits_reverse_table[(key>>24)      ];
93
}
94
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
95
#ifdef __cplusplus
96
}
97
#endif
98