VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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