[igt-dev] [PATCH i-g-t 2/6] igt/shell: Extend Math to perform simple array permutations

Chris Wilson chris at chris-wilson.co.uk
Mon Aug 20 13:01:45 UTC 2018


Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
---
 shell/igt-math.c  | 91 +++++++++++++++++++++++++++++++++++++++++++++++
 shell/meson.build |  1 +
 2 files changed, 92 insertions(+)
 create mode 100644 shell/igt-math.c

diff --git a/shell/igt-math.c b/shell/igt-math.c
new file mode 100644
index 000000000..19b32a9d1
--- /dev/null
+++ b/shell/igt-math.c
@@ -0,0 +1,91 @@
+#include "duktape.h"
+#include "igt-builtins.h"
+
+static uint32_t prng = 0x12345678;
+
+static uint32_t hars_petruska_f54_1_random(uint32_t *s)
+{
+#define rol(x,k) ((x << k) | (x >> (32-k)))
+        return *s = (*s ^ rol(*s, 5) ^ rol(*s, 24)) + 0x37798849;
+#undef rol
+}
+
+static inline uint32_t
+hars_petruska_f54_1_random_max(uint32_t *s, uint32_t ep_ro)
+{
+        return ((uint64_t)hars_petruska_f54_1_random(s) * ep_ro) >> 32;
+}
+
+static duk_ret_t igt_math_permute_array(duk_context *ctx)
+{
+	duk_size_t count;
+
+	duk_get_prop_string(ctx, -1, "length");
+	count = duk_get_uint(ctx, -1);
+	duk_pop(ctx);
+
+	for (duk_size_t i = 0; i < count; i++) {
+		duk_size_t j = hars_petruska_f54_1_random_max(&prng, count);
+
+		duk_get_prop_index(ctx, -1, i);
+		duk_get_prop_index(ctx, -2, j);
+
+		duk_put_prop_index(ctx, -3, i);
+		duk_put_prop_index(ctx, -2, j);
+	}
+
+	return 1;
+}
+
+static duk_ret_t igt_math_permute_buffer(duk_context *ctx)
+{
+	duk_size_t sz, el, count;
+	char tmp[8];
+	void *ptr;
+
+	ptr = duk_require_buffer_data(ctx, -1, &sz);
+
+	duk_get_prop_string(ctx, -1, "BYTES_PER_ELEMENT");
+	el = duk_get_uint(ctx, -1);
+	count = sz / el;
+	duk_pop(ctx);
+
+	for (duk_size_t i = 0; i < count; i++) {
+		duk_size_t j = hars_petruska_f54_1_random_max(&prng, count);
+
+		memcpy(tmp, ptr + j * el, el);
+		memcpy(ptr + j * el, ptr + i * el, el);
+		memcpy(ptr + i * el, tmp, el);
+	}
+
+	return 1;
+}
+
+static duk_ret_t igt_math_permute(duk_context *ctx)
+{
+	if (duk_is_array(ctx, -1))
+		return igt_math_permute_array(ctx);
+	else
+		return igt_math_permute_buffer(ctx);
+}
+
+static duk_ret_t igt_math_builtin_ctor(duk_context *ctx)
+{
+	duk_get_global_string(ctx, "Math");
+
+	duk_push_c_function(ctx, igt_math_permute, 1);
+	duk_put_prop_string(ctx, -2, "permute");
+
+	duk_pop(ctx);
+	return 0;
+}
+
+__attribute__((constructor))
+static void __drm_register_builtin__(void)
+{
+	static struct igt_builtin builtin = {
+		.name = "math",
+		.ctor = igt_math_builtin_ctor
+	};
+	igt_register_global(&builtin);
+}
diff --git a/shell/meson.build b/shell/meson.build
index 7eb081a85..53b0754e9 100644
--- a/shell/meson.build
+++ b/shell/meson.build
@@ -1,6 +1,7 @@
 executable('igt', [
 	     'duktape/duktape.c',
 	     'igt-builtins.c',
+	     'igt-math.c',
 	     'igt-shell.c',
 	   ], dependencies : [ math, libkmod ],
 	   include_directories : include_directories('include', '../include')
-- 
2.18.0



More information about the igt-dev mailing list