Back to home page

Redis cross reference

 
 

    


0001 /* Pseudo random number generation functions derived from the drand48()
0002  * function obtained from pysam source code.
0003  *
0004  * This functions are used in order to replace the default math.random()
0005  * Lua implementation with something having exactly the same behavior
0006  * across different systems (by default Lua uses libc's rand() that is not
0007  * required to implement a specific PRNG generating the same sequence
0008  * in different systems if seeded with the same integer).
0009  *
0010  * The original code appears to be under the public domain.
0011  * I modified it removing the non needed functions and all the
0012  * 1960-style C coding stuff...
0013  *
0014  * ----------------------------------------------------------------------------
0015  *
0016  * Copyright (c) 2010-2012, Salvatore Sanfilippo <antirez at gmail dot com>
0017  * All rights reserved.
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions are met:
0021  *
0022  *   * Redistributions of source code must retain the above copyright notice,
0023  *     this list of conditions and the following disclaimer.
0024  *   * Redistributions in binary form must reproduce the above copyright
0025  *     notice, this list of conditions and the following disclaimer in the
0026  *     documentation and/or other materials provided with the distribution.
0027  *   * Neither the name of Redis nor the names of its contributors may be used
0028  *     to endorse or promote products derived from this software without
0029  *     specific prior written permission.
0030  *
0031  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0032  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0034  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0035  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0036  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0037  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0038  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0039  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0040  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0041  * POSSIBILITY OF SUCH DAMAGE.
0042  */
0043 
0044 #include <stdint.h>
0045 
0046 #define N   16
0047 #define MASK    ((1 << (N - 1)) + (1 << (N - 1)) - 1)
0048 #define LOW(x)  ((unsigned)(x) & MASK)
0049 #define HIGH(x) LOW((x) >> N)
0050 #define MUL(x, y, z)    { int32_t l = (long)(x) * (long)(y); \
0051         (z)[0] = LOW(l); (z)[1] = HIGH(l); }
0052 #define CARRY(x, y) ((int32_t)(x) + (long)(y) > MASK)
0053 #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
0054 #define X0  0x330E
0055 #define X1  0xABCD
0056 #define X2  0x1234
0057 #define A0  0xE66D
0058 #define A1  0xDEEC
0059 #define A2  0x5
0060 #define C   0xB
0061 #define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
0062 #define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
0063 #define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
0064 #define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
0065         return (v);
0066 #define HI_BIT  (1L << (2 * N - 1))
0067 
0068 static uint32_t x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
0069 static void next();
0070 
0071 int32_t redisLrand48() {
0072     next();
0073     return (((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
0074 }
0075 
0076 void redisSrand48(int32_t seedval) {
0077     SEED(X0, LOW(seedval), HIGH(seedval));
0078 }
0079 
0080 static void next() {
0081     uint32_t p[2], q[2], r[2], carry0, carry1;
0082 
0083     MUL(a[0], x[0], p);
0084     ADDEQU(p[0], c, carry0);
0085     ADDEQU(p[1], carry0, carry1);
0086     MUL(a[0], x[1], q);
0087     ADDEQU(p[1], q[0], carry0);
0088     MUL(a[1], x[0], r);
0089     x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
0090             a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
0091     x[1] = LOW(p[1] + r[0]);
0092     x[0] = LOW(p[0]);
0093 }