Added new function break_fp().
This commit is contained in:
parent
d16ef6fcb2
commit
de57843e3a
@ -85,6 +85,15 @@ class QProcess;
|
|||||||
*/
|
*/
|
||||||
namespace MUtils
|
namespace MUtils
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* \brief This struct containes the parts of a floating-point number
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
double intpart, fractpart;
|
||||||
|
}
|
||||||
|
fp_parts_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Rerieves the full path of the application's *Temp* folder.
|
* \brief Rerieves the full path of the application's *Temp* folder.
|
||||||
*
|
*
|
||||||
@ -329,6 +338,15 @@ namespace MUtils
|
|||||||
*/
|
*/
|
||||||
MUTILS_API QStringList available_codepages(const bool &noAliases = true);
|
MUTILS_API QStringList available_codepages(const bool &noAliases = true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Break floating-point number into fractional and integral parts
|
||||||
|
*
|
||||||
|
* \param value The original floating-point value
|
||||||
|
*
|
||||||
|
* \return Returns a struct containing the fractional and integral parts
|
||||||
|
*/
|
||||||
|
MUTILS_API fp_parts_t break_fp(const double value);
|
||||||
|
|
||||||
//Internal
|
//Internal
|
||||||
namespace Internal
|
namespace Internal
|
||||||
{
|
{
|
||||||
|
@ -68,7 +68,7 @@ namespace MUtils
|
|||||||
#endif
|
#endif
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
#if (_MSC_VER == 1913)
|
#if (_MSC_VER == 1913)
|
||||||
#if((_MSC_FULL_VER >= 191326128) && (_MSC_FULL_VER <= 191326128))
|
#if((_MSC_FULL_VER >= 191326128) && (_MSC_FULL_VER <= 191326129))
|
||||||
"MSVC 2017.6";
|
"MSVC 2017.6";
|
||||||
#else
|
#else
|
||||||
#error Compiler version is not supported yet!
|
#error Compiler version is not supported yet!
|
||||||
|
@ -63,13 +63,12 @@
|
|||||||
//Per-thread init flag
|
//Per-thread init flag
|
||||||
static QThreadStorage<bool> g_srand_flag;
|
static QThreadStorage<bool> g_srand_flag;
|
||||||
|
|
||||||
|
//32-Bit wrapper for qrand()
|
||||||
|
#define QRAND() ((static_cast<quint32>(qrand()) & 0xFFFF) | (static_cast<quint32>(qrand()) << 16U))
|
||||||
|
|
||||||
//Robert Jenkins' 96 bit Mix Function
|
//Robert Jenkins' 96 bit Mix Function
|
||||||
static quint32 mix_function(const quint32 x, const quint32 y, const quint32 z)
|
static quint32 mix_function(quint32 a, quint32 b, quint32 c)
|
||||||
{
|
{
|
||||||
quint32 a = x;
|
|
||||||
quint32 b = y;
|
|
||||||
quint32 c = z;
|
|
||||||
|
|
||||||
a=a-b; a=a-c; a=a^(c >> 13);
|
a=a-b; a=a-c; a=a^(c >> 13);
|
||||||
b=b-c; b=b-a; b=b^(a << 8);
|
b=b-c; b=b-a; b=b^(a << 8);
|
||||||
c=c-a; c=c-b; c=c^(b >> 13);
|
c=c-a; c=c-b; c=c^(b >> 13);
|
||||||
@ -86,24 +85,30 @@ static quint32 mix_function(const quint32 x, const quint32 y, const quint32 z)
|
|||||||
static void seed_rand(void)
|
static void seed_rand(void)
|
||||||
{
|
{
|
||||||
QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
|
QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
|
||||||
const quint32 seed = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
|
const quint32 seed_0 = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
|
||||||
qsrand(mix_function(clock(), time(NULL), seed));
|
qsrand(mix_function(clock(), time(NULL), seed_0));
|
||||||
}
|
}
|
||||||
|
|
||||||
static quint32 rand_fallback(void)
|
static quint32 rand_fallback(void)
|
||||||
{
|
{
|
||||||
Q_ASSERT(RAND_MAX >= 0xFFF);
|
Q_ASSERT(RAND_MAX >= 0x7FFF);
|
||||||
|
|
||||||
if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
|
if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
|
||||||
{
|
{
|
||||||
seed_rand();
|
seed_rand();
|
||||||
g_srand_flag.setLocalData(true);
|
g_srand_flag.setLocalData(true);
|
||||||
}
|
}
|
||||||
quint32 rnd = 0x32288EA3;
|
|
||||||
for (size_t i = 0; i < 3; i++)
|
quint32 rnd_val = mix_function(0x32288EA3, clock(), time(NULL));
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 42; i++)
|
||||||
{
|
{
|
||||||
rnd = (rnd << 12) ^ qrand();
|
rnd_val = mix_function(rnd_val, QRAND(), QRAND());
|
||||||
|
rnd_val = mix_function(QRAND(), rnd_val, QRAND());
|
||||||
|
rnd_val = mix_function(QRAND(), QRAND(), rnd_val);
|
||||||
}
|
}
|
||||||
return rnd;
|
|
||||||
|
return rnd_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 MUtils::next_rand_u32(void)
|
quint32 MUtils::next_rand_u32(void)
|
||||||
@ -801,6 +806,17 @@ QStringList MUtils::available_codepages(const bool &noAliases)
|
|||||||
return codecList;
|
return codecList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// FP MATH SUPPORT
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
MUtils::fp_parts_t MUtils::break_fp(const double value)
|
||||||
|
{
|
||||||
|
fp_parts_t result;
|
||||||
|
result.fractpart = modf(value, &result.intpart);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// SELF-TEST
|
// SELF-TEST
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user