Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | COLOPL PHP Backwards Compatibility Extension. |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) COLOPL, Inc. |
6 : | Copyright (c) The PHP Group |
7 : +----------------------------------------------------------------------+
8 : | This source file is subject to version 3.01 of the PHP license, |
9 : | that is bundled with this package in the file LICENSE, and is |
10 : | available through the world-wide-web at the following url: |
11 : | http://www.php.net/license/3_01.txt |
12 : | If you did not receive a copy of the PHP license and are unable to |
13 : | obtain it through the world-wide-web, please send a note to |
14 : | license@php.net so we can mail you a copy immediately. |
15 : +----------------------------------------------------------------------+
16 : | Author: Go Kudo <g-kudo@colopl.co.jp> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : #ifndef PHP_COLOPL_BC_PHP70_H
21 : #define PHP_COLOPL_BC_PHP70_H
22 :
23 : #include "php_colopl_bc.h"
24 :
25 : #ifndef RAND_MAX
26 : # define RAND_MAX (1<<15)
27 : #endif
28 :
29 : #if !defined(ZTS) && (defined(HAVE_LRAND48) || defined(HAVE_RANDOM))
30 : # define PHP_COLOPL_BC_RAND_MAX 2147483647
31 : #else
32 : # define PHP_COLOPL_BC_RAND_MAX RAND_MAX
33 : #endif
34 :
35 : /* Emulate gcc + amd64 undefined behavior results. */
36 6410348 : static inline zend_long float_to_long_amd64(zend_long min, double f)
37 : {
38 6410348 : if (f > ((double) ZEND_LONG_MAX) || f < ((double) ZEND_LONG_MIN)) {
39 802116 : return 0;
40 : }
41 :
42 5608232 : return min + (zend_long) f;
43 : }
44 :
45 : # define PHP_COLOPL_BC_RAND_RANGE(__n, __min, __max, __tmax) \
46 : (__n) = float_to_long_amd64(__min, ((double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
47 :
48 : #ifdef PHP_WIN32
49 : # define COLOPL_BC_GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
50 : #else
51 : # define COLOPL_BC_GENERATE_SEED() (((zend_long) (time(0) * getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
52 : #endif
53 :
54 : /* rand.c */
55 :
56 : PHPAPI void php_colopl_bc_srand(zend_long seed);
57 : PHPAPI zend_long php_colopl_bc_rand(void);
58 :
59 : /* mt_rand.c */
60 :
61 : /*
62 : The following php_colopl_bc_mt_...() functions are based on a C++ class MTRand by
63 : Richard J. Wagner. For more information see the web page at
64 : http://www-personal.engin.umich.edu/~wagnerr/MersenneTwister.html
65 :
66 : Mersenne Twister random number generator -- a C++ class MTRand
67 : Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
68 : Richard J. Wagner v1.0 15 May 2003 rjwagner@writeme.com
69 :
70 : The Mersenne Twister is an algorithm for generating random numbers. It
71 : was designed with consideration of the flaws in various other generators.
72 : The period, 2^19937-1, and the order of equidistribution, 623 dimensions,
73 : are far greater. The generator is also fast; it avoids multiplication and
74 : division, and it benefits from caches and pipelines. For more information
75 : see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html
76 :
77 : Reference
78 : M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
79 : Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on
80 : Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
81 :
82 : Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
83 : Copyright (C) 2000 - 2003, Richard J. Wagner
84 : All rights reserved.
85 :
86 : Redistribution and use in source and binary forms, with or without
87 : modification, are permitted provided that the following conditions
88 : are met:
89 :
90 : 1. Redistributions of source code must retain the above copyright
91 : notice, this list of conditions and the following disclaimer.
92 :
93 : 2. Redistributions in binary form must reproduce the above copyright
94 : notice, this list of conditions and the following disclaimer in the
95 : documentation and/or other materials provided with the distribution.
96 :
97 : 3. The names of its contributors may not be used to endorse or promote
98 : products derived from this software without specific prior written
99 : permission.
100 :
101 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
102 : "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
103 : LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
104 : A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
105 : CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
106 : EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
107 : PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
108 : PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
109 : LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
110 : NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
111 : SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112 : */
113 :
114 : #define PHP_COLOPL_BC_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
115 :
116 : #define N (624) /* length of state vector */
117 : #define M (397) /* a period parameter */
118 : #define hiBit(u) ((u) & 0x80000000U) /* mask all but highest bit of u */
119 : #define loBit(u) ((u) & 0x00000001U) /* mask all but lowest bit of u */
120 : #define loBits(u) ((u) & 0x7FFFFFFFU) /* mask the highest bit of u */
121 : #define mixBits(u, v) (hiBit(u)|loBits(v)) /* move hi bit of u to hi bit of v */
122 :
123 : #define twist(m,u,v) (m ^ (mixBits(u,v)>>1) ^ ((uint32_t)(-(uint32_t)(loBit(u))) & 0x9908b0dfU))
124 :
125 : PHPAPI void php_colopl_bc_mt_srand(uint32_t seed);
126 : PHPAPI uint32_t php_colopl_bc_mt_rand(void);
127 :
128 : /* array.c */
129 :
130 : PHPAPI void php_colopl_bc_array_data_shuffle(zval *array);
131 :
132 : /* string.c */
133 :
134 : PHPAPI void php_colopl_bc_string_shuffle(char *str, zend_long len);
135 :
136 : #endif /* PHP_COLOPL_BC_PHP70_H */
|