Chris Wilson | 376b813 | 2016-07-03 09:42:38 +0100 | [diff] [blame] | 1 | #include "igt_rand.h" |
| 2 | |
Paul Kocialkowski | 76bce77 | 2017-07-20 17:11:52 +0300 | [diff] [blame] | 3 | /** |
| 4 | * SECTION:igt_rand |
| 5 | * @short_description: Random numbers helper library |
| 6 | * @title: Random |
| 7 | * @include: igt_rand.h |
| 8 | */ |
| 9 | |
Chris Wilson | 62a1f54 | 2017-05-09 12:42:41 +0100 | [diff] [blame] | 10 | static uint32_t global = 0x12345678; |
Chris Wilson | 376b813 | 2016-07-03 09:42:38 +0100 | [diff] [blame] | 11 | |
Chris Wilson | 62a1f54 | 2017-05-09 12:42:41 +0100 | [diff] [blame] | 12 | uint32_t hars_petruska_f54_1_random_seed(uint32_t new_state) |
Chris Wilson | 376b813 | 2016-07-03 09:42:38 +0100 | [diff] [blame] | 13 | { |
Chris Wilson | 62a1f54 | 2017-05-09 12:42:41 +0100 | [diff] [blame] | 14 | uint32_t old_state = global; |
| 15 | global = new_state; |
Chris Wilson | 376b813 | 2016-07-03 09:42:38 +0100 | [diff] [blame] | 16 | return old_state; |
| 17 | } |
| 18 | |
Chris Wilson | 62a1f54 | 2017-05-09 12:42:41 +0100 | [diff] [blame] | 19 | uint32_t hars_petruska_f54_1_random(uint32_t *s) |
Chris Wilson | 376b813 | 2016-07-03 09:42:38 +0100 | [diff] [blame] | 20 | { |
| 21 | #define rol(x,k) ((x << k) | (x >> (32-k))) |
Chris Wilson | 62a1f54 | 2017-05-09 12:42:41 +0100 | [diff] [blame] | 22 | return *s = (*s ^ rol(*s, 5) ^ rol(*s, 24)) + 0x37798849; |
Chris Wilson | 376b813 | 2016-07-03 09:42:38 +0100 | [diff] [blame] | 23 | #undef rol |
| 24 | } |
Chris Wilson | 62a1f54 | 2017-05-09 12:42:41 +0100 | [diff] [blame] | 25 | |
| 26 | uint32_t hars_petruska_f54_1_random_unsafe(void) |
| 27 | { |
| 28 | return hars_petruska_f54_1_random(&global); |
| 29 | } |