1
/*****************************************************************************
3
Copyright (C) 1994, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************************//**
20
@file include/ut0rnd.ic
21
Random numbers and hashing
23
Created 5/30/1994 Heikki Tuuri
24
*******************************************************************/
26
#define UT_HASH_RANDOM_MASK 1463735687
27
#define UT_HASH_RANDOM_MASK2 1653893711
28
#define UT_RND1 151117737
29
#define UT_RND2 119785373
30
#define UT_RND3 85689495
31
#define UT_RND4 76595339
32
#define UT_SUM_RND2 98781234
33
#define UT_SUM_RND3 126792457
34
#define UT_SUM_RND4 63498502
35
#define UT_XOR_RND1 187678878
36
#define UT_XOR_RND2 143537923
38
/** Seed value of ut_rnd_gen_ulint() */
39
extern ulint ut_rnd_ulint_counter;
41
/********************************************************//**
42
This is used to set the random number seed. */
47
ulint seed) /*!< in: seed */
49
ut_rnd_ulint_counter = seed;
52
/********************************************************//**
53
The following function generates a series of 'random' ulint integers.
54
@return the next 'random' number */
57
ut_rnd_gen_next_ulint(
58
/*==================*/
59
ulint rnd) /*!< in: the previous random number value */
63
n_bits = 8 * sizeof(ulint);
65
rnd = UT_RND2 * rnd + UT_SUM_RND3;
66
rnd = UT_XOR_RND1 ^ rnd;
67
rnd = (rnd << 20) + (rnd >> (n_bits - 20));
68
rnd = UT_RND3 * rnd + UT_SUM_RND4;
69
rnd = UT_XOR_RND2 ^ rnd;
70
rnd = (rnd << 20) + (rnd >> (n_bits - 20));
71
rnd = UT_RND1 * rnd + UT_SUM_RND2;
76
/********************************************************//**
77
The following function generates 'random' ulint integers which
78
enumerate the value space of ulint integers in a pseudo random
79
fashion. Note that the same integer is repeated always after
80
2 to power 32 calls to the generator (if ulint is 32-bit).
81
@return the 'random' number */
84
ut_rnd_gen_ulint(void)
85
/*==================*/
89
ut_rnd_ulint_counter = UT_RND1 * ut_rnd_ulint_counter + UT_RND2;
91
rnd = ut_rnd_gen_next_ulint(ut_rnd_ulint_counter);
96
/********************************************************//**
97
Generates a random integer from a given interval.
98
@return the 'random' number */
103
ulint low, /*!< in: low limit; can generate also this value */
104
ulint high) /*!< in: high limit; can generate also this value */
115
rnd = ut_rnd_gen_ulint();
117
return(low + (rnd % (high - low + 1)));
120
/*********************************************************//**
121
Generates a random iboolean value.
122
@return the random value */
125
ut_rnd_gen_ibool(void)
126
/*=================*/
130
x = ut_rnd_gen_ulint();
132
if (((x >> 20) + (x >> 15)) & 1) {
140
/*******************************************************//**
141
The following function generates a hash value for a ulint integer
142
to a hash table of size table_size, which should be a prime
143
or some random number for the hash table to work reliably.
144
@return hash value */
149
ulint key, /*!< in: value to be hashed */
150
ulint table_size) /*!< in: hash table size */
153
key = key ^ UT_HASH_RANDOM_MASK2;
155
return(key % table_size);
158
/*************************************************************//**
159
Folds a pair of ulints.
160
@return folded value */
165
ulint n1, /*!< in: ulint */
166
ulint n2) /*!< in: ulint */
168
return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
169
^ UT_HASH_RANDOM_MASK) + n2);
172
/*************************************************************//**
173
Folds a 64-bit integer.
174
@return folded value */
179
ib_uint64_t d) /*!< in: 64-bit integer */
181
return(ut_fold_ulint_pair((ulint) d & ULINT32_MASK,
185
/*************************************************************//**
186
Folds a character string ending in the null character.
187
@return folded value */
192
const char* str) /*!< in: null-terminated string */
198
while (*str != '\0') {
199
fold = ut_fold_ulint_pair(fold, (ulint)(*str));
206
/*************************************************************//**
207
Folds a binary string.
208
@return folded value */
213
const byte* str, /*!< in: string of bytes */
214
ulint len) /*!< in: length */
216
const byte* str_end = str + len;
221
while (str < str_end) {
222
fold = ut_fold_ulint_pair(fold, (ulint)(*str));