From 0294cb134cb29cd3f634979141bc9c51a9d47e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 27 Oct 2017 17:57:23 -0700 Subject: [PATCH 1/4] Fixed SIMD intrisic yBxA -> AxBy. --- include/bx/inline/simd128_langext.inl | 2 +- include/bx/inline/simd128_neon.inl | 2 +- include/bx/inline/simd128_ref.inl | 2 +- include/bx/inline/simd128_sse.inl | 2 +- include/bx/simd_t.h | 2 +- tests/simd_test.cpp | 5 +++-- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/bx/inline/simd128_langext.inl b/include/bx/inline/simd128_langext.inl index 72e4c56..9fa3f41 100644 --- a/include/bx/inline/simd128_langext.inl +++ b/include/bx/inline/simd128_langext.inl @@ -112,7 +112,7 @@ BX_SIMD128_IMPLEMENT_TEST(xyzw , 0xf); } template<> - BX_SIMD_FORCE_INLINE simd128_langext_t simd_shuf_yBxA(simd128_langext_t _a, simd128_langext_t _b) + BX_SIMD_FORCE_INLINE simd128_langext_t simd_shuf_AxBy(simd128_langext_t _a, simd128_langext_t _b) { simd128_langext_t result; result.vf = __builtin_shufflevector(_a.vf, _b.vf, 1, 5, 0, 4); diff --git a/include/bx/inline/simd128_neon.inl b/include/bx/inline/simd128_neon.inl index f36610c..356b7c7 100644 --- a/include/bx/inline/simd128_neon.inl +++ b/include/bx/inline/simd128_neon.inl @@ -102,7 +102,7 @@ BX_SIMD128_IMPLEMENT_TEST(yzw, yzww); } template<> - BX_SIMD_FORCE_INLINE simd128_neon_t simd_shuf_yBxA(simd128_neon_t _a, simd128_neon_t _b) + BX_SIMD_FORCE_INLINE simd128_neon_t simd_shuf_AxBy(simd128_neon_t _a, simd128_neon_t _b) { return __builtin_shuffle(_a, _b, (uint32x4_t){ 1, 5, 0, 4 }); } diff --git a/include/bx/inline/simd128_ref.inl b/include/bx/inline/simd128_ref.inl index 85d82cf..0ef8f68 100644 --- a/include/bx/inline/simd128_ref.inl +++ b/include/bx/inline/simd128_ref.inl @@ -130,7 +130,7 @@ BX_SIMD128_IMPLEMENT_TEST(xyzw , 0xf); } template<> - BX_SIMD_FORCE_INLINE simd128_ref_t simd_shuf_yBxA(simd128_ref_t _a, simd128_ref_t _b) + BX_SIMD_FORCE_INLINE simd128_ref_t simd_shuf_AxBy(simd128_ref_t _a, simd128_ref_t _b) { simd128_ref_t result; result.uxyzw[0] = _a.uxyzw[1]; diff --git a/include/bx/inline/simd128_sse.inl b/include/bx/inline/simd128_sse.inl index 29d74b1..69f37d1 100644 --- a/include/bx/inline/simd128_sse.inl +++ b/include/bx/inline/simd128_sse.inl @@ -90,7 +90,7 @@ BX_SIMD128_IMPLEMENT_TEST(xyzw , 0xf); } template<> - BX_SIMD_FORCE_INLINE simd128_sse_t simd_shuf_yBxA(simd128_sse_t _a, simd128_sse_t _b) + BX_SIMD_FORCE_INLINE simd128_sse_t simd_shuf_AxBy(simd128_sse_t _a, simd128_sse_t _b) { return _mm_unpacklo_ps(_b, _a); } diff --git a/include/bx/simd_t.h b/include/bx/simd_t.h index c7be8eb..f63dcfb 100644 --- a/include/bx/simd_t.h +++ b/include/bx/simd_t.h @@ -101,7 +101,7 @@ BX_SIMD128_IMPLEMENT_TEST(xyzw); Ty simd_shuf_xAyB(Ty _a, Ty _b); template - Ty simd_shuf_yBxA(Ty _a, Ty _b); + Ty simd_shuf_AxBy(Ty _a, Ty _b); template Ty simd_shuf_zCwD(Ty _a, Ty _b); diff --git a/tests/simd_test.cpp b/tests/simd_test.cpp index 97ae9bf..fcdc7a9 100644 --- a/tests/simd_test.cpp +++ b/tests/simd_test.cpp @@ -235,13 +235,14 @@ TEST_CASE("simd_shuffle", "") const simd128_t ABCD = simd_ild(0x41414141, 0x42424242, 0x43434343, 0x44444444); simd_check_string("xyAB", simd_shuf_xyAB(xyzw, ABCD) ); simd_check_string("ABxy", simd_shuf_ABxy(xyzw, ABCD) ); - simd_check_string("zwCD", simd_shuf_zwCD(xyzw, ABCD) ); simd_check_string("CDzw", simd_shuf_CDzw(xyzw, ABCD) ); + simd_check_string("zwCD", simd_shuf_zwCD(xyzw, ABCD) ); simd_check_string("xAyB", simd_shuf_xAyB(xyzw, ABCD) ); + simd_check_string("AxBy", simd_shuf_AxBy(xyzw, ABCD) ); simd_check_string("zCwD", simd_shuf_zCwD(xyzw, ABCD) ); + simd_check_string("CzDw", simd_shuf_CzDw(xyzw, ABCD) ); simd_check_string("xAzC", simd_shuf_xAzC(xyzw, ABCD) ); simd_check_string("yBwD", simd_shuf_yBwD(xyzw, ABCD) ); - simd_check_string("CzDw", simd_shuf_CzDw(xyzw, ABCD) ); } TEST_CASE("simd_compare", "") From 58c8c68062b081a363631fce2de5a40dfebd57b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 29 Oct 2017 09:24:40 -0700 Subject: [PATCH 2/4] Cleanup. --- include/bx/file.h | 3 ++ src/crtnone.cpp | 4 +++ src/file.cpp | 90 ++++++++++++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/include/bx/file.h b/include/bx/file.h index d0ea3ea..79ac24f 100644 --- a/include/bx/file.h +++ b/include/bx/file.h @@ -20,6 +20,9 @@ namespace bx /// WriterI* getStdErr(); + /// + WriterI* getNullOut(); + /// class FileReader : public FileReaderI { diff --git a/src/crtnone.cpp b/src/crtnone.cpp index 731126d..877dcd8 100644 --- a/src/crtnone.cpp +++ b/src/crtnone.cpp @@ -303,6 +303,10 @@ extern "C" int printf(const char* _format, ...) return -1; } +struct FILE +{ +}; + extern "C" int fprintf(FILE* _stream, const char* _format, ...) { BX_UNUSED(_stream, _format); diff --git a/src/file.cpp b/src/file.cpp index 3cd70e7..234e983 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -16,16 +16,51 @@ namespace bx { + class NoopWriterImpl : public FileWriterI + { + public: + NoopWriterImpl(void*) + { + } + + virtual ~NoopWriterImpl() + { + close(); + } + + virtual bool open(const FilePath& _filePath, bool _append, Error* _err) override + { + BX_UNUSED(_filePath, _append, _err); + return false; + } + + virtual void close() override + { + } + + virtual int64_t seek(int64_t _offset, Whence::Enum _whence) override + { + BX_UNUSED(_offset, _whence); + return 0; + } + + virtual int32_t write(const void* _data, int32_t _size, Error* _err) override + { + BX_UNUSED(_data, _size, _err); + return 0; + } + }; + #if BX_CONFIG_CRT_FILE_READER_WRITER # if BX_CRT_MSVC # define fseeko64 _fseeki64 # define ftello64 _ftelli64 -# elif 0 \ +# elif 0 \ || BX_PLATFORM_ANDROID \ - || BX_PLATFORM_BSD \ - || BX_PLATFORM_IOS \ - || BX_PLATFORM_OSX \ + || BX_PLATFORM_BSD \ + || BX_PLATFORM_IOS \ + || BX_PLATFORM_OSX \ || BX_PLATFORM_QNX # define fseeko64 fseeko # define ftello64 ftello @@ -34,7 +69,7 @@ namespace bx # define ftello64 ftell # endif // BX_ - class FileReaderImpl : public bx::FileReaderI + class FileReaderImpl : public FileReaderI { public: FileReaderImpl(FILE* _file) @@ -114,7 +149,7 @@ namespace bx bool m_open; }; - class FileWriterImpl : public bx::FileWriterI + class FileWriterImpl : public FileWriterI { public: FileWriterImpl(FILE* _file) @@ -189,7 +224,7 @@ namespace bx #else - class FileReaderImpl : public bx::FileReaderI + class FileReaderImpl : public FileReaderI { public: FileReaderImpl(void*) @@ -224,40 +259,7 @@ namespace bx } }; - class FileWriterImpl : public bx::FileWriterI - { - public: - FileWriterImpl(void*) - { - } - - virtual ~FileWriterImpl() - { - close(); - } - - virtual bool open(const FilePath& _filePath, bool _append, Error* _err) override - { - BX_UNUSED(_filePath, _append); - return false; - } - - virtual void close() override - { - } - - virtual int64_t seek(int64_t _offset, Whence::Enum _whence) override - { - BX_UNUSED(_offset, _whence); - return 0; - } - - virtual int32_t write(const void* _data, int32_t _size, Error* _err) override - { - BX_UNUSED(_data, _size, _err); - return 0; - } - }; + typedef NoopWriterImpl FileWriterImpl; #endif // BX_CONFIG_CRT_FILE_READER_WRITER @@ -351,6 +353,12 @@ namespace bx return &s_stdOut; } + WriterI* getNullOut() + { + static NoopWriterImpl s_nullOut(NULL); + return &s_nullOut; + } + bool stat(const char* _filePath, FileInfo& _fileInfo) { _fileInfo.m_size = 0; From bf39e556b0e29866aff35ea0708d3e08f5be8fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 29 Oct 2017 09:26:15 -0700 Subject: [PATCH 3/4] Added RNG unit test. --- include/bx/inline/rng.inl | 19 --------- include/bx/rng.h | 18 -------- tests/rng_test.cpp | 88 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 37 deletions(-) create mode 100644 tests/rng_test.cpp diff --git a/include/bx/inline/rng.inl b/include/bx/inline/rng.inl index 1cda221..df6c826 100644 --- a/include/bx/inline/rng.inl +++ b/include/bx/inline/rng.inl @@ -28,25 +28,6 @@ namespace bx return (m_z<<16)+m_w; } - inline RngFib::RngFib(uint32_t _a, uint32_t _b) - : m_a(_a) - , m_b(_b) - { - } - - inline void RngFib::reset(uint32_t _a, uint32_t _b) - { - m_a = _a; - m_b = _b; - } - - inline uint32_t RngFib::gen() - { - m_b = m_a+m_b; - m_a = m_b-m_a; - return m_a; - } - inline RngShr3::RngShr3(uint32_t _jsr) : m_jsr(_jsr) { diff --git a/include/bx/rng.h b/include/bx/rng.h index e2c9ca8..22fff27 100644 --- a/include/bx/rng.h +++ b/include/bx/rng.h @@ -30,24 +30,6 @@ namespace bx uint32_t m_w; }; - /// George Marsaglia's FIB - class RngFib - { - public: - /// - RngFib(uint32_t _a = 9983651, uint32_t _b = 95746118); - - /// - void reset(uint32_t _a = 9983651, uint32_t _b = 95746118); - - /// - uint32_t gen(); - - private: - uint32_t m_a; - uint32_t m_b; - }; - /// George Marsaglia's SHR3 class RngShr3 { diff --git a/tests/rng_test.cpp b/tests/rng_test.cpp new file mode 100644 index 0000000..89fef3f --- /dev/null +++ b/tests/rng_test.cpp @@ -0,0 +1,88 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#include "test.h" + +#include +#include + +const uint32_t kMax = 10<<20; + +template +void testRng(const char* _name, Ty* _rng) +{ + uint32_t histBits[32]; + uint32_t histUint8[256]; + + bx::memSet(histBits, 0, sizeof(histBits) ); + bx::memSet(histUint8, 0, sizeof(histUint8) ); + + for (uint32_t ii = 0; ii < kMax; ++ii) + { + uint32_t val = _rng->gen(); + + for (uint32_t shift = 0; shift < 32; ++shift) + { + const uint32_t mask = 1<>24; + const uint32_t m0f00 = (val & 0x00ff0000)>>16; + const uint32_t m00f0 = (val & 0x0000ff00)>> 8; + const uint32_t m000f = (val & 0x000000ff)>> 0; + + histUint8[mf000]++; + histUint8[m0f00]++; + histUint8[m00f0]++; + histUint8[m000f]++; + } + + bx::WriterI* writer = bx::getNullOut(); + bx::writePrintf(writer, "%s\n", _name); + + { + bx::writePrintf(writer, "\tbits histogram:\n"); + uint32_t min = UINT32_MAX; + uint32_t max = 0; + + for (uint32_t ii = 0; ii < BX_COUNTOF(histBits); ++ii) + { + bx::writePrintf(writer, "\t\t%3d: %d\n", ii, histBits[ii]); + min = bx::min(min, histBits[ii]); + max = bx::max(max, histBits[ii]); + } + + bx::writePrintf(writer, "\tmin: %d, max: %d (diff: %d)\n", min, max, max-min); + + REQUIRE(max-min < 8000); + } + + { + bx::writePrintf(writer, "\tuint8_t histogram:\n"); + uint32_t min = UINT32_MAX; + uint32_t max = 0; + + for (uint32_t ii = 0; ii < BX_COUNTOF(histUint8); ++ii) + { + bx::writePrintf(writer, "\t\t%3d: %d\n", ii, histUint8[ii]); + min = bx::min(min, histUint8[ii]); + max = bx::max(max, histUint8[ii]); + } + + bx::writePrintf(writer, "\tmin: %d, max: %d (diff: %d)\n", min, max, max-min); + + REQUIRE(max-min < 8000); + } +} + +TEST_CASE("Rng", "") +{ + bx::RngMwc mwc; + testRng("RngMwc", &mwc); + + bx::RngShr3 shr3; + testRng("RngShr3", &shr3); +} From ba7014c8a93a3469416e46eda90fea26032ffc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 29 Oct 2017 21:04:31 -0700 Subject: [PATCH 4/4] Cleanup. --- tests/easing_test.cpp | 63 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/tests/easing_test.cpp b/tests/easing_test.cpp index a15587b..9bf3c99 100644 --- a/tests/easing_test.cpp +++ b/tests/easing_test.cpp @@ -10,52 +10,49 @@ TEST_CASE("easing", "") { - if (BX_ENABLED(false) ) + bx::WriterI* writer = bx::getNullOut(); + + for (uint32_t ee = 0; ee < bx::Easing::Count; ++ee) { - bx::WriterI* writer = bx::getStdOut(); + bx::writePrintf(writer, "\n\n%d\n", ee); - for (uint32_t ee = 0; ee < bx::Easing::Count; ++ee) + const bx::EaseFn easing = bx::getEaseFunc(bx::Easing::Enum(ee) ); + + const int32_t nx = 64; + const int32_t ny = 10; + + bx::writePrintf(writer, "\t/// ^\n"); + + for (int32_t yy = ny+4; yy >= -5; --yy) { - bx::writePrintf(writer, "\n\n%d\n", ee); + const float ys = float(yy )/float(ny); + const float ye = float(yy+1.0)/float(ny); - const bx::EaseFn easing = bx::getEaseFunc(bx::Easing::Enum(ee) ); + bx::writePrintf(writer, "\t/// %c", yy != 0 ? '|' : '+'); - const int32_t nx = 64; - const int32_t ny = 10; - - bx::writePrintf(writer, "\t/// ^\n"); - - for (int32_t yy = ny+4; yy >= -5; --yy) + for (int32_t xx = 0; xx < nx; ++xx) { - const float ys = float(yy )/float(ny); - const float ye = float(yy+1.0)/float(ny); - - bx::writePrintf(writer, "\t/// %c", yy != 0 ? '|' : '+'); - - for (int32_t xx = 0; xx < nx; ++xx) + int32_t jj = 0; + for (; jj < 10; ++jj) { - int32_t jj = 0; - for (; jj < 10; ++jj) - { - const float tt = float(xx*10+jj)/10.0f/float(nx); - const float vv = easing(tt); + const float tt = float(xx*10+jj)/10.0f/float(nx); + const float vv = easing(tt); - if (vv >= ys - && vv < ye) - { - bx::writePrintf(writer, "*"); - break; - } - } - - if (jj == 10) + if (vv >= ys + && vv < ye) { - bx::writePrintf(writer, "%c", yy != 0 ? ' ' : '-'); + bx::writePrintf(writer, "*"); + break; } } - bx::writePrintf(writer, "%s\n", yy != 0 ? "" : ">"); + if (jj == 10) + { + bx::writePrintf(writer, "%c", yy != 0 ? ' ' : '-'); + } } + + bx::writePrintf(writer, "%s\n", yy != 0 ? "" : ">"); } } }