1 | /* $Id: randparkmiller.cpp 11523 2008-08-20 20:48:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Random Numbers, Park-Miller Pseudo Random.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/rand.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include "internal/rand.h"
|
---|
40 | #include "internal/magics.h"
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | DECLINLINE(uint32_t) rtRandParkMillerU31(uint32_t *pu32Ctx)
|
---|
45 | {
|
---|
46 | /*
|
---|
47 | * Park-Miller random number generator:
|
---|
48 | * X2 = X1 * g mod n.
|
---|
49 | *
|
---|
50 | * We use the constants suggested by Park and Miller:
|
---|
51 | * n = 2^31 - 1 = INT32_MAX
|
---|
52 | * g = 7^5 = 16807
|
---|
53 | *
|
---|
54 | * This will produce numbers in the range [0..INT32_MAX-1], which is
|
---|
55 | * almost 31-bits. We'll ignore the missing number for now and settle
|
---|
56 | * for just filling in the missing bit instead (the caller does this).
|
---|
57 | */
|
---|
58 | uint32_t x1 = *pu32Ctx;
|
---|
59 | if (!x1)
|
---|
60 | x1 = 20080806;
|
---|
61 | /*uint32_t x2 = ((uint64_t)x1 * 16807) % INT32_MAX;*/
|
---|
62 | uint32_t x2 = ASMModU64ByU32RetU32(ASMMult2xU32RetU64(x1, 16807), INT32_MAX);
|
---|
63 | return *pu32Ctx = x2;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /** @copydoc RTRANDINT::pfnGetU32 */
|
---|
68 | static DECLCALLBACK(uint32_t) rtRandParkMillerGetU32(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last)
|
---|
69 | {
|
---|
70 | uint32_t off;
|
---|
71 | uint32_t offLast = u32Last - u32First;
|
---|
72 | if (offLast == UINT32_MAX)
|
---|
73 | {
|
---|
74 | /* 30 + 2 bit (make up for the missing INT32_MAX value) */
|
---|
75 | off = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
|
---|
76 | if (pThis->u.ParkMiller.cBits < 2)
|
---|
77 | {
|
---|
78 | pThis->u.ParkMiller.u32Bits = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
|
---|
79 | pThis->u.ParkMiller.cBits = 30;
|
---|
80 | }
|
---|
81 | off >>= 1;
|
---|
82 | off |= (pThis->u.ParkMiller.u32Bits & 3) << 30;
|
---|
83 | pThis->u.ParkMiller.u32Bits >>= 2;
|
---|
84 | pThis->u.ParkMiller.cBits -= 2;
|
---|
85 | }
|
---|
86 | else if (offLast == (uint32_t)INT32_MAX - 1)
|
---|
87 | /* The exact range. */
|
---|
88 | off = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
|
---|
89 | else if (offLast < UINT32_C(0x07ffffff))
|
---|
90 | {
|
---|
91 | /* Requested 23 or fewer bits, just lose the lower bit. */
|
---|
92 | off = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
|
---|
93 | off >>= 1;
|
---|
94 | off %= (offLast + 1);
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | * 30 + 6 bits.
|
---|
100 | */
|
---|
101 | uint64_t off64 = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
|
---|
102 | if (pThis->u.ParkMiller.cBits < 6)
|
---|
103 | {
|
---|
104 | pThis->u.ParkMiller.u32Bits = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
|
---|
105 | pThis->u.ParkMiller.cBits = 30;
|
---|
106 | }
|
---|
107 | off64 >>= 1;
|
---|
108 | off64 |= (uint64_t)(pThis->u.ParkMiller.u32Bits & 0x3f) << 30;
|
---|
109 | pThis->u.ParkMiller.u32Bits >>= 6;
|
---|
110 | pThis->u.ParkMiller.cBits -= 6;
|
---|
111 | off = ASMModU64ByU32RetU32(off64, offLast + 1);
|
---|
112 | }
|
---|
113 | return off + u32First;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /** @copydoc RTRANDINT::pfnSeed */
|
---|
118 | static DECLCALLBACK(int) rtRandParkMillerSeed(PRTRANDINT pThis, uint64_t u64Seed)
|
---|
119 | {
|
---|
120 | pThis->u.ParkMiller.u32Ctx = u64Seed;
|
---|
121 | pThis->u.ParkMiller.u32Bits = 0;
|
---|
122 | pThis->u.ParkMiller.cBits = 0;
|
---|
123 | return VINF_SUCCESS;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /** @copydoc RTRANDINT::pfnSaveState */
|
---|
128 | static DECLCALLBACK(int) rtRandParkMillerSaveState(PRTRANDINT pThis, char *pszState, size_t *pcbState)
|
---|
129 | {
|
---|
130 | #define RTRAND_PARKMILLER_STATE_SIZE (3+8+1+8+1+2+1+1)
|
---|
131 |
|
---|
132 | if (*pcbState < RTRAND_PARKMILLER_STATE_SIZE)
|
---|
133 | {
|
---|
134 | *pcbState = RTRAND_PARKMILLER_STATE_SIZE;
|
---|
135 | return VERR_BUFFER_OVERFLOW;
|
---|
136 | }
|
---|
137 | RTStrPrintf(pszState, *pcbState, "PM:%08RX32,%08RX32,%02x;",
|
---|
138 | pThis->u.ParkMiller.u32Ctx,
|
---|
139 | pThis->u.ParkMiller.u32Bits,
|
---|
140 | pThis->u.ParkMiller.cBits);
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** @copydoc RTRANDINT::pfnRestoreState */
|
---|
146 | static DECLCALLBACK(int) rtRandParkMillerRestoreState(PRTRANDINT pThis, char const *pszState)
|
---|
147 | {
|
---|
148 | /* marker */
|
---|
149 | if ( pszState[0] != 'P'
|
---|
150 | || pszState[1] != 'M'
|
---|
151 | || pszState[2] != ':')
|
---|
152 | return VERR_PARSE_ERROR;
|
---|
153 | pszState += 3;
|
---|
154 |
|
---|
155 | /* u32Ctx */
|
---|
156 | char *pszNext = NULL;
|
---|
157 | uint32_t u32Ctx;
|
---|
158 | int rc = RTStrToUInt32Ex(pszState, &pszNext, 16, &u32Ctx);
|
---|
159 | if ( rc != VWRN_TRAILING_CHARS
|
---|
160 | || pszNext != pszState + 8
|
---|
161 | || *pszNext != ',')
|
---|
162 | return VERR_PARSE_ERROR;
|
---|
163 | pszState += 8 + 1;
|
---|
164 |
|
---|
165 | /* u32Bits */
|
---|
166 | uint32_t u32Bits;
|
---|
167 | rc = RTStrToUInt32Ex(pszState, &pszNext, 16, &u32Bits);
|
---|
168 | if ( rc != VWRN_TRAILING_CHARS
|
---|
169 | || pszNext != pszState + 8
|
---|
170 | || *pszNext != ',')
|
---|
171 | return VERR_PARSE_ERROR;
|
---|
172 | pszState += 8 + 1;
|
---|
173 |
|
---|
174 | /* cBits */
|
---|
175 | uint32_t cBits;
|
---|
176 | rc = RTStrToUInt32Ex(pszState, &pszNext, 16, &cBits);
|
---|
177 | if ( rc != VWRN_TRAILING_CHARS
|
---|
178 | || pszNext != pszState + 2
|
---|
179 | || *pszNext != ';'
|
---|
180 | || pszNext[1] != '\0')
|
---|
181 | return VERR_PARSE_ERROR;
|
---|
182 |
|
---|
183 | /* commit */
|
---|
184 | pThis->u.ParkMiller.u32Ctx = u32Ctx;
|
---|
185 | pThis->u.ParkMiller.u32Bits = u32Bits;
|
---|
186 | pThis->u.ParkMiller.cBits = cBits;
|
---|
187 | return VINF_SUCCESS;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | RTDECL(int) RTRandAdvCreateParkMiller(PRTRAND phRand) RT_NO_THROW
|
---|
192 | {
|
---|
193 | PRTRANDINT pThis = (PRTRANDINT)RTMemAlloc(sizeof(*pThis));
|
---|
194 | if (!pThis)
|
---|
195 | return VERR_NO_MEMORY;
|
---|
196 | pThis->u32Magic = RTRANDINT_MAGIC;
|
---|
197 | pThis->pfnGetBytes= rtRandAdvSynthesizeBytesFromU32;
|
---|
198 | pThis->pfnGetU32 = rtRandParkMillerGetU32;
|
---|
199 | pThis->pfnGetU64 = rtRandAdvSynthesizeU64FromU32;
|
---|
200 | pThis->pfnSeed = rtRandParkMillerSeed;
|
---|
201 | pThis->pfnSaveState = rtRandParkMillerSaveState;
|
---|
202 | pThis->pfnRestoreState = rtRandParkMillerRestoreState;
|
---|
203 | pThis->pfnDestroy = rtRandAdvDefaultDestroy;
|
---|
204 | pThis->u.ParkMiller.u32Ctx = 0x20080806;
|
---|
205 | pThis->u.ParkMiller.u32Bits = 0;
|
---|
206 | pThis->u.ParkMiller.cBits = 0;
|
---|
207 | *phRand = pThis;
|
---|
208 | return VINF_SUCCESS;
|
---|
209 | }
|
---|
210 |
|
---|