fixeria has uploaded this change for review.
core: add parity calculation functions for uint{32,16,8}_t
Change-Id: I2d6265eac5ce062276a40f4968b129d8702c6117
---
M include/osmocom/core/bits.h
1 file changed, 48 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/05/35005/1
diff --git a/include/osmocom/core/bits.h b/include/osmocom/core/bits.h
index b1b8040..4b80036 100644
--- a/include/osmocom/core/bits.h
+++ b/include/osmocom/core/bits.h
@@ -119,4 +119,43 @@
return (in << shift) | (in >> (16 - shift));
}
+/*! Calculate parity for the given 32-bit unsigned integer.
+ * \param[in] x a 32-bit unsigned integer.
+ * \returns 1 for even parity (even number of bits set to 1);
+ * 0 for odd parity (odd number of bits set to 1). */
+static inline uint8_t osmo_parity_u32(uint32_t x)
+{
+ x ^= x >> 16;
+ x ^= x >> 8;
+ x ^= x >> 4;
+ x ^= x >> 2;
+ x ^= x >> 1;
+ return (~x) & 1;
+}
+
+/*! Calculate parity for the given 16-bit unsigned integer.
+ * \param[in] x a 16-bit unsigned integer.
+ * \returns 1 for even parity (even number of bits set to 1);
+ * 0 for odd parity (odd number of bits set to 1). */
+static inline uint8_t osmo_parity_u16(uint16_t x)
+{
+ x ^= x >> 8;
+ x ^= x >> 4;
+ x ^= x >> 2;
+ x ^= x >> 1;
+ return (~x) & 1;
+}
+
+/*! Calculate parity for the given 8-bit unsigned integer.
+ * \param[in] x a 8-bit unsigned integer.
+ * \returns 1 for even parity (even number of bits set to 1);
+ * 0 for odd parity (odd number of bits set to 1). */
+static inline uint8_t osmo_parity_u8(uint8_t x)
+{
+ x ^= x >> 4;
+ x ^= x >> 2;
+ x ^= x >> 1;
+ return (~x) & 1;
+}
+
/*! @} */
To view, visit change 35005. To unsubscribe, or for help writing mail filters, visit settings.