57 lines
1.6 KiB
Diff
57 lines
1.6 KiB
Diff
|
|
From d568dc527cd3098df2460d3e991efe3ad8b80410 Mon Sep 17 00:00:00 2001
|
||
|
|
From: Michal Privoznik <mprivozn@redhat.com>
|
||
|
|
Date: Wed, 4 Nov 2020 19:41:27 +0100
|
||
|
|
Subject: [PATCH] internal.h: Introduce and use VIR_IS_POW2()
|
||
|
|
|
||
|
|
This macro checks whether given number is an integer power of
|
||
|
|
two. At the same time, I've identified two places where we check
|
||
|
|
for pow2 and I'm replacing them with the macro.
|
||
|
|
|
||
|
|
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
|
||
|
|
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
|
||
|
|
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
|
||
|
|
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
|
||
|
|
Tested-by: Han Han <hhan@redhat.com>
|
||
|
|
Reviewed-by: Shaokun Wei <weishaokun@kylinos.cn>
|
||
|
|
---
|
||
|
|
src/internal.h | 9 +++++++++
|
||
|
|
src/util/virrandom.c | 2 +-
|
||
|
|
2 files changed, 10 insertions(+), 1 deletion(-)
|
||
|
|
|
||
|
|
diff --git a/src/internal.h b/src/internal.h
|
||
|
|
index 440f01d370..40c993977a 100644
|
||
|
|
--- a/src/internal.h
|
||
|
|
+++ b/src/internal.h
|
||
|
|
@@ -216,6 +216,15 @@
|
||
|
|
(a) = (a) ^ (b); \
|
||
|
|
} while (0)
|
||
|
|
|
||
|
|
+/**
|
||
|
|
+ * VIR_IS_POW2:
|
||
|
|
+ *
|
||
|
|
+ * Returns true if given number is a power of two
|
||
|
|
+ */
|
||
|
|
+#define VIR_IS_POW2(x) \
|
||
|
|
+ ((x) && !((x) & ((x) - 1)))
|
||
|
|
+
|
||
|
|
+
|
||
|
|
/**
|
||
|
|
* virCheckFlags:
|
||
|
|
* @supported: an OR'ed set of supported flags
|
||
|
|
diff --git a/src/util/virrandom.c b/src/util/virrandom.c
|
||
|
|
index 1b4102cf58..500f55c956 100644
|
||
|
|
--- a/src/util/virrandom.c
|
||
|
|
+++ b/src/util/virrandom.c
|
||
|
|
@@ -89,7 +89,7 @@ double virRandom(void)
|
||
|
|
*/
|
||
|
|
uint32_t virRandomInt(uint32_t max)
|
||
|
|
{
|
||
|
|
- if ((max & (max - 1)) == 0)
|
||
|
|
+ if (VIR_IS_POW2(max))
|
||
|
|
return virRandomBits(__builtin_ffs(max) - 1);
|
||
|
|
|
||
|
|
double val = virRandom();
|
||
|
|
--
|
||
|
|
2.27.0
|
||
|
|
|