diff --git a/include/bx/bx.h b/include/bx/bx.h index 161270a..ea97d0d 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -15,40 +15,37 @@ #include "config.h" #include "macros.h" -namespace bx -{ - // http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/ - template char (&COUNTOF_REQUIRES_ARRAY_ARGUMENT(const T(&)[N]) )[N]; +/// #define BX_COUNTOF(_x) sizeof(bx::COUNTOF_REQUIRES_ARRAY_ARGUMENT(_x) ) - // Template for avoiding MSVC: C4127: conditional expression is constant - template - inline bool isEnabled() - { - return true; - } - - template<> - inline bool isEnabled() - { - return false; - } -#define BX_ENABLED(_x) bx::isEnabled() - - inline bool ignoreC4127(bool _x) - { - return _x; - } +/// #define BX_IGNORE_C4127(_x) bx::ignoreC4127(!!(_x) ) - template - inline void xchg(Ty& _a, Ty& _b) - { - Ty tmp = _a; _a = _b; _b = tmp; - } +/// +#define BX_ENABLED(_x) bx::isEnabled() + +namespace bx +{ + /// Template for avoiding MSVC: C4127: conditional expression is constant + template + bool isEnabled(); /// - void* memCopy(void* _dst, const void* _src, size_t _numBytes); + bool ignoreC4127(bool _x); + + /// + template + void xchg(Ty& _a, Ty& _b); + + /// + void xchg(void* _a, void* _b, size_t _numBytes); + + // http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/ + template + char (&COUNTOF_REQUIRES_ARRAY_ARGUMENT(const T(&)[N]) )[N]; + + /// + void memCopy(void* _dst, const void* _src, size_t _numBytes); /// void memCopy(void* _dst, const void* _src, uint32_t _size, uint32_t _num, uint32_t _srcPitch, uint32_t _dstPitch); @@ -60,11 +57,13 @@ namespace bx void scatter(void* _dst, const void* _src, uint32_t _size, uint32_t _num, uint32_t _dstPitch); /// - void* memMove(void* _dst, const void* _src, size_t _numBytes); + void memMove(void* _dst, const void* _src, size_t _numBytes); /// - void* memSet(void* _dst, uint8_t _ch, size_t _numBytes); + void memSet(void* _dst, uint8_t _ch, size_t _numBytes); } // namespace bx +#include "bx.inl" + #endif // BX_H_HEADER_GUARD diff --git a/include/bx/bx.inl b/include/bx/bx.inl new file mode 100644 index 0000000..f8e9c63 --- /dev/null +++ b/include/bx/bx.inl @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_H_HEADER_GUARD +# error "Must be included from bx/bx.h!" +#endif // BX_H_HEADER_GUARD + +namespace bx +{ + template + inline bool isEnabled() + { + return true; + } + + template<> + inline bool isEnabled() + { + return false; + } + + inline bool ignoreC4127(bool _x) + { + return _x; + } + + template + inline void xchg(Ty& _a, Ty& _b) + { + Ty tmp = _a; _a = _b; _b = tmp; + } + +} // namespace bx diff --git a/src/crt.cpp b/src/crt.cpp index fbc91c7..66c09bb 100644 --- a/src/crt.cpp +++ b/src/crt.cpp @@ -13,7 +13,18 @@ namespace bx { - void* memCopyRef(void* _dst, const void* _src, size_t _numBytes) + void xchg(void* _a, void* _b, size_t _numBytes) + { + uint8_t* lhs = (uint8_t*)_a; + uint8_t* rhs = (uint8_t*)_b; + const uint8_t* end = rhs + _numBytes; + while (rhs != end) + { + xchg(*lhs++, *rhs++); + } + } + + void memCopyRef(void* _dst, const void* _src, size_t _numBytes) { uint8_t* dst = (uint8_t*)_dst; const uint8_t* end = dst + _numBytes; @@ -22,16 +33,14 @@ namespace bx { *dst++ = *src++; } - - return _dst; } - void* memCopy(void* _dst, const void* _src, size_t _numBytes) + void memCopy(void* _dst, const void* _src, size_t _numBytes) { #if BX_CRT_NONE - return memCopyRef(_dst, _src, _numBytes); + memCopyRef(_dst, _src, _numBytes); #else - return ::memcpy(_dst, _src, _numBytes); + ::memcpy(_dst, _src, _numBytes); #endif // BX_CRT_NONE } @@ -60,7 +69,7 @@ namespace bx memCopy(_dst, _src, _size, _num, _size, _dstPitch); } - void* memMoveRef(void* _dst, const void* _src, size_t _numBytes) + void memMoveRef(void* _dst, const void* _src, size_t _numBytes) { uint8_t* dst = (uint8_t*)_dst; const uint8_t* src = (const uint8_t*)_src; @@ -68,33 +77,32 @@ namespace bx if (_numBytes == 0 || dst == src) { - return dst; + return; } // if (src+_numBytes <= dst || end <= src) if (dst < src) { - return memCopy(_dst, _src, _numBytes); + memCopy(_dst, _src, _numBytes); + return; } for (intptr_t ii = _numBytes-1; ii >= 0; --ii) { dst[ii] = src[ii]; } - - return _dst; } - void* memMove(void* _dst, const void* _src, size_t _numBytes) + void memMove(void* _dst, const void* _src, size_t _numBytes) { #if BX_CRT_NONE - return memMoveRef(_dst, _src, _numBytes); + memMoveRef(_dst, _src, _numBytes); #else - return ::memmove(_dst, _src, _numBytes); + ::memmove(_dst, _src, _numBytes); #endif // BX_CRT_NONE } - void* memSetRef(void* _dst, uint8_t _ch, size_t _numBytes) + void memSetRef(void* _dst, uint8_t _ch, size_t _numBytes) { uint8_t* dst = (uint8_t*)_dst; const uint8_t* end = dst + _numBytes; @@ -102,16 +110,14 @@ namespace bx { *dst++ = char(_ch); } - - return _dst; } - void* memSet(void* _dst, uint8_t _ch, size_t _numBytes) + void memSet(void* _dst, uint8_t _ch, size_t _numBytes) { #if BX_CRT_NONE - return memSetRef(_dst, _ch, _numBytes); + memSetRef(_dst, _ch, _numBytes); #else - return ::memset(_dst, _ch, _numBytes); + ::memset(_dst, _ch, _numBytes); #endif // BX_CRT_NONE } @@ -396,16 +402,19 @@ namespace bx #if BX_CRT_NONE extern "C" void* memcpy(void* _dst, const void* _src, size_t _numBytes) { - return bx::memCopy(_dst, _src, _numBytes); + bx::memCopy(_dst, _src, _numBytes); + return _dst; } extern "C" void* memmove(void* _dst, const void* _src, size_t _numBytes) { - return bx::memMove(_dst, _src, _numBytes); + bx::memMove(_dst, _src, _numBytes); + return _dst; } extern "C" void* memset(void* _dst, int _ch, size_t _numBytes) { - return bx::memSet(_dst, uint8_t(_ch), _numBytes); + bx::memSet(_dst, uint8_t(_ch), _numBytes); + return _dst; } #endif // BX_CRT_NONE diff --git a/tests/crt_test.cpp b/tests/crt_test.cpp index 7c4688c..fedb925 100644 --- a/tests/crt_test.cpp +++ b/tests/crt_test.cpp @@ -10,10 +10,10 @@ TEST_CASE("memSet", "") { char temp[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; - REQUIRE(temp == bx::memSet(temp, 0, 0) ); + bx::memSet(temp, 0, 0); REQUIRE(temp[0] == 1); - REQUIRE(temp == bx::memSet(temp, 0, 5) ); + bx::memSet(temp, 0, 5); REQUIRE(temp[0] == 0); REQUIRE(temp[1] == 0); REQUIRE(temp[2] == 0); @@ -24,17 +24,21 @@ TEST_CASE("memSet", "") TEST_CASE("memMove", "") { + const char* orignal = "xxxxabvgd"; char str[] = { 'x', 'x', 'x', 'x', 'a', 'b', 'v', 'g', 'd' }; - REQUIRE(bx::memMove(&str[4], &str[4], 0) ); - REQUIRE(bx::memMove(&str[4], &str[4], 5) ); + bx::memMove(&str[4], &str[4], 0); + REQUIRE(0 == bx::strncmp(str, orignal) ); - REQUIRE(&str[0] == bx::memMove(str, &str[4], 5) ); - REQUIRE( 0 == bx::strncmp(str, "abvgd", 5) ); + bx::memMove(&str[4], &str[4], 5); + REQUIRE(0 == bx::strncmp(str, orignal) ); - REQUIRE(&str[4] == bx::memMove(&str[4], str, 5) ); - REQUIRE( str[4] == 'a' ); + bx::memMove(str, &str[4], 5); + REQUIRE(0 == bx::strncmp(str, "abvgd", 5) ); + + bx::memMove(&str[4], str, 5); + REQUIRE(str[4] == 'a' ); bx::memSet(str, 'x', 4); - REQUIRE(0 == bx::strncmp(str, "xxxxabvgd", 9) ); + REQUIRE(0 == bx::strncmp(str, orignal) ); }