From 4fb75d220aeaa66d3e907cc2311acf6c29433800 Mon Sep 17 00:00:00 2001
From: archshift <admin@archshift.com>
Date: Wed, 18 Feb 2015 22:46:21 -0800
Subject: [PATCH] Misc cleanup of common and related functions

---
 src/common/common.h            | 72 +++++++++++-----------------------
 src/common/common_funcs.h      | 23 -----------
 src/common/hash.cpp            | 12 +++---
 src/core/hle/kernel/thread.cpp |  5 ++-
 4 files changed, 31 insertions(+), 81 deletions(-)

diff --git a/src/common/common.h b/src/common/common.h
index f5f2f8c34..948dc536a 100644
--- a/src/common/common.h
+++ b/src/common/common.h
@@ -35,13 +35,11 @@ private:
 #ifdef __APPLE__
 // The Darwin ABI requires that stack frames be aligned to 16-byte boundaries.
 // This is only needed on i386 gcc - x86_64 already aligns to 16 bytes.
-#if defined __i386__ && defined __GNUC__
-#undef STACKALIGN
-#define STACKALIGN __attribute__((__force_align_arg_pointer__))
-#endif
-
+    #if defined __i386__ && defined __GNUC__
+        #undef STACKALIGN
+        #define STACKALIGN __attribute__((__force_align_arg_pointer__))
+    #endif
 #elif defined _WIN32
-
 // Check MSC ver
     #if defined _MSC_VER && _MSC_VER <= 1000
         #error needs at least version 1000 of MSC
@@ -51,9 +49,6 @@ private:
     #define NOMINMAX
     #endif
 
-// Memory leak checks
-    #define CHECK_HEAP_INTEGRITY()
-
 // Alignment
     #define MEMORY_ALIGNED16(x) __declspec(align(16)) x
     #define MEMORY_ALIGNED32(x) __declspec(align(32)) x
@@ -61,57 +56,34 @@ private:
     #define MEMORY_ALIGNED128(x) __declspec(align(128)) x
     #define MEMORY_ALIGNED16_DECL(x) __declspec(align(16)) x
     #define MEMORY_ALIGNED64_DECL(x) __declspec(align(64)) x
-
-// Since they are always around on windows
-    #define HAVE_WX 1
-    #define HAVE_OPENAL 1
-
-    #define HAVE_PORTAUDIO 1
-
-// Debug definitions
-    #if defined(_DEBUG)
-        #include <crtdbg.h>
-        #undef CHECK_HEAP_INTEGRITY
-        #define CHECK_HEAP_INTEGRITY() {if (!_CrtCheckMemory()) PanicAlert("memory corruption detected. see log.");}
-        // If you want to see how much a pain in the ass singletons are, for example:
-        // {614} normal block at 0x030C5310, 188 bytes long.
-        // Data: <Master Log      > 4D 61 73 74 65 72 20 4C 6F 67 00 00 00 00 00 00
-        struct CrtDebugBreak { CrtDebugBreak(int spot) { _CrtSetBreakAlloc(spot); } };
-        //CrtDebugBreak breakAt(614);
-    #endif // end DEBUG/FAST
-
 #endif
 
 // Windows compatibility
 #ifndef _WIN32
-#ifdef _LP64
-#define _M_X64 1
-#else
-#define _M_IX86 1
-#endif
-#define __forceinline inline __attribute__((always_inline))
-#define MEMORY_ALIGNED16(x) __attribute__((aligned(16))) x
-#define MEMORY_ALIGNED32(x) __attribute__((aligned(32))) x
-#define MEMORY_ALIGNED64(x) __attribute__((aligned(64))) x
-#define MEMORY_ALIGNED128(x) __attribute__((aligned(128))) x
-#define MEMORY_ALIGNED16_DECL(x) __attribute__((aligned(16))) x
-#define MEMORY_ALIGNED64_DECL(x) __attribute__((aligned(64))) x
+    #ifdef _LP64
+        #define _M_X64 1
+    #else
+        #define _M_IX86 1
+    #endif
+    #define __forceinline inline __attribute__((always_inline))
+    #define MEMORY_ALIGNED16(x) __attribute__((aligned(16))) x
+    #define MEMORY_ALIGNED32(x) __attribute__((aligned(32))) x
+    #define MEMORY_ALIGNED64(x) __attribute__((aligned(64))) x
+    #define MEMORY_ALIGNED128(x) __attribute__((aligned(128))) x
+    #define MEMORY_ALIGNED16_DECL(x) __attribute__((aligned(16))) x
+    #define MEMORY_ALIGNED64_DECL(x) __attribute__((aligned(64))) x
 #endif
 
 #ifdef _MSC_VER
-#define __strdup _strdup
-#define __getcwd _getcwd
-#define __chdir _chdir
+    #define __strdup _strdup
+    #define __getcwd _getcwd
+    #define __chdir _chdir
 #else
-#define __strdup strdup
-#define __getcwd getcwd
-#define __chdir chdir
+    #define __strdup strdup
+    #define __getcwd getcwd
+    #define __chdir chdir
 #endif
 
-// Dummy macro for marking translatable strings that can not be immediately translated.
-// wxWidgets does not have a true dummy macro for this.
-#define _trans(a) a
-
 #if defined _M_GENERIC
 #  define _M_SSE 0x0
 #elif defined __GNUC__
diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h
index daec8f0ff..bc296ed3e 100644
--- a/src/common/common_funcs.h
+++ b/src/common/common_funcs.h
@@ -24,11 +24,6 @@ template<> struct CompileTimeAssert<true> {};
 #define b32(x)  (b16(x) | (b16(x) >>16) )
 #define ROUND_UP_POW2(x)    (b32(x - 1) + 1)
 
