Skip to contentSkip to conversation

lib: Add swap() macro

PATCH In review [email protected] 2 participants 6 messages Dec 5, 2014 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 lib: Add swap() macro

From: Ville Syrjälä <[email protected]>

swap() will swap its two arguments while keeping the required tmp variable hidden. Makes for neater code.

---
 lib/igt_aux.h | 6 ++++++
 1 file changed, 6 insertions(+)
lib/igt_aux.h +6 -0
index 6c83c53..63e1b06 100644
@@ -90,4 +90,10 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
+#define swap(a, b) do {		\
+	typeof(a) _tmp = (a);	\
+	(a) = (b);		\
+	(b) = _tmp;		\
+} while (0)
+
 #endif /* IGT_AUX_H */
Signature · 6 lines
2.0.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/3 lib/igt.cocci: Deal with min/max/swap

From: Ville Syrjälä <[email protected]>

Replace open coded min/max/swap with the macro invocation.

---
 lib/igt.cocci | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
lib/igt.cocci +36 -0
index adebb31..0d337bf 100644
@@ -91,3 +91,39 @@ expression E;
 @@
 - assert(E);
 + igt_assert(E);
+
+// Replace open-coded swap()
+@@
+type T;
+T a, b, tmp;
+@@
+- tmp = a;
+- a = b;
+- b = tmp;
++ swap(a, b);
+
+// Replace open-coded min()
+@@
+expression a;
+expression b;
+@@
+(
+- ((a) < (b) ? (a) : (b))
++ min(a, b)
+|
+- ((a) <= (b) ? (a) : (b))
++ min(a, b)
+)
+
+// Replace open-coded max()
+@@
+expression a;
+expression b;
+@@
+(
+- ((a) > (b) ? (a) : (b))
++ max(a, b)
+|
+- ((a) >= (b) ? (a) : (b))
++ max(a, b)
+)
Signature · 6 lines
2.0.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 3/3 tests: Run lib/igt.cocci

In reply to [email protected]

From: Ville Syrjälä <[email protected]>

Found some open coded min()/max()/swap() macros.

---
 tests/drv_hangman.c     |  4 ++--
 tests/eviction_common.c |  5 +----
 tests/gem_seqno_wrap.c  |  5 +----
 tests/gem_stress.c      |  7 ++-----
 tests/kms_flip.c        | 12 +++---------
 5 files changed, 9 insertions(+), 24 deletions(-)
tests/drv_hangman.c +2 -2
index cdbded2..ec28c9d 100644
@@ -33,6 +33,7 @@
 
 #include "intel_chipset.h"
 #include "drmtest.h"
+#include "igt_aux.h"
 #include "igt_debugfs.h"
 #include "ioctl_wrappers.h"
 
