staging: lustre: remove top level ccflags variable
[deliverable/linux.git] / drivers / staging / lustre / lustre / 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 #include "../../include/linux/libcfs/libcfs.h"
43
44 /*
45 From: George Marsaglia <geo@stat.fsu.edu>
46 Newsgroups: sci.math
47 Subject: Re: A RANDOM NUMBER GENERATOR FOR C
48 Date: Tue, 30 Sep 1997 05:29:35 -0700
49
50 * You may replace the two constants 36969 and 18000 by any
51 * pair of distinct constants from this list:
52 * 18000 18030 18273 18513 18879 19074 19098 19164 19215 19584
53 * 19599 19950 20088 20508 20544 20664 20814 20970 21153 21243
54 * 21423 21723 21954 22125 22188 22293 22860 22938 22965 22974
55 * 23109 23124 23163 23208 23508 23520 23553 23658 23865 24114
56 * 24219 24660 24699 24864 24948 25023 25308 25443 26004 26088
57 * 26154 26550 26679 26838 27183 27258 27753 27795 27810 27834
58 * 27960 28320 28380 28689 28710 28794 28854 28959 28980 29013
59 * 29379 29889 30135 30345 30459 30714 30903 30963 31059 31083
60 * (or any other 16-bit constants k for which both k*2^16-1
61 * and k*2^15-1 are prime) */
62
63 #define RANDOM_CONST_A 18030
64 #define RANDOM_CONST_B 29013
65
66 static unsigned int seed_x = 521288629;
67 static unsigned int seed_y = 362436069;
68
69 /**
70 * cfs_rand - creates new seeds
71 *
72 * First it creates new seeds from the previous seeds. Then it generates a
73 * new pseudo random number for use.
74 *
75 * Returns a pseudo-random 32-bit integer
76 */
77 unsigned int cfs_rand(void)
78 {
79 seed_x = RANDOM_CONST_A * (seed_x & 65535) + (seed_x >> 16);
80 seed_y = RANDOM_CONST_B * (seed_y & 65535) + (seed_y >> 16);
81
82 return ((seed_x << 16) + (seed_y & 65535));
83 }
84 EXPORT_SYMBOL(cfs_rand);
85
86 /**
87 * cfs_srand - sets the initial seed
88 * @seed1 : (seed_x) should have the most entropy in the low bits of the word
89 * @seed2 : (seed_y) should have the most entropy in the high bits of the word
90 *
91 * Replaces the original seeds with new values. Used to generate a new pseudo
92 * random numbers.
93 */
94 void cfs_srand(unsigned int seed1, unsigned int seed2)
95 {
96 if (seed1)
97 seed_x = seed1; /* use default seeds if parameter is 0 */
98 if (seed2)
99 seed_y = seed2;
100 }
101 EXPORT_SYMBOL(cfs_srand);
102
103 /**
104 * cfs_get_random_bytes - generate a bunch of random numbers
105 * @buf : buffer to fill with random numbers
106 * @size: size of passed in buffer
107 *
108 * Fills a buffer with random bytes
109 */
110 void cfs_get_random_bytes(void *buf, int size)
111 {
112 int *p = buf;
113 int rem, tmp;
114
115 LASSERT(size >= 0);
116
117 rem = min((int)((unsigned long)buf & (sizeof(int) - 1)), size);
118 if (rem) {
119 get_random_bytes(&tmp, sizeof(tmp));
120 tmp ^= cfs_rand();
121 memcpy(buf, &tmp, rem);
122 p = buf + rem;
123 size -= rem;
124 }
125
126 while (size >= sizeof(int)) {
127 get_random_bytes(&tmp, sizeof(tmp));
128 *p = cfs_rand() ^ tmp;
129 size -= sizeof(int);
130 p++;
131 }
132 buf = p;
133 if (size) {
134 get_random_bytes(&tmp, sizeof(tmp));
135 tmp ^= cfs_rand();
136 memcpy(buf, &tmp, size);
137 }
138 }
139 EXPORT_SYMBOL(cfs_get_random_bytes);
This page took 0.052198 seconds and 5 git commands to generate.