Skip to content

Commit 6fe606a

Browse files
committed
random: Fix γ-section for 32-bit systems
1 parent bb72e7d commit 6fe606a

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

ext/random/gammasection.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ PHPAPI double php_random_gammasection_closed_open(const php_random_algo *algo, p
6666
{
6767
double g = gamma_max(min, max);
6868
uint64_t hi = ceilint(min, max, g);
69-
uint64_t k = algo->range(status, 1, hi);
69+
uint64_t k = 1 + php_random_range64(algo, status, hi - 1); /* [1, hi] */
7070

7171
if (fabs(min) <= fabs(max)) {
7272
return k == hi ? min : max - k * g;
@@ -79,7 +79,7 @@ PHPAPI double php_random_gammasection_closed_closed(const php_random_algo *algo,
7979
{
8080
double g = gamma_max(min, max);
8181
uint64_t hi = ceilint(min, max, g);
82-
uint64_t k = algo->range(status, 0, hi);
82+
uint64_t k = php_random_range64(algo, status, hi); /* [0, hi] */
8383

8484
if (fabs(min) <= fabs(max)) {
8585
return k == hi ? min : max - k * g;
@@ -92,7 +92,7 @@ PHPAPI double php_random_gammasection_open_closed(const php_random_algo *algo, p
9292
{
9393
double g = gamma_max(min, max);
9494
uint64_t hi = ceilint(min, max, g);
95-
uint64_t k = algo->range(status, 0, hi - 1);
95+
uint64_t k = php_random_range64(algo, status, hi - 1); /* [0, hi - 1] */
9696

9797
if (fabs(min) <= fabs(max)) {
9898
return max - k * g;
@@ -105,7 +105,7 @@ PHPAPI double php_random_gammasection_open_open(const php_random_algo *algo, php
105105
{
106106
double g = gamma_max(min, max);
107107
uint64_t hi = ceilint(min, max, g);
108-
uint64_t k = algo->range(status, 1, hi - 1);
108+
uint64_t k = 1 + php_random_range64(algo, status, hi - 2); /* [1, hi - 1] */
109109

110110
if (fabs(min) <= fabs(max)) {
111111
return max - k * g;

ext/random/php_random.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ PHPAPI void php_random_status_free(php_random_status *status, const bool persist
293293
PHPAPI php_random_engine *php_random_engine_common_init(zend_class_entry *ce, zend_object_handlers *handlers, const php_random_algo *algo);
294294
PHPAPI void php_random_engine_common_free_object(zend_object *object);
295295
PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object);
296+
PHPAPI uint32_t php_random_range32(const php_random_algo *algo, php_random_status *status, uint32_t umax);
297+
PHPAPI uint64_t php_random_range64(const php_random_algo *algo, php_random_status *status, uint64_t umax);
296298
PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max);
297299
PHPAPI const php_random_algo *php_random_default_algo(void);
298300
PHPAPI php_random_status *php_random_default_status(void);

ext/random/random.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static zend_object_handlers random_engine_xoshiro256starstar_object_handlers;
8989
static zend_object_handlers random_engine_secure_object_handlers;
9090
static zend_object_handlers random_randomizer_object_handlers;
9191

92-
static inline uint32_t rand_range32(const php_random_algo *algo, php_random_status *status, uint32_t umax)
92+
PHPAPI uint32_t php_random_range32(const php_random_algo *algo, php_random_status *status, uint32_t umax)
9393
{
9494
uint32_t result, limit;
9595
size_t total_size = 0;
@@ -145,7 +145,7 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
145145
return result % umax;
146146
}
147147

148-
static inline uint64_t rand_range64(const php_random_algo *algo, php_random_status *status, uint64_t umax)
148+
PHPAPI uint64_t php_random_range64(const php_random_algo *algo, php_random_status *status, uint64_t umax)
149149
{
150150
uint64_t result, limit;
151151
size_t total_size = 0;
@@ -313,10 +313,10 @@ PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status
313313
zend_ulong umax = (zend_ulong) max - (zend_ulong) min;
314314

315315
if (umax > UINT32_MAX) {
316-
return (zend_long) (rand_range64(algo, status, umax) + min);
316+
return (zend_long) (php_random_range64(algo, status, umax) + min);
317317
}
318318

319-
return (zend_long) (rand_range32(algo, status, umax) + min);
319+
return (zend_long) (php_random_range32(algo, status, umax) + min);
320320
}
321321
/* }}} */
322322

0 commit comments

Comments
 (0)