diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-11-09 19:36:37 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-11-10 12:06:13 +0900 |
commit | b7e8876704648cee6866591ac1aca7a54faff742 (patch) | |
tree | e21c0a9176378eedfdcc46a64ba12456fa03429f /include/ruby/random.h | |
parent | 6eaed20e145c1ab4c9c4ccf669d0ebea934f4d36 (diff) | |
download | ruby-b7e8876704648cee6866591ac1aca7a54faff742.tar.gz |
[Bug #19100] Add `init_int32` function to `rb_random_interface_t`
Distinguish initialization by single word from initialization by
array.
Diffstat (limited to 'include/ruby/random.h')
-rw-r--r-- | include/ruby/random.h | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/include/ruby/random.h b/include/ruby/random.h index 989445f2f9..39bdb6f3e3 100644 --- a/include/ruby/random.h +++ b/include/ruby/random.h @@ -19,7 +19,7 @@ /* * version * 0: before versioning; deprecated - * 1: added version and flags + * 1: added version, flags and init_32bit function */ #define RUBY_RANDOM_INTERFACE_VERSION_MAJOR 1 #define RUBY_RANDOM_INTERFACE_VERSION_MINOR 0 @@ -68,6 +68,17 @@ typedef void rb_random_init_func(rb_random_t *rng, const uint32_t *buf, size_t l RBIMPL_ATTR_NONNULL(()) /** + * This is the type of functions called when your random object is initialised. + * Passed data is the seed integer. + * + * @param[out] rng Your random struct to fill in. + * @param[in] data Seed, single word. + * @post `rng` is initialised using the passed seeds. + */ +typedef void rb_random_init_int32_func(rb_random_t *rng, uint32_t data); + +RBIMPL_ATTR_NONNULL(()) +/** * This is the type of functions called from your object's `#rand` method. * * @param[out] rng Your random struct to extract an integer from. @@ -116,9 +127,12 @@ typedef struct { */ uint16_t flags; - /** Initialiser function. */ + /** Function to initialize from uint32_t array. */ rb_random_init_func *init; + /** Function to initialize from single uint32_t. */ + rb_random_init_int32_func *init_int32; + /** Function to obtain a random integer. */ rb_random_get_int32_func *get_int32; @@ -162,11 +176,12 @@ typedef struct { } rb_random_interface_t; /** - * This utility macro defines 3 functions named prefix_init, prefix_get_int32, - * prefix_get_bytes. + * This utility macro defines 4 functions named prefix_init, prefix_init_int32, + * prefix_get_int32, prefix_get_bytes. */ #define RB_RANDOM_INTERFACE_DECLARE(prefix) \ static void prefix##_init(rb_random_t *, const uint32_t *, size_t); \ + static void prefix##_init_int32(rb_random_t *, uint32_t); \ static unsigned int prefix##_get_int32(rb_random_t *); \ static void prefix##_get_bytes(rb_random_t *, void *, size_t) @@ -195,6 +210,7 @@ typedef struct { #define RB_RANDOM_INTERFACE_DEFINE(prefix) \ RUBY_RANDOM_INTERFACE_VERSION_INITIALIZER, 0, \ prefix##_init, \ + prefix##_init_int32, \ prefix##_get_int32, \ prefix##_get_bytes @@ -206,6 +222,12 @@ typedef struct { RB_RANDOM_INTERFACE_DEFINE(prefix), \ prefix##_get_real +#define RB_RANDOM_DEFINE_INIT_INT32_FUNC(prefix) \ + static void prefix##_init_int32(rb_random_t *rnd, uint32_t data) \ + { \ + prefix##_init(rnd, &data, 1); \ + } + #if defined _WIN32 && !defined __CYGWIN__ typedef rb_data_type_t rb_random_data_type_t; # define RB_RANDOM_PARENT 0 |