Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby32/include/ruby
File: random.h
#ifndef RUBY_RANDOM_H /*-*-C++-*-vi:se ft=cpp:*/
[0] Fix | Delete
#define RUBY_RANDOM_H 1
[1] Fix | Delete
/**
[2] Fix | Delete
* @file
[3] Fix | Delete
* @date Sat May 7 11:51:14 JST 2016
[4] Fix | Delete
* @copyright 2007-2020 Yukihiro Matsumoto
[5] Fix | Delete
* @copyright This file is a part of the programming language Ruby.
[6] Fix | Delete
* Permission is hereby granted, to either redistribute and/or
[7] Fix | Delete
* modify this file, provided that the conditions mentioned in the
[8] Fix | Delete
* file COPYING are met. Consult the file for details.
[9] Fix | Delete
*
[10] Fix | Delete
* This is a set of APIs to roll your own subclass of ::rb_cRandom. An
[11] Fix | Delete
* illustrative example of such PRNG can be found at
[12] Fix | Delete
* `ext/-test-/ramdom/loop.c`.
[13] Fix | Delete
*/
[14] Fix | Delete
[15] Fix | Delete
#include "ruby/ruby.h"
[16] Fix | Delete
[17] Fix | Delete
/*
[18] Fix | Delete
* version
[19] Fix | Delete
* 0: before versioning; deprecated
[20] Fix | Delete
* 1: added version, flags and init_32bit function
[21] Fix | Delete
*/
[22] Fix | Delete
#define RUBY_RANDOM_INTERFACE_VERSION_MAJOR 1
[23] Fix | Delete
#define RUBY_RANDOM_INTERFACE_VERSION_MINOR 0
[24] Fix | Delete
[25] Fix | Delete
#define RUBY_RANDOM_PASTE_VERSION_SUFFIX(x, y, z) x##_##y##_##z
[26] Fix | Delete
#define RUBY_RANDOM_WITH_VERSION_SUFFIX(name, major, minor) \
[27] Fix | Delete
RUBY_RANDOM_PASTE_VERSION_SUFFIX(name, major, minor)
[28] Fix | Delete
#define rb_random_data_type \
[29] Fix | Delete
RUBY_RANDOM_WITH_VERSION_SUFFIX(rb_random_data_type, \
[30] Fix | Delete
RUBY_RANDOM_INTERFACE_VERSION_MAJOR, \
[31] Fix | Delete
RUBY_RANDOM_INTERFACE_VERSION_MINOR)
[32] Fix | Delete
#define RUBY_RANDOM_INTERFACE_VERSION_INITIALIZER \
[33] Fix | Delete
{RUBY_RANDOM_INTERFACE_VERSION_MAJOR, RUBY_RANDOM_INTERFACE_VERSION_MINOR}
[34] Fix | Delete
#define RUBY_RANDOM_INTERFACE_VERSION_MAJOR_MAX 0xff
[35] Fix | Delete
#define RUBY_RANDOM_INTERFACE_VERSION_MINOR_MAX 0xff
[36] Fix | Delete
[37] Fix | Delete
RBIMPL_SYMBOL_EXPORT_BEGIN()
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Base components of the random interface.
[41] Fix | Delete
*
[42] Fix | Delete
* @internal
[43] Fix | Delete
*
[44] Fix | Delete
* Ideally this could be an empty class if we could assume C++, but in C a
[45] Fix | Delete
* struct must have at least one field.
[46] Fix | Delete
*/
[47] Fix | Delete
struct rb_random_struct {
[48] Fix | Delete
/** Seed, passed through e.g. `Random.new` */
[49] Fix | Delete
VALUE seed;
[50] Fix | Delete
};
[51] Fix | Delete
typedef struct rb_random_struct rb_random_t; /**< @see ::rb_random_struct */
[52] Fix | Delete
[53] Fix | Delete
RBIMPL_ATTR_NONNULL(())
[54] Fix | Delete
/**
[55] Fix | Delete
* This is the type of functions called when your random object is initialised.
[56] Fix | Delete
* Passed buffer is the seed object basically. But in Ruby a number can be
[57] Fix | Delete
* really big. This type of functions accept such big integers as a series of
[58] Fix | Delete
* machine words.
[59] Fix | Delete
*
[60] Fix | Delete
* @param[out] rng Your random struct to fill in.
[61] Fix | Delete
* @param[in] buf Seed, maybe converted from a bignum.
[62] Fix | Delete
* @param[in] len Number of words of `buf`.
[63] Fix | Delete
* @post `rng` is initialised using the passed seeds.
[64] Fix | Delete
*/
[65] Fix | Delete
typedef void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t len);
[66] Fix | Delete
[67] Fix | Delete
RBIMPL_ATTR_NONNULL(())
[68] Fix | Delete
/**
[69] Fix | Delete
* This is the type of functions called when your random object is initialised.
[70] Fix | Delete
* Passed data is the seed integer.
[71] Fix | Delete
*
[72] Fix | Delete
* @param[out] rng Your random struct to fill in.
[73] Fix | Delete
* @param[in] data Seed, single word.
[74] Fix | Delete
* @post `rng` is initialised using the passed seeds.
[75] Fix | Delete
*/
[76] Fix | Delete
typedef void rb_random_init_int32_func(rb_random_t *rng, uint32_t data);
[77] Fix | Delete
[78] Fix | Delete
RBIMPL_ATTR_NONNULL(())
[79] Fix | Delete
/**
[80] Fix | Delete
* This is the type of functions called from your object's `#rand` method.
[81] Fix | Delete
*
[82] Fix | Delete
* @param[out] rng Your random struct to extract an integer from.
[83] Fix | Delete
* @return A random number.
[84] Fix | Delete
* @post `rng` is consumed somehow.
[85] Fix | Delete
*/
[86] Fix | Delete
typedef unsigned int rb_random_get_int32_func(rb_random_t *rng);
[87] Fix | Delete
[88] Fix | Delete
RBIMPL_ATTR_NONNULL(())
[89] Fix | Delete
/**
[90] Fix | Delete
* This is the type of functions called from your object's `#bytes` method.
[91] Fix | Delete
*
[92] Fix | Delete
* @param[out] rng Your random struct to extract an integer from.
[93] Fix | Delete
* @param[out] buf Return buffer of at least `len` bytes length.
[94] Fix | Delete
* @param[in] len Number of bytes of `buf`.
[95] Fix | Delete
* @post `rng` is consumed somehow.
[96] Fix | Delete
* @post `buf` is filled with random bytes.
[97] Fix | Delete
*/
[98] Fix | Delete
typedef void rb_random_get_bytes_func(rb_random_t *rng, void *buf, size_t len);
[99] Fix | Delete
[100] Fix | Delete
RBIMPL_ATTR_NONNULL(())
[101] Fix | Delete
/**
[102] Fix | Delete
* This is the type of functions called from your object's `#rand` method.
[103] Fix | Delete
*
[104] Fix | Delete
* @param[out] rng Your random struct to extract an integer from.
[105] Fix | Delete
* @param[in] excl Pass nonzero value here to indicate you don't want 1.0.
[106] Fix | Delete
* @return A random number of range 0.0 to 1.0.
[107] Fix | Delete
* @post `rng` is consumed somehow.
[108] Fix | Delete
*/
[109] Fix | Delete
typedef double rb_random_get_real_func(rb_random_t *rng, int excl);
[110] Fix | Delete
[111] Fix | Delete
/** PRNG algorithmic interface, analogous to Ruby level classes. */
[112] Fix | Delete
typedef struct {
[113] Fix | Delete
/** Number of bits of seed numbers. */
[114] Fix | Delete
size_t default_seed_bits;
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* Major/minor versions of this interface
[118] Fix | Delete
*/
[119] Fix | Delete
struct {
[120] Fix | Delete
uint8_t major, minor;
[121] Fix | Delete
} version;
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Reserved flags
[125] Fix | Delete
*/
[126] Fix | Delete
uint16_t flags;
[127] Fix | Delete
[128] Fix | Delete
/** Function to initialize from uint32_t array. */
[129] Fix | Delete
rb_random_init_func *init;
[130] Fix | Delete
[131] Fix | Delete
/** Function to initialize from single uint32_t. */
[132] Fix | Delete
rb_random_init_int32_func *init_int32;
[133] Fix | Delete
[134] Fix | Delete
/** Function to obtain a random integer. */
[135] Fix | Delete
rb_random_get_int32_func *get_int32;
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Function to obtain a series of random bytes. If your PRNG have a native
[139] Fix | Delete
* method to yield arbitrary number of bytes use that to implement this.
[140] Fix | Delete
* But in case you lack such things, you can do so by using
[141] Fix | Delete
* rb_rand_bytes_int32()
[142] Fix | Delete
*
[143] Fix | Delete
* ```CXX
[144] Fix | Delete
* extern rb_random_get_int32_func your_get_int32_func;
[145] Fix | Delete
*
[146] Fix | Delete
* void
[147] Fix | Delete
* your_get_byes_func(rb_random_t *rng, void *buf, size_t len)
[148] Fix | Delete
* {
[149] Fix | Delete
* rb_rand_bytes_int32(your_get_int32_func, rng, buf, len);
[150] Fix | Delete
* }
[151] Fix | Delete
* ```
[152] Fix | Delete
*/
[153] Fix | Delete
rb_random_get_bytes_func *get_bytes;
[154] Fix | Delete
[155] Fix | Delete
/**
[156] Fix | Delete
* Function to obtain a random double. If your PRNG have a native method
[157] Fix | Delete
* to yield a floating point random number use that to implement this. But
[158] Fix | Delete
* in case you lack such things, you can do so by using
[159] Fix | Delete
* rb_int_pair_to_real().
[160] Fix | Delete
*
[161] Fix | Delete
* ```CXX
[162] Fix | Delete
* extern rb_random_get_int32_func your_get_int32_func;
[163] Fix | Delete
*
[164] Fix | Delete
* void
[165] Fix | Delete
* your_get_real_func(rb_random_t *rng, int excl)
[166] Fix | Delete
* {
[167] Fix | Delete
* auto a = your_get_int32_func(rng);
[168] Fix | Delete
* auto b = your_get_int32_func(rng);
[169] Fix | Delete
* return rb_int_pair_to_real(a, b, excl);
[170] Fix | Delete
* }
[171] Fix | Delete
* ```
[172] Fix | Delete
*/
[173] Fix | Delete
rb_random_get_real_func *get_real;
[174] Fix | Delete
} rb_random_interface_t;
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* This utility macro defines 4 functions named prefix_init, prefix_init_int32,
[178] Fix | Delete
* prefix_get_int32, prefix_get_bytes.
[179] Fix | Delete
*/
[180] Fix | Delete
#define RB_RANDOM_INTERFACE_DECLARE(prefix) \
[181] Fix | Delete
static void prefix##_init(rb_random_t *, const uint32_t *, size_t); \
[182] Fix | Delete
static void prefix##_init_int32(rb_random_t *, uint32_t); \
[183] Fix | Delete
static unsigned int prefix##_get_int32(rb_random_t *); \
[184] Fix | Delete
static void prefix##_get_bytes(rb_random_t *, void *, size_t)
[185] Fix | Delete
[186] Fix | Delete
/**
[187] Fix | Delete
* Identical to #RB_RANDOM_INTERFACE_DECLARE except it also declares
[188] Fix | Delete
* prefix_get_real.
[189] Fix | Delete
*/
[190] Fix | Delete
#define RB_RANDOM_INTERFACE_DECLARE_WITH_REAL(prefix) \
[191] Fix | Delete
RB_RANDOM_INTERFACE_DECLARE(prefix); \
[192] Fix | Delete
static double prefix##_get_real(rb_random_t *, int)
[193] Fix | Delete
[194] Fix | Delete
/**
[195] Fix | Delete
* This utility macro expands to the names declared using
[196] Fix | Delete
* #RB_RANDOM_INTERFACE_DECLARE. Expected to be used inside of a
[197] Fix | Delete
* ::rb_random_interface_t initialiser:
[198] Fix | Delete
*
[199] Fix | Delete
* ```CXX
[200] Fix | Delete
* RB_RANDOM_INTERFACE_DECLARE(foo);
[201] Fix | Delete
*
[202] Fix | Delete
* static inline constexpr rb_random_interface_t foo_interface = {
[203] Fix | Delete
* 32768, // bits
[204] Fix | Delete
* RB_RANDOM_INTERFACE_DEFINE(foo),
[205] Fix | Delete
* };
[206] Fix | Delete
* ```
[207] Fix | Delete
*/
[208] Fix | Delete
#define RB_RANDOM_INTERFACE_DEFINE(prefix) \
[209] Fix | Delete
RUBY_RANDOM_INTERFACE_VERSION_INITIALIZER, 0, \
[210] Fix | Delete
prefix##_init, \
[211] Fix | Delete
prefix##_init_int32, \
[212] Fix | Delete
prefix##_get_int32, \
[213] Fix | Delete
prefix##_get_bytes
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* Identical to #RB_RANDOM_INTERFACE_DEFINE except it also defines
[217] Fix | Delete
* prefix_get_real.
[218] Fix | Delete
*/
[219] Fix | Delete
#define RB_RANDOM_INTERFACE_DEFINE_WITH_REAL(prefix) \
[220] Fix | Delete
RB_RANDOM_INTERFACE_DEFINE(prefix), \
[221] Fix | Delete
prefix##_get_real
[222] Fix | Delete
[223] Fix | Delete
#define RB_RANDOM_DEFINE_INIT_INT32_FUNC(prefix) \
[224] Fix | Delete
static void prefix##_init_int32(rb_random_t *rnd, uint32_t data) \
[225] Fix | Delete
{ \
[226] Fix | Delete
prefix##_init(rnd, &data, 1); \
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
#if defined _WIN32 && !defined __CYGWIN__
[230] Fix | Delete
typedef rb_data_type_t rb_random_data_type_t;
[231] Fix | Delete
# define RB_RANDOM_PARENT 0
[232] Fix | Delete
#else
[233] Fix | Delete
[234] Fix | Delete
/** This is the type of ::rb_random_data_type. */
[235] Fix | Delete
typedef const rb_data_type_t rb_random_data_type_t;
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* This utility macro can be used when you define your own PRNG type:
[239] Fix | Delete
*
[240] Fix | Delete
* ```CXX
[241] Fix | Delete
* static inline constexpr rb_random_interface_t your_if = {
[242] Fix | Delete
* 0, RB_RANDOM_INTERFACE_DEFINE(your),
[243] Fix | Delete
* };
[244] Fix | Delete
*
[245] Fix | Delete
* static inline constexpr rb_random_data_type_t your_prng_type = {
[246] Fix | Delete
* "your PRNG",
[247] Fix | Delete
* { rb_random_mark, },
[248] Fix | Delete
* RB_RANDOM_PARENT, // <<-- HERE
[249] Fix | Delete
* &your_if,
[250] Fix | Delete
* 0,
[251] Fix | Delete
* }
[252] Fix | Delete
* ```
[253] Fix | Delete
*/
[254] Fix | Delete
# define RB_RANDOM_PARENT &rb_random_data_type
[255] Fix | Delete
#endif
[256] Fix | Delete
[257] Fix | Delete
/**
[258] Fix | Delete
* This macro is expected to be called exactly once at the beginning of a
[259] Fix | Delete
* program, possibly from inside of your `Init_Foo()` function. Depending on
[260] Fix | Delete
* platforms #RB_RANDOM_PARENT can require a fixup. This routine does that
[261] Fix | Delete
* when necessary.
[262] Fix | Delete
*/
[263] Fix | Delete
#define RB_RANDOM_DATA_INIT_PARENT(random_data) \
[264] Fix | Delete
rbimpl_random_data_init_parent(&random_data)
[265] Fix | Delete
[266] Fix | Delete
/**
[267] Fix | Delete
* This is the implementation of ::rb_data_type_struct::dmark for
[268] Fix | Delete
* ::rb_random_data_type. In case your PRNG does not involve Ruby objects at
[269] Fix | Delete
* all (which is quite likely), you can simply reuse it.
[270] Fix | Delete
*
[271] Fix | Delete
* @param[out] ptr Target to mark, which is a ::rb_random_t this case.
[272] Fix | Delete
*/
[273] Fix | Delete
void rb_random_mark(void *ptr);
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Initialises an allocated ::rb_random_t instance. Call it from your own
[277] Fix | Delete
* initialiser appropriately.
[278] Fix | Delete
*
[279] Fix | Delete
* @param[out] rnd Your PRNG's base part.
[280] Fix | Delete
* @post `rnd` is filled with an initial state.
[281] Fix | Delete
*/
[282] Fix | Delete
void rb_random_base_init(rb_random_t *rnd);
[283] Fix | Delete
[284] Fix | Delete
/**
[285] Fix | Delete
* Generates a 64 bit floating point number by concatenating two 32bit unsigned
[286] Fix | Delete
* integers.
[287] Fix | Delete
*
[288] Fix | Delete
* @param[in] a Most significant 32 bits of the result.
[289] Fix | Delete
* @param[in] b Least significant 32 bits of the result.
[290] Fix | Delete
* @param[in] excl Whether the result should exclude 1.0 or not.
[291] Fix | Delete
* @return A double, whose range is either `[0, 1)` or `[0, 1]`.
[292] Fix | Delete
* @see ::rb_random_interface_t::get_real()
[293] Fix | Delete
*
[294] Fix | Delete
* @internal
[295] Fix | Delete
*
[296] Fix | Delete
* This in fact has nothing to do with PRNGs.
[297] Fix | Delete
*/
[298] Fix | Delete
double rb_int_pair_to_real(uint32_t a, uint32_t b, int excl);
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* Repeatedly calls the passed function over and over again until the passed
[302] Fix | Delete
* buffer is filled with random bytes.
[303] Fix | Delete
*
[304] Fix | Delete
* @param[in] func Generator function.
[305] Fix | Delete
* @param[out] prng Passed as-is to `func`.
[306] Fix | Delete
* @param[out] buff Return buffer.
[307] Fix | Delete
* @param[in] size Number of words of `buff`.
[308] Fix | Delete
* @post `buff` is filled with random bytes.
[309] Fix | Delete
* @post `prng` is updated by `func`.
[310] Fix | Delete
* @see ::rb_random_interface_t::get_bytes()
[311] Fix | Delete
*/
[312] Fix | Delete
void rb_rand_bytes_int32(rb_random_get_int32_func *func, rb_random_t *prng, void *buff, size_t size);
[313] Fix | Delete
[314] Fix | Delete
/**
[315] Fix | Delete
* The data that holds the backend type of ::rb_cRandom. Used as your PRNG's
[316] Fix | Delete
* ::rb_data_type_struct::parent.
[317] Fix | Delete
*/
[318] Fix | Delete
RUBY_EXTERN const rb_data_type_t rb_random_data_type;
[319] Fix | Delete
[320] Fix | Delete
RBIMPL_SYMBOL_EXPORT_END()
[321] Fix | Delete
[322] Fix | Delete
RBIMPL_ATTR_PURE_UNLESS_DEBUG()
[323] Fix | Delete
/* :TODO: can this function be __attribute__((returns_nonnull)) or not? */
[324] Fix | Delete
/**
[325] Fix | Delete
* Queries the interface of the passed random object.
[326] Fix | Delete
*
[327] Fix | Delete
* @param[in] obj An instance (of a subclass) of ::rb_cRandom.
[328] Fix | Delete
* @return Its corresponding ::rb_random_interface_t interface.
[329] Fix | Delete
*/
[330] Fix | Delete
static inline const rb_random_interface_t *
[331] Fix | Delete
rb_rand_if(VALUE obj)
[332] Fix | Delete
{
[333] Fix | Delete
RBIMPL_ASSERT_OR_ASSUME(RTYPEDDATA_P(obj));
[334] Fix | Delete
const struct rb_data_type_struct *t = RTYPEDDATA_TYPE(obj);
[335] Fix | Delete
const void *ret = t->data;
[336] Fix | Delete
return RBIMPL_CAST((const rb_random_interface_t *)ret);
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
RBIMPL_ATTR_NOALIAS()
[340] Fix | Delete
/**
[341] Fix | Delete
* @private
[342] Fix | Delete
*
[343] Fix | Delete
* This is an implementation detail of #RB_RANDOM_DATA_INIT_PARENT. People
[344] Fix | Delete
* don't use it directly.
[345] Fix | Delete
*
[346] Fix | Delete
* @param[out] random_data Region to fill.
[347] Fix | Delete
* @post ::rb_random_data_type is filled appropriately.
[348] Fix | Delete
*/
[349] Fix | Delete
static inline void
[350] Fix | Delete
rbimpl_random_data_init_parent(rb_random_data_type_t *random_data)
[351] Fix | Delete
{
[352] Fix | Delete
#if defined _WIN32 && !defined __CYGWIN__
[353] Fix | Delete
random_data->parent = &rb_random_data_type;
[354] Fix | Delete
#endif
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
#endif /* RUBY_RANDOM_H */
[358] Fix | Delete
[359] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function