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

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