mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-13 23:26:24 +00:00
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.
This commit is contained in:
committed by
Corentin Wallez
parent
bd0594bab8
commit
fd589f3919
@@ -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
|
||||
|
||||
namespace backend {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 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));
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user