mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Removed alignment macros, and added functions instead.
This commit is contained in:
@@ -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__)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user