mirror of https://github.com/f4exb/sdrangel.git
qrtplib: simplify random functions
This commit is contained in:
parent
afe3fbabd3
commit
e86e2f25a2
|
@ -38,18 +38,11 @@
|
||||||
#include "rtprandomrands.h"
|
#include "rtprandomrands.h"
|
||||||
#include "rtprandomurandom.h"
|
#include "rtprandomurandom.h"
|
||||||
#include "rtprandomrand48.h"
|
#include "rtprandomrand48.h"
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#ifndef WIN32
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#else
|
|
||||||
#ifndef _WIN32_WCE
|
#include <QDateTime>
|
||||||
#include <process.h>
|
|
||||||
#else
|
|
||||||
#include <windows.h>
|
|
||||||
#include <kfuncs.h>
|
|
||||||
#endif // _WIN32_WINCE
|
|
||||||
#include <stdlib.h>
|
|
||||||
#endif // WIN32
|
|
||||||
|
|
||||||
namespace qrtplib
|
namespace qrtplib
|
||||||
{
|
{
|
||||||
|
@ -57,39 +50,21 @@ namespace qrtplib
|
||||||
uint32_t RTPRandom::PickSeed()
|
uint32_t RTPRandom::PickSeed()
|
||||||
{
|
{
|
||||||
uint32_t x;
|
uint32_t x;
|
||||||
#if defined(WIN32) || defined(_WIN32_WINCE)
|
|
||||||
#ifndef _WIN32_WCE
|
|
||||||
x = (uint32_t)_getpid();
|
|
||||||
x += (uint32_t)time(0);
|
|
||||||
x += (uint32_t)clock();
|
|
||||||
#else
|
|
||||||
x = (uint32_t)GetCurrentProcessId();
|
|
||||||
|
|
||||||
FILETIME ft;
|
|
||||||
SYSTEMTIME st;
|
|
||||||
|
|
||||||
GetSystemTime(&st);
|
|
||||||
SystemTimeToFileTime(&st,&ft);
|
|
||||||
|
|
||||||
x += ft.dwLowDateTime;
|
|
||||||
#endif // _WIN32_WCE
|
|
||||||
x ^= (uint32_t)((uint8_t *)this - (uint8_t *)0);
|
|
||||||
#else
|
|
||||||
x = (uint32_t) getpid();
|
x = (uint32_t) getpid();
|
||||||
x += (uint32_t) time(0);
|
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||||
x += (uint32_t) clock();
|
x += currentDateTime.toTime_t();
|
||||||
x ^= (uint32_t) ((uint8_t *) this - (uint8_t *) 0);
|
#if defined(WIN32)
|
||||||
|
x += QDateTime::currentMSecsSinceEpoch() % 1000;
|
||||||
|
#else
|
||||||
|
x += (uint32_t)clock();
|
||||||
#endif
|
#endif
|
||||||
|
x ^= (uint32_t)((uint8_t *)this - (uint8_t *)0);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
RTPRandom *RTPRandom::CreateDefaultRandomNumberGenerator()
|
RTPRandom *RTPRandom::CreateDefaultRandomNumberGenerator()
|
||||||
{
|
{
|
||||||
#ifdef RTP_HAVE_RAND_S
|
|
||||||
RTPRandomRandS *r = new RTPRandomRandS();
|
|
||||||
#else
|
|
||||||
RTPRandomURandom *r = new RTPRandomURandom();
|
RTPRandomURandom *r = new RTPRandomURandom();
|
||||||
#endif // RTP_HAVE_RAND_S
|
|
||||||
RTPRandom *rRet = r;
|
RTPRandom *rRet = r;
|
||||||
|
|
||||||
if (r->Init() < 0) // fall back to rand48
|
if (r->Init() < 0) // fall back to rand48
|
||||||
|
|
|
@ -51,12 +51,7 @@ RTPRandomRand48::~RTPRandomRand48()
|
||||||
|
|
||||||
void RTPRandomRand48::SetSeed(uint32_t seed)
|
void RTPRandomRand48::SetSeed(uint32_t seed)
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
state = ((uint64_t)seed) << 16 | 0x330Eui64;
|
|
||||||
#else
|
|
||||||
state = ((uint64_t) seed) << 16 | 0x330EULL;
|
state = ((uint64_t) seed) << 16 | 0x330EULL;
|
||||||
#endif // RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t RTPRandomRand48::GetRandom8()
|
uint8_t RTPRandomRand48::GetRandom8()
|
||||||
|
@ -75,16 +70,8 @@ uint16_t RTPRandomRand48::GetRandom16()
|
||||||
|
|
||||||
uint32_t RTPRandomRand48::GetRandom32()
|
uint32_t RTPRandomRand48::GetRandom32()
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
state = ((0x5DEECE66Dui64*state) + 0xBui64)&0x0000ffffffffffffui64;
|
|
||||||
|
|
||||||
uint32_t x = (uint32_t)((state>>16)&0xffffffffui64);
|
|
||||||
#else
|
|
||||||
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
|
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
|
||||||
|
|
||||||
uint32_t x = (uint32_t) ((state >> 16) & 0xffffffffULL);
|
uint32_t x = (uint32_t) ((state >> 16) & 0xffffffffULL);
|
||||||
#endif // RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
@ -92,15 +79,8 @@ uint32_t RTPRandomRand48::GetRandom32()
|
||||||
double RTPRandomRand48::GetRandomDouble()
|
double RTPRandomRand48::GetRandomDouble()
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
state = ((0x5DEECE66Dui64*state) + 0xBui64)&0x0000ffffffffffffui64;
|
|
||||||
|
|
||||||
int64_t x = (int64_t)state;
|
|
||||||
#else
|
|
||||||
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
|
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
|
||||||
|
|
||||||
int64_t x = (int64_t) state;
|
int64_t x = (int64_t) state;
|
||||||
#endif // RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
|
|
||||||
double y = 3.552713678800500929355621337890625e-15 * (double) x;
|
double y = 3.552713678800500929355621337890625e-15 * (double) x;
|
||||||
return y;
|
return y;
|
||||||
|
|
|
@ -116,17 +116,11 @@ double RTPRandomURandom::GetRandomDouble()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
value &= 0x7fffffffffffffffui64;
|
|
||||||
#else
|
|
||||||
value &= 0x7fffffffffffffffULL;
|
value &= 0x7fffffffffffffffULL;
|
||||||
#endif // RTP_HAVE_VSUINT64SUFFIX
|
|
||||||
|
|
||||||
int64_t value2 = (int64_t) value;
|
int64_t value2 = (int64_t) value;
|
||||||
double x = RTPRANDOM_2POWMIN63 * (double) value2;
|
double x = RTPRANDOM_2POWMIN63 * (double) value2;
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace
|
} // end namespace
|
||||||
|
|
Loading…
Reference in New Issue