proxmark3/common/parity.h

62 lines
1.2 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Parity functions
//-----------------------------------------------------------------------------
// all functions defined in header file by purpose. Allows compiler optimizations.
#ifndef __PARITY_H
#define __PARITY_H
2017-01-25 07:35:11 +08:00
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
extern const uint8_t OddByteParity[256];
static inline bool oddparity8(const uint8_t x) {
2019-03-10 02:19:50 +08:00
return OddByteParity[x];
}
static inline bool evenparity8(const uint8_t x) {
2019-03-10 02:19:50 +08:00
return !OddByteParity[x];
}
static inline bool evenparity32(uint32_t x)
{
#if !defined __GNUC__
2019-03-10 02:19:50 +08:00
x ^= x >> 16;
x ^= x >> 8;
return evenparity8(x);
#else
2019-03-10 02:19:50 +08:00
return __builtin_parity(x);
#endif
}
static inline bool oddparity32(uint32_t x)
{
#if !defined __GNUC__
2019-03-10 02:19:50 +08:00
x ^= x >> 16;
x ^= x >> 8;
return oddparity8(x);
#else
2019-03-10 02:19:50 +08:00
return !__builtin_parity(x);
#endif
}
2017-01-25 07:35:11 +08:00
#ifdef __cplusplus
}
#endif
#endif /* __PARITY_H */