diff --git a/six/modules/c++/cphd/source/ByteSwap.cpp b/six/modules/c++/cphd/source/ByteSwap.cpp index 3f5ce7ae0..241d3dd98 100644 --- a/six/modules/c++/cphd/source/ByteSwap.cpp +++ b/six/modules/c++/cphd/source/ByteSwap.cpp @@ -21,59 +21,34 @@ */ #include +#include + #include -#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include #include #include +#include #include namespace { -// TODO: Maybe this should go in sys/Conf.h -// It's more flexible in that it properly handles float's - you can't -// just call sys::byteSwap(floatVal) because the compiler may change the -// byte-swapped float value into a valid IEEE value beforehand. -// TODO: If we're really looking to optimize this, could specialize it for -// sizes of 2, 4, and 8 to eliminate the for loop template -inline -void byteSwap(const void* in, T& out) +inline void byteSwap(const void* in, T& out) { - const std::byte* const inPtr = static_cast(in); - std::byte* const outPtr = reinterpret_cast(&out); - - for (size_t ii = 0, jj = sizeof(T) - 1; ii < jj; ++ii, --jj) - { - outPtr[ii] = inPtr[jj]; - outPtr[jj] = inPtr[ii]; - } + auto const inBytes = sys::make_span(in, sizeof(T)); + out = sys::swapBytes(inBytes); } -struct ByteSwapRunnable final : public sys::Runnable -{ - ByteSwapRunnable(void* buffer, - size_t elemSize, - size_t startElement, - size_t numElements) : - mBuffer(static_cast(buffer) + startElement * elemSize), - mElemSize(static_cast(elemSize)), - mNumElements(numElements) - { - } - - virtual void run() - { - sys::byteSwap(mBuffer, mElemSize, mNumElements); - } - -private: - std::byte* const mBuffer; - const unsigned short mElemSize; - const size_t mNumElements; -}; - inline const std::byte* calc_offset(const void* input_, size_t offset) { auto input = static_cast(input_); @@ -94,7 +69,7 @@ struct ByteSwapAndPromoteRunnable final : public sys::Runnable { } - virtual void run() + void run() override { InT real(0); InT imag(0); @@ -123,9 +98,8 @@ struct ByteSwapAndPromoteRunnable final : public sys::Runnable template -class ByteSwapAndScaleRunnable : public sys::Runnable +struct ByteSwapAndScaleRunnable final : public sys::Runnable { -public: ByteSwapAndScaleRunnable(const void* input, size_t startRow, size_t numRows, @@ -139,7 +113,7 @@ class ByteSwapAndScaleRunnable : public sys::Runnable { } - virtual void run() + void run() override { InT real(0); InT imag(0); @@ -249,36 +223,9 @@ void byteSwapAndScale(const void* input, namespace cphd { -void byteSwap(void* buffer, - size_t elemSize, - size_t numElements, - size_t numThreads) +void byteSwap(void* buffer, size_t elemSize, size_t numElements, size_t numThreads) { - if (numThreads <= 1) - { - sys::byteSwap(buffer, elemSize, numElements); - } - else - { - mt::ThreadGroup threads; - const mt::ThreadPlanner planner(numElements, numThreads); - - size_t threadNum(0); - size_t startElement(0); - size_t numElementsThisThread(0); - while (planner.getThreadInfo(threadNum++, - startElement, - numElementsThisThread)) - { - auto thread = std::make_unique( - buffer, - elemSize, - startElement, - numElementsThisThread); - threads.createThread(std::move(thread)); - } - threads.joinAll(); - } + return mt::threadedByteSwap(buffer, elemSize, numElements, numThreads); } void byteSwapAndPromote(const void* input, diff --git a/six/modules/c++/six.sicd/tests/test_sicd_byte_provider.cpp b/six/modules/c++/six.sicd/tests/test_sicd_byte_provider.cpp index e143cdc79..c2121f0da 100644 --- a/six/modules/c++/six.sicd/tests/test_sicd_byte_provider.cpp +++ b/six/modules/c++/six.sicd/tests/test_sicd_byte_provider.cpp @@ -66,11 +66,12 @@ struct Tester final } mBigEndianImage = mImage; - if (std::endian::native == std::endian::little) + if (sys::isLittleEndianSystem()) { - sys::byteSwap(mBigEndianImage.data(), - sizeof(DataTypeT), - mBigEndianImage.size() * 2); + void* const buffer = mBigEndianImage.data(); + constexpr auto elemSize = sizeof(DataTypeT); + const auto numElems = mBigEndianImage.size() * 2; // real and imag + sys::byteSwap(buffer, elemSize, numElems); } normalWrite();