All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_random.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2016 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /******************************************************************************
27  * Types
28  *****************************************************************************/
29 
30 /**
31  * Random seeds used in xorshift128+ algorithm: http://xorshift.di.unimi.it
32  * Not thread-safe. Instantiate once per thread.
33  */
34 typedef struct as_random_s {
35  uint64_t seed0;
36  uint64_t seed1;
38 } as_random;
39 
40 /**
41  * Thread local random instance. Do not access directly.
42  */
43 extern __thread as_random as_rand;
44 
45 /*******************************************************************************
46  * Functions
47  ******************************************************************************/
48 
49 /**
50  * Initialize random instance.
51  */
52 void
53 as_random_init(as_random* random);
54 
55 /**
56  * Get thread local random instance.
57  */
58 static inline as_random*
60 {
61  as_random* random = &as_rand;
62 
63  if (! random->initialized) {
64  as_random_init(random);
65  }
66  return random;
67 }
68 
69 /**
70  * Get random unsigned 64 bit integer from given as_random instance
71  * using xorshift128+ algorithm: http://xorshift.di.unimi.it
72  */
73 static inline uint64_t
75 {
76  // Use xorshift128+ algorithm.
77  uint64_t s1 = random->seed0;
78  const uint64_t s0 = random->seed1;
79  random->seed0 = s0;
80  s1 ^= s1 << 23;
81  random->seed1 = (s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5));
82  return random->seed1 + s0;
83 }
84 
85 /**
86  * Get random unsigned 32 bit integer from given as_random instance.
87  */
88 static inline uint32_t
90 {
91  return (uint32_t)as_random_next_uint64(random);
92 }
93 
94 /**
95  * Get random bytes of specified length from given as_random instance.
96  */
97 void
98 as_random_next_bytes(as_random* random, uint8_t* bytes, uint32_t len);
99 
100 /**
101  * Get random unsigned 64 bit integer from thread local instance.
102  */
103 uint64_t
105 
106 /**
107  * Get random unsigned 32 bit integer from thread local instance.
108  */
109 static inline uint32_t
111 {
112  return (uint32_t)as_random_get_uint64();
113 }
114 
115 /**
116  * Get random bytes of specified length from thread local instance.
117  */
118 static inline void
119 as_random_get_bytes(uint8_t* bytes, uint32_t len)
120 {
121  as_random* random = as_random_instance();
122  as_random_next_bytes(random, bytes, len);
123 }
124 
125 #ifdef __cplusplus
126 } // end extern "C"
127 #endif
static uint32_t as_random_get_uint32()
Definition: as_random.h:110
uint64_t seed1
Definition: as_random.h:36
static uint32_t as_random_next_uint32(as_random *random)
Definition: as_random.h:89
static uint64_t as_random_next_uint64(as_random *random)
Definition: as_random.h:74
__thread as_random as_rand
static as_random * as_random_instance()
Definition: as_random.h:59
void as_random_init(as_random *random)
bool initialized
Definition: as_random.h:37
uint64_t seed0
Definition: as_random.h:35
uint64_t as_random_get_uint64()
void as_random_next_bytes(as_random *random, uint8_t *bytes, uint32_t len)
static void as_random_get_bytes(uint8_t *bytes, uint32_t len)
Definition: as_random.h:119