Cleaning up asserts and file/line macros.

This commit is contained in:
Ben Vanik
2014-07-12 16:51:52 -07:00
parent 840357413c
commit bf882714d0
92 changed files with 636 additions and 613 deletions

77
src/poly/assert.h Normal file
View File

@@ -0,0 +1,77 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_ASSERT_H_
#define POLY_ASSERT_H_
#include <assert.h>
#include <poly/config.h>
#include <poly/platform.h>
namespace poly {
#define static_assert_size(type, size) \
static_assert(sizeof(type) == size, \
"bad definition for "## #type##": must be "## #size##" bytes")
// We rely on assert being compiled out in NDEBUG.
#define poly_assert assert
#define __POLY_EXPAND(x) x
#define __POLY_ARGC(...) \
__POLY_EXPAND(__POLY_ARGC_IMPL(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, \
6, 5, 4, 3, 2, 1, 0))
#define __POLY_ARGC_IMPL(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
x13, x14, x15, N, ...) \
N
#define __POLY_MACRO_DISPATCH(func, ...) \
__POLY_MACRO_DISPATCH_(func, __POLY_ARGC(__VA_ARGS__))
#define __POLY_MACRO_DISPATCH_(func, nargs) __POLY_MACRO_DISPATCH__(func, nargs)
#define __POLY_MACRO_DISPATCH__(func, nargs) func##nargs
#define assert_always(...) poly_assert(false)
#define assert_true(...) \
__POLY_MACRO_DISPATCH(assert_true, __VA_ARGS__)(__VA_ARGS__)
#define assert_true1(expr) poly_assert(expr)
#define assert_true2(expr, message) poly_assert((expr) || !message)
#define assert_false(...) \
__POLY_MACRO_DISPATCH(assert_false, __VA_ARGS__)(__VA_ARGS__)
#define assert_false1(expr) poly_assert(!(expr))
#define assert_false2(expr, message) poly_assert(!(expr) || !message)
#define assert_zero(...) \
__POLY_MACRO_DISPATCH(assert_zero, __VA_ARGS__)(__VA_ARGS__)
#define assert_zero1(expr) poly_assert((expr) == 0)
#define assert_zero2(expr, message) poly_assert((expr) == 0 || !message)
#define assert_not_zero(...) \
__POLY_MACRO_DISPATCH(assert_not_zero, __VA_ARGS__)(__VA_ARGS__)
#define assert_not_zero1(expr) poly_assert((expr) != 0)
#define assert_not_zero2(expr, message) poly_assert((expr) != 0 || !message)
#define assert_null(...) \
__POLY_MACRO_DISPATCH(assert_null, __VA_ARGS__)(__VA_ARGS__)
#define assert_null1(expr) poly_assert((expr) == nullptr)
#define assert_null2(expr, message) poly_assert((expr) == nullptr || !message)
#define assert_not_null(...) \
__POLY_MACRO_DISPATCH(assert_not_null, __VA_ARGS__)(__VA_ARGS__)
#define assert_not_null1(expr) poly_assert((expr) != nullptr)
#define assert_not_null2(expr, message) \
poly_assert((expr) != nullptr || !message)
#define assert_unhandled_case(variable) \
assert_always("unhandled switch("## #variable##") case")
} // namespace poly
#endif // POLY_ASSERT_H_

View File

@@ -11,7 +11,7 @@
#define POLY_CONFIG_H_
#if defined(DEBUG) || defined(_DEBUG)
#define XE_DEBUG 1
#define XE_DEBUG 1
#endif // DEBUG
#endif // POLY_CONFIG_H_

View File

@@ -16,6 +16,7 @@
// http://en.cppreference.com/w/cpp/language/storage_duration
#if XE_COMPILER_MSVC
// VC++2014 may have this.
#define _ALLOW_KEYWORD_MACROS 1
#define thread_local __declspec(thread)
#elif XE_LIKE_OSX
// Clang supports it on OSX but the runtime doesn't.

View File

@@ -25,15 +25,25 @@ namespace poly {
// return value is the size of the input operand (8, 16, 32, or 64). If the most
// significant bit of value is one, the return value is zero.
#if XE_COMPILER_MSVC
inline uint8_t lzcnt(uint8_t v) { return static_cast<uint8_t>(__lzcnt16(v) - 8); }
inline uint8_t lzcnt(uint8_t v) {
return static_cast<uint8_t>(__lzcnt16(v) - 8);
}
inline uint8_t lzcnt(uint16_t v) { return static_cast<uint8_t>(__lzcnt16(v)); }
inline uint8_t lzcnt(uint32_t v) { return static_cast<uint8_t>(__lzcnt(v)); }
inline uint8_t lzcnt(uint64_t v) { return static_cast<uint8_t>(__lzcnt64(v)); }
#else
inline uint8_t lzcnt(uint8_t v) { return static_cast<uint8_t>(__builtin_clzs(v) - 8); }
inline uint8_t lzcnt(uint16_t v) { return static_cast<uint8_t>(__builtin_clzs(v)); }
inline uint8_t lzcnt(uint32_t v) { return static_cast<uint8_t>(__builtin_clz(v)); }
inline uint8_t lzcnt(uint64_t v) { return static_cast<uint8_t>(__builtin_clzll(v)); }
inline uint8_t lzcnt(uint8_t v) {
return static_cast<uint8_t>(__builtin_clzs(v) - 8);
}
inline uint8_t lzcnt(uint16_t v) {
return static_cast<uint8_t>(__builtin_clzs(v));
}
inline uint8_t lzcnt(uint32_t v) {
return static_cast<uint8_t>(__builtin_clz(v));
}
inline uint8_t lzcnt(uint64_t v) {
return static_cast<uint8_t>(__builtin_clzll(v));
}
#endif // XE_COMPILER_MSVC
inline uint8_t lzcnt(int8_t v) { return lzcnt(static_cast<uint8_t>(v)); }
inline uint8_t lzcnt(int16_t v) { return lzcnt(static_cast<uint16_t>(v)); }
@@ -49,7 +59,8 @@ inline bool bit_scan_forward(uint32_t v, uint32_t* out_first_set_index) {
return _BitScanForward(reinterpret_cast<DWORD*>(out_first_set_index), v) != 0;
}
inline bool bit_scan_forward(uint64_t v, uint32_t* out_first_set_index) {
return _BitScanForward64(reinterpret_cast<DWORD*>(out_first_set_index), v) != 0;
return _BitScanForward64(reinterpret_cast<DWORD*>(out_first_set_index), v) !=
0;
}
#else
inline bool bit_scan_forward(uint32_t v, uint32_t* out_first_set_index) {

View File

@@ -10,6 +10,7 @@
#ifndef POLY_POLY_H_
#define POLY_POLY_H_
#include <poly/assert.h>
#include <poly/config.h>
#include <poly/cxx_compat.h>
#include <poly/math.h>

View File

@@ -1,9 +1,11 @@
# Copyright 2014 Ben Vanik. All Rights Reserved.
{
'sources': [
'assert.h',
'config.h',
'cxx_compat.h',
'math.h',
'platform.h',
'poly-private.h',
'poly.cc',
'poly.h',