Added sharedMath and sharedRandom libraries

This commit is contained in:
Anonymous
2014-01-14 02:05:25 -07:00
parent 377fab52c5
commit a4f2c06f77
187 changed files with 21231 additions and 1 deletions
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 2.8)
project(sharedRandom)
if(WIN32)
add_definitions(/D_CRT_SECURE_NO_WARNINGS)
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/public)
add_subdirectory(src)
@@ -0,0 +1 @@
#include "../../src/shared/ConfigSharedRandom.h"
@@ -0,0 +1 @@
#include "../../src/shared/FastRandomGenerator.h"
@@ -0,0 +1 @@
#include "../../src/shared/FirstSharedRandom.h"
@@ -0,0 +1 @@
#include "../../src/shared/Random.h"
@@ -0,0 +1 @@
#include "../../src/shared/RandomGenerator.h"
@@ -0,0 +1 @@
#include "../../src/shared/SetupSharedRandom.h"
@@ -0,0 +1,36 @@
set(SHARED_SOURCES
shared/ConfigSharedRandom.cpp
shared/ConfigSharedRandom.h
shared/FastRandomGenerator.h
shared/FirstSharedRandom.h
shared/Random.cpp
shared/Random.h
shared/RandomGenerator.cpp
shared/RandomGenerator.h
shared/SetupSharedRandom.cpp
shared/SetupSharedRandom.h
)
if(WIN32)
set(PLATFORM_SOURCES
win32/FirstSharedRandom.cpp
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/win32)
else()
set(PLATFORM_SOURCES "")
endif()
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/shared
${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedDebug/include/public
${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedFoundation/include/public
${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedFoundationTypes/include/public
${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedMemoryManager/include/public
)
add_library(sharedRandom STATIC
${SHARED_SOURCES}
${PLATFORM_SOURCES}
)
@@ -0,0 +1,42 @@
// ======================================================================
//
// ConfigSharedRandom.cpp
// copyright 2004 Sony Online Entertainment
//
// ======================================================================
#include "sharedRandom/FirstSharedRandom.h"
#include "sharedRandom/ConfigSharedRandom.h"
#include "sharedFoundation/ConfigFile.h"
// ======================================================================
#define KEY_BOOL(a,b) (ms_ ## a = ConfigFile::getKeyBool("SharedRandom", #a, (b)))
// ======================================================================
namespace ConfigSharedRandomNamespace
{
bool ms_sample;
}
using namespace ConfigSharedRandomNamespace;
// ======================================================================
void ConfigSharedRandom::install()
{
KEY_BOOL(sample, false);
}
// ----------------------------------------------------------------------
bool ConfigSharedRandom::getSample()
{
return ms_sample;
}
// ======================================================================
@@ -0,0 +1,24 @@
// ======================================================================
//
// ConfigSharedRandom.h
// Copyright 2004, Sony Online Entertainment Inc.
// All Rights Reserved.
//
// ======================================================================
#ifndef INCLUDED_ConfigSharedRandom_H
#define INCLUDED_ConfigSharedRandom_H
// ======================================================================
class ConfigSharedRandom
{
public:
static void install();
static bool getSample();
};
// ======================================================================
#endif
@@ -0,0 +1,71 @@
// ======================================================================
//
// RandomGenerator.h
// By Ben Earhart
//
// copyright 2005 Bootprint Entertainment
//
// ======================================================================
#ifndef _FAST_RANDOM_GENERATOR_H_
#define _FAST_RANDOM_GENERATOR_H_
// ======================================================================
class FastRandomGenerator
{
public:
FastRandomGenerator() { m_I=ZERO_ALT; }
FastRandomGenerator(long s) { setSeed(s); }
void setSeed(long s)
{
const long ps = s&0x7fffffff;
m_I = (ps==0) ? ZERO_ALT : ps;
}
// return a random number r where 0<=r<1
float randomFloat()
{
long f = (_rand()>>8)|IEEE_FLOAT_BITS;
return (*(float *)(void *)&f)-1.0f;
}
float randomFloat(float range) { return randomFloat()*range; }
float randomFloat(float low, float high) { return randomFloat()*(high-low) + low; }
int random() { return _rand(); }
int random(int range) { return random()%range; }
int random(int low, int high) { return (random()%(high-low)) + low; }
protected:
enum { IEEE_FLOAT_BITS=0x3f800000 };
enum {
IA = 16807,
IM = 2147483647,
IQ = 127773,
IR = 2836,
ZERO_ALT = 123459876
};
long _rand()
{
long k;
k=m_I/IQ;
m_I=IA*(m_I-k*IQ)-IR*k;
if (m_I<0) m_I += IM;
k = m_I;
return k;
}
long _randMax() { return IM-1; }
long m_I;
};
// ======================================================================
#endif
@@ -0,0 +1,19 @@
// ======================================================================
//
// FirstRandom.h
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_FirstRandom_H
#define INCLUDED_FirstRandom_H
// ======================================================================
#include "sharedDebug/FirstSharedDebug.h"
#include "sharedFoundation/FirstSharedFoundation.h"
// ======================================================================
#endif
@@ -0,0 +1,50 @@
// ======================================================================
//
// Random.cpp
// By Eric Sebesta
//
// copyright 1999 Bootprint Entertainment
//
// ======================================================================
#include "sharedRandom/FirstSharedRandom.h"
#include "sharedRandom/Random.h"
#include "sharedRandom/RandomGenerator.h"
#include "sharedFoundation/ExitChain.h"
// ======================================================================
RandomGenerator* Random::rand;
bool Random::installed;
// ======================================================================
// Static function install this module
//
// Remarks:
//
// None.
void Random::install(uint32 newSeed)
{
DEBUG_FATAL(installed, ("already installed"));
installed = true;
ExitChain::add(Random::remove, "Random::remove", 0, false);
rand = new RandomGenerator(newSeed);
}
// ----------------------------------------------------------------------
/**
* Static function to remove this module.
*
* None.
*/
void Random::remove(void)
{
DEBUG_FATAL(!installed, ("not installed"));
installed = false;
delete rand;
}
// ======================================================================
@@ -0,0 +1,190 @@
// ======================================================================
//
// Random.h
// By Eric Sebesta
//
// copyright 1999 Bootprint Entertainment
//
// ======================================================================
#ifndef _RANDOM_H_
#define _RANDOM_H_
#include "sharedRandom/RandomGenerator.h"
// ======================================================================
// Provides an interface to a global random number generator
//
// Remarks:
//
// It must have install() called before use. It creates a global random number
// generator. To create local random number generators, use RandomGenerator.
// The RandomGenerator class depends on this class to generate the seeds for it.
class Random
{
private:
//private static data members
static RandomGenerator* rand;
static bool installed;
public:
//public static member functions
static void install (uint32 newSeed);
static void remove (void);
static real randomReal (void);
static real randomReal (real range);
static real randomReal (real low, real high);
static int32 random (void);
static int32 random (int32 range);
static int32 random (int32 low, int32 high);
static int32 getSeed (void);
static void setSeed (uint32 newSeed);
static bool isInstalled (void);
private:
// disable: default constructor, copy constructor, assignment operator
Random (void);
Random (const Random&);
Random &operator =(const Random&);
};
// ----------------------------------------------------------------------
/**
* Static function to get a real random number between 0 and 1.
*
* None.
*
* @return A real random number between (and including) 0 and max(real)
*/
inline real Random::randomReal (void)
{
DEBUG_FATAL(!installed, ("not installed"));
return rand->randomReal();
}
// ----------------------------------------------------------------------
/**
* Static function to get a int32 random number between 0 and max(int32).
*
* None.
*
* @return A int32 random number between (and including) 0 and max(int32)
*/
inline int32 Random::random (void)
{
DEBUG_FATAL(!installed, ("not installed"));
return rand->random();
}
// ----------------------------------------------------------------------
/**
* Static function to get a real random number between low and high.
*
* None.
*
* @return A real random number between (and including) low and high.
*/
inline real Random::randomReal (real low, real high)
{
DEBUG_FATAL(!installed, ("not installed"));
return rand->randomReal(low, high);
}
// ----------------------------------------------------------------------
/**
* Static function to get a int32 random number between low and high.
*
* None.
*
* @return A int32 random number between (and including) low and high.
*/
inline int32 Random::random (int32 low, int32 high)
{
DEBUG_FATAL(!installed, ("not installed"));
return rand->random(low, high);
}
// ----------------------------------------------------------------------
/**
* Static function to get a int32 random number between 0 and range.
*
* None.
*
* @return A int32 random number between (and including) 0 and range.
*/
inline int32 Random::random (int32 range)
{
DEBUG_FATAL(!installed, ("not installed"));
DEBUG_FATAL(range < 0, ("range < 0, use random(-range, 0)"));
return rand->random(0, range);
}
// ----------------------------------------------------------------------
/**
* Static function to get a real random number between 0 and range.
*
* None.
*
* @return A real random number between (and including) 0 and range.
*/
inline real Random::randomReal (real range)
{
DEBUG_FATAL(!installed, ("not installed"));
DEBUG_FATAL(range < 0, ("range < 0, use randomReal(-range, 0)"));
return rand->randomReal (CONST_REAL(0), range);
}
// ----------------------------------------------------------------------
/**
* Static function to get the current global seed.
*
* None.
*
* @return The current seed.
*/
inline int32 Random::getSeed (void)
{
DEBUG_FATAL(!installed, ("not installed"));
return rand->getSeed();
}
// ----------------------------------------------------------------------
/**
* Static function to set the global seed.
*
* Can be used midgame to change the seed. Note that if seeded to a previously used value, repetition may result.
*/
inline void Random::setSeed (uint32 newSeed)
{
DEBUG_FATAL(!installed, ("not installed"));
rand->setSeed(newSeed);
}
// ----------------------------------------------------------------------
/**
* Static function determine if this module is installed.
*
* None
*/
inline bool Random::isInstalled ()
{
DEBUG_FATAL(!installed, ("not installed"));
return installed;
}
// ======================================================================
#endif
@@ -0,0 +1,72 @@
// ======================================================================
//
// RandomGenerator.h
// By Eric Sebesta
//
// copyright 1999 Bootprint Entertainment
//
// ======================================================================
#include "sharedRandom/FirstSharedRandom.h"
#include "sharedRandom/RandomGenerator.h"
#include "sharedRandom/Random.h"
// ======================================================================
const real RandomGenerator::AM = CONST_REAL(1.0/IM);
const real RandomGenerator::NDIV = CONST_REAL(1+(IM-1)/static_cast<real>(NTAB));
const real RandomGenerator::EPS = CONST_REAL(1.2e-7);
const real RandomGenerator::RNMX = CONST_REAL(1.0-EPS);
// ======================================================================
// Construct a RandomGenerator
//
// Remarks:
//
// Sets seed based on the global Random::random() call (i.e different for every RandomGenerator).
RandomGenerator::RandomGenerator (void)
: idnum (-Random::random()+1), //+1 so that our seed is different from the global one
iy (0)
{
memset(iv, 0, sizeof(iv));
}
// ----------------------------------------------------------------------
int32 RandomGenerator::randomNumber (void)
{
// This random algorithm comes from ran1() in Numerical Recipes in C, p. 280
int32 j;
int32 k;
if(idnum <= 0 || !iy)
{
if(-(idnum) < 1) //initialize
idnum = 1;
else
idnum = -(idnum);
for (j = NTAB+7; j >= 0; --j) //use idnum as the "seed value"
{
k = (idnum)/IQ;
idnum = IA * (idnum-k *IQ) - IR*k;
if (idnum < 0)
idnum += IM;
if (j < NTAB)
iv[j] = idnum;
}
iy=iv[0];
}
k = (idnum) / IQ; //start here when not initializing
idnum = IA*(idnum-k*IQ)-IR*k; //generate random number (Schrage's Method)
if (idnum < 0)
idnum += IM;
j = static_cast<int32>(iy/NDIV); //put in range 0...NTAB-1
iy = iv[j]; //iy is our new random number
iv[j] = idnum;
return iy;
}
// ======================================================================
@@ -0,0 +1,217 @@
// ======================================================================
//
// RandomGenerator.h
// By Eric Sebesta
//
// copyright 1999 Bootprint Entertainment
//
// ======================================================================
#ifndef _RANDOM_GENERATOR_H_
#define _RANDOM_GENERATOR_H_
// ======================================================================
// A class to generate random numbers
//
// Remarks:
//
// This class generates 31-bit random numbers returned as either 32-bit
// integers or as reals. Note that Random::install() MUST be called prior
// to creation of RandomGenerator classes. Random represents a
// global random number generator, and is used to generate seeds for all
// RandomGenerator classes. Failing to call Random::install() will Fatal in
// debug builds and have undefined behavior in release builds. This class
// uses a random number generation algorithm found in Numerical Recipes in C,
// on p. 280.
class RandomGenerator
{
private:
//private member functions
int32 randomNumber (void);
private:
enum
{
IM = 2147483647,
NTAB = 322,
IA = 16807,
IQ = 127773,
IR = 2836
};
static const real AM;
static const real NDIV;
static const real EPS;
static const real RNMX;
private:
//private member variables
int32 idnum;
int32 iy;
int32 iv[NTAB];
public:
//constructors and destructors
RandomGenerator (void);
explicit RandomGenerator (uint32 newSeed);
~RandomGenerator (void);
//public member functions
real randomReal (void);
real randomReal (real range);
real randomReal (real low, real high);
int32 random (void);
int32 random (int32 range);
int32 random (int32 low, int32 high);
int32 getSeed (void);
void setSeed (uint32 newSeed);
};
// ======================================================================
// Construct a RandomGenerator
//
// Remarks:
//
// Uses the user-supplied seed.
inline RandomGenerator::RandomGenerator (uint32 newSeed)
: idnum (-static_cast<int32>(newSeed)),
iy (0)
{
memset(iv, 0, sizeof(iv));
}
// ======================================================================
// Destruct a RandomGenerator
//
// Remarks:
//
// None.
inline RandomGenerator::~RandomGenerator (void)
{
}
// ----------------------------------------------------------------------
/**
* Get a real random number between 0 and max(real).
*
* None.
*
* @return A real random number between (and including) 0 and 1
*/
inline real RandomGenerator::randomReal (void)
{
return static_cast<real>( randomNumber () ) / static_cast<real>( IM );
}
// ----------------------------------------------------------------------
/**
* Get a int32 random number between 0 and max(int32).
*
* None.
*
* @return A int32 random number between (and including) 0 and max(int32)
*/
inline int32 RandomGenerator::random (void)
{
return randomNumber ();
}
// ----------------------------------------------------------------------
/**
* Get a real random number between low and high.
*
* None.
*
* @return A real random number between (and including) low and high.
*/
inline real RandomGenerator::randomReal (real low, real high)
{
DEBUG_FATAL(low > high, ("low > high"));
return static_cast<real>(randomReal() * (high - low) + low);
}
// ----------------------------------------------------------------------
/**
* Get a int32 random number between low and high.
*
* None.
*
* @return A int32 random number between (and including) low and high.
*/
inline int32 RandomGenerator::random (int32 low, int32 high)
{
DEBUG_FATAL(low > high, ("low > high"));
return ( random () % ( high - low + 1 ) + low );
}
// ----------------------------------------------------------------------
/**
* Get a int32 random number between 0 and range.
*
* None.
*
* @return A int32 random number between (and including) 0 and range.
*/
inline int32 RandomGenerator::random (int32 range)
{
DEBUG_FATAL(range < 0, ("range < 0, use random(-range, 0)"));
return random ( 0, range );
}
// ----------------------------------------------------------------------
/**
* Get a real random number between 0 and range.
*
* None.
*
* @return A real random number between (and including) 0 and range.
*/
inline real RandomGenerator::randomReal (real range)
{
DEBUG_FATAL(range < 0, ("range < 0, use randomReal(-range, 0)"));
return randomReal ( CONST_REAL(0), range );
}
// ----------------------------------------------------------------------
/**
* Get the current seed.
*
* None.
*
* @return The current seed.
*/
inline int32 RandomGenerator::getSeed (void)
{
return iy;
}
// ----------------------------------------------------------------------
/**
* Set the seed.
*
* Can be used midgame to change the seed. Note that if seeded to a previously used value, repetition may result.
*/
inline void RandomGenerator::setSeed (uint32 newSeed)
{
iy = 0;
idnum = -(static_cast<int32>(newSeed)); //since a negative value will cause the ran1() as used in randomNumber to "reseed"
}
// ======================================================================
#endif
@@ -0,0 +1,25 @@
// ======================================================================
//
// SetupSharedRandom.cpp
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedRandom/FirstSharedRandom.h"
#include "sharedRandom/SetupSharedRandom.h"
#include "sharedDebug/InstallTimer.h"
#include "sharedRandom/ConfigSharedRandom.h"
#include "sharedRandom/Random.h"
// ======================================================================
void SetupSharedRandom::install(uint32 seed)
{
InstallTimer const installTimer("SetupSharedRandom::install");
ConfigSharedRandom::install();
Random::install(seed);
}
// ======================================================================
@@ -0,0 +1,27 @@
// ======================================================================
//
// SetupSharedRandom.h
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_SetupSharedRandom_H
#define INCLUDED_SetupSharedRandom_H
// ======================================================================
class SetupSharedRandom
{
public:
static void install(uint32 seed);
private:
SetupSharedRandom();
SetupSharedRandom(const SetupSharedRandom &);
SetupSharedRandom &operator =(const SetupSharedRandom &);
};
// ======================================================================
#endif
@@ -0,0 +1,8 @@
// ======================================================================
//
// FirstRandom.cpp
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedRandom/FirstSharedRandom.h"