]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
3 | // at your option, any later version. See the LICENSE.txt file for the text of | |
4 | // the license. | |
5 | //----------------------------------------------------------------------------- | |
6 | // Parity functions | |
7 | //----------------------------------------------------------------------------- | |
8 | ||
9 | // all functions defined in header file by purpose. Allows compiler optimizations. | |
10 | ||
11 | #ifndef __PARITY_H | |
12 | #define __PARITY_H | |
13 | ||
14 | #include <stdint.h> | |
15 | #include <stdbool.h> | |
16 | #include "string.h" | |
17 | ||
18 | extern const uint8_t OddByteParity[256]; | |
19 | ||
20 | ||
21 | static inline bool oddparity8(const uint8_t x) { | |
22 | return OddByteParity[x]; | |
23 | } | |
24 | ||
25 | static inline void oddparitybuf(const uint8_t *x, size_t len, uint8_t *parity) { | |
26 | memset(parity, 0x00, (len - 1) / 8 + 1); | |
27 | for (int i = 0; i < len; i++) | |
28 | parity[i / 8] |= oddparity8(x[i]) << (7 - (i % 8)); | |
29 | } | |
30 | ||
31 | static inline bool evenparity8(const uint8_t x) { | |
32 | return !OddByteParity[x]; | |
33 | } | |
34 | ||
35 | ||
36 | static inline bool evenparity32(uint32_t x) | |
37 | { | |
38 | #if !defined __GNUC__ | |
39 | x ^= x >> 16; | |
40 | x ^= x >> 8; | |
41 | return evenparity8(x); | |
42 | #else | |
43 | return __builtin_parity(x); | |
44 | #endif | |
45 | } | |
46 | ||
47 | ||
48 | static inline bool oddparity32(uint32_t x) | |
49 | { | |
50 | #if !defined __GNUC__ | |
51 | x ^= x >> 16; | |
52 | x ^= x >> 8; | |
53 | return oddparity8(x); | |
54 | #else | |
55 | return !__builtin_parity(x); | |
56 | #endif | |
57 | } | |
58 | ||
59 | #endif /* __PARITY_H */ |