1
/******************************************************************
2
Random numbers and hashing
4
(c) 1994, 1995 Innobase Oy
6
Created 5/30/1994 Heikki Tuuri
7
*******************************************************************/
9
#define UT_HASH_RANDOM_MASK 1463735687
10
#define UT_HASH_RANDOM_MASK2 1653893711
11
#define UT_RND1 151117737
12
#define UT_RND2 119785373
13
#define UT_RND3 85689495
14
#define UT_RND4 76595339
15
#define UT_SUM_RND2 98781234
16
#define UT_SUM_RND3 126792457
17
#define UT_SUM_RND4 63498502
18
#define UT_XOR_RND1 187678878
19
#define UT_XOR_RND2 143537923
21
extern ulint ut_rnd_ulint_counter;
23
/************************************************************
24
This is used to set the random number seed. */
29
ulint seed) /* in: seed */
31
ut_rnd_ulint_counter = seed;
34
/************************************************************
35
The following function generates a series of 'random' ulint integers. */
38
ut_rnd_gen_next_ulint(
39
/*==================*/
40
/* out: the next 'random' number */
41
ulint rnd) /* in: the previous random number value */
45
n_bits = 8 * sizeof(ulint);
47
rnd = UT_RND2 * rnd + UT_SUM_RND3;
48
rnd = UT_XOR_RND1 ^ rnd;
49
rnd = (rnd << 20) + (rnd >> (n_bits - 20));
50
rnd = UT_RND3 * rnd + UT_SUM_RND4;
51
rnd = UT_XOR_RND2 ^ rnd;
52
rnd = (rnd << 20) + (rnd >> (n_bits - 20));
53
rnd = UT_RND1 * rnd + UT_SUM_RND2;
58
/************************************************************
59
The following function generates 'random' ulint integers which
60
enumerate the value space of ulint integers in a pseudo random
61
fashion. Note that the same integer is repeated always after
62
2 to power 32 calls to the generator (if ulint is 32-bit). */
65
ut_rnd_gen_ulint(void)
66
/*==================*/
67
/* out: the 'random' number */
72
n_bits = 8 * sizeof(ulint);
74
ut_rnd_ulint_counter = UT_RND1 * ut_rnd_ulint_counter + UT_RND2;
76
rnd = ut_rnd_gen_next_ulint(ut_rnd_ulint_counter);
81
/************************************************************
82
Generates a random integer from a given interval. */
87
/* out: the 'random' number */
88
ulint low, /* in: low limit; can generate also this value */
89
ulint high) /* in: high limit; can generate also this value */
100
rnd = ut_rnd_gen_ulint();
102
return(low + (rnd % (high - low + 1)));
105
/*************************************************************
106
Generates a random iboolean value. */
109
ut_rnd_gen_ibool(void)
110
/*=================*/
111
/* out: the random value */
115
x = ut_rnd_gen_ulint();
117
if (((x >> 20) + (x >> 15)) & 1) {
125
/***********************************************************
126
The following function generates a hash value for a ulint integer
127
to a hash table of size table_size, which should be a prime
128
or some random number for the hash table to work reliably. */
133
/* out: hash value */
134
ulint key, /* in: value to be hashed */
135
ulint table_size) /* in: hash table size */
137
key = key ^ UT_HASH_RANDOM_MASK2;
139
return(key % table_size);
142
/*****************************************************************
143
Folds a pair of ulints. */
148
/* out: folded value */
149
ulint n1, /* in: ulint */
150
ulint n2) /* in: ulint */
152
return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
153
^ UT_HASH_RANDOM_MASK) + n2);
156
/*****************************************************************
162
/* out: folded value */
163
dulint d) /* in: dulint */
165
return(ut_fold_ulint_pair(ut_dulint_get_low(d),
166
ut_dulint_get_high(d)));
169
/*****************************************************************
170
Folds a character string ending in the null character. */
175
/* out: folded value */
176
const char* str) /* in: null-terminated string */
185
while (*str != '\0') {
192
fold = ut_fold_ulint_pair(fold, (ulint)(*str));
199
/*****************************************************************
200
Folds a binary string. */
205
/* out: folded value */
206
const byte* str, /* in: string of bytes */
207
ulint len) /* in: length */
209
const byte* str_end = str + len;
214
while (str < str_end) {
215
fold = ut_fold_ulint_pair(fold, (ulint)(*str));