Add an internal ASSERT macro
This macro has some advantages over the standard library one:
- It prints the place where the macro was triggered
- It "references" the condition even in Release to avoid warnings
- In release, if possible, it gives compiler hints
It is basically is stripped down version of the ASSERT macros I wrote
for the Daemon engine in src/common/Assert.h
This commit also removes the stray "backend" namespaces for common/
code.
diff --git a/src/common/Math.cpp b/src/common/Math.cpp
index ec9b3e9..0c6a7a6 100644
--- a/src/common/Math.cpp
+++ b/src/common/Math.cpp
@@ -14,53 +14,48 @@
#include "common/Math.h"
+#include "common/Assert.h"
+
#if defined(_WIN32) || defined(_WIN64)
#include <intrin.h>
#endif
-#include <cassert>
-#define ASSERT assert
+uint32_t ScanForward(uint32_t bits) {
+ ASSERT(bits != 0);
+ #if defined(_WIN32) || defined(_WIN64)
+ unsigned long firstBitIndex = 0ul;
+ unsigned char ret = _BitScanForward(&firstBitIndex, bits);
+ ASSERT(ret != 0);
+ return firstBitIndex;
+ #else
+ return static_cast<unsigned long>(__builtin_ctz(bits));
+ #endif
+}
-namespace backend {
+uint32_t Log2(uint32_t value) {
+ ASSERT(value != 0);
+ #if defined(_WIN32) || defined(_WIN64)
+ unsigned long firstBitIndex = 0ul;
+ unsigned char ret = _BitScanReverse(&firstBitIndex, value);
+ ASSERT(ret != 0);
+ return firstBitIndex;
+ #else
+ return 31 - __builtin_clz(value);
+ #endif
+}
- uint32_t ScanForward(uint32_t bits) {
- ASSERT(bits != 0);
- #if defined(_WIN32) || defined(_WIN64)
- unsigned long firstBitIndex = 0ul;
- unsigned char ret = _BitScanForward(&firstBitIndex, bits);
- ASSERT(ret != 0);
- return firstBitIndex;
- #else
- return static_cast<unsigned long>(__builtin_ctz(bits));
- #endif
- }
+bool IsPowerOfTwo(size_t n) {
+ ASSERT(n != 0);
+ return (n & (n - 1)) == 0;
+}
- uint32_t Log2(uint32_t value) {
- ASSERT(value != 0);
- #if defined(_WIN32) || defined(_WIN64)
- unsigned long firstBitIndex = 0ul;
- unsigned char ret = _BitScanReverse(&firstBitIndex, value);
- ASSERT(ret != 0);
- return firstBitIndex;
- #else
- return 31 - __builtin_clz(value);
- #endif
- }
+bool IsAligned(const void* ptr, size_t alignment) {
+ ASSERT(IsPowerOfTwo(alignment));
+ ASSERT(alignment != 0);
+ return (reinterpret_cast<intptr_t>(ptr) & (alignment - 1)) == 0;
+}
- bool IsPowerOfTwo(size_t n) {
- ASSERT(n != 0);
- return (n & (n - 1)) == 0;
- }
-
- bool IsAligned(const void* ptr, size_t alignment) {
- ASSERT(IsPowerOfTwo(alignment));
- ASSERT(alignment != 0);
- return (reinterpret_cast<intptr_t>(ptr) & (alignment - 1)) == 0;
- }
-
- void* AlignVoidPtr(void* ptr, size_t alignment) {
- ASSERT(alignment != 0);
- return reinterpret_cast<void*>((reinterpret_cast<intptr_t>(ptr) + (alignment - 1)) & ~(alignment - 1));
- }
-
+void* AlignVoidPtr(void* ptr, size_t alignment) {
+ ASSERT(alignment != 0);
+ return reinterpret_cast<void*>((reinterpret_cast<intptr_t>(ptr) + (alignment - 1)) & ~(alignment - 1));
}