aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/lib/drm_random.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2016-12-22 03:36:08 -0500
committerDaniel Vetter <daniel.vetter@ffwll.ch>2016-12-27 06:34:00 -0500
commita33d42dd03352d2e9d3d2c00bfa435c7a5ebab25 (patch)
treea2d74a1f6f6e31bdf0788d24c66feee224056003 /drivers/gpu/drm/lib/drm_random.c
parentcf4a7207b1cb4a3c3fe3aa11a83c9d673722a7f5 (diff)
drm: Add a simple generator of random permutations
When testing, we want a random but yet reproducible order in which to process elements. Here we create an array which is a random (using the Tausworthe PRNG) permutation of the order in which to execute. Note these are simple helpers intended to be merged upstream in lib/ v2: Tidier code by David Herrmann v3: Add reminder that this code is intended to be temporary, with at least the bulk of the prandom changes going to lib/ Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> Cc: David Herrmann <dh.herrmann@gmail.com> Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: http://patchwork.freedesktop.org/patch/msgid/20161222083641.2691-6-chris@chris-wilson.co.uk
Diffstat (limited to 'drivers/gpu/drm/lib/drm_random.c')
-rw-r--r--drivers/gpu/drm/lib/drm_random.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/drivers/gpu/drm/lib/drm_random.c b/drivers/gpu/drm/lib/drm_random.c
new file mode 100644
index 000000000000..7b12a68c3b54
--- /dev/null
+++ b/drivers/gpu/drm/lib/drm_random.c
@@ -0,0 +1,41 @@
1#include <linux/bitops.h>
2#include <linux/kernel.h>
3#include <linux/random.h>
4#include <linux/slab.h>
5#include <linux/types.h>
6
7#include "drm_random.h"
8
9static inline u32 drm_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state)
10{
11 return upper_32_bits((u64)prandom_u32_state(state) * ep_ro);
12}
13
14void drm_random_reorder(unsigned int *order, unsigned int count,
15 struct rnd_state *state)
16{
17 unsigned int i, j;
18
19 for (i = 0; i < count; ++i) {
20 BUILD_BUG_ON(sizeof(unsigned int) > sizeof(u32));
21 j = drm_prandom_u32_max_state(count, state);
22 swap(order[i], order[j]);
23 }
24}
25EXPORT_SYMBOL(drm_random_reorder);
26
27unsigned int *drm_random_order(unsigned int count, struct rnd_state *state)
28{
29 unsigned int *order, i;
30
31 order = kmalloc_array(count, sizeof(*order), GFP_TEMPORARY);
32 if (!order)
33 return order;
34
35 for (i = 0; i < count; i++)
36 order[i] = i;
37
38 drm_random_reorder(order, count, state);
39 return order;
40}
41EXPORT_SYMBOL(drm_random_order);