1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2021, Oracle and/or its affiliates.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, 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 Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /***************************************************************//**
28 @file ut/ut0rnd.cc
29 Random numbers and hashing
30 
31 Created 5/11/1994 Heikki Tuuri
32 ********************************************************************/
33 
34 #include "ut0rnd.h"
35 
36 #ifdef UNIV_NONINL
37 #include "ut0rnd.ic"
38 #endif
39 
40 /** These random numbers are used in ut_find_prime */
41 /*@{*/
42 #define	UT_RANDOM_1	1.0412321
43 #define	UT_RANDOM_2	1.1131347
44 #define UT_RANDOM_3	1.0132677
45 /*@}*/
46 
47 /** Seed value of ut_rnd_gen_ulint(). */
48 ulint	ut_rnd_ulint_counter = 65654363;
49 
50 /***********************************************************//**
51 Looks for a prime number slightly greater than the given argument.
52 The prime is chosen so that it is not near any power of 2.
53 @return prime */
54 ulint
ut_find_prime(ulint n)55 ut_find_prime(
56 /*==========*/
57 	ulint	n)	/*!< in: positive number > 100 */
58 {
59 	ulint	pow2;
60 	ulint	i;
61 
62 	n += 100;
63 
64 	pow2 = 1;
65 	while (pow2 * 2 < n) {
66 		pow2 = 2 * pow2;
67 	}
68 
69 	if ((double) n < 1.05 * (double) pow2) {
70 		n = (ulint) ((double) n * UT_RANDOM_1);
71 	}
72 
73 	pow2 = 2 * pow2;
74 
75 	if ((double) n > 0.95 * (double) pow2) {
76 		n = (ulint) ((double) n * UT_RANDOM_2);
77 	}
78 
79 	if (n > pow2 - 20) {
80 		n += 30;
81 	}
82 
83 	/* Now we have n far enough from powers of 2. To make
84 	n more random (especially, if it was not near
85 	a power of 2), we then multiply it by a random number. */
86 
87 	n = (ulint) ((double) n * UT_RANDOM_3);
88 
89 	for (;; n++) {
90 		i = 2;
91 		while (i * i <= n) {
92 			if (n % i == 0) {
93 				goto next_n;
94 			}
95 			i++;
96 		}
97 
98 		/* Found a prime */
99 		break;
100 next_n:		;
101 	}
102 
103 	return(n);
104 }
105