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