blob: 664a4764f1b2a922a234deb5c962e337c6da461b [file] [log] [blame]
Chris Wilson376b8132016-07-03 09:42:38 +01001#include "igt_rand.h"
2
Paul Kocialkowski76bce772017-07-20 17:11:52 +03003/**
4 * SECTION:igt_rand
5 * @short_description: Random numbers helper library
6 * @title: Random
7 * @include: igt_rand.h
8 */
9
Chris Wilson62a1f542017-05-09 12:42:41 +010010static uint32_t global = 0x12345678;
Chris Wilson376b8132016-07-03 09:42:38 +010011
Chris Wilson62a1f542017-05-09 12:42:41 +010012uint32_t hars_petruska_f54_1_random_seed(uint32_t new_state)
Chris Wilson376b8132016-07-03 09:42:38 +010013{
Chris Wilson62a1f542017-05-09 12:42:41 +010014 uint32_t old_state = global;
15 global = new_state;
Chris Wilson376b8132016-07-03 09:42:38 +010016 return old_state;
17}
18
Chris Wilson62a1f542017-05-09 12:42:41 +010019uint32_t hars_petruska_f54_1_random(uint32_t *s)
Chris Wilson376b8132016-07-03 09:42:38 +010020{
21#define rol(x,k) ((x << k) | (x >> (32-k)))
Chris Wilson62a1f542017-05-09 12:42:41 +010022 return *s = (*s ^ rol(*s, 5) ^ rol(*s, 24)) + 0x37798849;
Chris Wilson376b8132016-07-03 09:42:38 +010023#undef rol
24}
Chris Wilson62a1f542017-05-09 12:42:41 +010025
26uint32_t hars_petruska_f54_1_random_unsafe(void)
27{
28 return hars_petruska_f54_1_random(&global);
29}