-#define MIN(a, b)   ((a)<(b)?(a):(b))
-#define MAX(a, b)   ((a)>(b)?(a):(b))
-
-#define CLAMP(x, min, max)  (((x) > max) ? max : (((x) < min) ? min : (x)))
-
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
 /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
@@ -147,15 +142,6 @@ inline u64 _rotr64(u64 x, unsigned int shift){
     #define Crash() {DebugBreak();}
 #endif // _MSC_VER ndef
 
-// Dolphin's min and max functions
-#undef min
-#undef max
-
-template<class T>
-inline T min(const T& a, const T& b) {return a > b ? b : a;}
-template<class T>
-inline T max(const T& a, const T& b) {return a > b ? a : b;}
-
 // Generic function to get last error message.
 // Call directly after the command or use the error num.
 // This function might change the error code.
@@ -232,13 +218,4 @@ inline void swap<8>(u8* data)
     *reinterpret_cast<u64*>(data) = swap64(data);
 }
 
-template <typename T>
-inline T FromBigEndian(T data)
-{
-    //static_assert(std::is_arithmetic<T>::value, "function only makes sense with arithmetic types");
-
-    swap<sizeof(data)>(reinterpret_cast<u8*>(&data));
-    return data;
-}
-
 }  // Namespace Common
diff --git a/src/common/hash.cpp b/src/common/hash.cpp
index 50c7cb5f9..0624dab8d 100644
--- a/src/common/hash.cpp
+++ b/src/common/hash.cpp
@@ -156,7 +156,7 @@ u64 GetMurmurHash3(const u8 *src, int len, u32 samples)
     const u8 * data = (const u8*)src;
     const int nblocks = len / 16;
     u32 Step = (len / 8);
-    if(samples == 0) samples = max(Step, 1u);
+    if(samples == 0) samples = std::max(Step, 1u);
     Step = Step / samples;
     if(Step < 1) Step = 1;
 
@@ -234,7 +234,7 @@ u64 GetCRC32(const u8 *src, int len, u32 samples)
     u32 Step = (len / 8);
     const u64 *data = (const u64 *)src;
     const u64 *end = data + Step;
-    if(samples == 0) samples = max(Step, 1u);
+    if(samples == 0) samples = std::max(Step, 1u);
     Step = Step / samples;
     if(Step < 1) Step = 1;
     while(data < end)
@@ -266,7 +266,7 @@ u64 GetHashHiresTexture(const u8 *src, int len, u32 samples)
     u32 Step = (len / 8);
     const u64 *data = (const u64 *)src;
     const u64 *end = data + Step;
-    if(samples == 0) samples = max(Step, 1u);
+    if(samples == 0) samples = std::max(Step, 1u);
     Step = Step / samples;
     if(Step < 1) Step = 1;
     while(data < end)
@@ -309,7 +309,7 @@ u64 GetCRC32(const u8 *src, int len, u32 samples)
     u32 Step = (len/4);
     const u32 *data = (const u32 *)src;
     const u32 *end = data + Step;
-    if(samples == 0) samples = max(Step, 1u);
+    if(samples == 0) samples = std::max(Step, 1u);
     Step  = Step / samples;
     if(Step < 1) Step = 1;
     while(data < end)
@@ -381,7 +381,7 @@ u64 GetMurmurHash3(const u8* src, int len, u32 samples)
     u32 out[2];
     const int nblocks = len / 8;
     u32 Step = (len / 4);
-    if(samples == 0) samples = max(Step, 1u);
+    if(samples == 0) samples = std::max(Step, 1u);
     Step = Step / samples;
     if(Step < 1) Step = 1;
 
@@ -457,7 +457,7 @@ u64 GetHashHiresTexture(const u8 *src, int len, u32 samples)
     u32 Step = (len / 8);
     const u64 *data = (const u64 *)src;
     const u64 *end = data + Step;
-    if(samples == 0) samples = max(Step, 1u);
+    if(samples == 0) samples = std::max(Step, 1u);
     Step = Step / samples;
     if(Step < 1) Step = 1;
     while(data < end)
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index f8c834a8d..be1aed615 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -7,6 +7,7 @@
 #include <vector>
 
 #include "common/common.h"
+#include "common/math_util.h"
 #include "common/thread_queue_list.h"
 
 #include "core/arm/arm_interface.h"
@@ -339,7 +340,7 @@ static void DebugThreadQueue() {
 ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, s32 priority,
         u32 arg, s32 processor_id, VAddr stack_top) {
     if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
-        s32 new_priority = CLAMP(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
+        s32 new_priority = MathUtil::Clamp<s32>(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
         LOG_WARNING(Kernel_SVC, "(name=%s): invalid priority=%d, clamping to %d",
             name.c_str(), priority, new_priority);
         // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
@@ -387,7 +388,7 @@ static void ClampPriority(const Thread* thread, s32* priority) {
     if (*priority < THREADPRIO_HIGHEST || *priority > THREADPRIO_LOWEST) {
         DEBUG_ASSERT_MSG(false, "Application passed an out of range priority. An error should be returned.");
 
-        s32 new_priority = CLAMP(*priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
+        s32 new_priority = MathUtil::Clamp<s32>(*priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
         LOG_WARNING(Kernel_SVC, "(name=%s): invalid priority=%d, clamping to %d",
                     thread->name.c_str(), *priority, new_priority);
         // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm