[igt-dev] [PATCH i-g-t 3/3] lib: Add roundup_power_of_two()

Ville Syrjala ville.syrjala at linux.intel.com
Mon Sep 10 15:34:07 UTC 2018


From: Ville Syrjälä <ville.syrjala at linux.intel.com>

Add a helper to round a value to the next power of two size.

And since we need fls() to implement that, reuse that elsewhere
as well.

Signed-off-by: Ville Syrjälä <ville.syrjala at linux.intel.com>
---
 lib/igt_aux.h          |  6 ++++++
 lib/igt_fb.c           | 12 ++++--------
 tests/gem_exec_reuse.c | 12 +-----------
 3 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/lib/igt_aux.h b/lib/igt_aux.h
index ef89faa9bd30..192c3ad88658 100644
--- a/lib/igt_aux.h
+++ b/lib/igt_aux.h
@@ -287,4 +287,10 @@ void igt_lsof(const char *dpath);
 
 #define is_power_of_two(x)  (((x) & ((x)-1)) == 0)
 
+#define igt_fls(x) ((x) ? __builtin_choose_expr(sizeof(x) == 8, \
+						64 - __builtin_clzll(x), \
+						32 - __builtin_clz(x)) : 0)
+
+#define roundup_power_of_two(x) ((x) != 0 ? 1 << igt_fls((x) - 1) : 0)
+
 #endif /* IGT_AUX_H */
diff --git a/lib/igt_fb.c b/lib/igt_fb.c
index 26720a7bc2ac..4af1881de959 100644
--- a/lib/igt_fb.c
+++ b/lib/igt_fb.c
@@ -261,8 +261,6 @@ static void calc_fb_size_packed(int fd, int width, int height,
 
 	if (tiling != LOCAL_DRM_FORMAT_MOD_NONE &&
 	    intel_gen(intel_get_drm_devid(fd)) <= 3) {
-		int v;
-
 		/* Round the tiling up to the next power-of-two and the region
 		 * up to the next pot fence size so that this works on all
 		 * generations.
@@ -271,13 +269,11 @@ static void calc_fb_size_packed(int fd, int width, int height,
 		 * tiled. But then that failure is expected.
 		 */
 
-		v = byte_width;
-		for (stride = 512; stride < v; stride *= 2)
-			;
+		stride = max(byte_width, 512);
+		stride = roundup_power_of_two(stride);
 
-		v = stride * height;
-		for (size = 1024*1024; size < v; size *= 2)
-			;
+		size = max((uint64_t) stride * height, 1024*1024);
+		size = roundup_power_of_two(size);
 	} else {
 		stride = ALIGN(byte_width, tile_width);
 		size = (uint64_t) stride * ALIGN(height, tile_height);
diff --git a/tests/gem_exec_reuse.c b/tests/gem_exec_reuse.c
index 8ee38d2b6f20..df220be7bab8 100644
--- a/tests/gem_exec_reuse.c
+++ b/tests/gem_exec_reuse.c
@@ -56,16 +56,6 @@ static void noop(struct noop *n,
 	gem_execbuf(n->fd, &execbuf);
 }
 
-static int fls(uint64_t x)
-{
-	int t;
-
-	for (t = 0; x >> t; t++)
-		;
-
-	return t;
-}
-
 static bool allow_unlimited_files(void)
 {
 	struct rlimit rlim;
@@ -151,7 +141,7 @@ igt_main
 		if (max < gtt_size)
 			gtt_size = max;
 
-		no.nhandles = 1 << (fls(gtt_size) - 1);
+		no.nhandles = 1 << (igt_fls(gtt_size) - 1);
 		intel_require_memory(no.nhandles, 4096, CHECK_RAM);
 
 		no.max_age = no.nhandles / 2;
-- 
2.16.4



More information about the igt-dev mailing list