forked from glidernet/diy-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcount.h
71 lines (53 loc) · 2.15 KB
/
bitcount.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Fast bit counting for (forward) error correction codes
// (c) 2003, Pawel Jalocha, [email protected]
#ifndef __BITCOUNT_H__
#define __BITCOUNT_H__
#include <stdint.h>
#define BITCOUNT_USE_BUILTIN
#define BITCOUNT_SAVE_FLASH
// ==========================================================================
// a table for fast bit counting
#ifdef BITCOUNT_SAVE_FLASH
extern const uint8_t ByteCount1s[16];
#else
extern const uint8_t ByteCount1s[256];
#endif
// ==========================================================================
#ifdef BITCOUNT_USE_BUILTIN
inline uint8_t Count1s(uint8_t Byte) { return __builtin_popcount(Byte); }
#else
#ifdef BITCOUNT_SAVE_FLASH
inline uint8_t Count1s(uint8_t Byte) { return ByteCount1s[Byte&0x0F] + ByteCount1s[Byte>>4]; }
#else
inline uint8_t Count1s(uint8_t Byte) { return ByteCount1s[Byte]; }
#endif
#endif
inline uint8_t Count1s(int8_t Byte) { return Count1s((uint8_t)Byte); }
#ifdef BITCOUNT_USE_BUILTIN
inline uint8_t Count1s(uint16_t Word) { return __builtin_popcount(Word); }
#else
inline uint8_t Count1s(uint16_t Word)
{ return Count1s((uint8_t)Word)
+Count1s((uint8_t)(Word>>8)); }
#endif
inline uint8_t Count1s(int16_t Word) { return Count1s((uint16_t)Word); }
#ifdef BITCOUNT_USE_BUILTIN
inline uint8_t Count1s(uint32_t LongWord) { return __builtin_popcountl(LongWord); }
#else
inline uint8_t Count1s(uint32_t LongWord)
{ return Count1s((uint16_t)LongWord)
+Count1s((uint16_t)(LongWord>>16)); }
#endif
inline uint8_t Count1s(int32_t LongWord) { return Count1s((uint32_t)LongWord); }
#ifdef BITCOUNT_USE_BUILTIN
inline uint8_t Count1s(uint64_t LongWord) { return __builtin_popcountll(LongWord); }
#else
inline uint8_t Count1s(uint64_t LongWord)
{ return Count1s((uint32_t)LongWord)
+Count1s((uint32_t)(LongWord>>32)); }
#endif
inline uint8_t Count1s(int64_t LongWord) { return Count1s((uint64_t)LongWord); }
int Count1s(const uint8_t *Byte, int Bytes);
// ==========================================================================
// use __builtin_popcount(unsigned int) ? http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer
#endif // of __BITCOUNT_H__