Add options to enable sharding
[hiphop-php.git] / hphp / zend / zend-math.h
blob8e8bbfe84cf41f75e71b43af3f9ba8efc474f6a0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1998-2010 Zend Technologies Ltd. (http://www.zend.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt. |
12 | If you did not receive a copy of the Zend license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@zend.com so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #ifndef incl_HPHP_ZEND_MATH_H_
19 #define incl_HPHP_ZEND_MATH_H_
21 #include <cmath>
22 #include <cstdint>
24 namespace HPHP {
25 ///////////////////////////////////////////////////////////////////////////////
27 #define PHP_ROUND_HALF_UP 1
28 #define PHP_ROUND_HALF_DOWN 2
29 #define PHP_ROUND_HALF_EVEN 3
30 #define PHP_ROUND_HALF_ODD 4
32 double php_math_round(double value, int places, int mode = PHP_ROUND_HALF_UP);
34 /* System Rand functions */
35 #ifndef RAND_MAX
36 #define RAND_MAX (1<<15)
37 #endif
39 #define MT_RAND_MAX 0x7FFFFFFFLL
42 * A bit of tricky math here. We want to avoid using a modulus because
43 * that simply tosses the high-order bits and might skew the distribution
44 * of random values over the range. Instead we map the range directly.
46 * We need to map the range from 0...M evenly to the range a...b
47 * Let n = the random number and n' = the mapped random number
49 * Then we have: n' = a + n(b-a)/M
51 * We have a problem here in that only n==M will get mapped to b which
52 # means the chances of getting b is much much less than getting any of
53 # the other values in the range. We can fix this by increasing our range
54 # artifically and using:
56 # n' = a + n(b-a+1)/M
58 # Now we only have a problem if n==M which would cause us to produce a
59 # number of b+1 which would be bad. So we bump M up by one to make sure
60 # this will never happen, and the final algorithm looks like this:
62 # n' = a + n(b-a+1)/(M+1)
64 * -RL
66 #define RAND_RANGE(__n, __min, __max, __tmax) \
67 (__n) = (__min) + (int64_t) ((double) ((double)(__max) - (__min) + 1.0) * \
68 ((__n) / ((__tmax) + 1.0)))
70 #define GENERATE_SEED() \
71 (((long) (time(0) * getpid())) ^ ((long) (1000000.0 * math_combined_lcg())))
74 ///////////////////////////////////////////////////////////////////////////////
76 void math_mt_srand(uint32_t seed);
77 int64_t math_mt_rand(int64_t min = 0, int64_t max = RAND_MAX);
79 double math_combined_lcg();
80 int64_t math_generate_seed();
83 * The zend rand bookkeeping gets initialized at thread startup, and gets
84 * reseeded on its first use in a request. This is done by wiping the 'seeded'
85 * bit at the end of each request.
87 void zend_rand_init();
88 void zend_rand_unseed();
90 ///////////////////////////////////////////////////////////////////////////////
93 #endif // incl_HPHP_ZEND_MATH_H_