17259Sdr146992 /*
27259Sdr146992 * CDDL HEADER START
37259Sdr146992 *
47259Sdr146992 * The contents of this file are subject to the terms of the
57259Sdr146992 * Common Development and Distribution License (the "License").
67259Sdr146992 * You may not use this file except in compliance with the License.
77259Sdr146992 *
87259Sdr146992 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97259Sdr146992 * or http://www.opensolaris.org/os/licensing.
107259Sdr146992 * See the License for the specific language governing permissions
117259Sdr146992 * and limitations under the License.
127259Sdr146992 *
137259Sdr146992 * When distributing Covered Code, include this CDDL HEADER in each
147259Sdr146992 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157259Sdr146992 * If applicable, add the following below this CDDL HEADER, with the
167259Sdr146992 * fields enclosed by brackets "[]" replaced with your own identifying
177259Sdr146992 * information: Portions Copyright [yyyy] [name of copyright owner]
187259Sdr146992 *
197259Sdr146992 * CDDL HEADER END
207259Sdr146992 */
217259Sdr146992
227259Sdr146992 /*
23*8690SDarren.Reed@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247259Sdr146992 * Use is subject to license terms.
257259Sdr146992 */
267259Sdr146992
277259Sdr146992 /* Copyright (c) 1988 AT&T */
287259Sdr146992 /* All Rights Reserved */
297259Sdr146992
307259Sdr146992 /*
317259Sdr146992 * drand48, etc. pseudo-random number generator
327259Sdr146992 * This implementation assumes unsigned short integers of at least
337259Sdr146992 * 16 bits, long integers of at least 32 bits, and ignores
347259Sdr146992 * overflows on adding or multiplying two unsigned integers.
357259Sdr146992 * Two's-complement representation is assumed in a few places.
367259Sdr146992 * Some extra masking is done if unsigneds are exactly 16 bits
377259Sdr146992 * or longs are exactly 32 bits, but so what?
387259Sdr146992 * An assembly-language implementation would run significantly faster.
397259Sdr146992 */
407259Sdr146992 /*
417259Sdr146992 * New assumptions (supercede those stated above) for 64-bit work.
427259Sdr146992 * Longs are now 64 bits, and we are bound by standards to return
437259Sdr146992 * type long, hovever all internal calculations where long was
447259Sdr146992 * previously used (32 bit precision) are now using the int32_t
457259Sdr146992 * type (32 bit precision in both ILP32 and LP64 worlds).
467259Sdr146992 */
477259Sdr146992
487259Sdr146992 #include <sys/mutex.h>
497259Sdr146992
507259Sdr146992 static kmutex_t seed_lock;
517259Sdr146992 static int init48done = 0;
527259Sdr146992
537259Sdr146992 #define EXPORT0(TYPE, fn, fnu) TYPE fn() { \
547259Sdr146992 TYPE res; \
557259Sdr146992 mutex_enter(&seed_lock); \
567259Sdr146992 res = fnu(); \
577259Sdr146992 mutex_exit(&seed_lock); \
587259Sdr146992 return (res); }
597259Sdr146992 #define EXPORT1(TYPE, fn, fnu) TYPE fn(unsigned short xsubi[3]) { \
607259Sdr146992 TYPE res; \
617259Sdr146992 mutex_enter(&seed_lock); \
627259Sdr146992 res = fnu(xsubi); \
637259Sdr146992 mutex_exit(&seed_lock); \
647259Sdr146992 return (res); }
657259Sdr146992
667259Sdr146992 #define N 16
677259Sdr146992 #define MASK ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
687259Sdr146992 #define LOW(x) ((unsigned)(x) & MASK)
697259Sdr146992 #define HIGH(x) LOW((x) >> N)
707259Sdr146992 #define MUL(x, y, z) { int32_t l = (int32_t)(x) * (int32_t)(y); \
717259Sdr146992 (z)[0] = LOW(l); (z)[1] = HIGH(l); }
727259Sdr146992 #define CARRY(x, y) ((int32_t)(x) + (int32_t)(y) > MASK)
737259Sdr146992 #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
747259Sdr146992 #define X0 0x330E
757259Sdr146992 #define X1 0xABCD
767259Sdr146992 #define X2 0x1234
777259Sdr146992 #define A0 0xE66D
787259Sdr146992 #define A1 0xDEEC
797259Sdr146992 #define A2 0x5
807259Sdr146992 #define C 0xB
817259Sdr146992 #define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
827259Sdr146992 #define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
837259Sdr146992 #define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
847259Sdr146992 #define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
857259Sdr146992 return (v)
867259Sdr146992 #define NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
877259Sdr146992 int i; TYPE v; unsigned temp[3]; \
887259Sdr146992 for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); } \
897259Sdr146992 v = F(); REST(v); }
907259Sdr146992
917259Sdr146992 /* Way ugly solution to problem names, but it works */
927259Sdr146992 #define x _drand48_x
937259Sdr146992 #define a _drand48_a
947259Sdr146992 #define c _drand48_c
957259Sdr146992 /* End way ugly */
967259Sdr146992 static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
977259Sdr146992 static unsigned short lastx[3];
987259Sdr146992 static void next(void);
997259Sdr146992
1007259Sdr146992 static long
ipf_r_lrand48_u(void)1017259Sdr146992 ipf_r_lrand48_u(void)
1027259Sdr146992 {
1037259Sdr146992 next();
1047259Sdr146992 return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
1057259Sdr146992 }
1067259Sdr146992
1077259Sdr146992 static void
init48(void)1087259Sdr146992 init48(void)
1097259Sdr146992 {
1107259Sdr146992 mutex_init(&seed_lock, 0L, MUTEX_DRIVER, 0L);
1117259Sdr146992 init48done = 1;
1127259Sdr146992 }
1137259Sdr146992
1147259Sdr146992 static long
ipf_r_mrand48_u(void)1157259Sdr146992 ipf_r_mrand48_u(void)
1167259Sdr146992 {
1177259Sdr146992 next();
1187259Sdr146992 return ((long)((int32_t)x[2] << N) + x[1]);
1197259Sdr146992 }
1207259Sdr146992
1217259Sdr146992 static void
next(void)1227259Sdr146992 next(void)
1237259Sdr146992 {
1247259Sdr146992 unsigned p[2], q[2], r[2], carry0, carry1;
1257259Sdr146992
1267259Sdr146992 MUL(a[0], x[0], p);
1277259Sdr146992 ADDEQU(p[0], c, carry0);
1287259Sdr146992 ADDEQU(p[1], carry0, carry1);
1297259Sdr146992 MUL(a[0], x[1], q);
1307259Sdr146992 ADDEQU(p[1], q[0], carry0);
1317259Sdr146992 MUL(a[1], x[0], r);
1327259Sdr146992 x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
1337259Sdr146992 a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
1347259Sdr146992 x[1] = LOW(p[1] + r[0]);
1357259Sdr146992 x[0] = LOW(p[0]);
1367259Sdr146992 }
1377259Sdr146992
1387259Sdr146992 void
ipf_r_srand48(long seedval)1397259Sdr146992 ipf_r_srand48(long seedval)
1407259Sdr146992 {
1417259Sdr146992 int32_t fixseed = (int32_t)seedval; /* limit to 32 bits */
1427259Sdr146992
1437259Sdr146992 if (init48done == 0)
1447259Sdr146992 init48();
1457259Sdr146992 mutex_enter(&seed_lock);
1467259Sdr146992 SEED(X0, LOW(fixseed), HIGH(fixseed));
1477259Sdr146992 mutex_exit(&seed_lock);
1487259Sdr146992 }
1497259Sdr146992
EXPORT0(long,ipf_r_lrand48,ipf_r_lrand48_u)1507259Sdr146992 EXPORT0(long, ipf_r_lrand48, ipf_r_lrand48_u)
1517259Sdr146992
1527259Sdr146992 #include <sys/random.h>
1537259Sdr146992
1547259Sdr146992 unsigned
1557259Sdr146992 ipf_random()
1567259Sdr146992 {
1577259Sdr146992 static int seeded = 0;
1587259Sdr146992
1597259Sdr146992 if (seeded == 0) {
1607259Sdr146992 long seed;
1617259Sdr146992
1627259Sdr146992 /*
1637259Sdr146992 * Keep reseeding until some good randomness comes from the
1647259Sdr146992 * kernel. One of two things will happen: it will succeed or
1657259Sdr146992 * it will fail (with poor randomness), thus creating NAT
1667259Sdr146992 * sessions will be "slow" until enough randomness is gained
1677259Sdr146992 * to not need to get more. It isn't necessary to initialise
1687259Sdr146992 * seed as it will just pickup whatever random garbage has
1697259Sdr146992 * been left on the heap and that's good enough until we
1707259Sdr146992 * get some good garbage.
1717259Sdr146992 */
1727259Sdr146992 if (random_get_bytes((uint8_t *)&seed, sizeof (seed)) == 0)
1737259Sdr146992 seeded = 1;
1747259Sdr146992 ipf_r_srand48(seed);
1757259Sdr146992 }
1767259Sdr146992
1777259Sdr146992 return (unsigned)ipf_r_lrand48();
1787259Sdr146992 }
179