@@ -117,8 +118,7 @@ static void read_dfs(const char *fname, char *d, int maxlen)
 static void _assert_dfs_entry(const char *fname, const char *s, bool inverse)
 {
 	char tmp[1024];
-	const int l = strlen(s) < sizeof(tmp) ?
-		strlen(s) : sizeof(tmp);
+	const int l = min(strlen(s), sizeof(tmp));
 
 	read_dfs(fname, tmp, l + 1);
 	if (!inverse) {
tests/eviction_common.c +1 -4
index 52578e2..4a12dcb 100644
@@ -54,11 +54,8 @@ struct igt_eviction_test_ops
 static void exchange_uint32_t(void *array, unsigned i, unsigned j)
 {
 	uint32_t *i_arr = array;
-	uint32_t i_tmp;
 
-	i_tmp = i_arr[i];
-	i_arr[i] = i_arr[j];
-	i_arr[j] = i_tmp;
+	swap(i_arr[i], i_arr[j]);
 }
 
 static int minor_evictions(int fd, struct igt_eviction_test_ops *ops,
tests/gem_seqno_wrap.c +1 -4
index be4ab3d..7da7fdf 100644
@@ -171,11 +171,8 @@ static void render_copyfunc(struct igt_buf *src,
 static void exchange_uint(void *array, unsigned i, unsigned j)
 {
 	unsigned *i_arr = array;
-	unsigned i_tmp;
 
-	i_tmp = i_arr[i];
-	i_arr[i] = i_arr[j];
-	i_arr[j] = i_tmp;
+	swap(i_arr[i], i_arr[j]);
 }
 
 static void run_sync_test(int num_buffers, bool verify)
tests/gem_stress.c +2 -5
index 6e3a64c..9f20bde 100644
@@ -570,11 +570,8 @@ static void init_set(unsigned set)
 static void exchange_uint(void *array, unsigned i, unsigned j)
 {
 	unsigned *i_arr = array;
-	unsigned i_tmp;
 
-	i_tmp = i_arr[i];
-	i_arr[i] = i_arr[j];
-	i_arr[j] = i_tmp;
+	swap(i_arr[i], i_arr[j]);
 }
 
 static void copy_tiles(unsigned *permutation)
@@ -741,7 +738,7 @@ static void init(void)
 
 	if (options.num_buffers == 0) {
 		tmp = gem_aperture_size(drm_fd);
-		tmp = tmp > 256*(1024*1024) ? 256*(1024*1024) : tmp;
+		tmp = min(256 * (1024 * 1024), tmp);
 		num_buffers = 2 * tmp / options.scratch_buf_size / 3;
 		num_buffers /= 2;
 		igt_info("using %u buffers\n", num_buffers);
tests/kms_flip.c +3 -9
index 041f46a..e579ce0 100644
@@ -165,7 +165,7 @@ static unsigned long gettime_us(void)
 static void emit_dummy_load__bcs(struct test_output *o)
 {
 	int i, limit;
-	drm_intel_bo *dummy_bo, *target_bo, *tmp_bo;
+	drm_intel_bo *dummy_bo, *target_bo;
 	struct igt_fb *fb_info = &o->fb_info[o->current_fb_id];
 	unsigned pitch = fb_info->stride;
 
@@ -197,9 +197,7 @@ static void emit_dummy_load__bcs(struct test_output *o)
 			ADVANCE_BATCH();
 		}
 
-		tmp_bo = dummy_bo;
-		dummy_bo = target_bo;
-		target_bo = tmp_bo;
+		swap(dummy_bo, target_bo);
 	}
 	intel_batchbuffer_flush(batch);
 
@@ -282,16 +280,12 @@ static void emit_dummy_load__rcs(struct test_output *o)
 	dst = &sb[1];
 
 	for (i = 0; i < limit; i++) {
-		struct igt_buf *tmp;
-
 		copyfunc(batch, NULL,
 			 src, 0, 0,
 			 o->fb_width, o->fb_height,
 			 dst, 0, 0);
 
-		tmp = src;
-		src = dst;
-		dst = tmp;
+		swap(src, dst);
 	}
 	intel_batchbuffer_flush(batch);
 
Signature · 6 lines
2.0.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw
In reply to [email protected]
Quoted 15 lines · On Fri, 05 Dec 2014, [email protected] wrote:

From: Ville Syrjälä <[email protected]>

swap() will swap its two arguments while keeping the required tmp variable hidden. Makes for neater code.

---
 lib/igt_aux.h | 6 ++++++
 1 file changed, 6 insertions(+)
lib/igt_aux.h +0 -0
index 6c83c53..63e1b06 100644

lib/igt_aux.h void intel_require_memory(uint32_t count, uint32_t size, unsigned mode); · line 97

Quoted 8 lines
lib/igt_aux.h +5 -0
@@ -90,4 +90,10 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
+#define swap(a, b) do {		\
+	typeof(a) _tmp = (a);	\
+	(a) = (b);		\
+	(b) = _tmp;		\
+} while (0)

Nitpick, make the macro take pointers instead, and amend the cocci patch accordingly? To a grumpy old C coder, swap(&a, &b) is so much more obvious than swap(a, b)...

Jani.

Quoted 9 lines
lib/igt_aux.h +1 -0
+
 #endif /* IGT_AUX_H */
Signature · 6 lines
2.0.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 5 lines
Jani Nikula, Intel Open Source Technology Center
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw
Quoted 19 lines · Jani Nikula
Quoted 18 lines · On Fri, 05 Dec 2014, [email protected] wrote:

From: Ville Syrjälä <[email protected]>

swap() will swap its two arguments while keeping the required tmp variable hidden. Makes for neater code.

---
 lib/igt_aux.h | 6 ++++++
 1 file changed, 6 insertions(+)
lib/igt_aux.h +0 -0
index 6c83c53..63e1b06 100644
@@ -90,4 +90,10 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
Quoted 9 lines

lib/igt_aux.h void intel_require_memory(uint32_t count, uint32_t size, unsigned mode); · line 97

Quoted 5 lines
lib/igt_aux.h +5 -0
+#define swap(a, b) do {		\
+	typeof(a) _tmp = (a);	\
+	(a) = (b);		\
+	(b) = _tmp;		\
+} while (0)

Nitpick, make the macro take pointers instead, and amend the cocci patch accordingly? To a grumpy old C coder, swap(&a, &b) is so much more obvious than swap(a, b)...

But then it's different than the kernel swap() we all love.

Quoted 16 lines

Jani.

Quoted 9 lines
lib/igt_aux.h +1 -0
+
 #endif /* IGT_AUX_H */
Signature · 6 lines
2.0.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 1 line
Jani Nikula, Intel Open Source Technology Center
Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw
Quoted 30 lines · Ville Syrjälä
Quoted 19 lines · Jani Nikula
Quoted 18 lines · On Fri, 05 Dec 2014, [email protected] wrote:

From: Ville Syrjälä <[email protected]>

swap() will swap its two arguments while keeping the required tmp variable hidden. Makes for neater code.

---
 lib/igt_aux.h | 6 ++++++
 1 file changed, 6 insertions(+)
lib/igt_aux.h +0 -0
index 6c83c53..63e1b06 100644
@@ -90,4 +90,10 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
Quoted 9 lines

lib/igt_aux.h void intel_require_memory(uint32_t count, uint32_t size, unsigned mode); · line 97

Quoted 5 lines
lib/igt_aux.h +5 -0
+#define swap(a, b) do {		\
+	typeof(a) _tmp = (a);	\
+	(a) = (b);		\
+	(b) = _tmp;		\
+} while (0)

Nitpick, make the macro take pointers instead, and amend the cocci patch accordingly? To a grumpy old C coder, swap(&a, &b) is so much more obvious than swap(a, b)...

Quoted 1 line

But then it's different than the kernel swap() we all love.

Fair enough. I'll cry a little. I'll feel better soon.

Quoted 21 lines
Quoted 17 lines

Jani.

Quoted 9 lines
lib/igt_aux.h +1 -0
+
 #endif /* IGT_AUX_H */
Signature · 6 lines
2.0.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 1 line
Jani Nikula, Intel Open Source Technology Center
Signature · 2 lines
Ville Syrjälä
Intel OTC
Signature · 5 lines
Jani Nikula, Intel Open Source Technology Center
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx