Path: blob/master/src/hotspot/share/jfr/utilities/jfrRandom.inline.hpp
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018, Google and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef SHARE_JFR_UTILITIES_JFRRANDOM_INLINE_HPP26#define SHARE_JFR_UTILITIES_JFRRANDOM_INLINE_HPP2728#include "jfr/utilities/jfrRandom.hpp"2930inline JfrPRNG::JfrPRNG(const void* seed) : _rnd(reinterpret_cast<uint64_t>(seed)) {31assert(seed != NULL, "invariant");32}3334// Returns the next prng value.35// pRNG is: aX+b mod c with a = 0x5DEECE66D, b = 0xB, c = 1<<4836// This is the lrand64 generator.37inline uint64_t next(uint64_t rnd) {38static const uint64_t PrngMult = 0x5DEECE66DLL;39static const uint64_t PrngAdd = 0xB;40static const uint64_t PrngModPower = 48;41static const uint64_t PrngModMask = (static_cast<uint64_t>(1) << PrngModPower) - 1;42return (PrngMult * rnd + PrngAdd) & PrngModMask;43}4445inline double JfrPRNG::next_uniform() const {46_rnd = next(_rnd);47// Take the top 26 bits as the random number48// (This plus a 1<<58 sampling bound gives a max possible step of49// 5194297183973780480 bytes. In this case,50// for sample_parameter = 1<<19, max possible step is51// 9448372 bytes (24 bits).52static const uint64_t PrngModPower = 48; // Number of bits in prng53// The uint32_t cast is to prevent a (hard-to-reproduce) NAN54// under piii debug for some binaries.55// the n_rand value is between 0 and 2**26-1 so it needs to be normalized by dividing by 2**26 (67108864)56return (static_cast<uint32_t>(_rnd >> (PrngModPower - 26)) / static_cast<double>(67108864));57}5859#endif // SHARE_JFR_UTILITIES_JFRRANDOM_INLINE_HPP606162