xref: /PHP-7.4/ext/standard/lcg.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Sascha Schumann <sascha@schumann.cx>                         |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "php_lcg.h"
21 
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 
26 #ifdef PHP_WIN32
27 #include "win32/time.h"
28 #else
29 #include <sys/time.h>
30 #endif
31 
32 #ifdef ZTS
33 int lcg_globals_id;
34 #else
35 static php_lcg_globals lcg_globals;
36 #endif
37 
38 #ifdef PHP_WIN32
39 #include <process.h>
40 #endif
41 
42 /*
43  * combinedLCG() returns a pseudo random number in the range of (0, 1).
44  * The function combines two CGs with periods of
45  * 2^31 - 85 and 2^31 - 249. The period of this function
46  * is equal to the product of both primes.
47  */
48 
49 #define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m
50 
51 static void lcg_seed(void);
52 
php_combined_lcg(void)53 PHPAPI double php_combined_lcg(void) /* {{{ */
54 {
55 	int32_t q;
56 	int32_t z;
57 
58 	if (!LCG(seeded)) {
59 		lcg_seed();
60 	}
61 
62 	MODMULT(53668, 40014, 12211, 2147483563L, LCG(s1));
63 	MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2));
64 
65 	z = LCG(s1) - LCG(s2);
66 	if (z < 1) {
67 		z += 2147483562;
68 	}
69 
70 	return z * 4.656613e-10;
71 }
72 /* }}} */
73 
lcg_seed(void)74 static void lcg_seed(void) /* {{{ */
75 {
76 	struct timeval tv;
77 
78 	if (gettimeofday(&tv, NULL) == 0) {
79 		LCG(s1) = tv.tv_sec ^ (tv.tv_usec<<11);
80 	} else {
81 		LCG(s1) = 1;
82 	}
83 #ifdef ZTS
84 	LCG(s2) = (zend_long) tsrm_thread_id();
85 #else
86 	LCG(s2) = (zend_long) getpid();
87 #endif
88 
89 	/* Add entropy to s2 by calling gettimeofday() again */
90 	if (gettimeofday(&tv, NULL) == 0) {
91 		LCG(s2) ^= (tv.tv_usec<<11);
92 	}
93 
94 	LCG(seeded) = 1;
95 }
96 /* }}} */
97 
lcg_init_globals(php_lcg_globals * lcg_globals_p)98 static void lcg_init_globals(php_lcg_globals *lcg_globals_p) /* {{{ */
99 {
100 	LCG(seeded) = 0;
101 }
102 /* }}} */
103 
PHP_MINIT_FUNCTION(lcg)104 PHP_MINIT_FUNCTION(lcg) /* {{{ */
105 {
106 #ifdef ZTS
107 	ts_allocate_id(&lcg_globals_id, sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL);
108 #else
109 	lcg_init_globals(&lcg_globals);
110 #endif
111 	return SUCCESS;
112 }
113 /* }}} */
114 
115 /* {{{ proto float lcg_value()
116    Returns a value from the combined linear congruential generator */
PHP_FUNCTION(lcg_value)117 PHP_FUNCTION(lcg_value)
118 {
119 	if (zend_parse_parameters_none() == FAILURE) {
120 		return;
121 	}
122 	RETURN_DOUBLE(php_combined_lcg());
123 }
124 /* }}} */
125