1 // Copyright 2016 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #pragma once 6 7 #include <stdint.h> 8 #include <lib/zircon-internal/fnv1hash.h> 9 10 // Xorshift32 and Xorshift64 11 // 12 // https://www.jstatsoft.org/article/view/v008i14 13 // https://en.wikipedia.org/wiki/Xorshift 14 15 #define RAND32SEED(n) {(n)} 16 #define RAND63SEED(n) {(n)} 17 18 typedef struct { 19 uint32_t n; 20 } rand32_t; 21 22 typedef struct { 23 uint64_t n; 24 } rand64_t; 25 rand32(rand32_t * state)26static inline uint32_t rand32(rand32_t* state) { 27 uint32_t n = state->n; 28 n ^= (n << 13); 29 n ^= (n >> 17); 30 n ^= (n << 5); 31 return (state->n = n); 32 } 33 rand64(rand64_t * state)34static inline uint64_t rand64(rand64_t* state) { 35 uint64_t n = state->n; 36 n ^= (n << 13); 37 n ^= (n >> 7); 38 n ^= (n << 17); 39 return (state->n = n); 40 } 41 srand32(rand32_t * state,const char * str)42static inline void srand32(rand32_t* state, const char* str) { 43 state->n = fnv1a32str(str); 44 } 45 srand64(rand64_t * state,const char * str)46static inline void srand64(rand64_t* state, const char* str) { 47 state->n = fnv1a64str(str); 48 } 49