~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/ut/ut0rnd.c

  • Committer: Brian Aker
  • Date: 2009-04-17 01:45:33 UTC
  • Revision ID: brian@gaz-20090417014533-exdrtriab9zecqs2
Refactor get_variable to session

Show diffs side-by-side

added added

removed removed

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