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