Removed alignment macros, and added functions instead.

This commit is contained in:
Бранимир Караџић
2020-04-19 18:22:33 -07:00
parent f6b1c97cb9
commit 39711f53df
8 changed files with 137 additions and 30 deletions

View File

@@ -7,6 +7,7 @@
#define BX_ALLOCATOR_H_HEADER_GUARD
#include "bx.h"
#include "uint32_t.h"
#if BX_CONFIG_ALLOCATOR_DEBUG
# define BX_ALLOC(_allocator, _size) bx::alloc(_allocator, _size, 0, __FILE__, __LINE__)

View File

@@ -22,20 +22,12 @@ namespace bx
{
}
inline bool isAligned(const void* _ptr, size_t _align)
{
union { const void* ptr; uintptr_t addr; } un;
un.ptr = _ptr;
return 0 == (un.addr & (_align-1) );
}
inline void* alignPtr(void* _ptr, size_t _extra, size_t _align)
{
union { void* ptr; uintptr_t addr; } un;
un.ptr = _ptr;
uintptr_t unaligned = un.addr + _extra; // space for header
uintptr_t mask = _align-1;
uintptr_t aligned = BX_ALIGN_MASK(unaligned, mask);
uintptr_t aligned = bx::alignUp(unaligned, _align);
un.addr = aligned;
return un.ptr;
}

View File

@@ -239,7 +239,7 @@ namespace bx
if (0 < morecore)
{
morecore = BX_ALIGN_MASK(morecore, 0xfff);
morecore = alignUp(morecore, 0x1000);
m_data = (uint8_t*)m_memBlock->more(morecore);
m_size = m_memBlock->getSize();
}

View File

@@ -687,6 +687,72 @@ namespace bx
return result;
}
template <typename Ty>
inline bool isAligned(Ty _a, int32_t _align)
{
const Ty mask = _align - 1;
return 0 == (_a & mask);
}
template <typename Ty>
inline bool isAligned(const Ty* _ptr, int32_t _align)
{
union { const void* ptr; uintptr_t addr; } un = { _ptr };
return isAligned(un.addr, _align);
}
template <typename Ty>
inline bool isAligned(Ty* _ptr, int32_t _align)
{
return isAligned( (const void*)_ptr, _align);
}
template <typename Ty>
inline Ty alignDown(Ty _a, int32_t _align)
{
const Ty mask = _align - 1;
return Ty(_a & ~mask);
}
template <typename Ty>
inline Ty* alignDown(Ty* _ptr, int32_t _align)
{
union { Ty* ptr; uintptr_t addr; } un = { _ptr };
un.addr = alignDown(un.addr, _align);
return un.ptr;
}
template <typename Ty>
inline const Ty* alignDown(const Ty* _ptr, int32_t _align)
{
union { const Ty* ptr; uintptr_t addr; } un = { _ptr };
un.addr = alignDown(un.addr, _align);
return un.ptr;
}
template <typename Ty>
inline Ty alignUp(Ty _a, int32_t _align)
{
const Ty mask = _align - 1;
return Ty( (_a + mask) & ~mask);
}
template <typename Ty>
inline Ty* alignUp(Ty* _ptr, int32_t _align)
{
union { Ty* ptr; uintptr_t addr; } un = { _ptr };
un.addr = alignUp(un.addr, _align);
return un.ptr;
}
template <typename Ty>
inline const Ty* alignUp(const Ty* _ptr, int32_t _align)
{
union { const Ty* ptr; uintptr_t addr; } un = { _ptr };
un.addr = alignUp(un.addr, _align);
return un.ptr;
}
inline BX_CONST_FUNC uint16_t halfFromFloat(float _a)
{
union { uint32_t ui; float flt; } ftou;

View File

@@ -42,12 +42,6 @@
///
#define BX_FILE_LINE_LITERAL "" __FILE__ "(" BX_STRINGIZE(__LINE__) "): "
///
#define BX_ALIGN_MASK(_value, _mask) ( ( (_value)+(_mask) ) & ( (~0)&(~(_mask) ) ) )
#define BX_ALIGN_16(_value) BX_ALIGN_MASK(_value, 0xf)
#define BX_ALIGN_256(_value) BX_ALIGN_MASK(_value, 0xff)
#define BX_ALIGN_4096(_value) BX_ALIGN_MASK(_value, 0xfff)
///
#define BX_ALIGNOF(_type) __alignof(_type)

View File

@@ -251,6 +251,42 @@ namespace bx
template<uint32_t Min>
BX_CONSTEXPR_FUNC uint32_t strideAlign(uint32_t _offset, uint32_t _stride);
///
template <typename Ty>
bool isAligned(Ty _a, int32_t _align);
///
template <typename Ty>
bool isAligned(void* _ptr, int32_t _align);
///
template <typename Ty>
bool isAligned(const void* _ptr, int32_t _align);
///
template <typename Ty>
Ty alignDown(Ty _a, int32_t _align);
///
template <typename Ty>
Ty* alignDown(Ty* _ptr, int32_t _align);
///
template <typename Ty>
const Ty* alignDown(const Ty* _ptr, int32_t _align);
///
template <typename Ty>
Ty alignUp(Ty _a, int32_t _align);
///
template <typename Ty>
Ty* alignUp(Ty* _ptr, int32_t _align);
///
template <typename Ty>
const Ty* alignUp(const Ty* _ptr, int32_t _align);
/// Convert float to half-float.
///
BX_CONST_FUNC uint16_t halfFromFloat(float _a);