Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize RNG to support std::uniform_random_bit_generator requirement #3223

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/gnu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ add_library(
Poisson.cpp
Rand.cpp
Random.cpp
RNG.cpp
Uniform.cpp
Weibull.cpp)
set_property(TARGET nrngnu PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand Down
132 changes: 0 additions & 132 deletions src/gnu/RNG.cpp

This file was deleted.

68 changes: 20 additions & 48 deletions src/gnu/RNG.h
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
// This may look like C code, but it is really -*- C++ -*-
/*
Copyright (C) 1988 Free Software Foundation
written by Dirk Grunwald ([email protected])

This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#pragma once
/*
* int32_t and uint32_t have been defined by the configure procedure. Just
* use these in place of the ones that libg++ used to provide.
*/
#include <cstdint>
#include <cassert>
#include <cmath>

union PrivateRNGSingleType { // used to access floats as unsigneds
float s;
uint32_t u;
};

union PrivateRNGDoubleType { // used to access doubles as unsigneds
double d;
uint32_t u[2];
};
#include <limits>

//
// Base class for Random Number Generators.
//
class RNG {
static PrivateRNGSingleType singleMantissa; // mantissa bit vector
static PrivateRNGDoubleType doubleMantissa; // mantissa bit vector
public:
RNG();
virtual ~RNG();
//
using result_type = std::uint32_t;

RNG() = default;
virtual ~RNG() = default;

// Return a long-words word of random bits
//
virtual uint32_t asLong() = 0;
virtual result_type asLong() = 0;
virtual void reset() = 0;
//
// Return random bits converted to either a float or a double
//
virtual float asFloat();
virtual double asDouble();

// Return random bits converted to a double
virtual double asDouble() = 0;

static constexpr result_type min() {
return std::numeric_limits<result_type>::min();
}
static constexpr result_type max() {
return std::numeric_limits<result_type>::max();
}
result_type operator()() {
alkino marked this conversation as resolved.
Show resolved Hide resolved
return asLong();
}
};
Loading