ab073b324433ebc8947d28ade932d29d0e809795dr * CDDL HEADER START
ab073b324433ebc8947d28ade932d29d0e809795dr * The contents of this file are subject to the terms of the
ab073b324433ebc8947d28ade932d29d0e809795dr * Common Development and Distribution License (the "License").
ab073b324433ebc8947d28ade932d29d0e809795dr * You may not use this file except in compliance with the License.
ab073b324433ebc8947d28ade932d29d0e809795dr * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
ab073b324433ebc8947d28ade932d29d0e809795dr * See the License for the specific language governing permissions
ab073b324433ebc8947d28ade932d29d0e809795dr * and limitations under the License.
ab073b324433ebc8947d28ade932d29d0e809795dr * When distributing Covered Code, include this CDDL HEADER in each
ab073b324433ebc8947d28ade932d29d0e809795dr * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
ab073b324433ebc8947d28ade932d29d0e809795dr * If applicable, add the following below this CDDL HEADER, with the
ab073b324433ebc8947d28ade932d29d0e809795dr * fields enclosed by brackets "[]" replaced with your own identifying
ab073b324433ebc8947d28ade932d29d0e809795dr * information: Portions Copyright [yyyy] [name of copyright owner]
ab073b324433ebc8947d28ade932d29d0e809795dr * CDDL HEADER END
62d17c5fefb1ca81dc75288f899e19485b3f72f1Darren Reed * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
ab073b324433ebc8947d28ade932d29d0e809795dr * Use is subject to license terms.
ab073b324433ebc8947d28ade932d29d0e809795dr/* Copyright (c) 1988 AT&T */
ab073b324433ebc8947d28ade932d29d0e809795dr/* All Rights Reserved */
ab073b324433ebc8947d28ade932d29d0e809795dr * drand48, etc. pseudo-random number generator
ab073b324433ebc8947d28ade932d29d0e809795dr * This implementation assumes unsigned short integers of at least
ab073b324433ebc8947d28ade932d29d0e809795dr * 16 bits, long integers of at least 32 bits, and ignores
ab073b324433ebc8947d28ade932d29d0e809795dr * overflows on adding or multiplying two unsigned integers.
ab073b324433ebc8947d28ade932d29d0e809795dr * Two's-complement representation is assumed in a few places.
ab073b324433ebc8947d28ade932d29d0e809795dr * Some extra masking is done if unsigneds are exactly 16 bits
ab073b324433ebc8947d28ade932d29d0e809795dr * or longs are exactly 32 bits, but so what?
ab073b324433ebc8947d28ade932d29d0e809795dr * An assembly-language implementation would run significantly faster.
ab073b324433ebc8947d28ade932d29d0e809795dr * New assumptions (supercede those stated above) for 64-bit work.
ab073b324433ebc8947d28ade932d29d0e809795dr * Longs are now 64 bits, and we are bound by standards to return
ab073b324433ebc8947d28ade932d29d0e809795dr * type long, hovever all internal calculations where long was
ab073b324433ebc8947d28ade932d29d0e809795dr * previously used (32 bit precision) are now using the int32_t
ab073b324433ebc8947d28ade932d29d0e809795dr * type (32 bit precision in both ILP32 and LP64 worlds).
ab073b324433ebc8947d28ade932d29d0e809795dr return (res); }
ab073b324433ebc8947d28ade932d29d0e809795dr#define EXPORT1(TYPE, fn, fnu) TYPE fn(unsigned short xsubi[3]) { \
ab073b324433ebc8947d28ade932d29d0e809795dr return (res); }
ab073b324433ebc8947d28ade932d29d0e809795dr#define N 16
ab073b324433ebc8947d28ade932d29d0e809795dr#define MASK ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
ab073b324433ebc8947d28ade932d29d0e809795dr#define MUL(x, y, z) { int32_t l = (int32_t)(x) * (int32_t)(y); \
ab073b324433ebc8947d28ade932d29d0e809795dr#define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
ab073b324433ebc8947d28ade932d29d0e809795dr#define C 0xB
ab073b324433ebc8947d28ade932d29d0e809795dr#define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
ab073b324433ebc8947d28ade932d29d0e809795dr#define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
ab073b324433ebc8947d28ade932d29d0e809795dr#define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
ab073b324433ebc8947d28ade932d29d0e809795dr#define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
ab073b324433ebc8947d28ade932d29d0e809795dr return (v)
ab073b324433ebc8947d28ade932d29d0e809795dr#define NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
ab073b324433ebc8947d28ade932d29d0e809795dr for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); } \
ab073b324433ebc8947d28ade932d29d0e809795dr v = F(); REST(v); }
ab073b324433ebc8947d28ade932d29d0e809795dr/* Way ugly solution to problem names, but it works */
ab073b324433ebc8947d28ade932d29d0e809795dr/* End way ugly */
ab073b324433ebc8947d28ade932d29d0e809795drstatic unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
ab073b324433ebc8947d28ade932d29d0e809795drstatic void next(void);
ab073b324433ebc8947d28ade932d29d0e809795drstatic long
ab073b324433ebc8947d28ade932d29d0e809795drstatic void
ab073b324433ebc8947d28ade932d29d0e809795drstatic long
ab073b324433ebc8947d28ade932d29d0e809795drstatic void
ab073b324433ebc8947d28ade932d29d0e809795dr MUL(a[0], x[0], p);
ab073b324433ebc8947d28ade932d29d0e809795dr x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
ab073b324433ebc8947d28ade932d29d0e809795dr x[0] = LOW(p[0]);
ab073b324433ebc8947d28ade932d29d0e809795dr int32_t fixseed = (int32_t)seedval; /* limit to 32 bits */
ab073b324433ebc8947d28ade932d29d0e809795dr if (seeded == 0) {
ab073b324433ebc8947d28ade932d29d0e809795dr * Keep reseeding until some good randomness comes from the
ab073b324433ebc8947d28ade932d29d0e809795dr * kernel. One of two things will happen: it will succeed or
ab073b324433ebc8947d28ade932d29d0e809795dr * it will fail (with poor randomness), thus creating NAT
ab073b324433ebc8947d28ade932d29d0e809795dr * sessions will be "slow" until enough randomness is gained
ab073b324433ebc8947d28ade932d29d0e809795dr * to not need to get more. It isn't necessary to initialise
ab073b324433ebc8947d28ade932d29d0e809795dr * seed as it will just pickup whatever random garbage has
ab073b324433ebc8947d28ade932d29d0e809795dr * been left on the heap and that's good enough until we
ab073b324433ebc8947d28ade932d29d0e809795dr * get some good garbage.
ab073b324433ebc8947d28ade932d29d0e809795dr if (random_get_bytes((uint8_t *)&seed, sizeof (seed)) == 0)
ab073b324433ebc8947d28ade932d29d0e809795dr return (unsigned)ipf_r_lrand48();