PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - lcg.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 25 26 96.2 %
Date: 2014-08-04 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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             : /* $Id$ */
      20             : 
      21             : #include "php.h"
      22             : #include "php_lcg.h"
      23             : 
      24             : #if HAVE_UNISTD_H
      25             : #include <unistd.h>
      26             : #endif
      27             : 
      28             : #ifdef PHP_WIN32
      29             : #include "win32/time.h"
      30             : #else
      31             : #include <sys/time.h>
      32             : #endif
      33             : 
      34             : #ifdef ZTS
      35             : int lcg_globals_id;
      36             : #else
      37             : static php_lcg_globals lcg_globals;
      38             : #endif
      39             : 
      40             : #ifdef PHP_WIN32
      41             : #include <process.h>
      42             : #endif
      43             : 
      44             : /*
      45             :  * combinedLCG() returns a pseudo random number in the range of (0, 1).
      46             :  * The function combines two CGs with periods of
      47             :  * 2^31 - 85 and 2^31 - 249. The period of this function
      48             :  * is equal to the product of both primes.
      49             :  */
      50             : 
      51             : #define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m
      52             : 
      53             : static void lcg_seed(TSRMLS_D);
      54             : 
      55         794 : PHPAPI double php_combined_lcg(TSRMLS_D) /* {{{ */
      56             : {
      57             :         php_int32 q;
      58             :         php_int32 z;
      59             : 
      60         794 :         if (!LCG(seeded)) {
      61         322 :                 lcg_seed(TSRMLS_C);
      62             :         }
      63             : 
      64         794 :         MODMULT(53668, 40014, 12211, 2147483563L, LCG(s1));
      65         794 :         MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2));
      66             : 
      67         794 :         z = LCG(s1) - LCG(s2);
      68         794 :         if (z < 1) {
      69         376 :                 z += 2147483562;
      70             :         }
      71             : 
      72         794 :         return z * 4.656613e-10;
      73             : }
      74             : /* }}} */
      75             : 
      76         322 : static void lcg_seed(TSRMLS_D) /* {{{ */
      77             : {
      78             :         struct timeval tv;
      79             : 
      80         322 :         if (gettimeofday(&tv, NULL) == 0) {
      81         322 :                 LCG(s1) = tv.tv_sec ^ (tv.tv_usec<<11);
      82             :         } else {
      83           0 :                 LCG(s1) = 1;
      84             :         }
      85             : #ifdef ZTS
      86             :         LCG(s2) = (long) tsrm_thread_id();
      87             : #else
      88         322 :         LCG(s2) = (long) getpid();
      89             : #endif
      90             : 
      91             :         /* Add entropy to s2 by calling gettimeofday() again */
      92         322 :         if (gettimeofday(&tv, NULL) == 0) {
      93         322 :                 LCG(s2) ^= (tv.tv_usec<<11);
      94             :         }
      95             : 
      96         322 :         LCG(seeded) = 1;
      97         322 : }
      98             : /* }}} */
      99             : 
     100       21265 : static void lcg_init_globals(php_lcg_globals *lcg_globals_p TSRMLS_DC) /* {{{ */
     101             : {
     102       21265 :         LCG(seeded) = 0;
     103       21265 : }
     104             : /* }}} */
     105             : 
     106       21265 : PHP_MINIT_FUNCTION(lcg) /* {{{ */
     107             : {
     108             : #ifdef ZTS
     109             :         ts_allocate_id(&lcg_globals_id, sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL);
     110             : #else
     111       21265 :         lcg_init_globals(&lcg_globals);
     112             : #endif
     113       21265 :         return SUCCESS;
     114             : }
     115             : /* }}} */
     116             : 
     117             : /* {{{ proto float lcg_value()
     118             :    Returns a value from the combined linear congruential generator */
     119         102 : PHP_FUNCTION(lcg_value)
     120             : {
     121         102 :         RETURN_DOUBLE(php_combined_lcg(TSRMLS_C));
     122             : }
     123             : /* }}} */
     124             : 
     125             : /*
     126             :  * Local variables:
     127             :  * tab-width: 4
     128             :  * c-basic-offset: 4
     129             :  * End:
     130             :  * vim600: sw=4 ts=4 fdm=marker
     131             :  * vim<600: sw=4 ts=4
     132             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:15 +0000 (43 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.