Exemple #1
0
/*
 * Generate and munge a 64bit number.
 */
u64 rand64(void)
{
	unsigned long r = 0;

	if (rand_bool()) {
		/* 32-bit ranges. */
		r = rand32();

	} else {
		/* 33:64-bit ranges. */
		switch (rand() % 7) {
		case 0:	r = rand_single_bit(64);
			break;
		case 1:	r = randbits(64);
			break;
		case 2:	r = rand32() | rand32() << 31;
			break;
		case 3:	r = taviso();
			break;
		case 4:	r = rand8x8();
			break;
		case 5:	r = rept8(8);
			break;
		/* Sometimes pick a not-so-random number. */
		case 6:	return get_interesting_value();
		}

		/* limit the size */
		switch (rand() % 4) {
		case 0: r &= 0x000000ffffffffffULL;
			break;
		case 1: r &= 0x0000ffffffffffffULL;
			break;
		case 2: r &= 0x00ffffffffffffffULL;
			break;
		}
	}

	/* Sometimes invert the generated number. */
	if (rand_bool())
		r = ~r;

	/* increase distribution in MSB */
	if ((rand() % 10)) {
		unsigned int i;
		unsigned int rounds;

		rounds = rand() % 4;
		for (i = 0; i < rounds; i++)
			r |= (1UL << ((__WORDSIZE - 1) - (rand() % 8)));
	}

	/* randomly flip sign bit. */
	if (rand_bool())
		r |= (1UL << (__WORDSIZE - 1));

	return r;
}
Exemple #2
0
/*
 * Generate and munge a 64bit number.
 */
u64 rand64(void)
{
	unsigned long r = 0;

	if (RAND_BOOL()) {
		/* 32-bit ranges. */
		r = rand32();

	} else {
		/* 33:64-bit ranges. */
		switch (rand() % 5) {
		case 0:	r = rand_single_bit(64);
			break;
		case 1:	r = randbits(64);
			break;
		case 2:	r = RAND_64();
			break;
		case 3:	r = rept_byte();
			break;
		/* Sometimes pick a not-so-random number. */
		case 4:	return get_interesting_value();
		}

		/* limit the size */
		switch (rand() % 4) {
		case 0: r &= 0x000000ffffffffffULL;
			break;
		case 1: r &= 0x0000ffffffffffffULL;
			break;
		case 2: r &= 0x00ffffffffffffffULL;
			break;
		default: /* no limiting. */
			break;
		}
	}

	/* Sometimes invert the generated number. */
	if (ONE_IN(25))
		r = ~r;

	/* increase distribution in MSB */
	if (ONE_IN(10)) {
		unsigned int i;
		unsigned int rounds;

		rounds = rand() % 4;
		for (i = 0; i < rounds; i++)
			r |= (1UL << ((__WORDSIZE - 1) - (rand() % 8)));
	}

	/* Sometimes flip sign */
	if (ONE_IN(25))
		r = ~r + 1;

	return r;
}
Exemple #3
0
/*
 * Generate, and munge a 32bit number.
 */
unsigned int rand32(void)
{
	unsigned long r = 0;

	switch (rnd() % 7) {
	case 0:	r = RAND_BYTE();
		break;

	case 1:	r = rand16();
		break;

	case 2: r = rand_single_bit(32);
		break;
	case 3:	r = randbits(32);
		break;
	case 4: r = rnd();
		break;
	case 5:	r = rept_byte();
		break;

	case 6:	return get_interesting_value();
	}

	/* Sometimes deduct it from INT_MAX */
	if (ONE_IN(25))
		r = INT_MAX - r;

	/* Sometimes flip sign */
	if (ONE_IN(25))
		r = ~r + 1;

	/* we might get lucky if something is counting ints/longs etc. */
	if (ONE_IN(4)) {
		int _div = 1 << RAND_RANGE(1, 4);	/* 2,4,8 or 16 */
		r /= _div;
	}

	/* limit the size */
	switch (rnd() % 5) {
	case 0: r &= 0xff;
		break;
	case 1: r &= 0xffff;
		break;
	case 2: r &= PAGE_MASK;
		break;
	case 3: r &= 0xffffff;
		break;
	case 4:	// do nothing
		break;
	}

	return r;
}
Exemple #4
0
/*
 * Generate, and munge a 16bit number.
 */
unsigned short rand16(void)
{
	unsigned short r = 0, r2;

	switch (rnd() % 5) {
	case 0:	r = RAND_BYTE();
		break;

	case 1: r = rand_single_bit(16);
		break;
	case 2:	r = randbits(16);
		break;
	case 3: r = rnd();
		break;
	case 4:	r2 = rnd() & 0xff;
		r = r2 | r2 << 8;
		break;
	}

	/* Sometimes flip sign */
	if (ONE_IN(25))
		r = ~r + 1;

	if (ONE_IN(4)) {
		int _div = 1 << RAND_RANGE(1, 4);	/* 2,4,8 or 16 */
		r /= _div;
	}

	/* limit the size */
	switch (rnd() % 4) {
	case 0: r &= 0xff;
		break;
	case 1: r &= 0xfff;
		break;
	case 2: r &= PAGE_MASK;
		break;
	case 3:	// do nothing
		break;
	}
	return r;
}
Exemple #5
0
/*
 * "selector" function for 32bit random.
 * only called from rand32()
 */
static unsigned int __rand32(void)
{
	unsigned long r = 0;

	switch (rand() % 7) {
	case 0: r = rand_single_bit(32);
		break;
	case 1:	r = randbits(32);
		break;
	case 2: r = rand();
		break;
	case 3:	r = taviso();
		break;
	case 4:	r = rand8x8();
		break;
	case 5:	r = rept8(4);
		break;
	case 6:	return get_interesting_32bit_value();
	}

	return r;
}