Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: pgRandom.c | |
5 | * Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved | |
6 | * 4150 Network Circle, Santa Clara, California 95054, U.S.A. | |
7 | * | |
8 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; version 2 of the License. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | * | |
23 | * For the avoidance of doubt, and except that if any non-GPL license | |
24 | * choice is available it will apply instead, Sun elects to use only | |
25 | * the General Public License version 2 (GPLv2) at this time for any | |
26 | * software where a choice of GPL license versions is made | |
27 | * available with the language indicating that GPLv2 or any later version | |
28 | * may be used, or where a choice of which version of the GPL is applied is | |
29 | * otherwise unspecified. | |
30 | * | |
31 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
32 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
33 | * have any questions. | |
34 | * | |
35 | * | |
36 | * ========== Copyright Header End ============================================ | |
37 | */ | |
38 | #include <stdio.h> | |
39 | #include "vera_directc.h" | |
40 | #include <stdlib.h> | |
41 | #define N 25 | |
42 | #define M 7 | |
43 | ||
44 | //unsigned int rand(); /* returns a random integer */ | |
45 | void init( unsigned int ); /* initialize the generator */ | |
46 | ||
47 | ||
48 | /* return a random float >= 0 and < 1 */ | |
49 | #define rand_float ((double)rand() / 4294967296.0) | |
50 | ||
51 | ||
52 | static unsigned int x[N]; /* the 25 seeds */ | |
53 | ||
54 | static unsigned int mag01[2]; | |
55 | static int randk; | |
56 | void InitpgSeed( unsigned int seed ) | |
57 | { | |
58 | int k; | |
59 | ||
60 | x[0] = (seed|1) & 0xffffffff; | |
61 | for (k=1; k<N; k++) | |
62 | x[k] = (69069 * x[k-1]) & 0xffffffff; | |
63 | mag01[0]= 0x0; | |
64 | mag01[1]= 0x8ebfd028; /* "magic" vector */ | |
65 | randk = 0; | |
66 | } | |
67 | ||
68 | ||
69 | vec32 * pgRand() | |
70 | { | |
71 | unsigned int y; | |
72 | vec32 *retval; | |
73 | int kk; | |
74 | ||
75 | retval = (vec32 *) malloc(sizeof(vec32)); | |
76 | ||
77 | if (randk==N) | |
78 | { | |
79 | for (kk=0; kk < N-M; kk++) | |
80 | x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] & 1]; | |
81 | ||
82 | for (; kk < N; kk++) | |
83 | x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] & 1]; | |
84 | ||
85 | randk=0; | |
86 | } | |
87 | y = x[randk++]; | |
88 | y ^= (y << 7) & 0x2b5b2500; | |
89 | y ^= (y << 15) & 0xdb8b0000; | |
90 | y &= 0xffffffff; /* you may delete this line if word size = 32 */ | |
91 | y ^= (y >> 16); | |
92 | ||
93 | retval->d = y; | |
94 | retval->c = 0; | |
95 | return (retval); | |
96 | } | |
97 |