VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rand/randparkmiller.cpp@ 21848

Last change on this file since 21848 was 21337, checked in by vboxsync, 15 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: randparkmiller.cpp 21337 2009-07-07 14:58:27Z 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 "internal/iprt.h"
36
37#include <iprt/asm.h>
38#include <iprt/mem.h>
39#include <iprt/string.h>
40#include <iprt/err.h>
41#include "internal/rand.h"
42#include "internal/magics.h"
43
44
45
46DECLINLINE(uint32_t) rtRandParkMillerU31(uint32_t *pu32Ctx)
47{
48 /*
49 * Park-Miller random number generator:
50 * X2 = X1 * g mod n.
51 *
52 * We use the constants suggested by Park and Miller:
53 * n = 2^31 - 1 = INT32_MAX
54 * g = 7^5 = 16807
55 *
56 * This will produce numbers in the range [0..INT32_MAX-1], which is
57 * almost 31-bits. We'll ignore the missing number for now and settle
58 * for just filling in the missing bit instead (the caller does this).
59 */
60 uint32_t x1 = *pu32Ctx;
61 if (!x1)
62 x1 = 20080806;
63 /*uint32_t x2 = ((uint64_t)x1 * 16807) % INT32_MAX;*/
64 uint32_t x2 = ASMModU64ByU32RetU32(ASMMult2xU32RetU64(x1, 16807), INT32_MAX);
65 return *pu32Ctx = x2;
66}
67
68
69/** @copydoc RTRANDINT::pfnGetU32 */
70static DECLCALLBACK(uint32_t) rtRandParkMillerGetU32(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last)
71{
72 uint32_t off;
73 uint32_t offLast = u32Last - u32First;
74 if (offLast == UINT32_MAX)
75 {
76 /* 30 + 2 bit (make up for the missing INT32_MAX value) */
77 off = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
78 if (pThis->u.ParkMiller.cBits < 2)
79 {
80 pThis->u.ParkMiller.u32Bits = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
81 pThis->u.ParkMiller.cBits = 30;
82 }
83 off >>= 1;
84 off |= (pThis->u.ParkMiller.u32Bits & 3) << 30;
85 pThis->u.ParkMiller.u32Bits >>= 2;
86 pThis->u.ParkMiller.cBits -= 2;
87 }
88 else if (offLast == (uint32_t)INT32_MAX - 1)
89 /* The exact range. */
90 off = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
91 else if (offLast < UINT32_C(0x07ffffff))
92 {
93 /* Requested 23 or fewer bits, just lose the lower bit. */
94 off = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
95 off >>= 1;
96 off %= (offLast + 1);
97 }
98 else
99 {
100 /*
101 * 30 + 6 bits.
102 */
103 uint64_t off64 = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
104 if (pThis->u.ParkMiller.cBits < 6)
105 {
106 pThis->u.ParkMiller.u32Bits = rtRandParkMillerU31(&pThis->u.ParkMiller.u32Ctx);
107 pThis->u.ParkMiller.cBits = 30;
108 }
109 off64 >>= 1;
110 off64 |= (uint64_t)(pThis->u.ParkMiller.u32Bits & 0x3f) << 30;
111 pThis->u.ParkMiller.u32Bits >>= 6;
112 pThis->u.ParkMiller.cBits -= 6;
113 off = ASMModU64ByU32RetU32(off64, offLast + 1);
114 }
115 return off + u32First;
116}
117
118
119/** @copydoc RTRANDINT::pfnSeed */
120static DECLCALLBACK(int) rtRandParkMillerSeed(PRTRANDINT pThis, uint64_t u64Seed)
121{
122 pThis->u.ParkMiller.u32Ctx = (uint32_t)u64Seed;
123 pThis->u.ParkMiller.u32Bits = 0;
124 pThis->u.ParkMiller.cBits = 0;
125 return VINF_SUCCESS;
126}
127
128
129/** @copydoc RTRANDINT::pfnSaveState */
130static DECLCALLBACK(int) rtRandParkMillerSaveState(PRTRANDINT pThis, char *pszState, size_t *pcbState)
131{
132#define RTRAND_PARKMILLER_STATE_SIZE (3+8+1+8+1+2+1+1)
133
134 if (*pcbState < RTRAND_PARKMILLER_STATE_SIZE)
135 {
136 *pcbState = RTRAND_PARKMILLER_STATE_SIZE;
137 return VERR_BUFFER_OVERFLOW;
138 }
139 RTStrPrintf(pszState, *pcbState, "PM:%08RX32,%08RX32,%02x;",
140 pThis->u.ParkMiller.u32Ctx,
141 pThis->u.ParkMiller.u32Bits,
142 pThis->u.ParkMiller.cBits);
143 return VINF_SUCCESS;
144}
145
146
147/** @copydoc RTRANDINT::pfnRestoreState */
148static DECLCALLBACK(int) rtRandParkMillerRestoreState(PRTRANDINT pThis, char const *pszState)
149{
150 /* marker */
151 if ( pszState[0] != 'P'
152 || pszState[1] != 'M'
153 || pszState[2] != ':')
154 return VERR_PARSE_ERROR;
155 pszState += 3;
156
157 /* u32Ctx */
158 char *pszNext = NULL;
159 uint32_t u32Ctx;
160 int rc = RTStrToUInt32Ex(pszState, &pszNext, 16, &u32Ctx);
161 if ( rc != VWRN_TRAILING_CHARS
162 || pszNext != pszState + 8
163 || *pszNext != ',')
164 return VERR_PARSE_ERROR;
165 pszState += 8 + 1;
166
167 /* u32Bits */
168 uint32_t u32Bits;
169 rc = RTStrToUInt32Ex(pszState, &pszNext, 16, &u32Bits);
170 if ( rc != VWRN_TRAILING_CHARS
171 || pszNext != pszState + 8
172 || *pszNext != ',')
173 return VERR_PARSE_ERROR;
174 pszState += 8 + 1;
175
176 /* cBits */
177 uint32_t cBits;
178 rc = RTStrToUInt32Ex(pszState, &pszNext, 16, &cBits);
179 if ( rc != VWRN_TRAILING_CHARS
180 || pszNext != pszState + 2
181 || *pszNext != ';'
182 || pszNext[1] != '\0')
183 return VERR_PARSE_ERROR;
184
185 /* commit */
186 pThis->u.ParkMiller.u32Ctx = u32Ctx;
187 pThis->u.ParkMiller.u32Bits = u32Bits;
188 pThis->u.ParkMiller.cBits = cBits;
189 return VINF_SUCCESS;
190}
191
192
193RTDECL(int) RTRandAdvCreateParkMiller(PRTRAND phRand) RT_NO_THROW
194{
195 PRTRANDINT pThis = (PRTRANDINT)RTMemAlloc(sizeof(*pThis));
196 if (!pThis)
197 return VERR_NO_MEMORY;
198 pThis->u32Magic = RTRANDINT_MAGIC;
199 pThis->pfnGetBytes= rtRandAdvSynthesizeBytesFromU32;
200 pThis->pfnGetU32 = rtRandParkMillerGetU32;
201 pThis->pfnGetU64 = rtRandAdvSynthesizeU64FromU32;
202 pThis->pfnSeed = rtRandParkMillerSeed;
203 pThis->pfnSaveState = rtRandParkMillerSaveState;
204 pThis->pfnRestoreState = rtRandParkMillerRestoreState;
205 pThis->pfnDestroy = rtRandAdvDefaultDestroy;
206 pThis->u.ParkMiller.u32Ctx = 0x20080806;
207 pThis->u.ParkMiller.u32Bits = 0;
208 pThis->u.ParkMiller.cBits = 0;
209 *phRand = pThis;
210 return VINF_SUCCESS;
211}
212RT_EXPORT_SYMBOL(RTRandAdvCreateParkMiller);
213
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette