1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2020, Oracle and/or its affiliates. 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, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 
27 /** @file include/ut0rnd.h
28  Random numbers and hashing
29 
30  Created 1/20/1994 Heikki Tuuri
31  ***********************************************************************/
32 
33 #ifndef ut0rnd_h
34 #define ut0rnd_h
35 
36 #include "univ.i"
37 #include "ut0byte.h"
38 
39 /** The 'character code' for end of field or string (used
40 in folding records */
41 #define UT_END_OF_FIELD 257
42 
43 /** The following function generates a series of 'random' ulint integers.
44  @return the next 'random' number */
45 UNIV_INLINE
46 ulint ut_rnd_gen_next_ulint(
47     ulint rnd); /*!< in: the previous random number value */
48 /** The following function generates 'random' ulint integers which
49  enumerate the value space (let there be N of them) of ulint integers
50  in a pseudo-random fashion. Note that the same integer is repeated
51  always after N calls to the generator.
52  @return the 'random' number */
53 UNIV_INLINE
54 ulint ut_rnd_gen_ulint(void);
55 
56 /** Generates a random integer from a given interval.
57 @param[in]	low	low limit; can generate also this value
58 @param[in]	high	high limit; can generate also this value
59 @return the 'random' number */
60 UNIV_INLINE
61 ulint ut_rnd_interval(ulint low, ulint high);
62 
63 /** The following function generates a hash value for a ulint integer to a
64 hash table of size table_size, which should be a prime or some random number
65 to work reliably.
66 @param[in]	key		value to be hashed
67 @param[in]	table_size	hash table size
68 @return hash value */
69 UNIV_INLINE
70 ulint ut_hash_ulint(ulint key, ulint table_size);
71 
72 /** Folds a 64-bit integer.
73 @param[in]	d	64-bit integer
74 @return folded value */
75 UNIV_INLINE
76 ulint ut_fold_ull(ib_uint64_t d);
77 
78 /** Folds a character string ending in the null character.
79  @return folded value */
80 UNIV_INLINE
81 ulint ut_fold_string(const char *str) /*!< in: null-terminated string */
82     MY_ATTRIBUTE((warn_unused_result));
83 
84 /** Looks for a prime number slightly greater than the given argument.
85 The prime is chosen so that it is not near any power of 2.
86 @param[in]	n  positive number > 100
87 @return prime */
88 ulint ut_find_prime(ulint n);
89 
90 /** Folds a pair of ulints.
91 @param[in]	n1	first ulint
92 @param[in]	n2	second ulint
93 @return folded value */
94 UNIV_INLINE
95 ulint ut_fold_ulint_pair(ulint n1, ulint n2);
96 
97 /** Folds a binary string.
98 @param[in]	str		string of bytes
99 @param[in]	len		length
100 @return folded value */
101 UNIV_INLINE
102 ulint ut_fold_binary(const byte *str, ulint len);
103 
104 #include "ut0rnd.ic"
105 
106 #endif
107