1/*****************************************************************************
2
3Copyright (c) 1994, 2018, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24
25*****************************************************************************/
26
27/** @file include/ut0rnd.ic
28 Random numbers and hashing
29
30 Created 5/30/1994 Heikki Tuuri
31 *******************************************************************/
32
33#define UT_HASH_RANDOM_MASK 1463735687
34#define UT_HASH_RANDOM_MASK2 1653893711
35
36#define UT_RND1 151117737
37#define UT_RND2 119785373
38#define UT_RND3 85689495
39#define UT_RND4 76595339
40#define UT_SUM_RND2 98781234
41#define UT_SUM_RND3 126792457
42#define UT_SUM_RND4 63498502
43#define UT_XOR_RND1 187678878
44#define UT_XOR_RND2 143537923
45
46#include <my_thread_local.h>
47
48/** Seed value of ut_rnd_gen_ulint() */
49extern thread_local ulint ut_rnd_ulint_counter;
50
51/** The following function generates a series of 'random' ulint integers.
52 This function is now based on thread local variables.
53 @return the next 'random' number */
54UNIV_INLINE
55ulint ut_rnd_gen_next_ulint(
56    ulint rnd) /*!< in: the previous random number value */
57{
58  ulint n_bits;
59
60  n_bits = 8 * sizeof(ulint);
61
62  rnd = UT_RND2 * rnd + UT_SUM_RND3;
63  rnd = UT_XOR_RND1 ^ rnd;
64  rnd = (rnd << 20) + (rnd >> (n_bits - 20));
65  rnd = UT_RND3 * rnd + UT_SUM_RND4;
66  rnd = UT_XOR_RND2 ^ rnd;
67  rnd = (rnd << 20) + (rnd >> (n_bits - 20));
68  rnd = UT_RND1 * rnd + UT_SUM_RND2;
69
70  return (rnd);
71}
72
73/** The following function generates 'random' ulint integers which
74enumerate the value space of ulint integers in a pseudo random
75fashion. Note that the same integer is repeated always after
762 to power 32 calls to the generator (if ulint is 32-bit).
77@return pseudo random number */
78ulint ut_rnd_gen_ulint() {
79  ulint rnd = ut_rnd_ulint_counter;
80  if (rnd == 0) {
81    rnd = 65654363;
82  }
83
84  rnd = UT_RND1 * rnd + UT_RND2;
85
86  ut_rnd_ulint_counter = rnd;
87
88  return (ut_rnd_gen_next_ulint(rnd));
89}
90
91/** Generates a random integer from a given interval.
92 This function is now based on thread local variables.
93 @return the 'random' number */
94UNIV_INLINE
95ulint ut_rnd_interval(
96    ulint low,  /*!< in: low limit; can generate also this value */
97    ulint high) /*!< in: high limit; can generate also this value */
98{
99  ulint rnd;
100
101  ut_ad(high >= low);
102
103  if (low == high) {
104    return (low);
105  }
106
107  rnd = ut_rnd_gen_ulint();
108
109  return (low + (rnd % (high - low)));
110}
111
112/** The following function generates a hash value for a ulint integer
113 to a hash table of size table_size, which should be a prime
114 or some random number for the hash table to work reliably.
115 @return hash value */
116UNIV_INLINE
117ulint ut_hash_ulint(ulint key,        /*!< in: value to be hashed */
118                    ulint table_size) /*!< in: hash table size */
119{
120  ut_ad(table_size);
121  key = key ^ UT_HASH_RANDOM_MASK2;
122
123  return (key % table_size);
124}
125
126/** Folds a 64-bit integer.
127 @return folded value */
128UNIV_INLINE
129ulint ut_fold_ull(ib_uint64_t d) /*!< in: 64-bit integer */
130{
131  return (ut_fold_ulint_pair((ulint)d & ULINT32_MASK, (ulint)(d >> 32)));
132}
133
134/** Folds a character string ending in the null character.
135 @return folded value */
136UNIV_INLINE
137ulint ut_fold_string(const char *str) /*!< in: null-terminated string */
138{
139  ulint fold = 0;
140
141  ut_ad(str);
142
143  while (*str != '\0') {
144    fold = ut_fold_ulint_pair(fold, (ulint)(*str));
145    str++;
146  }
147
148  return (fold);
149}
150
151/** Folds a pair of ulints.
152 @return folded value */
153UNIV_INLINE
154ulint ut_fold_ulint_pair(ulint n1, /*!< in: ulint */
155                         ulint n2) /*!< in: ulint */
156{
157  return (
158      ((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1) ^ UT_HASH_RANDOM_MASK) +
159      n2);
160}
161
162/** Folds a binary string.
163 @return folded value */
164UNIV_INLINE
165ulint ut_fold_binary(const byte *str, /*!< in: string of bytes */
166                     ulint len)       /*!< in: length */
167{
168  ulint fold = 0;
169  const byte *str_end = str + (len & 0xFFFFFFF8);
170
171  ut_ad(str || !len);
172
173  while (str < str_end) {
174    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
175    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
176    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
177    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
178    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
179    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
180    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
181    fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
182  }
183
184  switch (len & 0x7) {
185    case 7:
186      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
187      // Fall through.
188    case 6:
189      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
190      // Fall through.
191    case 5:
192      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
193      // Fall through.
194    case 4:
195      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
196      // Fall through.
197    case 3:
198      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
199      // Fall through.
200    case 2:
201      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
202      // Fall through.
203    case 1:
204      fold = ut_fold_ulint_pair(fold, (ulint)(*str++));
205  }
206
207  return (fold);
208}
209