Skip to content

Commit 2971714

Browse files
committed
re change implementation order
1 parent 44c7d3c commit 2971714

File tree

1 file changed

+150
-150
lines changed

1 file changed

+150
-150
lines changed

ext/random/random.c

Lines changed: 150 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -297,88 +297,112 @@ static zend_object *php_random_engine_common_clone_obj(zend_object *old_object)
297297
return &new_engine->std;
298298
}
299299

300-
/* XorShift128Plus begin */
300+
/* CombinedLCG begin */
301301

302-
static inline size_t xorshift128plus_dynamic_generate_size(void *state) {
303-
return sizeof(uint64_t);
302+
static inline size_t combinedlcg_dynamic_generate__size(void *state) {
303+
return sizeof(uint32_t);
304304
}
305305

306-
static inline uint64_t xorshift128plus_splitmix64(uint64_t *seed) {
307-
uint64_t r;
306+
static uint64_t combinedlcg_generate(void *state) {
307+
int32_t q, z;
308+
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *) state;
308309

309-
r = (*seed += UINT64_C(0x9e3779b97f4a7c15));
310-
r = (r ^ (r >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
311-
r = (r ^ (r >> 27)) * UINT64_C(0x94d049bb133111eb);
312-
return (r ^ (r >> 31));
310+
MODMULT(53668, 40014, 12211, 2147483563L, s->s[0]);
311+
MODMULT(52774, 40692, 3791, 2147483399L, s->s[1]);
312+
313+
z = s->s[0] - s->s[1];
314+
if (z < 1) {
315+
z += 2147483562;
316+
}
317+
318+
return (uint64_t) z;
313319
}
314320

315-
static uint64_t xorshift128plus_generate(void *state) {
316-
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
317-
uint64_t s0, s1, r;
321+
static void combinedlcg_seed(void *state, const uint64_t seed) {
322+
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *) state;
318323

319-
s1 = s->s[0];
320-
s0 = s->s[1];
321-
r = s0 + s1;
322-
s->s[0] = s0;
323-
s1 ^= s1 << 23;
324-
s->s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
324+
s->s[0] = seed & 0xffffffffU;
325+
s->s[1] = seed >> 32;
325326

326-
return r;
327+
/* Seed only once */
328+
s->seeded = true;
327329
}
328330

329-
static void xorshift128plus_seed(void *state, const uint64_t seed) {
330-
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
331-
uint64_t sd = seed;
331+
static void combinedlcg_seed_default(php_random_engine_state_combinedlcg *state) {
332+
struct timeval tv;
332333

333-
s->s[0] = xorshift128plus_splitmix64(&sd);
334-
s->s[1] = xorshift128plus_splitmix64(&sd);
334+
if (gettimeofday(&tv, NULL) == 0) {
335+
state->s[0] = tv.tv_usec ^ (tv.tv_usec << 11);
336+
} else {
337+
state->s[0] = 1;
338+
}
339+
340+
#ifdef ZTS
341+
state->s[1] = (zend_long) tsrm_thread_id();
342+
#else
343+
state->s[1] = (zend_long) getpid();
344+
#endif
345+
346+
/* Add entropy to s2 by calling gettimeofday() again */
347+
if (gettimeofday(&tv, NULL) == 0) {
348+
state->s[1] ^= (tv.tv_usec << 11);
349+
}
350+
351+
/* Seed only once */
352+
state->seeded = true;
335353
}
336354

337-
static int xorshift128plus_serialize(void *state, HashTable *data) {
338-
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
355+
static int combinedlcg_serialize(void *state, HashTable *data) {
356+
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *)state;
339357
zval tmp;
340358
int i;
341359

342360
for (i = 0; i < 2; i++) {
343-
ZVAL_STR(&tmp, zend_strpprintf(0, "%" PRIu64, s->s[i]));
361+
ZVAL_LONG(&tmp, s->s[i]);
344362
zend_hash_next_index_insert(data, &tmp);
345363
}
364+
ZVAL_LONG(&tmp, s->seeded);
365+
zend_hash_next_index_insert(data, &tmp);
346366

347367
return SUCCESS;
348368
}
349369

350-
static int xorshift128plus_unserialize(void *state, HashTable *data) {
351-
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
370+
static int combinedlcg_unserialize(void *state, HashTable *data) {
371+
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *)state;
352372
zval *tmp;
353373
int i;
354374

355375
for (i = 0; i < 2; i++) {
356376
tmp = zend_hash_index_find(data, i);
357-
if (!tmp || Z_TYPE_P(tmp) != IS_STRING) {
377+
if (!tmp || Z_TYPE_P(tmp) != IS_LONG) {
358378
return FAILURE;
359379
}
360-
361-
s->s[i] = strtoull(ZSTR_VAL(Z_STR_P(tmp)), NULL, 10);
380+
s->s[i] = Z_LVAL_P(tmp);
362381
}
382+
tmp = zend_hash_index_find(data, 2);
383+
if (!tmp || Z_TYPE_P(tmp) != IS_LONG) {
384+
return FAILURE;
385+
}
386+
s->seeded = Z_LVAL_P(tmp);
363387

364388
return SUCCESS;
365389
}
366390

367-
static zend_object *php_random_engine_xorshift128plus_new(zend_class_entry *ce) {
368-
return php_random_engine_common_init(ce, &php_random_engine_algo_xorshift128plus, &random_engine_xorshift128plus_object_handlers);
391+
static zend_object *php_random_engine_combinedlcg_new(zend_class_entry *ce) {
392+
return php_random_engine_common_init(ce, &php_random_engine_algo_combinedlcg, &random_engine_combinedlcg_object_handlers);
369393
}
370394

371-
const php_random_engine_algo php_random_engine_algo_xorshift128plus = {
372-
sizeof(uint64_t),
373-
xorshift128plus_dynamic_generate_size,
374-
sizeof(php_random_engine_state_xorshift128plus),
375-
xorshift128plus_generate,
376-
xorshift128plus_seed,
377-
xorshift128plus_serialize,
378-
xorshift128plus_unserialize
395+
const php_random_engine_algo php_random_engine_algo_combinedlcg = {
396+
sizeof(uint32_t),
397+
combinedlcg_dynamic_generate__size,
398+
sizeof(php_random_engine_state_combinedlcg),
399+
combinedlcg_generate,
400+
combinedlcg_seed,
401+
combinedlcg_serialize,
402+
combinedlcg_unserialize
379403
};
380404

381-
/* XorShift128Plus end */
405+
/* CombinedLCG end */
382406

383407
/* MersenneTwister begin */
384408

@@ -512,113 +536,6 @@ const php_random_engine_algo php_random_engine_algo_mersennetwister = {
512536

513537
/* MersenneTwister end */
514538

515-
/* CombinedLCG begin */
516-
517-
static inline size_t combinedlcg_dynamic_generate__size(void *state) {
518-
return sizeof(uint32_t);
519-
}
520-
521-
static uint64_t combinedlcg_generate(void *state) {
522-
int32_t q, z;
523-
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *) state;
524-
525-
MODMULT(53668, 40014, 12211, 2147483563L, s->s[0]);
526-
MODMULT(52774, 40692, 3791, 2147483399L, s->s[1]);
527-
528-
z = s->s[0] - s->s[1];
529-
if (z < 1) {
530-
z += 2147483562;
531-
}
532-
533-
return (uint64_t) z;
534-
}
535-
536-
static void combinedlcg_seed(void *state, const uint64_t seed) {
537-
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *) state;
538-
539-
s->s[0] = seed & 0xffffffffU;
540-
s->s[1] = seed >> 32;
541-
542-
/* Seed only once */
543-
s->seeded = true;
544-
}
545-
546-
static void combinedlcg_seed_default(php_random_engine_state_combinedlcg *state) {
547-
struct timeval tv;
548-
549-
if (gettimeofday(&tv, NULL) == 0) {
550-
state->s[0] = tv.tv_usec ^ (tv.tv_usec << 11);
551-
} else {
552-
state->s[0] = 1;
553-
}
554-
555-
#ifdef ZTS
556-
state->s[1] = (zend_long) tsrm_thread_id();
557-
#else
558-
state->s[1] = (zend_long) getpid();
559-
#endif
560-
561-
/* Add entropy to s2 by calling gettimeofday() again */
562-
if (gettimeofday(&tv, NULL) == 0) {
563-
state->s[1] ^= (tv.tv_usec << 11);
564-
}
565-
566-
/* Seed only once */
567-
state->seeded = true;
568-
}
569-
570-
static int combinedlcg_serialize(void *state, HashTable *data) {
571-
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *)state;
572-
zval tmp;
573-
int i;
574-
575-
for (i = 0; i < 2; i++) {
576-
ZVAL_LONG(&tmp, s->s[i]);
577-
zend_hash_next_index_insert(data, &tmp);
578-
}
579-
ZVAL_LONG(&tmp, s->seeded);
580-
zend_hash_next_index_insert(data, &tmp);
581-
582-
return SUCCESS;
583-
}
584-
585-
static int combinedlcg_unserialize(void *state, HashTable *data) {
586-
php_random_engine_state_combinedlcg *s = (php_random_engine_state_combinedlcg *)state;
587-
zval *tmp;
588-
int i;
589-
590-
for (i = 0; i < 2; i++) {
591-
tmp = zend_hash_index_find(data, i);
592-
if (!tmp || Z_TYPE_P(tmp) != IS_LONG) {
593-
return FAILURE;
594-
}
595-
s->s[i] = Z_LVAL_P(tmp);
596-
}
597-
tmp = zend_hash_index_find(data, 2);
598-
if (!tmp || Z_TYPE_P(tmp) != IS_LONG) {
599-
return FAILURE;
600-
}
601-
s->seeded = Z_LVAL_P(tmp);
602-
603-
return SUCCESS;
604-
}
605-
606-
static zend_object *php_random_engine_combinedlcg_new(zend_class_entry *ce) {
607-
return php_random_engine_common_init(ce, &php_random_engine_algo_combinedlcg, &random_engine_combinedlcg_object_handlers);
608-
}
609-
610-
const php_random_engine_algo php_random_engine_algo_combinedlcg = {
611-
sizeof(uint32_t),
612-
combinedlcg_dynamic_generate__size,
613-
sizeof(php_random_engine_state_combinedlcg),
614-
combinedlcg_generate,
615-
combinedlcg_seed,
616-
combinedlcg_serialize,
617-
combinedlcg_unserialize
618-
};
619-
620-
/* CombinedLCG end */
621-
622539
/* Secure begin */
623540

624541
static inline size_t secure_dynamic_generate_size(void *state) {
@@ -694,6 +611,89 @@ const php_random_engine_algo php_random_engine_algo_user = {
694611

695612
/* User end */
696613

614+
/* XorShift128Plus begin */
615+
616+
static inline size_t xorshift128plus_dynamic_generate_size(void *state) {
617+
return sizeof(uint64_t);
618+
}
619+
620+
static inline uint64_t xorshift128plus_splitmix64(uint64_t *seed) {
621+
uint64_t r;
622+
623+
r = (*seed += UINT64_C(0x9e3779b97f4a7c15));
624+
r = (r ^ (r >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
625+
r = (r ^ (r >> 27)) * UINT64_C(0x94d049bb133111eb);
626+
return (r ^ (r >> 31));
627+
}
628+
629+
static uint64_t xorshift128plus_generate(void *state) {
630+
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
631+
uint64_t s0, s1, r;
632+
633+
s1 = s->s[0];
634+
s0 = s->s[1];
635+
r = s0 + s1;
636+
s->s[0] = s0;
637+
s1 ^= s1 << 23;
638+
s->s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
639+
640+
return r;
641+
}
642+
643+
static void xorshift128plus_seed(void *state, const uint64_t seed) {
644+
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
645+
uint64_t sd = seed;
646+
647+
s->s[0] = xorshift128plus_splitmix64(&sd);
648+
s->s[1] = xorshift128plus_splitmix64(&sd);
649+
}
650+
651+
static int xorshift128plus_serialize(void *state, HashTable *data) {
652+
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
653+
zval tmp;
654+
int i;
655+
656+
for (i = 0; i < 2; i++) {
657+
ZVAL_STR(&tmp, zend_strpprintf(0, "%" PRIu64, s->s[i]));
658+
zend_hash_next_index_insert(data, &tmp);
659+
}
660+
661+
return SUCCESS;
662+
}
663+
664+
static int xorshift128plus_unserialize(void *state, HashTable *data) {
665+
php_random_engine_state_xorshift128plus *s = (php_random_engine_state_xorshift128plus *)state;
666+
zval *tmp;
667+
int i;
668+
669+
for (i = 0; i < 2; i++) {
670+
tmp = zend_hash_index_find(data, i);
671+
if (!tmp || Z_TYPE_P(tmp) != IS_STRING) {
672+
return FAILURE;
673+
}
674+
675+
s->s[i] = strtoull(ZSTR_VAL(Z_STR_P(tmp)), NULL, 10);
676+
}
677+
678+
return SUCCESS;
679+
}
680+
681+
static zend_object *php_random_engine_xorshift128plus_new(zend_class_entry *ce) {
682+
return php_random_engine_common_init(ce, &php_random_engine_algo_xorshift128plus, &random_engine_xorshift128plus_object_handlers);
683+
}
684+
685+
const php_random_engine_algo php_random_engine_algo_xorshift128plus = {
686+
sizeof(uint64_t),
687+
xorshift128plus_dynamic_generate_size,
688+
sizeof(php_random_engine_state_xorshift128plus),
689+
xorshift128plus_generate,
690+
xorshift128plus_seed,
691+
xorshift128plus_serialize,
692+
xorshift128plus_unserialize
693+
};
694+
695+
/* XorShift128Plus end */
696+
697697
static zend_object *php_random_randomizer_new(zend_class_entry *ce) {
698698
php_random_randomizer *randomizer = zend_object_alloc(sizeof(php_random_randomizer), ce);
699699

0 commit comments

Comments
 (0)