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