~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
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., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
16
17
*****************************************************************************/
18
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
19
/***************************************************************//**
20
@file ut/ut0rnd.c
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
21
Random numbers and hashing
22
23
Created 5/11/1994 Heikki Tuuri
24
********************************************************************/
25
26
#include "ut0rnd.h"
27
28
#ifdef UNIV_NONINL
29
#include "ut0rnd.ic"
30
#endif
31
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
32
/** These random numbers are used in ut_find_prime */
33
/*@{*/
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
34
#define	UT_RANDOM_1	1.0412321
35
#define	UT_RANDOM_2	1.1131347
36
#define UT_RANDOM_3	1.0132677
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
37
/*@}*/
38
39
/** Seed value of ut_rnd_gen_ulint(). */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
40
UNIV_INTERN ulint	ut_rnd_ulint_counter = 65654363;
41
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
42
/***********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
43
Looks for a prime number slightly greater than the given argument.
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
44
The prime is chosen so that it is not near any power of 2.
45
@return	prime */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
46
UNIV_INTERN
47
ulint
48
ut_find_prime(
49
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
50
	ulint	n)	/*!< in: positive number > 100 */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
51
{
52
	ulint	pow2;
53
	ulint	i;
54
55
	n += 100;
56
57
	pow2 = 1;
58
	while (pow2 * 2 < n) {
59
		pow2 = 2 * pow2;
60
	}
61
62
	if ((double)n < 1.05 * (double)pow2) {
63
		n = (ulint) ((double)n * UT_RANDOM_1);
64
	}
65
66
	pow2 = 2 * pow2;
67
68
	if ((double)n > 0.95 * (double)pow2) {
69
		n = (ulint) ((double)n * UT_RANDOM_2);
70
	}
71
72
	if (n > pow2 - 20) {
73
		n += 30;
74
	}
75
76
	/* Now we have n far enough from powers of 2. To make
77
	n more random (especially, if it was not near
78
	a power of 2), we then multiply it by a random number. */
79
80
	n = (ulint) ((double)n * UT_RANDOM_3);
81
82
	for (;; n++) {
83
		i = 2;
84
		while (i * i <= n) {
85
			if (n % i == 0) {
86
				goto next_n;
87
			}
88
			i++;
89
		}
90
91
		/* Found a prime */
92
		break;
93
next_n:		;
94
	}
95
96
	return(n);
97
}