OSDN Git Service

Added new function break_fp().
authorLoRd_MuldeR <mulder2@gmx.de>
Sun, 1 Apr 2018 13:41:16 +0000 (15:41 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Sun, 1 Apr 2018 13:41:16 +0000 (15:41 +0200)
include/MUtils/Global.h
include/MUtils/Version.h
src/Global.cpp

index 937a319..956ca94 100644 (file)
@@ -86,6 +86,15 @@ class QProcess;
 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.
        *
        * The application's *Temp* folder is a unique application-specific folder, intended to store any temporary files that the application may need. It will be created when this function is called for the first time (lazy initialization); subsequent calls are guaranteed to return the same path. Usually the application's *Temp* folder will be created as a sub-folder of the system's global *Temp* folder, as indicated by the `TMP` or `TEMP` environment variables. However, it may be created at a different place (e.g. in the users *Profile* directory), if those environment variables don't point to a usable directory. In any case, this function makes sure that the application's *Temp* folder exists for the whole lifetime of the application and that it is writable. When the application terminates normally, the application's *Temp* folder and all files or sub-directories thereof will be *removed* automatically!
@@ -329,6 +338,15 @@ namespace MUtils
        */
        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
        namespace Internal
        {
index 9c6d11d..da4f1a5 100644 (file)
@@ -68,7 +68,7 @@ namespace MUtils
                                        #endif
                                #elif defined(_MSC_VER)
                                        #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";
                                                #else
                                                        #error Compiler version is not supported yet!
index 026ddff..2028e69 100644 (file)
 //Per-thread init 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
-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);
        b=b-c;  b=b-a;  b=b^(a <<  8); 
        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)
 {
        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());
-       qsrand(mix_function(clock(), time(NULL), seed));
+       const quint32 seed_0 = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
+       qsrand(mix_function(clock(), time(NULL), seed_0));
 }
 
 static quint32 rand_fallback(void)
 {
-       Q_ASSERT(RAND_MAX >= 0xFFF);
+       Q_ASSERT(RAND_MAX >= 0x7FFF);
+
        if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
        {
                seed_rand();
                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)
@@ -802,6 +807,17 @@ QStringList MUtils::available_codepages(const bool &noAliases)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// 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
 ///////////////////////////////////////////////////////////////////////////////