MUtilities development repository
修订版 | de57843e3a4a1965b8b8c99ac68225ad99233d3a (tree) |
---|---|
时间 | 2018-04-01 22:41:16 |
作者 | ![]() |
Commiter | LoRd_MuldeR |
Added new function break_fp().
@@ -86,6 +86,15 @@ class QProcess; | ||
86 | 86 | namespace MUtils |
87 | 87 | { |
88 | 88 | /** |
89 | + * \brief This struct containes the parts of a floating-point number | |
90 | + */ | |
91 | + typedef struct | |
92 | + { | |
93 | + double intpart, fractpart; | |
94 | + } | |
95 | + fp_parts_t; | |
96 | + | |
97 | + /** | |
89 | 98 | * \brief Rerieves the full path of the application's *Temp* folder. |
90 | 99 | * |
91 | 100 | * 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 | ||
329 | 338 | */ |
330 | 339 | MUTILS_API QStringList available_codepages(const bool &noAliases = true); |
331 | 340 | |
341 | + /** | |
342 | + * \brief Break floating-point number into fractional and integral parts | |
343 | + * | |
344 | + * \param value The original floating-point value | |
345 | + * | |
346 | + * \return Returns a struct containing the fractional and integral parts | |
347 | + */ | |
348 | + MUTILS_API fp_parts_t break_fp(const double value); | |
349 | + | |
332 | 350 | //Internal |
333 | 351 | namespace Internal |
334 | 352 | { |
@@ -68,7 +68,7 @@ namespace MUtils | ||
68 | 68 | #endif |
69 | 69 | #elif defined(_MSC_VER) |
70 | 70 | #if (_MSC_VER == 1913) |
71 | - #if((_MSC_FULL_VER >= 191326128) && (_MSC_FULL_VER <= 191326128)) | |
71 | + #if((_MSC_FULL_VER >= 191326128) && (_MSC_FULL_VER <= 191326129)) | |
72 | 72 | "MSVC 2017.6"; |
73 | 73 | #else |
74 | 74 | #error Compiler version is not supported yet! |
@@ -63,13 +63,12 @@ | ||
63 | 63 | //Per-thread init flag |
64 | 64 | static QThreadStorage<bool> g_srand_flag; |
65 | 65 | |
66 | +//32-Bit wrapper for qrand() | |
67 | +#define QRAND() ((static_cast<quint32>(qrand()) & 0xFFFF) | (static_cast<quint32>(qrand()) << 16U)) | |
68 | + | |
66 | 69 | //Robert Jenkins' 96 bit Mix Function |
67 | -static quint32 mix_function(const quint32 x, const quint32 y, const quint32 z) | |
70 | +static quint32 mix_function(quint32 a, quint32 b, quint32 c) | |
68 | 71 | { |
69 | - quint32 a = x; | |
70 | - quint32 b = y; | |
71 | - quint32 c = z; | |
72 | - | |
73 | 72 | a=a-b; a=a-c; a=a^(c >> 13); |
74 | 73 | b=b-c; b=b-a; b=b^(a << 8); |
75 | 74 | 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) | ||
86 | 85 | static void seed_rand(void) |
87 | 86 | { |
88 | 87 | QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time()); |
89 | - const quint32 seed = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch()); | |
90 | - qsrand(mix_function(clock(), time(NULL), seed)); | |
88 | + const quint32 seed_0 = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch()); | |
89 | + qsrand(mix_function(clock(), time(NULL), seed_0)); | |
91 | 90 | } |
92 | 91 | |
93 | 92 | static quint32 rand_fallback(void) |
94 | 93 | { |
95 | - Q_ASSERT(RAND_MAX >= 0xFFF); | |
94 | + Q_ASSERT(RAND_MAX >= 0x7FFF); | |
95 | + | |
96 | 96 | if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData())) |
97 | 97 | { |
98 | 98 | seed_rand(); |
99 | 99 | g_srand_flag.setLocalData(true); |
100 | 100 | } |
101 | - quint32 rnd = 0x32288EA3; | |
102 | - for (size_t i = 0; i < 3; i++) | |
101 | + | |
102 | + quint32 rnd_val = mix_function(0x32288EA3, clock(), time(NULL)); | |
103 | + | |
104 | + for (size_t i = 0; i < 42; i++) | |
103 | 105 | { |
104 | - rnd = (rnd << 12) ^ qrand(); | |
106 | + rnd_val = mix_function(rnd_val, QRAND(), QRAND()); | |
107 | + rnd_val = mix_function(QRAND(), rnd_val, QRAND()); | |
108 | + rnd_val = mix_function(QRAND(), QRAND(), rnd_val); | |
105 | 109 | } |
106 | - return rnd; | |
110 | + | |
111 | + return rnd_val; | |
107 | 112 | } |
108 | 113 | |
109 | 114 | quint32 MUtils::next_rand_u32(void) |
@@ -802,6 +807,17 @@ QStringList MUtils::available_codepages(const bool &noAliases) | ||
802 | 807 | } |
803 | 808 | |
804 | 809 | /////////////////////////////////////////////////////////////////////////////// |
810 | +// FP MATH SUPPORT | |
811 | +/////////////////////////////////////////////////////////////////////////////// | |
812 | + | |
813 | +MUtils::fp_parts_t MUtils::break_fp(const double value) | |
814 | +{ | |
815 | + fp_parts_t result; | |
816 | + result.fractpart = modf(value, &result.intpart); | |
817 | + return result; | |
818 | +} | |
819 | + | |
820 | +/////////////////////////////////////////////////////////////////////////////// | |
805 | 821 | // SELF-TEST |
806 | 822 | /////////////////////////////////////////////////////////////////////////////// |
807 | 823 |