From 35a7dc47f8cb18a1b47a18e3981ca98caf4830d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Tue, 19 Nov 2024 23:32:38 -0800 Subject: [PATCH] Cleanup. --- include/bx/file.h | 8 +++---- include/bx/inline/allocator.inl | 2 +- include/bx/inline/bx.inl | 4 ++-- include/bx/inline/handlealloc.inl | 4 ++-- include/bx/inline/math.inl | 38 +++++++++++++++++++++++------- include/bx/inline/readerwriter.inl | 14 +++++------ include/bx/inline/rng.inl | 16 ++++++------- include/bx/inline/spscqueue.inl | 4 ++-- include/bx/inline/string.inl | 28 +++++++++++----------- include/bx/math.h | 16 +++++++++++++ include/bx/rng.h | 6 ++--- include/bx/string.h | 2 +- 12 files changed, 89 insertions(+), 53 deletions(-) diff --git a/include/bx/file.h b/include/bx/file.h index 93932b9..8d2789b 100644 --- a/include/bx/file.h +++ b/include/bx/file.h @@ -124,19 +124,19 @@ namespace bx /// Creates a directory named `_filePath`. /// - bool make(const FilePath& _filePath, Error* _err = bx::ErrorIgnore{}); + bool make(const FilePath& _filePath, Error* _err = ErrorIgnore{}); /// Creates a directory named `_filePath` along with all necessary parents. /// - bool makeAll(const FilePath& _filePath, Error* _err = bx::ErrorIgnore{}); + bool makeAll(const FilePath& _filePath, Error* _err = ErrorIgnore{}); /// Removes file or directory. /// - bool remove(const FilePath& _filePath, Error* _err = bx::ErrorIgnore{}); + bool remove(const FilePath& _filePath, Error* _err = ErrorIgnore{}); /// Removes file or directory recursively. /// - bool removeAll(const FilePath& _filePath, Error* _err = bx::ErrorIgnore{}); + bool removeAll(const FilePath& _filePath, Error* _err = ErrorIgnore{}); } // namespace bx diff --git a/include/bx/inline/allocator.inl b/include/bx/inline/allocator.inl index 646ebbc..60d3ca3 100644 --- a/include/bx/inline/allocator.inl +++ b/include/bx/inline/allocator.inl @@ -26,7 +26,7 @@ namespace bx { const uintptr_t addr = bitCast(_ptr); const uintptr_t unaligned = addr + _extra; // space for header - const uintptr_t aligned = bx::alignUp(unaligned, int32_t(_align) ); + const uintptr_t aligned = alignUp(unaligned, int32_t(_align) ); return bitCast(aligned); } diff --git a/include/bx/inline/bx.inl b/include/bx/inline/bx.inl index 2fdd8ca..f2c1885 100644 --- a/include/bx/inline/bx.inl +++ b/include/bx/inline/bx.inl @@ -96,13 +96,13 @@ namespace bx template inline constexpr Ty max() { - return bx::LimitsT::max; + return LimitsT::max; } template inline constexpr Ty min() { - return bx::LimitsT::min; + return LimitsT::min; } template diff --git a/include/bx/inline/handlealloc.inl b/include/bx/inline/handlealloc.inl index 57c904e..613a624 100644 --- a/include/bx/inline/handlealloc.inl +++ b/include/bx/inline/handlealloc.inl @@ -103,14 +103,14 @@ namespace bx inline HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles) { - uint8_t* ptr = (uint8_t*)bx::alloc(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) ); + uint8_t* ptr = (uint8_t*)alloc(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) ); return BX_PLACEMENT_NEW(ptr, HandleAlloc)(_maxHandles); } inline void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc) { _handleAlloc->~HandleAlloc(); - bx::free(_allocator, _handleAlloc); + free(_allocator, _handleAlloc); } template diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index a0306bd..a869943 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -366,12 +366,12 @@ namespace bx return countTrailingZeros(_val); } - template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(uint8_t _val) { return bx::min(8, countTrailingZeros(_val) ); } - template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int8_t _val) { return countTrailingZeros(_val); } - template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(uint16_t _val) { return bx::min(16, countTrailingZeros(_val) ); } - template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int16_t _val) { return countTrailingZeros(_val); } - template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int32_t _val) { return countTrailingZeros(_val); } - template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int64_t _val) { return countTrailingZeros(_val); } + template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(uint8_t _val) { return min(8, countTrailingZeros(_val) ); } + template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int8_t _val) { return countTrailingZeros(_val); } + template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(uint16_t _val) { return min(16, countTrailingZeros(_val) ); } + template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int16_t _val) { return countTrailingZeros(_val); } + template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int32_t _val) { return countTrailingZeros(_val); } + template<> inline BX_CONSTEXPR_FUNC uint8_t countTrailingZeros(int64_t _val) { return countTrailingZeros(_val); } template inline BX_CONSTEXPR_FUNC uint8_t findFirstSet(Ty _val) @@ -536,9 +536,29 @@ namespace bx return rcp(copySign(max(kFloatSmallest, abs(_a) ), _a) ); } + inline BX_CONSTEXPR_FUNC float div(float _a, float _b) + { + return mul(_a, rcp(_b) ); + } + + inline BX_CONSTEXPR_FUNC float divSafe(float _a, float _b) + { + return mul(_a, rcpSafe(_b) ); + } + + inline BX_CONSTEXPR_FUNC float ceilDiv(float _a, float _b) + { + return div(_a + _b - 1, _b); + } + + inline BX_CONSTEXPR_FUNC float ceilDivSafe(float _a, float _b) + { + return divSafe(_a + _b - 1, _b); + } + inline BX_CONSTEXPR_FUNC float mod(float _a, float _b) { - return _a - _b * floor(mul(_a, rcp(_b) ) ); + return _a - _b * floor(div(_a, _b) ); } inline BX_CONSTEXPR_FUNC bool isEqual(float _a, float _b, float _epsilon) @@ -900,8 +920,8 @@ namespace bx inline BX_CONST_FUNC Vec3 normalize(const Vec3 _a) { - const float invLen = rcpSafe(length(_a) ); - const Vec3 result = mul(_a, invLen); + const float len = length(_a); + const Vec3 result = divSafe(_a, len); return result; } diff --git a/include/bx/inline/readerwriter.inl b/include/bx/inline/readerwriter.inl index dd5cef2..b15d76d 100644 --- a/include/bx/inline/readerwriter.inl +++ b/include/bx/inline/readerwriter.inl @@ -67,7 +67,7 @@ namespace bx inline MemoryBlock::~MemoryBlock() { - bx::free(m_allocator, m_data); + free(m_allocator, m_data); } inline void* MemoryBlock::more(uint32_t _size) @@ -75,7 +75,7 @@ namespace bx if (0 < _size) { m_size += _size; - m_data = bx::realloc(m_allocator, m_data, m_size); + m_data = realloc(m_allocator, m_data, m_size); } return m_data; @@ -404,9 +404,9 @@ namespace bx inline int32_t peek(ReaderSeekerI* _reader, void* _data, int32_t _size, Error* _err) { BX_ERROR_SCOPE(_err); - int64_t offset = bx::seek(_reader); + int64_t offset = seek(_reader); int32_t size = _reader->read(_data, _size, _err); - bx::seek(_reader, offset, bx::Whence::Begin); + seek(_reader, offset, Whence::Begin); return size; } @@ -421,12 +421,12 @@ namespace bx inline int32_t align(ReaderSeekerI* _reader, uint32_t _alignment, Error* _err) { BX_ERROR_SCOPE(_err); - const int64_t current = bx::seek(_reader); + const int64_t current = seek(_reader); const int64_t aligned = ( (current + _alignment-1)/_alignment) * _alignment; const int32_t size = int32_t(aligned - current); if (0 != size) { - const int64_t offset = bx::seek(_reader, size); + const int64_t offset = seek(_reader, size); if (offset != aligned) { BX_ERROR_SET(_err, kErrorReaderWriterWrite, "Align: read truncated."); @@ -440,7 +440,7 @@ namespace bx inline int32_t align(WriterSeekerI* _writer, uint32_t _alignment, Error* _err) { BX_ERROR_SCOPE(_err); - const int64_t current = bx::seek(_writer); + const int64_t current = seek(_writer); const int64_t aligned = ( (current + _alignment-1)/_alignment) * _alignment; const int32_t size = int32_t(aligned - current); if (0 != size) diff --git a/include/bx/inline/rng.inl b/include/bx/inline/rng.inl index 1a1305c..80a96eb 100644 --- a/include/bx/inline/rng.inl +++ b/include/bx/inline/rng.inl @@ -56,11 +56,11 @@ namespace bx template inline float frndh(Rng* _rng) { - return 2.0f * bx::frnd(_rng) - 1.0f; + return 2.0f * frnd(_rng) - 1.0f; } template - inline bx::Vec3 randUnitCircle(Rng* _rng) + inline Vec3 randUnitCircle(Rng* _rng) { const float angle = frnd(_rng) * kPi2; @@ -73,7 +73,7 @@ namespace bx } template - inline bx::Vec3 randUnitSphere(Rng* _rng) + inline Vec3 randUnitSphere(Rng* _rng) { const float rand0 = frnd(_rng) * 2.0f - 1.0f; const float rand1 = frnd(_rng) * kPi2; @@ -88,14 +88,14 @@ namespace bx } template - inline bx::Vec3 randUnitHemisphere(Ty* _rng, const bx::Vec3& _normal) + inline Vec3 randUnitHemisphere(Ty* _rng, const Vec3& _normal) { - const bx::Vec3 dir = randUnitSphere(_rng); - const float ddotn = bx::dot(dir, _normal); + const Vec3 dir = randUnitSphere(_rng); + const float ddotn = dot(dir, _normal); if (0.0f > ddotn) { - return bx::neg(dir); + return neg(dir); } return dir; @@ -142,7 +142,7 @@ namespace bx for (uint32_t ii = 0, num = _num-1; ii < num; ++ii) { uint32_t jj = ii + 1 + _rng->gen() % (num - ii); - bx::swap(_array[ii], _array[jj]); + swap(_array[ii], _array[jj]); } } diff --git a/include/bx/inline/spscqueue.inl b/include/bx/inline/spscqueue.inl index d462714..8576b9c 100644 --- a/include/bx/inline/spscqueue.inl +++ b/include/bx/inline/spscqueue.inl @@ -27,7 +27,7 @@ namespace bx { Node* node = m_first; m_first = node->m_next; - bx::deleteObject(m_allocator, node); + deleteObject(m_allocator, node); } } @@ -39,7 +39,7 @@ namespace bx { Node* node = m_first; m_first = m_first->m_next; - bx::deleteObject(m_allocator, node); + deleteObject(m_allocator, node); } } diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index bc755d3..32bf906 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -187,7 +187,7 @@ namespace bx return m_0terminated; } - template + template inline StringT::StringT() : StringView() , m_capacity(0) @@ -195,7 +195,7 @@ namespace bx clear(); } - template + template inline StringT::StringT(const StringT& _rhs) : StringView() , m_capacity(0) @@ -203,7 +203,7 @@ namespace bx set(_rhs); } - template + template inline StringT::StringT(const StringView& _rhs) : StringView() , m_capacity(0) @@ -211,27 +211,27 @@ namespace bx set(_rhs); } - template + template inline StringT::~StringT() { clear(); } - template + template inline StringT& StringT::operator=(const StringT& _rhs) { set(_rhs); return *this; } - template + template inline void StringT::set(const StringView& _str) { clear(); append(_str); } - template + template inline void StringT::append(const StringView& _str) { if (0 != _str.getLength() ) @@ -244,7 +244,7 @@ namespace bx if (len+1 > m_capacity) { const int32_t capacity = alignUp(len+1, 256); - ptr = (char*)bx::realloc(*AllocatorT, 0 != m_capacity ? ptr : NULL, capacity); + ptr = (char*)realloc(*AllocatorT, 0 != m_capacity ? ptr : NULL, capacity); *const_cast(&m_ptr) = ptr; m_capacity = capacity; @@ -255,27 +255,27 @@ namespace bx } } - template + template inline void StringT::append(const char* _ptr, const char* _term) { append(StringView(_ptr, _term) ); } - template + template inline void StringT::clear() { m_0terminated = true; if (0 != m_capacity) { - bx::free(*AllocatorT, const_cast(m_ptr) ); + free(*AllocatorT, const_cast(m_ptr) ); StringView::clear(); m_capacity = 0; } } - template + template inline const char* StringT::getCPtr() const { return getPtr(); @@ -286,7 +286,7 @@ namespace bx return StringView(_str, _start, _len); } - inline LineReader::LineReader(const bx::StringView& _str) + inline LineReader::LineReader(const StringView& _str) : m_str(_str) { reset(); @@ -305,7 +305,7 @@ namespace bx ++m_line; StringView curr(m_curr); - m_curr = bx::strFindNl(m_curr); + m_curr = strFindNl(m_curr); StringView line(curr.getPtr(), m_curr.getPtr() ); diff --git a/include/bx/math.h b/include/bx/math.h index ba08e28..1763569 100644 --- a/include/bx/math.h +++ b/include/bx/math.h @@ -364,6 +364,22 @@ namespace bx /// BX_CONSTEXPR_FUNC float rcpSafe(float _a); + /// Returns result of division (_a / _b). + /// + BX_CONSTEXPR_FUNC float div(float _a, float _b); + + /// Returns result of division (_a / _b). Avoids divide by zero. + /// + BX_CONSTEXPR_FUNC float divSafe(float _a, float _b); + + /// Returns result of division (_a / _b), and rounds upwards. + /// + BX_CONSTEXPR_FUNC float ceilDiv(float _a, float _b); + + /// Returns result of division (_a / _b), and rounds upwards. Avoids divide by zero. + /// + BX_CONSTEXPR_FUNC float ceilDivSafe(float _a, float _b); + /// Returns the floating-point remainder of the division operation _a/_b. /// BX_CONSTEXPR_FUNC float mod(float _a, float _b); diff --git a/include/bx/rng.h b/include/bx/rng.h index 2751247..5c348cf 100644 --- a/include/bx/rng.h +++ b/include/bx/rng.h @@ -57,15 +57,15 @@ namespace bx /// Generate random point on unit circle. template - bx::Vec3 randUnitCircle(Rng* _rng); + Vec3 randUnitCircle(Rng* _rng); /// Generate random point on unit sphere. template - bx::Vec3 randUnitSphere(Rng* _rng); + Vec3 randUnitSphere(Rng* _rng); /// Generate random point on unit hemisphere. template - bx::Vec3 randUnitHemisphere(Ty* _rng, const bx::Vec3& _normal); + Vec3 randUnitHemisphere(Ty* _rng, const Vec3& _normal); /// Sampling with Hammersley and Halton Points. void generateSphereHammersley(void* _data, uint32_t _stride, uint32_t _num, float _scale = 1.0f); diff --git a/include/bx/string.h b/include/bx/string.h index cd5e1e8..8e706d6 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -138,7 +138,7 @@ namespace bx }; /// ASCII string - template + template class StringT : public StringView { public: