Commit d146e3dc authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-21256: Simplify ut_rnd_interval()

ut_rnd_interval(): Remove the first parameter, which was mostly
passed as 0. Implement as a simple wrapper around ut_rnd_gen().
Trivially return 0 if the size of the interval is smaller than 2.

ut_rnd_ulint_counter, ut_rnd_gen_next_ulint(), ut_rnd_gen_ulint(): Remove.
parent 51fc8ab7
...@@ -1747,7 +1747,7 @@ dict_stats_analyze_index_for_n_prefix( ...@@ -1747,7 +1747,7 @@ dict_stats_analyze_index_for_n_prefix(
ut_rnd_interval() to work with too big numbers since ut_rnd_interval() to work with too big numbers since
ib_uint64_t could be bigger than ulint */ ib_uint64_t could be bigger than ulint */
const ulint rnd = ut_rnd_interval( const ulint rnd = ut_rnd_interval(
0, static_cast<ulint>(right - left)); static_cast<ulint>(right - left));
const ib_uint64_t dive_below_idx const ib_uint64_t dive_below_idx
= boundaries->at(static_cast<unsigned>(left + rnd)); = boundaries->at(static_cast<unsigned>(left + rnd));
......
...@@ -225,7 +225,7 @@ struct TTASFutexMutex { ...@@ -225,7 +225,7 @@ struct TTASFutexMutex {
return; return;
} }
ut_delay(ut_rnd_interval(0, max_delay)); ut_delay(ut_rnd_interval(max_delay));
} }
for (n_waits= 0;; n_waits++) { for (n_waits= 0;; n_waits++) {
...@@ -362,7 +362,7 @@ struct TTASMutex { ...@@ -362,7 +362,7 @@ struct TTASMutex {
uint32_t n_spins = 0; uint32_t n_spins = 0;
while (!try_lock()) { while (!try_lock()) {
ut_delay(ut_rnd_interval(0, max_delay)); ut_delay(ut_rnd_interval(max_delay));
if (++n_spins == max_spins) { if (++n_spins == max_spins) {
os_thread_yield(); os_thread_yield();
max_spins+= step; max_spins+= step;
...@@ -516,7 +516,7 @@ struct TTASEventMutex { ...@@ -516,7 +516,7 @@ struct TTASEventMutex {
sync_array_wait_event(sync_arr, cell); sync_array_wait_event(sync_arr, cell);
} }
} else { } else {
ut_delay(ut_rnd_interval(0, max_delay)); ut_delay(ut_rnd_interval(max_delay));
} }
} }
......
...@@ -56,15 +56,11 @@ inline uint64_t ut_rnd_gen() ...@@ -56,15 +56,11 @@ inline uint64_t ut_rnd_gen()
return ut_rnd_current; return ut_rnd_current;
} }
/********************************************************//** /** @return a random number between 0 and n-1, inclusive */
Generates a random integer from a given interval. inline ulint ut_rnd_interval(ulint n)
@return the 'random' number */ {
UNIV_INLINE return n > 1 ? static_cast<ulint>(ut_rnd_gen() % n) : 0;
ulint }
ut_rnd_interval(
/*============*/
ulint low, /*!< in: low limit; can generate also this value */
ulint high); /*!< in: high limit; can generate also this value */
/*******************************************************//** /*******************************************************//**
The following function generates a hash value for a ulint integer The following function generates a hash value for a ulint integer
......
...@@ -29,87 +29,6 @@ Created 5/30/1994 Heikki Tuuri ...@@ -29,87 +29,6 @@ Created 5/30/1994 Heikki Tuuri
#ifndef UNIV_INNOCHECKSUM #ifndef UNIV_INNOCHECKSUM
#define UT_RND1 151117737
#define UT_RND2 119785373
#define UT_RND3 85689495
#define UT_RND4 76595339
#define UT_SUM_RND2 98781234
#define UT_SUM_RND3 126792457
#define UT_SUM_RND4 63498502
#define UT_XOR_RND1 187678878
#define UT_XOR_RND2 143537923
/** Seed value of ut_rnd_gen_ulint() */
extern ulint ut_rnd_ulint_counter;
/********************************************************//**
The following function generates a series of 'random' ulint integers.
@return the next 'random' number */
UNIV_INLINE
ulint
ut_rnd_gen_next_ulint(
/*==================*/
ulint rnd) /*!< in: the previous random number value */
{
ulint n_bits;
n_bits = 8 * sizeof(ulint);
rnd = UT_RND2 * rnd + UT_SUM_RND3;
rnd = UT_XOR_RND1 ^ rnd;
rnd = (rnd << 20) + (rnd >> (n_bits - 20));
rnd = UT_RND3 * rnd + UT_SUM_RND4;
rnd = UT_XOR_RND2 ^ rnd;
rnd = (rnd << 20) + (rnd >> (n_bits - 20));
rnd = UT_RND1 * rnd + UT_SUM_RND2;
return(rnd);
}
/********************************************************//**
The following function generates 'random' ulint integers which
enumerate the value space of ulint integers in a pseudo random
fashion. Note that the same integer is repeated always after
2 to power 32 calls to the generator (if ulint is 32-bit).
@return the 'random' number */
UNIV_INLINE
ulint
ut_rnd_gen_ulint(void)
/*==================*/
{
ulint rnd;
ut_rnd_ulint_counter = UT_RND1 * ut_rnd_ulint_counter + UT_RND2;
rnd = ut_rnd_gen_next_ulint(ut_rnd_ulint_counter);
return(rnd);
}
/********************************************************//**
Generates a random integer from a given interval.
@return the 'random' number */
UNIV_INLINE
ulint
ut_rnd_interval(
/*============*/
ulint low, /*!< in: low limit; can generate also this value */
ulint high) /*!< in: high limit; can generate also this value */
{
ulint rnd;
ut_ad(high >= low);
if (low == high) {
return(low);
}
rnd = ut_rnd_gen_ulint();
return(low + (rnd % (high - low)));
}
/*******************************************************//** /*******************************************************//**
The following function generates a hash value for a ulint integer The following function generates a hash value for a ulint integer
to a hash table of size table_size, which should be a prime to a hash table of size table_size, which should be a prime
......
...@@ -792,8 +792,7 @@ page_cur_open_on_rnd_user_rec( ...@@ -792,8 +792,7 @@ page_cur_open_on_rnd_user_rec(
} }
cursor->rec = page_rec_get_nth(block->frame, cursor->rec = page_rec_get_nth(block->frame,
static_cast<ulint> ut_rnd_interval(n_recs) + 1);
(ut_rnd_gen() % n_recs) + 1);
} }
/** Write a redo log record of inserting a record into an index page. /** Write a redo log record of inserting a record into an index page.
......
...@@ -299,7 +299,7 @@ rw_lock_s_lock_spin( ...@@ -299,7 +299,7 @@ rw_lock_s_lock_spin(
HMT_low(); HMT_low();
while (i < srv_n_spin_wait_rounds && lock->lock_word <= 0) { while (i < srv_n_spin_wait_rounds && lock->lock_word <= 0) {
if (srv_spin_wait_delay) { if (srv_spin_wait_delay) {
ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); ut_delay(ut_rnd_interval(srv_spin_wait_delay));
} }
i++; i++;
...@@ -425,7 +425,7 @@ rw_lock_x_lock_wait_func( ...@@ -425,7 +425,7 @@ rw_lock_x_lock_wait_func(
HMT_low(); HMT_low();
if (srv_spin_wait_delay) { if (srv_spin_wait_delay) {
ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); ut_delay(ut_rnd_interval(srv_spin_wait_delay));
} }
if (i < srv_n_spin_wait_rounds) { if (i < srv_n_spin_wait_rounds) {
...@@ -684,8 +684,7 @@ rw_lock_x_lock_func( ...@@ -684,8 +684,7 @@ rw_lock_x_lock_func(
&& lock->lock_word <= X_LOCK_HALF_DECR) { && lock->lock_word <= X_LOCK_HALF_DECR) {
if (srv_spin_wait_delay) { if (srv_spin_wait_delay) {
ut_delay(ut_rnd_interval( ut_delay(ut_rnd_interval(srv_spin_wait_delay));
0, srv_spin_wait_delay));
} }
i++; i++;
...@@ -790,8 +789,7 @@ rw_lock_sx_lock_func( ...@@ -790,8 +789,7 @@ rw_lock_sx_lock_func(
&& lock->lock_word <= X_LOCK_HALF_DECR) { && lock->lock_word <= X_LOCK_HALF_DECR) {
if (srv_spin_wait_delay) { if (srv_spin_wait_delay) {
ut_delay(ut_rnd_interval( ut_delay(ut_rnd_interval(srv_spin_wait_delay));
0, srv_spin_wait_delay));
} }
i++; i++;
......
...@@ -36,9 +36,6 @@ uint64_t ut_rnd_current; ...@@ -36,9 +36,6 @@ uint64_t ut_rnd_current;
#define UT_RANDOM_3 1.0132677 #define UT_RANDOM_3 1.0132677
/*@}*/ /*@}*/
/** Seed value of ut_rnd_gen_ulint(). */
ulint ut_rnd_ulint_counter = 65654363;
/***********************************************************//** /***********************************************************//**
Looks for a prime number slightly greater than the given argument. Looks for a prime number slightly greater than the given argument.
The prime is chosen so that it is not near any power of 2. The prime is chosen so that it is not near any power of 2.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment