Ruby: include/ruby/random.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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
39 RBIMPL_SYMBOL_EXPORT_BEGIN()
40
41
49struct rb_random_struct {
51 VALUE seed;
52};
53 typedef struct rb_random_struct rb_random_t;
55 RBIMPL_ATTR_NONNULL(())
67typedef void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len);
68
69 RBIMPL_ATTR_NONNULL(())
78typedef void rb_random_init_int32_func(rb_random_t *rng, uint32_t data);
79
80 RBIMPL_ATTR_NONNULL(())
88typedef unsigned int rb_random_get_int32_func(rb_random_t *rng);
89
90 RBIMPL_ATTR_NONNULL(())
100typedef void rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len);
101
102 RBIMPL_ATTR_NONNULL(())
111typedef double rb_random_get_real_func(rb_random_t *rng, int excl);
112
114typedef struct {
116 size_t default_seed_bits;
117
121 struct {
122 uint8_t major, minor;
123 } version;
124
128 uint16_t flags;
129
131 rb_random_init_func *init;
132
134 rb_random_init_int32_func *init_int32;
135
137 rb_random_get_int32_func *get_int32;
138
155 rb_random_get_bytes_func *get_bytes;
156
175 rb_random_get_real_func *get_real;
176} rb_random_interface_t;
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__
232typedef rb_data_type_t rb_random_data_type_t;
233# define RB_RANDOM_PARENT 0
234#else
235
237 typedef const rb_data_type_t rb_random_data_type_t;
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
284void rb_random_base_init(rb_random_t *rnd);
285
300double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl);
301
314void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size);
315
320 RUBY_EXTERN const rb_data_type_t rb_random_data_type;
321
322 RBIMPL_SYMBOL_EXPORT_END()
323
324 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
325/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
332static inline const rb_random_interface_t *
333rb_rand_if(VALUE obj)
334{
335 RBIMPL_ASSERT_OR_ASSUME(RTYPEDDATA_P(obj));
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
341 RBIMPL_ATTR_NOALIAS()
351static inline void
352rbimpl_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 */
RBIMPL_ASSERT_OR_ASSUME
#define RBIMPL_ASSERT_OR_ASSUME(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:229
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:47
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:106
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:97
rb_random_mark
void rb_random_mark(void *ptr)
This is the implementation of rb_data_type_struct::dmark for rb_random_data_type.
rb_random_get_bytes_func
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
rb_random_init_func
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
rb_rand_bytes_int32
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:1272
rb_random_get_int32_func
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
rb_random_data_type_t
const rb_data_type_t rb_random_data_type_t
This is the type of rb_random_data_type.
Definition random.h:237
rb_int_pair_to_real
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:1126
rb_rand_if
static const rb_random_interface_t * rb_rand_if(VALUE obj)
Queries the interface of the passed random object.
Definition random.h:333
rb_random_get_real_func
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
rb_random_init_int32_func
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
rb_random_base_init
void rb_random_base_init(rb_random_t *rnd)
Initialises an allocated rb_random_t instance.
Definition random.c:329
RBIMPL_ATTR_NOALIAS
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition noalias.h:66
RBIMPL_ATTR_NONNULL
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
RBIMPL_ATTR_PURE_UNLESS_DEBUG
#define RBIMPL_ATTR_PURE_UNLESS_DEBUG()
Enables RBIMPL_ATTR_PURE if and only if.
Definition pure.h:38
RTYPEDDATA_P
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:540
RTYPEDDATA_TYPE
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:563
ruby.h
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:190
rb_random_interface_t
PRNG algorithmic interface, analogous to Ruby level classes.
Definition random.h:114
rb_random_interface_t::init
rb_random_init_func * init
Function to initialize from uint32_t array.
Definition random.h:131
rb_random_interface_t::init_int32
rb_random_init_int32_func * init_int32
Function to initialize from single uint32_t.
Definition random.h:134
rb_random_interface_t::default_seed_bits
size_t default_seed_bits
Number of bits of seed numbers.
Definition random.h:116
rb_random_interface_t::get_int32
rb_random_get_int32_func * get_int32
Function to obtain a random integer.
Definition random.h:137
rb_random_interface_t::flags
uint16_t flags
Reserved flags.
Definition random.h:128
rb_random_interface_t::get_real
rb_random_get_real_func * get_real
Function to obtain a random double.
Definition random.h:175
rb_random_interface_t::get_bytes
rb_random_get_bytes_func * get_bytes
Function to obtain a series of random bytes.
Definition random.h:155
rb_random_struct
Base components of the random interface.
Definition random.h:49
rb_random_struct::seed
VALUE seed
Seed, passed through e.g.
Definition random.h:51
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40