Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / libcfs / prng.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/libcfs/prng.c
35  *
36  * concatenation of following two 16-bit multiply with carry generators
37  * x(n)=a*x(n-1)+carry mod 2^16 and y(n)=b*y(n-1)+carry mod 2^16,
38  * number and carry packed within the same 32 bit integer.
39  * algorithm recommended by Marsaglia
40 */
41
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45
46 #include <libcfs/libcfs.h>
47
48 /*
49 From: George Marsaglia <geo@stat.fsu.edu>
50 Newsgroups: sci.math
51 Subject: Re: A RANDOM NUMBER GENERATOR FOR C
52 Date: Tue, 30 Sep 1997 05:29:35 -0700
53
54  * You may replace the two constants 36969 and 18000 by any
55  * pair of distinct constants from this list:
56  * 18000 18030 18273 18513 18879 19074 19098 19164 19215 19584
57  * 19599 19950 20088 20508 20544 20664 20814 20970 21153 21243
58  * 21423 21723 21954 22125 22188 22293 22860 22938 22965 22974
59  * 23109 23124 23163 23208 23508 23520 23553 23658 23865 24114
60  * 24219 24660 24699 24864 24948 25023 25308 25443 26004 26088
61  * 26154 26550 26679 26838 27183 27258 27753 27795 27810 27834
62  * 27960 28320 28380 28689 28710 28794 28854 28959 28980 29013
63  * 29379 29889 30135 30345 30459 30714 30903 30963 31059 31083
64  * (or any other 16-bit constants k for which both k*2^16-1
65  * and k*2^15-1 are prime) */
66
67 #define RANDOM_CONST_A 18030
68 #define RANDOM_CONST_B 29013
69
70 static unsigned int seed_x = 521288629;
71 static unsigned int seed_y = 362436069;
72
73 /**
74  * cfs_rand - creates new seeds
75  *
76  * First it creates new seeds from the previous seeds. Then it generates a
77  * new psuedo random number for use.
78  *
79  * Returns a pseudo-random 32-bit integer
80  */
81 unsigned int cfs_rand(void)
82 {
83         seed_x = RANDOM_CONST_A * (seed_x & 65535) + (seed_x >> 16);
84         seed_y = RANDOM_CONST_B * (seed_y & 65535) + (seed_y >> 16);
85
86         return ((seed_x << 16) + (seed_y & 65535));
87 }
88 CFS_EXPORT_SYMBOL(cfs_rand);
89
90 /**
91  * cfs_srand - sets the inital seed
92  * @seed1 : (seed_x) should have the most entropy in the low bits of the word
93  * @seed2 : (seed_y) should have the most entropy in the high bits of the word
94  *
95  * Replaces the original seeds with new values. Used to generate a new pseudo
96  * random numbers.
97  */
98 void cfs_srand(unsigned int seed1, unsigned int seed2)
99 {
100         if (seed1)
101                 seed_x = seed1; /* use default seeds if parameter is 0 */
102         if (seed2)
103                 seed_y = seed2;
104 }
105 CFS_EXPORT_SYMBOL(cfs_srand);
106
107 /**
108  * cfs_get_random_bytes - generate a bunch of random numbers
109  * @buf : buffer to fill with random numbers
110  * @size: size of passed in buffer
111  *
112  * Fills a buffer with random bytes
113  */
114 void cfs_get_random_bytes(void *buf, int size)
115 {
116         int *p = buf;
117         int rem, tmp;
118
119         LASSERT(size >= 0);
120
121         rem = min((int)((unsigned long)buf & (sizeof(int) - 1)), size);
122         if (rem) {
123                 cfs_get_random_bytes_prim(&tmp, sizeof(tmp));
124                 tmp ^= cfs_rand();
125                 memcpy(buf, &tmp, rem);
126                 p = buf + rem;
127                 size -= rem;
128         }
129
130         while (size >= sizeof(int)) {
131                 cfs_get_random_bytes_prim(&tmp, sizeof(tmp));
132                 *p = cfs_rand() ^ tmp;
133                 size -= sizeof(int);
134                 p++;
135         }
136         buf = p;
137         if (size) {
138                 cfs_get_random_bytes_prim(&tmp, sizeof(tmp));
139                 tmp ^= cfs_rand();
140                 memcpy(buf, &tmp, size);
141         }
142 }
143 CFS_EXPORT_SYMBOL(cfs_get_random_bytes);