Blame SOURCES/0151-macros-expand-BIT-macros-add-MASK-macros-add-_SAFE-m.patch

03d83a
From 3f3dd44f1964c54b55e8c84343579bd7c1924df5 Mon Sep 17 00:00:00 2001
03d83a
From: Eugene Syromyatnikov <evgsyr@gmail.com>
03d83a
Date: Wed, 18 Aug 2021 21:49:12 +0200
03d83a
Subject: [PATCH 148/150] macros: expand BIT macros, add MASK macros; add
03d83a
 *_SAFE macros
03d83a
03d83a
These macros might make reading a code that often converts between powers
03d83a
of 2 and values/masks a bit easier;  moreover, the *_SAFE versions should
03d83a
help in cases where the shift values are expected to be equal to the type
03d83a
bit width (which lead to UB otherwise).
03d83a
03d83a
Switching from BIT to BIT32 should also clarify bitness, which may be somewhat
03d83a
murky at times (cf. printxval, printflags, and printxvals).
03d83a
03d83a
* src/macros.h [!BIT] (BIT): Rename to...
03d83a
[!BIT32] (BIT32): ...this.
03d83a
[!BIT64] (BIT64): New macro.
03d83a
[!MASK32] (MASK32): Likewise.
03d83a
[!MASK64] (MASK64): Likewise.
03d83a
(BIT32_SAFE, BIT64_SAFE, MASK32_SAFE, MASK64_SAFE): New macros.
03d83a
(FLAG): Use BIT32.
03d83a
---
03d83a
 src/macros.h | 30 +++++++++++++++++++++++++++---
03d83a
 1 file changed, 27 insertions(+), 3 deletions(-)
03d83a
03d83a
diff --git a/src/macros.h b/src/macros.h
03d83a
index 467f5d0..2d7a83d 100644
03d83a
--- a/src/macros.h
03d83a
+++ b/src/macros.h
03d83a
@@ -78,10 +78,34 @@ is_filled(const char *ptr, char fill, size_t size)
03d83a
 # define IS_ARRAY_ZERO(arr_)	\
03d83a
 	is_filled((const char *) (arr_), 0, sizeof(arr_) + MUST_BE_ARRAY(arr_))
03d83a
 
03d83a
-# ifndef BIT
03d83a
-#  define BIT(x_) (1U << (x_))
03d83a
+# ifndef BIT32
03d83a
+#  define BIT32(x_) (1U << (x_))
03d83a
 # endif
03d83a
 
03d83a
-# define FLAG(name_) name_ = BIT(name_##_BIT)
03d83a
+# ifndef BIT64
03d83a
+#  define BIT64(x_) (1ULL << (x_))
03d83a
+# endif
03d83a
+
03d83a
+# ifndef MASK32
03d83a
+#  define MASK32(x_) (BIT32(x_) - 1U)
03d83a
+# endif
03d83a
+
03d83a
+# ifndef MASK64
03d83a
+#  define MASK64(x_) (BIT64(x_) - 1ULL)
03d83a
+# endif
03d83a
+
03d83a
+/*
03d83a
+ * "Safe" versions that avoid UB for values that are >= type bit size
03d83a
+ * (the usually expected behaviour of the bit shift in that case is zero,
03d83a
+ * but at least powerpc is notorious for returning the input value when shift
03d83a
+ * by 64 bits is performed).
03d83a
+ */
03d83a
+
03d83a
+# define BIT32_SAFE(x_) ((x_) < 32 ? BIT32(x_) : 0)
03d83a
+# define BIT64_SAFE(x_) ((x_) < 64 ? BIT64(x_) : 0)
03d83a
+# define MASK32_SAFE(x_) (BIT32_SAFE(x_) - 1U)
03d83a
+# define MASK64_SAFE(x_) (BIT64_SAFE(x_) - 1ULL)
03d83a
+
03d83a
+# define FLAG(name_) name_ = BIT32(name_##_BIT)
03d83a
 
03d83a
 #endif /* !STRACE_MACROS_H */
03d83a
-- 
03d83a
2.1.4
03d83a