clang-format
This commit is contained in:
@@ -52,7 +52,6 @@
|
||||
static void srandom_unlocked(unsigned long);
|
||||
static long random_unlocked(void);
|
||||
|
||||
|
||||
/*
|
||||
* random.c:
|
||||
*
|
||||
@@ -119,39 +118,39 @@ static long random_unlocked(void);
|
||||
* for the polynomial (actually a trinomial) that the R.N.G. is based on, and
|
||||
* the separation between the two lower order coefficients of the trinomial.
|
||||
*/
|
||||
#define TYPE_0 0 /* linear congruential */
|
||||
#define BREAK_0 8
|
||||
#define DEG_0 0
|
||||
#define SEP_0 0
|
||||
#define TYPE_0 0 /* linear congruential */
|
||||
#define BREAK_0 8
|
||||
#define DEG_0 0
|
||||
#define SEP_0 0
|
||||
|
||||
#define TYPE_1 1 /* x**7 + x**3 + 1 */
|
||||
#define BREAK_1 32
|
||||
#define DEG_1 7
|
||||
#define SEP_1 3
|
||||
#define TYPE_1 1 /* x**7 + x**3 + 1 */
|
||||
#define BREAK_1 32
|
||||
#define DEG_1 7
|
||||
#define SEP_1 3
|
||||
|
||||
#define TYPE_2 2 /* x**15 + x + 1 */
|
||||
#define BREAK_2 64
|
||||
#define DEG_2 15
|
||||
#define SEP_2 1
|
||||
#define TYPE_2 2 /* x**15 + x + 1 */
|
||||
#define BREAK_2 64
|
||||
#define DEG_2 15
|
||||
#define SEP_2 1
|
||||
|
||||
#define TYPE_3 3 /* x**31 + x**3 + 1 */
|
||||
#define BREAK_3 128
|
||||
#define DEG_3 31
|
||||
#define SEP_3 3
|
||||
#define TYPE_3 3 /* x**31 + x**3 + 1 */
|
||||
#define BREAK_3 128
|
||||
#define DEG_3 31
|
||||
#define SEP_3 3
|
||||
|
||||
#define TYPE_4 4 /* x**63 + x + 1 */
|
||||
#define BREAK_4 256
|
||||
#define DEG_4 63
|
||||
#define SEP_4 1
|
||||
#define TYPE_4 4 /* x**63 + x + 1 */
|
||||
#define BREAK_4 256
|
||||
#define DEG_4 63
|
||||
#define SEP_4 1
|
||||
|
||||
/*
|
||||
* Array versions of the above information to make code run faster --
|
||||
* relies on fact that TYPE_i == i.
|
||||
*/
|
||||
#define MAX_TYPES 5 /* max number of types above */
|
||||
#define MAX_TYPES 5 /* max number of types above */
|
||||
|
||||
static const int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
|
||||
static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
|
||||
static const int degrees[MAX_TYPES] = {DEG_0, DEG_1, DEG_2, DEG_3, DEG_4};
|
||||
static const int seps[MAX_TYPES] = {SEP_0, SEP_1, SEP_2, SEP_3, SEP_4};
|
||||
|
||||
/*
|
||||
* Initially, everything is set up as if from:
|
||||
@@ -168,18 +167,38 @@ static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
|
||||
*/
|
||||
|
||||
static long randtbl[DEG_3 + 1] = {
|
||||
TYPE_3,
|
||||
(long)0x9a319039L, (long)0x32d9c024L, (long)0x9b663182L,
|
||||
(long)0x5da1f342L, (long)0xde3b81e0L, (long)0xdf0a6fb5L,
|
||||
(long)0xf103bc02L, (long)0x48f340fbL, (long)0x7449e56bL,
|
||||
(long)0xbeb1dbb0L, (long)0xab5c5918L, (long)0x946554fdL,
|
||||
(long)0x8c2e680fL, (long)0xeb3d799fL, (long)0xb11ee0b7L,
|
||||
(long)0x2d436b86L, (long)0xda672e2aL, (long)0x1588ca88L,
|
||||
(long)0xe369735dL, (long)0x904f35f7L, (long)0xd7158fd6L,
|
||||
(long)0x6fa6f051L, (long)0x616e6b96L, (long)0xac94efdcL,
|
||||
(long)0x36413f93L, (long)0xc622c298L, (long)0xf5a42ab8L,
|
||||
(long)0x8a88d77bL, (long)0xf5ad9d0eL, (long)0x8999220bL,
|
||||
(long)0x27fb47b9L,
|
||||
TYPE_3,
|
||||
(long)0x9a319039L,
|
||||
(long)0x32d9c024L,
|
||||
(long)0x9b663182L,
|
||||
(long)0x5da1f342L,
|
||||
(long)0xde3b81e0L,
|
||||
(long)0xdf0a6fb5L,
|
||||
(long)0xf103bc02L,
|
||||
(long)0x48f340fbL,
|
||||
(long)0x7449e56bL,
|
||||
(long)0xbeb1dbb0L,
|
||||
(long)0xab5c5918L,
|
||||
(long)0x946554fdL,
|
||||
(long)0x8c2e680fL,
|
||||
(long)0xeb3d799fL,
|
||||
(long)0xb11ee0b7L,
|
||||
(long)0x2d436b86L,
|
||||
(long)0xda672e2aL,
|
||||
(long)0x1588ca88L,
|
||||
(long)0xe369735dL,
|
||||
(long)0x904f35f7L,
|
||||
(long)0xd7158fd6L,
|
||||
(long)0x6fa6f051L,
|
||||
(long)0x616e6b96L,
|
||||
(long)0xac94efdcL,
|
||||
(long)0x36413f93L,
|
||||
(long)0xc622c298L,
|
||||
(long)0xf5a42ab8L,
|
||||
(long)0x8a88d77bL,
|
||||
(long)0xf5ad9d0eL,
|
||||
(long)0x8999220bL,
|
||||
(long)0x27fb47b9L,
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -227,32 +246,27 @@ static long *end_ptr = &randtbl[DEG_3 + 1];
|
||||
* introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
|
||||
* for default usage relies on values produced by this routine.
|
||||
*/
|
||||
static
|
||||
void
|
||||
srandom_unlocked(unsigned long x)
|
||||
{
|
||||
int i;
|
||||
static void srandom_unlocked(unsigned long x) {
|
||||
int i;
|
||||
|
||||
if (rand_type == TYPE_0)
|
||||
state[0] = x;
|
||||
else {
|
||||
state[0] = x;
|
||||
for (i = 1; i < rand_deg; i++)
|
||||
state[i] = 1103515245L * state[i - 1] + 12345L;
|
||||
fptr = &state[rand_sep];
|
||||
rptr = &state[0];
|
||||
for (i = 0; i < 10 * rand_deg; i++)
|
||||
(void)random_unlocked();
|
||||
}
|
||||
if (rand_type == TYPE_0)
|
||||
state[0] = x;
|
||||
else {
|
||||
state[0] = x;
|
||||
for (i = 1; i < rand_deg; i++)
|
||||
state[i] = 1103515245L * state[i - 1] + 12345L;
|
||||
fptr = &state[rand_sep];
|
||||
rptr = &state[0];
|
||||
for (i = 0; i < 10 * rand_deg; i++)
|
||||
(void)random_unlocked();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
srandom(unsigned long x)
|
||||
{
|
||||
void srandom(unsigned long x) {
|
||||
|
||||
LOCKME();
|
||||
srandom_unlocked(x);
|
||||
UNLOCKME();
|
||||
LOCKME();
|
||||
srandom_unlocked(x);
|
||||
UNLOCKME();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -278,57 +292,55 @@ srandom(unsigned long x)
|
||||
* word boundary; otherwise a bus error will occur. Even so, lint will
|
||||
* complain about mis-alignment, but you should disregard these messages.
|
||||
*/
|
||||
char *
|
||||
initstate(
|
||||
unsigned long seed, /* seed for R.N.G. */
|
||||
char *arg_state, /* pointer to state array */
|
||||
size_t n) /* # bytes of state info */
|
||||
char *initstate(unsigned long seed, /* seed for R.N.G. */
|
||||
char *arg_state, /* pointer to state array */
|
||||
size_t n) /* # bytes of state info */
|
||||
{
|
||||
void *ostate = (void *)(&state[-1]);
|
||||
long *long_arg_state;
|
||||
void *ostate = (void *)(&state[-1]);
|
||||
long *long_arg_state;
|
||||
|
||||
assert(arg_state != NULL);
|
||||
assert(arg_state != NULL);
|
||||
|
||||
long_arg_state = (long *)(void *)arg_state;
|
||||
long_arg_state = (long *)(void *)arg_state;
|
||||
|
||||
LOCKME();
|
||||
if (rand_type == TYPE_0)
|
||||
state[-1] = rand_type;
|
||||
else
|
||||
state[-1] = MAX_TYPES * (rptr - state) + rand_type;
|
||||
if (n < BREAK_0) {
|
||||
UNLOCKME();
|
||||
return (NULL);
|
||||
} else if (n < BREAK_1) {
|
||||
rand_type = TYPE_0;
|
||||
rand_deg = DEG_0;
|
||||
rand_sep = SEP_0;
|
||||
} else if (n < BREAK_2) {
|
||||
rand_type = TYPE_1;
|
||||
rand_deg = DEG_1;
|
||||
rand_sep = SEP_1;
|
||||
} else if (n < BREAK_3) {
|
||||
rand_type = TYPE_2;
|
||||
rand_deg = DEG_2;
|
||||
rand_sep = SEP_2;
|
||||
} else if (n < BREAK_4) {
|
||||
rand_type = TYPE_3;
|
||||
rand_deg = DEG_3;
|
||||
rand_sep = SEP_3;
|
||||
} else {
|
||||
rand_type = TYPE_4;
|
||||
rand_deg = DEG_4;
|
||||
rand_sep = SEP_4;
|
||||
}
|
||||
state = (long *) (long_arg_state + 1); /* first location */
|
||||
end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */
|
||||
srandom_unlocked(seed);
|
||||
if (rand_type == TYPE_0)
|
||||
long_arg_state[0] = rand_type;
|
||||
else
|
||||
long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
|
||||
UNLOCKME();
|
||||
return((char *)ostate);
|
||||
LOCKME();
|
||||
if (rand_type == TYPE_0)
|
||||
state[-1] = rand_type;
|
||||
else
|
||||
state[-1] = MAX_TYPES * (rptr - state) + rand_type;
|
||||
if (n < BREAK_0) {
|
||||
UNLOCKME();
|
||||
return (NULL);
|
||||
} else if (n < BREAK_1) {
|
||||
rand_type = TYPE_0;
|
||||
rand_deg = DEG_0;
|
||||
rand_sep = SEP_0;
|
||||
} else if (n < BREAK_2) {
|
||||
rand_type = TYPE_1;
|
||||
rand_deg = DEG_1;
|
||||
rand_sep = SEP_1;
|
||||
} else if (n < BREAK_3) {
|
||||
rand_type = TYPE_2;
|
||||
rand_deg = DEG_2;
|
||||
rand_sep = SEP_2;
|
||||
} else if (n < BREAK_4) {
|
||||
rand_type = TYPE_3;
|
||||
rand_deg = DEG_3;
|
||||
rand_sep = SEP_3;
|
||||
} else {
|
||||
rand_type = TYPE_4;
|
||||
rand_deg = DEG_4;
|
||||
rand_sep = SEP_4;
|
||||
}
|
||||
state = (long *)(long_arg_state + 1); /* first location */
|
||||
end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */
|
||||
srandom_unlocked(seed);
|
||||
if (rand_type == TYPE_0)
|
||||
long_arg_state[0] = rand_type;
|
||||
else
|
||||
long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
|
||||
UNLOCKME();
|
||||
return ((char *)ostate);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -350,47 +362,46 @@ initstate(
|
||||
* word boundary; otherwise a bus error will occur. Even so, lint will
|
||||
* complain about mis-alignment, but you should disregard these messages.
|
||||
*/
|
||||
char *
|
||||
setstate(char *arg_state) /* pointer to state array */
|
||||
char *setstate(char *arg_state) /* pointer to state array */
|
||||
{
|
||||
long *new_state;
|
||||
int type;
|
||||
int rear;
|
||||
void *ostate = (void *)(&state[-1]);
|
||||
long *new_state;
|
||||
int type;
|
||||
int rear;
|
||||
void *ostate = (void *)(&state[-1]);
|
||||
|
||||
assert(arg_state != NULL);
|
||||
assert(arg_state != NULL);
|
||||
|
||||
new_state = (long *)(void *)arg_state;
|
||||
type = (int)(new_state[0] % MAX_TYPES);
|
||||
rear = (int)(new_state[0] / MAX_TYPES);
|
||||
new_state = (long *)(void *)arg_state;
|
||||
type = (int)(new_state[0] % MAX_TYPES);
|
||||
rear = (int)(new_state[0] / MAX_TYPES);
|
||||
|
||||
LOCKME();
|
||||
if (rand_type == TYPE_0)
|
||||
state[-1] = rand_type;
|
||||
else
|
||||
state[-1] = MAX_TYPES * (rptr - state) + rand_type;
|
||||
switch(type) {
|
||||
case TYPE_0:
|
||||
case TYPE_1:
|
||||
case TYPE_2:
|
||||
case TYPE_3:
|
||||
case TYPE_4:
|
||||
rand_type = type;
|
||||
rand_deg = degrees[type];
|
||||
rand_sep = seps[type];
|
||||
break;
|
||||
default:
|
||||
UNLOCKME();
|
||||
return (NULL);
|
||||
}
|
||||
state = (long *) (new_state + 1);
|
||||
if (rand_type != TYPE_0) {
|
||||
rptr = &state[rear];
|
||||
fptr = &state[(rear + rand_sep) % rand_deg];
|
||||
}
|
||||
end_ptr = &state[rand_deg]; /* set end_ptr too */
|
||||
UNLOCKME();
|
||||
return((char *)ostate);
|
||||
LOCKME();
|
||||
if (rand_type == TYPE_0)
|
||||
state[-1] = rand_type;
|
||||
else
|
||||
state[-1] = MAX_TYPES * (rptr - state) + rand_type;
|
||||
switch (type) {
|
||||
case TYPE_0:
|
||||
case TYPE_1:
|
||||
case TYPE_2:
|
||||
case TYPE_3:
|
||||
case TYPE_4:
|
||||
rand_type = type;
|
||||
rand_deg = degrees[type];
|
||||
rand_sep = seps[type];
|
||||
break;
|
||||
default:
|
||||
UNLOCKME();
|
||||
return (NULL);
|
||||
}
|
||||
state = (long *)(new_state + 1);
|
||||
if (rand_type != TYPE_0) {
|
||||
rptr = &state[rear];
|
||||
fptr = &state[(rear + rand_sep) % rand_deg];
|
||||
}
|
||||
end_ptr = &state[rand_deg]; /* set end_ptr too */
|
||||
UNLOCKME();
|
||||
return ((char *)ostate);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -410,44 +421,40 @@ setstate(char *arg_state) /* pointer to state array */
|
||||
*
|
||||
* Returns a 31-bit random number.
|
||||
*/
|
||||
static
|
||||
long
|
||||
random_unlocked(void)
|
||||
{
|
||||
long i;
|
||||
long *f, *r;
|
||||
static long random_unlocked(void) {
|
||||
long i;
|
||||
long *f, *r;
|
||||
|
||||
if (rand_type == TYPE_0) {
|
||||
i = state[0];
|
||||
state[0] = i = (i * 1103515245L + 12345L) & 0x7fffffff;
|
||||
} else {
|
||||
/*
|
||||
* Use local variables rather than static variables for speed.
|
||||
*/
|
||||
f = fptr; r = rptr;
|
||||
*f += *r;
|
||||
/* chucking least random bit */
|
||||
i = ((unsigned long)*f >> 1) & 0x7fffffff;
|
||||
if (++f >= end_ptr) {
|
||||
f = state;
|
||||
++r;
|
||||
}
|
||||
else if (++r >= end_ptr) {
|
||||
r = state;
|
||||
}
|
||||
if (rand_type == TYPE_0) {
|
||||
i = state[0];
|
||||
state[0] = i = (i * 1103515245L + 12345L) & 0x7fffffff;
|
||||
} else {
|
||||
/*
|
||||
* Use local variables rather than static variables for speed.
|
||||
*/
|
||||
f = fptr;
|
||||
r = rptr;
|
||||
*f += *r;
|
||||
/* chucking least random bit */
|
||||
i = ((unsigned long)*f >> 1) & 0x7fffffff;
|
||||
if (++f >= end_ptr) {
|
||||
f = state;
|
||||
++r;
|
||||
} else if (++r >= end_ptr) {
|
||||
r = state;
|
||||
}
|
||||
|
||||
fptr = f; rptr = r;
|
||||
}
|
||||
return(i);
|
||||
fptr = f;
|
||||
rptr = r;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
long
|
||||
random(void)
|
||||
{
|
||||
long r;
|
||||
long random(void) {
|
||||
long r;
|
||||
|
||||
LOCKME();
|
||||
r = random_unlocked();
|
||||
UNLOCKME();
|
||||
return (r);
|
||||
LOCKME();
|
||||
r = random_unlocked();
|
||||
UNLOCKME();
|
||||
return (r);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user