Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
random.h
Go to the documentation of this file.
1 #ifndef RUBY_RANDOM_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RUBY_RANDOM_H 1
17 #include "ruby/ruby.h"
18 
19 /*
20  * version
21  * 0: before versioning; deprecated
22  * 1: added version, flags and init_32bit function
23  */
24 #define RUBY_RANDOM_INTERFACE_VERSION_MAJOR 1
25 #define RUBY_RANDOM_INTERFACE_VERSION_MINOR 0
26 
27 #define RUBY_RANDOM_PASTE_VERSION_SUFFIX(x, y, z) x##_##y##_##z
28 #define RUBY_RANDOM_WITH_VERSION_SUFFIX(name, major, minor) \
29  RUBY_RANDOM_PASTE_VERSION_SUFFIX(name, major, minor)
30 #define rb_random_data_type \
31  RUBY_RANDOM_WITH_VERSION_SUFFIX(rb_random_data_type, \
32  RUBY_RANDOM_INTERFACE_VERSION_MAJOR, \
33  RUBY_RANDOM_INTERFACE_VERSION_MINOR)
34 #define RUBY_RANDOM_INTERFACE_VERSION_INITIALIZER \
35  {RUBY_RANDOM_INTERFACE_VERSION_MAJOR, RUBY_RANDOM_INTERFACE_VERSION_MINOR}
36 #define RUBY_RANDOM_INTERFACE_VERSION_MAJOR_MAX 0xff
37 #define RUBY_RANDOM_INTERFACE_VERSION_MINOR_MAX 0xff
38 
40 
41 
52 };
53 typedef struct rb_random_struct rb_random_t;
67 typedef void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len);
68 
78 typedef void rb_random_init_int32_func(rb_random_t *rng, uint32_t data);
79 
88 typedef unsigned int rb_random_get_int32_func(rb_random_t *rng);
89 
100 typedef void rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len);
101 
111 typedef double rb_random_get_real_func(rb_random_t *rng, int excl);
112 
114 typedef struct {
117 
121  struct {
122  uint8_t major, minor;
123  } version;
124 
128  uint16_t flags;
129 
132 
135 
138 
156 
177 
182 #define RB_RANDOM_INTERFACE_DECLARE(prefix) \
183  static void prefix##_init(rb_random_t *, const uint32_t *, size_t); \
184  static void prefix##_init_int32(rb_random_t *, uint32_t); \
185  static unsigned int prefix##_get_int32(rb_random_t *); \
186  static void prefix##_get_bytes(rb_random_t *, void *, size_t)
187 
192 #define RB_RANDOM_INTERFACE_DECLARE_WITH_REAL(prefix) \
193  RB_RANDOM_INTERFACE_DECLARE(prefix); \
194  static double prefix##_get_real(rb_random_t *, int)
195 
210 #define RB_RANDOM_INTERFACE_DEFINE(prefix) \
211  RUBY_RANDOM_INTERFACE_VERSION_INITIALIZER, 0, \
212  prefix##_init, \
213  prefix##_init_int32, \
214  prefix##_get_int32, \
215  prefix##_get_bytes
216 
221 #define RB_RANDOM_INTERFACE_DEFINE_WITH_REAL(prefix) \
222  RB_RANDOM_INTERFACE_DEFINE(prefix), \
223  prefix##_get_real
224 
225 #define RB_RANDOM_DEFINE_INIT_INT32_FUNC(prefix) \
226  static void prefix##_init_int32(rb_random_t *rnd, uint32_t data) \
227  { \
228  prefix##_init(rnd, &data, 1); \
229  }
230 
231 #if defined _WIN32 && !defined __CYGWIN__
233 # define RB_RANDOM_PARENT 0
234 #else
235 
238 
256 # define RB_RANDOM_PARENT &rb_random_data_type
257 #endif
258 
265 #define RB_RANDOM_DATA_INIT_PARENT(random_data) \
266  rbimpl_random_data_init_parent(&random_data)
267 
275 void rb_random_mark(void *ptr);
276 
285 
300 double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl);
301 
314 void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size);
315 
321 
323 
325 /* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
332 static inline const rb_random_interface_t *
334 {
336  const struct rb_data_type_struct *t = RTYPEDDATA_TYPE(obj);
337  const void *ret = t->data;
338  return RBIMPL_CAST((const rb_random_interface_t *)ret);
339 }
340 
351 static inline void
352 rbimpl_random_data_init_parent(rb_random_data_type_t *random_data)
353 {
354 #if defined _WIN32 && !defined __CYGWIN__
355  random_data->parent = &rb_random_data_type;
356 #endif
357 }
358 
359 #endif /* RUBY_RANDOM_H */
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition: assert.h:311
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:45
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:65
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
int len
Length of the buffer.
Definition: io.h:8
static const rb_random_interface_t * rb_rand_if(VALUE obj)
Queries the interface of the passed random object.
Definition: random.h:333
const rb_data_type_t rb_random_data_type
The data that holds the backend type of rb_cRandom.
Definition: random.c:259
void rb_random_mark(void *ptr)
This is the implementation of rb_data_type_struct::dmark for rb_random_data_type.
void rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len)
This is the type of functions called from your object's #bytes method.
Definition: random.h:100
void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len)
This is the type of functions called when your random object is initialised.
Definition: random.h:67
void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size)
Repeatedly calls the passed function over and over again until the passed buffer is filled with rando...
Definition: random.c:1283
unsigned int rb_random_get_int32_func(rb_random_t *rng)
This is the type of functions called from your object's #rand method.
Definition: random.h:88
const rb_data_type_t rb_random_data_type_t
This is the type of rb_random_data_type.
Definition: random.h:237
double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl)
Generates a 64 bit floating point number by concatenating two 32bit unsigned integers.
Definition: random.c:1137
double rb_random_get_real_func(rb_random_t *rng, int excl)
This is the type of functions called from your object's #rand method.
Definition: random.h:111
void rb_random_init_int32_func(rb_random_t *rng, uint32_t data)
This is the type of functions called when your random object is initialised.
Definition: random.h:78
void rb_random_base_init(rb_random_t *rnd)
Initialises an allocated rb_random_t instance.
Definition: random.c:336
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:62
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition: defines.h:88
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition: pure.h:38
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition: rtypeddata.h:579
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition: rtypeddata.h:602
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:200
PRNG algorithmic interface, analogous to Ruby level classes.
Definition: random.h:114
rb_random_init_func * init
Function to initialize from uint32_t array.
Definition: random.h:131
rb_random_init_int32_func * init_int32
Function to initialize from single uint32_t.
Definition: random.h:134
size_t default_seed_bits
Number of bits of seed numbers.
Definition: random.h:116
rb_random_get_int32_func * get_int32
Function to obtain a random integer.
Definition: random.h:137
uint16_t flags
Reserved flags.
Definition: random.h:128
rb_random_get_real_func * get_real
Function to obtain a random double.
Definition: random.h:175
rb_random_get_bytes_func * get_bytes
Function to obtain a series of random bytes.
Definition: random.h:155
Base components of the random interface.
Definition: random.h:49
VALUE seed
Seed, passed through e.g.
Definition: random.h:51
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40