From 5600f889b2cb8d2b11b54d711273c40916c4b4fa Mon Sep 17 00:00:00 2001 From: Stig Bakken <ssb@php.net> Date: Sun, 29 Aug 1999 20:12:12 +0000 Subject: [PATCH] moved lcg stuff to ext/standard --- ext/standard/Makefile.am | 2 +- ext/standard/lcg.c | 98 ++++++++++++++++++++++++++++++++++++ ext/standard/php_lcg.h | 43 ++++++++++++++++ main/internal_functions.c.in | 2 + 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 ext/standard/lcg.c create mode 100644 ext/standard/php_lcg.h diff --git a/ext/standard/Makefile.am b/ext/standard/Makefile.am index 0f4f063c8a..a0ca94e0aa 100644 --- a/ext/standard/Makefile.am +++ b/ext/standard/Makefile.am @@ -8,7 +8,7 @@ libphpext_standard_a_SOURCES=\ pack.c pageinfo.c rand.c reg.c soundex.c string.c \ syslog.c type.c uniqid.c url.c iptc.c var.c quot_print.c \ cyr_convert.c flock_compat.c crypt.c dl.c head.c post.c \ - parsedate.y + parsedate.y lcg.c #number.o: number.c # $(CC) $(CFLAGS) -w@WARNING_LEVEL@ -c $< -o $@ diff --git a/ext/standard/lcg.c b/ext/standard/lcg.c new file mode 100644 index 0000000000..8fe4950aa0 --- /dev/null +++ b/ext/standard/lcg.c @@ -0,0 +1,98 @@ +/* + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997, 1998, 1999 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.0 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann <ss@2ns.de> | + +----------------------------------------------------------------------+ + */ + +#include "php.h" +#include "php_lcg.h" + +#if HAVE_UNISTD_H +#include <unistd.h> +#endif + +#ifdef ZTS +int lcg_globals_id; +#else +static php_lcg_globals lcg_globals; +#endif + +/* + * combinedLCG() returns a pseudo random number in the range of (0,1). + * The function combines two CGs with periods of + * 2^31 - 85 and 2^31 - 249. The period of this function + * is equal to the product of both primes. + */ + +#define MODMULT(a,b,c,m,s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m + +double php_combined_lcg(void) +{ + long q; + long z; + LCGLS_FETCH(); + + MODMULT(53668,40014,12211,2147483563L, LCG(s1)); + MODMULT(52774,40692,3791, 2147483399L, LCG(s2)); + + z = LCG(s1) - LCG(s2); + if(z < 1) { + z += 2147483562; + } + + return z * 4.656613e-10; +} + +static void init_globals(LCGLS_D) +{ + LCG(s1) = 1; +#ifdef ZTS + LCG(s2) = (long) tsrm_thread_id(); +#else + LCG(s2) = (long) getpid(); +#endif +} + +static int php_minit_lcg(INIT_FUNC_ARGS) +{ +#ifdef ZTS + lcg_globals_id = ts_allocate_id(sizeof(php_lcg_globals), init_globals, NULL); +#else + init_globals(); +#endif + return SUCCESS; +} + +/* {{{ proto double lcg_value() + returns a value from the combined linear congruential generator */ +PHP_FUNCTION(lcg_value) +{ + RETURN_DOUBLE(php_combined_lcg()); +} +/* }}} */ + +static function_entry lcg_functions[] = { + PHP_FE(lcg_value, NULL) + {0} +}; + +zend_module_entry lcg_module_entry = { + "LCG", + lcg_functions, + php_minit_lcg, NULL, + NULL, NULL, + NULL, + STANDARD_MODULE_PROPERTIES +}; diff --git a/ext/standard/php_lcg.h b/ext/standard/php_lcg.h new file mode 100644 index 0000000000..3476460dfa --- /dev/null +++ b/ext/standard/php_lcg.h @@ -0,0 +1,43 @@ +/* + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997, 1998, 1999 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.0 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann <ss@2ns.de> | + +----------------------------------------------------------------------+ + */ + +#ifndef PHP_LCG_H +#define PHP_LCG_H + +typedef struct { + long s1; + long s2; +} php_lcg_globals; + +double php_combined_lcg(void); +PHP_FUNCTION(lcg_value); + +#ifdef ZTS +#define LCGLS_D php_lcg_globals *lcg_globals +#define LCG(v) (lcg_globals->v) +#define LCGLS_FETCH() php_lcg_globals *lcg_globals = ts_resource(lcg_globals_id) +#else +#define LCGLS_D +#define LCG(v) (lcg_globals.v) +#define LCGLS_FETCH() +#endif + +extern zend_module_entry lcg_module_entry; +#define phpext_lcg_ptr &lcg_module_entry + +#endif diff --git a/main/internal_functions.c.in b/main/internal_functions.c.in index d849b80faf..d63679b52d 100644 --- a/main/internal_functions.c.in +++ b/main/internal_functions.c.in @@ -52,6 +52,7 @@ #include "ext/standard/php3_filestat.h" #include "ext/standard/php3_mail.h" #include "ext/standard/php3_syslog.h" +#include "ext/standard/php_lcg.h" @EXT_INCLUDE_CODE@ /* SNMP has to be moved to ext */ @@ -75,6 +76,7 @@ zend_module_entry *php3_builtin_modules[] = { phpext_filestat_ptr, phpext_mail_ptr, phpext_syslog_ptr, + phpext_lcg_ptr, @EXT_MODULE_PTRS@ }; -- 2.40.0