[bootwrapper PATCH 02/13] Add bit-field macros

Mark Rutland mark.rutland at arm.com
Tue Jan 11 05:06:42 PST 2022


Arm architectural documentation typically defines bit-fields as
`[msb,lsb]` and single-bit fields as `[bit]`. For clarity it would be
helpful if we could define fields in the same way.

Add helpers so that we can do so, along with helper to extract/insert
bit-field values.

There should be no functional change as a result of this patch.

Signed-off-by: Mark Rutland <mark.rutland at arm.com>
---
 include/bits.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 include/bits.h

diff --git a/include/bits.h b/include/bits.h
new file mode 100644
index 0000000..8824a38
--- /dev/null
+++ b/include/bits.h
@@ -0,0 +1,33 @@
+/*
+ * include/bits.h - helpers for bit-field definitions.
+ *
+ * Copyright (C) 2021 ARM Limited. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE.txt file.
+ */
+#ifndef __BITS_H
+#define __BITS_H
+
+#ifdef __ASSEMBLY__
+#define UL(x)	x
+#define ULL(x)	x
+#else
+#define UL(x)	x##UL
+#define ULL(x)	x##ULL
+#endif
+
+#define BITS(msb, lsb) \
+((~ULL(0) >> (63 - msb)) & (~ULL(0) << lsb))
+
+#define BIT(b)	BITS(b, b)
+
+#define BITS_LSB(bits)	(__builtin_ffsll(bits) - 1)
+
+#define BITS_EXTRACT(val, bits) \
+	(((val) & (bits)) >> BITS_LSB(bits))
+
+#define BITS_INSERT(bits, val) \
+	(((val) << BITS_LSB(bits)) & (bits))
+
+#endif
-- 
2.30.2




More information about the linux-arm-kernel mailing list