~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/include/ut0rnd.ic

  • Committer: lbieber
  • Date: 2010-10-02 19:48:35 UTC
  • mfrom: (1730.6.19 drizzle-make-lcov)
  • Revision ID: lbieber@orisndriz08-20101002194835-q5zd9qc4lvx1xnfo
Merge Hartmut - clean up lex, now require flex to build, also "make lcov" improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 1994, 2009, Innobase Oy. All Rights Reserved.
4
 
 
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.
8
 
 
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.
12
 
 
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
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************************//**
20
 
@file include/ut0rnd.ic
21
 
Random numbers and hashing
22
 
 
23
 
Created 5/30/1994 Heikki Tuuri
24
 
*******************************************************************/
25
 
 
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
37
 
 
38
 
/** Seed value of ut_rnd_gen_ulint() */
39
 
extern  ulint    ut_rnd_ulint_counter;
40
 
 
41
 
/********************************************************//**
42
 
This is used to set the random number seed. */
43
 
UNIV_INLINE
44
 
void
45
 
ut_rnd_set_seed(
46
 
/*============*/
47
 
        ulint    seed)           /*!< in: seed */
48
 
{
49
 
        ut_rnd_ulint_counter = seed;
50
 
}
51
 
 
52
 
/********************************************************//**
53
 
The following function generates a series of 'random' ulint integers.
54
 
@return the next 'random' number */
55
 
UNIV_INLINE
56
 
ulint
57
 
ut_rnd_gen_next_ulint(
58
 
/*==================*/
59
 
        ulint   rnd)    /*!< in: the previous random number value */
60
 
{
61
 
        ulint   n_bits;
62
 
 
63
 
        n_bits = 8 * sizeof(ulint);
64
 
 
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;
72
 
 
73
 
        return(rnd);
74
 
}
75
 
 
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 */
82
 
UNIV_INLINE
83
 
ulint
84
 
ut_rnd_gen_ulint(void)
85
 
/*==================*/
86
 
{
87
 
        ulint   rnd;
88
 
 
89
 
        ut_rnd_ulint_counter = UT_RND1 * ut_rnd_ulint_counter + UT_RND2;
90
 
 
91
 
        rnd = ut_rnd_gen_next_ulint(ut_rnd_ulint_counter);
92
 
 
93
 
        return(rnd);
94
 
}
95
 
 
96
 
/********************************************************//**
97
 
Generates a random integer from a given interval.
98
 
@return the 'random' number */
99
 
UNIV_INLINE
100
 
ulint
101
 
ut_rnd_interval(
102
 
/*============*/
103
 
        ulint   low,    /*!< in: low limit; can generate also this value */
104
 
        ulint   high)   /*!< in: high limit; can generate also this value */
105
 
{
106
 
        ulint   rnd;
107
 
 
108
 
        ut_ad(high >= low);
109
 
 
110
 
        if (low == high) {
111
 
 
112
 
                return(low);
113
 
        }
114
 
 
115
 
        rnd = ut_rnd_gen_ulint();
116
 
 
117
 
        return(low + (rnd % (high - low + 1)));
118
 
}
119
 
 
120
 
/*********************************************************//**
121
 
Generates a random iboolean value.
122
 
@return the random value */
123
 
UNIV_INLINE
124
 
ibool
125
 
ut_rnd_gen_ibool(void)
126
 
/*=================*/
127
 
{
128
 
        ulint    x;
129
 
 
130
 
        x = ut_rnd_gen_ulint();
131
 
 
132
 
        if (((x >> 20) + (x >> 15)) & 1) {
133
 
 
134
 
                return(TRUE);
135
 
        }
136
 
 
137
 
        return(FALSE);
138
 
}
139
 
 
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 */
145
 
UNIV_INLINE
146
 
ulint
147
 
ut_hash_ulint(
148
 
/*==========*/
149
 
        ulint    key,           /*!< in: value to be hashed */
150
 
        ulint    table_size)    /*!< in: hash table size */
151
 
{
152
 
        ut_ad(table_size);
153
 
        key = key ^ UT_HASH_RANDOM_MASK2;
154
 
 
155
 
        return(key % table_size);
156
 
}
157
 
 
158
 
/*************************************************************//**
159
 
Folds a pair of ulints.
160
 
@return folded value */
161
 
UNIV_INLINE
162
 
ulint
163
 
ut_fold_ulint_pair(
164
 
/*===============*/
165
 
        ulint   n1,     /*!< in: ulint */
166
 
        ulint   n2)     /*!< in: ulint */
167
 
{
168
 
        return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
169
 
                ^ UT_HASH_RANDOM_MASK) + n2);
170
 
}
171
 
 
172
 
/*************************************************************//**
173
 
Folds a 64-bit integer.
174
 
@return folded value */
175
 
UNIV_INLINE
176
 
ulint
177
 
ut_fold_ull(
178
 
/*========*/
179
 
        ib_uint64_t     d)      /*!< in: 64-bit integer */
180
 
{
181
 
        return(ut_fold_ulint_pair((ulint) d & ULINT32_MASK,
182
 
                                  (ulint) (d >> 32)));
183
 
}
184
 
 
185
 
/*************************************************************//**
186
 
Folds a character string ending in the null character.
187
 
@return folded value */
188
 
UNIV_INLINE
189
 
ulint
190
 
ut_fold_string(
191
 
/*===========*/
192
 
        const char*     str)    /*!< in: null-terminated string */
193
 
{
194
 
        ulint   fold = 0;
195
 
 
196
 
        ut_ad(str);
197
 
 
198
 
        while (*str != '\0') {
199
 
                fold = ut_fold_ulint_pair(fold, (ulint)(*str));
200
 
                str++;
201
 
        }
202
 
 
203
 
        return(fold);
204
 
}
205
 
 
206
 
/*************************************************************//**
207
 
Folds a binary string.
208
 
@return folded value */
209
 
UNIV_INLINE
210
 
ulint
211
 
ut_fold_binary(
212
 
/*===========*/
213
 
        const byte*     str,    /*!< in: string of bytes */
214
 
        ulint           len)    /*!< in: length */
215
 
{
216
 
        const byte*     str_end = str + len;
217
 
        ulint           fold = 0;
218
 
 
219
 
        ut_ad(str || !len);
220
 
 
221
 
        while (str < str_end) {
222
 
                fold = ut_fold_ulint_pair(fold, (ulint)(*str));
223
 
 
224
 
                str++;
225
 
        }
226
 
 
227
 
        return(fold);
228
 
}