Merge pull request #2654 from Antiklesys/master
[RRG-proxmark3.git] / common / parity.h
blob3985a1e040e134aabfe8623b8a6a8a7d0c8b9157
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Parity functions
17 //-----------------------------------------------------------------------------
19 // all functions defined in header file by purpose. Allows compiler optimizations.
21 #ifndef __PARITY_H
22 #define __PARITY_H
24 #include "common.h"
26 static const uint8_t g_odd_byte_parity[256] = {
27 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
28 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
29 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
30 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
31 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
32 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
33 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
34 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
35 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
36 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
37 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
38 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
39 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
40 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
41 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
42 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
45 #define ODD_PARITY8(x) g_odd_byte_parity[x]
46 #define EVEN_PARITY8(x) !g_odd_byte_parity[x]
48 static inline uint8_t oddparity8(const uint8_t x) {
49 return g_odd_byte_parity[x];
52 static inline uint8_t evenparity8(const uint8_t x) {
53 return !g_odd_byte_parity[x];
56 static inline uint8_t evenparity16(uint16_t x) {
57 #if !defined __GNUC__
58 x ^= x >> 8;
59 return EVEN_PARITY8(x) ;
60 #else
61 return __builtin_parity(x);
62 #endif
65 static inline uint8_t oddparity16(uint16_t x) {
66 #if !defined __GNUC__
67 x ^= x >> 8;
68 return ODD_PARITY8(x);
69 #else
70 return !__builtin_parity(x);
71 #endif
74 static inline uint8_t evenparity32(uint32_t x) {
75 #if !defined __GNUC__
76 x ^= x >> 16;
77 x ^= x >> 8;
78 return EVEN_PARITY8(x) ;
79 #else
80 return __builtin_parity(x);
81 #endif
84 static inline uint8_t oddparity32(uint32_t x) {
85 #if !defined __GNUC__
86 x ^= x >> 16;
87 x ^= x >> 8;
88 return ODD_PARITY8(x);
89 #else
90 return !__builtin_parity(x);
91 #endif
94 #endif /* __PARITY_H */