summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Paul <[email protected]>2017-11-15 16:17:44 -0700
committerBrian Paul <[email protected]>2017-11-16 20:35:17 -0700
commitfe81e1f9751cca8de33c3c45f6fc181c626c57f0 (patch)
treef5593d5b4e9977617558e2fb86f7672eaa57f783
parentc8ce3c26893e91ae4e07b1c2d6cf4b89e8c297aa (diff)
util: add new ASSERT_BITFIELD_SIZE() macro (v3)
For checking that bitfields are large enough to hold the largest expected value. v2: move into existing util/macros.h header where STATIC_ASSERT() lives. v3: add MAYBE_UNUSED to variable declaration Reviewed-by: Ian Romanick <[email protected]>
-rw-r--r--src/util/macros.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/util/macros.h b/src/util/macros.h
index a9a52a1a478..aa36253217c 100644
--- a/src/util/macros.h
+++ b/src/util/macros.h
@@ -241,6 +241,23 @@ do { \
#define ATTRIBUTE_NOINLINE
#endif
+
+/**
+ * Check that STRUCT::FIELD can hold MAXVAL. We use a lot of bitfields
+ * in Mesa/gallium. We have to be sure they're of sufficient size to
+ * hold the largest expected value.
+ * Note that with MSVC, enums are signed and enum bitfields need one extra
+ * high bit (always zero) to ensure the max value is handled correctly.
+ * This macro will detect that with MSVC, but not GCC.
+ */
+#define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \
+ do { \
+ MAYBE_UNUSED STRUCT s; \
+ s.FIELD = (MAXVAL); \
+ assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \
+ } while (0)
+
+
/** Compute ceiling of integer quotient of A divided by B. */
#define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )