1 /*****************************************************************************
2
3 Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2019, 2020, MariaDB Corporation.
5
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17
18 *****************************************************************************/
19
20 /******************************************************************//**
21 @file include/ut0rnd.h
22 Random numbers and hashing
23
24 Created 1/20/1994 Heikki Tuuri
25 ***********************************************************************/
26
27 #ifndef ut0rnd_h
28 #define ut0rnd_h
29
30 #include "ut0byte.h"
31 #include <my_sys.h>
32
33 #ifndef UNIV_INNOCHECKSUM
34 /** Seed value of ut_rnd_gen() */
35 extern std::atomic<uint32_t> ut_rnd_current;
36
37 /** @return a pseudo-random 32-bit number */
ut_rnd_gen()38 inline uint32_t ut_rnd_gen()
39 {
40 /* This is a Galois linear-feedback shift register.
41 https://en.wikipedia.org/wiki/Linear-feedback_shift_register#Galois_LFSRs
42 The generating primitive Galois Field polynomial is the Castagnoli
43 polynomial that was made popular by CRC-32C:
44 x^32+x^28+x^27+x^26+x^25+x^23+x^22+x^20+
45 x^19+x^18+x^14+x^13+x^11+x^10+x^9+x^8+x^6+1 */
46 const uint32_t crc32c= 0x1edc6f41;
47
48 uint32_t rnd= ut_rnd_current.load(std::memory_order_relaxed);
49
50 if (UNIV_UNLIKELY(rnd == 0))
51 {
52 rnd= static_cast<uint32_t>(my_interval_timer());
53 if (!rnd) rnd= 1;
54 }
55 else
56 {
57 bool lsb= rnd & 1;
58 rnd>>= 1;
59 if (lsb)
60 rnd^= crc32c;
61 }
62
63 ut_rnd_current.store(rnd, std::memory_order_relaxed);
64 return rnd;
65 }
66
67 /** @return a random number between 0 and n-1, inclusive */
ut_rnd_interval(ulint n)68 inline ulint ut_rnd_interval(ulint n)
69 {
70 return n > 1 ? static_cast<ulint>(ut_rnd_gen() % n) : 0;
71 }
72
73 /*******************************************************//**
74 The following function generates a hash value for a ulint integer
75 to a hash table of size table_size, which should be a prime or some
76 random number to work reliably.
77 @return hash value */
78 UNIV_INLINE
79 ulint
80 ut_hash_ulint(
81 /*==========*/
82 ulint key, /*!< in: value to be hashed */
83 ulint table_size); /*!< in: hash table size */
84 /*************************************************************//**
85 Folds a 64-bit integer.
86 @return folded value */
87 UNIV_INLINE
88 ulint
89 ut_fold_ull(
90 /*========*/
91 ib_uint64_t d) /*!< in: 64-bit integer */
92 MY_ATTRIBUTE((const));
93 /*************************************************************//**
94 Folds a character string ending in the null character.
95 @return folded value */
96 UNIV_INLINE
97 ulint
98 ut_fold_string(
99 /*===========*/
100 const char* str) /*!< in: null-terminated string */
101 MY_ATTRIBUTE((warn_unused_result));
102 /***********************************************************//**
103 Looks for a prime number slightly greater than the given argument.
104 The prime is chosen so that it is not near any power of 2.
105 @return prime */
106 ulint
107 ut_find_prime(
108 /*==========*/
109 ulint n) /*!< in: positive number > 100 */
110 MY_ATTRIBUTE((const));
111
112 #endif /* !UNIV_INNOCHECKSUM */
113
114 /*************************************************************//**
115 Folds a pair of ulints.
116 @return folded value */
117 UNIV_INLINE
118 ulint
119 ut_fold_ulint_pair(
120 /*===============*/
121 ulint n1, /*!< in: ulint */
122 ulint n2) /*!< in: ulint */
123 MY_ATTRIBUTE((const));
124 /*************************************************************//**
125 Folds a binary string.
126 @return folded value */
127 UNIV_INLINE
128 ulint
129 ut_fold_binary(
130 /*===========*/
131 const byte* str, /*!< in: string of bytes */
132 ulint len) /*!< in: length */
133 MY_ATTRIBUTE((pure));
134
135 #include "ut0rnd.inl"
136
137 #endif
138