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