From 0d18f41f91a2e69f7b0ec403ec6ce9807fd99d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 9 Jan 2024 19:42:10 +0100 Subject: [PATCH 1/3] random: Narrow the parameter types of seed128/seed256 These internal-only functions accepted a `php_random_status`, just to extract the internal state from the `state` pointer. Make them take the state struct directly to improve type safety. --- ext/random/engine_pcgoneseq128xslrr64.c | 7 +++---- ext/random/engine_xoshiro256starstar.c | 8 +++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ext/random/engine_pcgoneseq128xslrr64.c b/ext/random/engine_pcgoneseq128xslrr64.c index f7c5f0ebbcfc4..107e552846728 100644 --- a/ext/random/engine_pcgoneseq128xslrr64.c +++ b/ext/random/engine_pcgoneseq128xslrr64.c @@ -34,9 +34,8 @@ static inline void step(php_random_status_state_pcgoneseq128xslrr64 *s) ); } -static inline void seed128(php_random_status *status, php_random_uint128_t seed) +static inline void seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed) { - php_random_status_state_pcgoneseq128xslrr64 *s = status->state; s->state = php_random_uint128_constant(0ULL, 0ULL); step(s); s->state = php_random_uint128_add(s->state, seed); @@ -45,7 +44,7 @@ static inline void seed128(php_random_status *status, php_random_uint128_t seed) static void seed(php_random_status *status, uint64_t seed) { - seed128(status, php_random_uint128_constant(0ULL, seed)); + seed128(status->state, php_random_uint128_constant(0ULL, seed)); } static php_random_result generate(php_random_status *status) @@ -172,7 +171,7 @@ PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, __construct) } } - seed128(engine->status, php_random_uint128_constant(t[0], t[1])); + seed128(state, php_random_uint128_constant(t[0], t[1])); } else { zend_argument_value_error(1, "must be a 16 byte (128 bit) string"); RETURN_THROWS(); diff --git a/ext/random/engine_xoshiro256starstar.c b/ext/random/engine_xoshiro256starstar.c index 5d4dad08a1407..0f85d1de0e31d 100644 --- a/ext/random/engine_xoshiro256starstar.c +++ b/ext/random/engine_xoshiro256starstar.c @@ -81,10 +81,8 @@ static inline void jump(php_random_status_state_xoshiro256starstar *state, const state->state[3] = s3; } -static inline void seed256(php_random_status *status, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3) +static inline void seed256(php_random_status_state_xoshiro256starstar *s, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3) { - php_random_status_state_xoshiro256starstar *s = status->state; - s->state[0] = s0; s->state[1] = s1; s->state[2] = s2; @@ -100,7 +98,7 @@ static void seed(php_random_status *status, uint64_t seed) s[2] = splitmix64(&seed); s[3] = splitmix64(&seed); - seed256(status, s[0], s[1], s[2], s[3]); + seed256(status->state, s[0], s[1], s[2], s[3]); } static php_random_result generate(php_random_status *status) @@ -237,7 +235,7 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct) RETURN_THROWS(); } - seed256(engine->status, t[0], t[1], t[2], t[3]); + seed256(state, t[0], t[1], t[2], t[3]); } else { zend_argument_value_error(1, "must be a 32 byte (256 bit) string"); RETURN_THROWS(); From 26170219bde8672122b68d5291cf58b632e7c680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 29 Jan 2024 16:56:42 +0100 Subject: [PATCH 2/3] random: Add explicitly named seed64() helper for xoshiro256** --- ext/random/engine_xoshiro256starstar.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/random/engine_xoshiro256starstar.c b/ext/random/engine_xoshiro256starstar.c index 0f85d1de0e31d..c4f71c0c3d9a0 100644 --- a/ext/random/engine_xoshiro256starstar.c +++ b/ext/random/engine_xoshiro256starstar.c @@ -89,7 +89,7 @@ static inline void seed256(php_random_status_state_xoshiro256starstar *s, uint64 s->state[3] = s3; } -static void seed(php_random_status *status, uint64_t seed) +static inline void seed64(php_random_status_state_xoshiro256starstar *state, uint64_t seed) { uint64_t s[4]; @@ -98,7 +98,12 @@ static void seed(php_random_status *status, uint64_t seed) s[2] = splitmix64(&seed); s[3] = splitmix64(&seed); - seed256(status->state, s[0], s[1], s[2], s[3]); + seed256(state, s[0], s[1], s[2], s[3]); +} + +static void seed(php_random_status *status, uint64_t seed) +{ + seed64(status->state, seed); } static php_random_result generate(php_random_status *status) From f3809dd2af2009476ebd8dc0b743fabbd36d1dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 29 Jan 2024 17:00:11 +0100 Subject: [PATCH 3/3] random: Call int-seeding functions directly As the `__construct()` implementation is engine-specific anyway, we know what engine were dealing with and can just call the seeding function directly instead of going through a function pointer. This likely improves construction performance a little, but I did not measure. --- ext/random/engine_mt19937.c | 2 +- ext/random/engine_pcgoneseq128xslrr64.c | 2 +- ext/random/engine_xoshiro256starstar.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/random/engine_mt19937.c b/ext/random/engine_mt19937.c index 377526550a6e5..7559f1486150a 100644 --- a/ext/random/engine_mt19937.c +++ b/ext/random/engine_mt19937.c @@ -282,7 +282,7 @@ PHP_METHOD(Random_Engine_Mt19937, __construct) } } - engine->algo->seed(engine->status, seed); + mt19937_seed_state(state, seed); } /* }}} */ diff --git a/ext/random/engine_pcgoneseq128xslrr64.c b/ext/random/engine_pcgoneseq128xslrr64.c index 107e552846728..b0d6d418f321d 100644 --- a/ext/random/engine_pcgoneseq128xslrr64.c +++ b/ext/random/engine_pcgoneseq128xslrr64.c @@ -177,7 +177,7 @@ PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, __construct) RETURN_THROWS(); } } else { - engine->algo->seed(engine->status, int_seed); + seed128(state, php_random_uint128_constant(0ULL, (uint64_t) int_seed)); } } } diff --git a/ext/random/engine_xoshiro256starstar.c b/ext/random/engine_xoshiro256starstar.c index c4f71c0c3d9a0..51971833b5583 100644 --- a/ext/random/engine_xoshiro256starstar.c +++ b/ext/random/engine_xoshiro256starstar.c @@ -246,7 +246,7 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct) RETURN_THROWS(); } } else { - engine->algo->seed(engine->status, (uint64_t) int_seed); + seed64(state, (uint64_t) int_seed); } } }