Skip to content

Commit

Permalink
Merge branch 'cpp17' into cpp20
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Jun 15, 2023
2 parents 26a16b5 + 8bade7c commit d0f21ca
Show file tree
Hide file tree
Showing 22 changed files with 267 additions and 224 deletions.
14 changes: 12 additions & 2 deletions six/modules/c++/cphd/include/cphd/PVPBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,23 @@ struct AddedPVP
}
};
template<typename T>
struct AddedPVP<types::complex<T> >
struct AddedPVP<std::complex<T>>
{
types::complex<T> getAddedPVP(const six::Parameter& val) const
auto getAddedPVP(const six::Parameter& val) const
{
return val.getComplex<T>();
}
};
//#if CODA_OSS_types_unique_zinteger
//template<typename T>
//struct AddedPVP<types::zinteger<T> >
//{
// auto getAddedPVP(const six::Parameter& val) const
// {
// return val.getComplex<T>();
// }
//};
//#endif
template<>
struct AddedPVP<std::string>
{
Expand Down
8 changes: 4 additions & 4 deletions six/modules/c++/cphd/include/cphd/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ namespace cphd

using zfloat = six::zfloat;
using zdouble = six::zdouble;
using zint16_t = six::zint16_t;

using zint8_t = types::zint8_t;
using zint16_t = types::zint16_t;
using zint32_t = types::zint32_t;
using zint64_t = types::zint64_t;
using zint8_t = std::complex<int8_t>; // TODO: types::zint8_t;
using zint32_t = std::complex<int32_t>; // TODO: types::zint32_t;
using zint64_t = std::complex<int64_t>; // TODO: types::zint64_t;

typedef six::Vector2 Vector2;

Expand Down
51 changes: 28 additions & 23 deletions six/modules/c++/cphd/source/ByteSwap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <std/span>
#include <std/cstddef>
#include <tuple>
#include <type_traits>

#include <types/complex.h>
#include <sys/Conf.h>
Expand All @@ -41,6 +42,8 @@
#include <mt/ThreadedByteSwap.h>
#include <nitf/coda-oss.hpp>

#include <cphd/Types.h>

namespace
{
template <typename T>
Expand All @@ -56,35 +59,36 @@ inline const std::byte* calc_offset(const void* input_, size_t offset)
return input + offset;
}

template <typename InT>
template <typename ComplexInT>
struct ByteSwapAndPromoteRunnable final : public sys::Runnable
{
ByteSwapAndPromoteRunnable(const void* input,
size_t startRow,
size_t numRows,
size_t numCols,
cphd::zfloat* output) :
mInput(calc_offset(input, startRow * numCols * sizeof(types::complex<InT>))),
mInput(calc_offset(input, startRow * numCols * sizeof(ComplexInT))),
mDims(numRows, numCols),
mOutput(output + startRow * numCols)
{
}

void run() override
{
InT real(0);
InT imag(0);
using value_type = typename ComplexInT::value_type;
value_type real(0);
value_type imag(0);

for (size_t row = 0, inIdx = 0, outIdx = 0; row < mDims.row; ++row)
{
for (size_t col = 0; col < mDims.col; ++col, inIdx += sizeof(types::complex<InT>), ++outIdx)
for (size_t col = 0; col < mDims.col; ++col, inIdx += sizeof(ComplexInT), ++outIdx)
{
// Have to be careful here - can't treat mInput as a
// types::complex<InT> directly in case InT is a float (see
// std::complex_t<InT> directly in case InT is a float (see
// explanation in byteSwap() comments)
const auto input = calc_offset(mInput, inIdx);
byteSwap(input, real);
byteSwap(calc_offset(input, sizeof(InT)), imag);
byteSwap(calc_offset(input, sizeof(value_type)), imag);

mOutput[outIdx] = cphd::zfloat(real, imag);
}
Expand All @@ -98,7 +102,7 @@ struct ByteSwapAndPromoteRunnable final : public sys::Runnable
};


template <typename InT>
template <typename ComplexInT>
struct ByteSwapAndScaleRunnable final : public sys::Runnable
{
ByteSwapAndScaleRunnable(const void* input,
Expand All @@ -107,7 +111,7 @@ struct ByteSwapAndScaleRunnable final : public sys::Runnable
size_t numCols,
const double* scaleFactors,
cphd::zfloat* output) :
mInput(calc_offset(input, startRow * numCols * sizeof(types::complex<InT>))),
mInput(calc_offset(input, startRow * numCols * sizeof(ComplexInT))),
mDims(numRows, numCols),
mScaleFactors(scaleFactors + startRow),
mOutput(output + startRow * numCols)
Expand All @@ -116,23 +120,24 @@ struct ByteSwapAndScaleRunnable final : public sys::Runnable

void run() override
{
InT real(0);
InT imag(0);
using value_type = typename ComplexInT::value_type;
value_type real(0);
value_type imag(0);

for (size_t row = 0, inIdx = 0, outIdx = 0; row < mDims.row; ++row)
{
const double scaleFactor(mScaleFactors[row]);

for (size_t col = 0;
col < mDims.col;
++col, inIdx += sizeof(types::complex<InT>), ++outIdx)
++col, inIdx += sizeof(ComplexInT), ++outIdx)
{
// Have to be careful here - can't treat mInput as a
// types::complex<InT> directly in case InT is a float (see
// std::ComplexInT directly in case InT is a float (see
// explanation in byteSwap() comments)
const auto input = calc_offset(mInput, inIdx);
byteSwap(input, real);
byteSwap(calc_offset(input, sizeof(InT)), imag);
byteSwap(calc_offset(input, sizeof(value_type)), imag);

mOutput[outIdx] = cphd::zfloat(
static_cast<float>(real * scaleFactor),
Expand All @@ -148,15 +153,15 @@ struct ByteSwapAndScaleRunnable final : public sys::Runnable
cphd::zfloat* const mOutput;
};

template <typename InT>
template <typename ComplexInT>
void byteSwapAndPromote(const void* input,
const types::RowCol<size_t>& dims,
size_t numThreads,
cphd::zfloat* output)
{
if (numThreads <= 1)
{
ByteSwapAndPromoteRunnable<InT>(input, 0, dims.row, dims.col,output).run();
ByteSwapAndPromoteRunnable<ComplexInT>(input, 0, dims.row, dims.col,output).run();
}
else
{
Expand All @@ -170,7 +175,7 @@ void byteSwapAndPromote(const void* input,
startRow,
numRowsThisThread))
{
auto scaler = std::make_unique<ByteSwapAndPromoteRunnable<InT>>(
auto scaler = std::make_unique<ByteSwapAndPromoteRunnable<ComplexInT>>(
input,
startRow,
numRowsThisThread,
Expand Down Expand Up @@ -238,13 +243,13 @@ void byteSwapAndPromote(const void* input,
switch (elementSize)
{
case 2:
::byteSwapAndPromote<int8_t>(input, dims, numThreads, output);
::byteSwapAndPromote<cphd::zint8_t>(input, dims, numThreads, output);
break;
case 4:
::byteSwapAndPromote<int16_t>(input, dims, numThreads, output);
::byteSwapAndPromote<cphd::zint16_t>(input, dims, numThreads, output);
break;
case 8:
::byteSwapAndPromote<float>(input, dims, numThreads, output);
::byteSwapAndPromote<cphd::zfloat>(input, dims, numThreads, output);
break;
default:
throw except::Exception(Ctxt(
Expand All @@ -262,15 +267,15 @@ void byteSwapAndScale(const void* input,
switch (elementSize)
{
case 2:
::byteSwapAndScale<int8_t>(input, dims, scaleFactors, numThreads,
::byteSwapAndScale<cphd::zint8_t>(input, dims, scaleFactors, numThreads,
output);
break;
case 4:
::byteSwapAndScale<int16_t>(input, dims, scaleFactors, numThreads,
::byteSwapAndScale<cphd::zint16_t>(input, dims, scaleFactors, numThreads,
output);
break;
case 8:
::byteSwapAndScale<float>(input, dims, scaleFactors, numThreads,
::byteSwapAndScale<cphd::zfloat>(input, dims, scaleFactors, numThreads,
output);
break;
default:
Expand Down
21 changes: 15 additions & 6 deletions six/modules/c++/cphd/source/PVPBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ void PVPBlock::PVPSet::write(const PVPBlock& pvpBlock, const Pvp& p, const sys::
}
}

template<typename TComplex>
auto findComplex(const std::unordered_map<std::string, six::Parameter>& addedPVP, std::map<std::string, APVPType>::const_iterator it)
{
using value_type = typename TComplex::value_type; // help the compiler find the right overload
TComplex retval;
addedPVP.find(it->first)->second.getComplex<value_type>(retval);
return retval;
}

void PVPBlock::PVPSet::read(const Pvp& p, sys::ubyte* dest_) const
{
auto dest = reinterpret_cast<std::byte*>(dest_);
Expand Down Expand Up @@ -407,27 +416,27 @@ void PVPBlock::PVPSet::read(const Pvp& p, sys::ubyte* dest_) const
}
else if (it->second.getFormat() == "CI2")
{
::getData(dest + it->second.getByteOffset(), addedPVP.find(it->first)->second.getComplex<std::int8_t>());
::getData(dest + it->second.getByteOffset(), findComplex<cphd::zint8_t>(addedPVP, it));
}
else if (it->second.getFormat() == "CI4")
{
::getData(dest + it->second.getByteOffset(), addedPVP.find(it->first)->second.getComplex<std::int16_t>());
::getData(dest + it->second.getByteOffset(), findComplex<cphd::zint16_t>(addedPVP, it));
}
else if (it->second.getFormat() == "CI8")
{
::getData(dest + it->second.getByteOffset(), addedPVP.find(it->first)->second.getComplex<std::int32_t>());
::getData(dest + it->second.getByteOffset(), findComplex<cphd::zint32_t>(addedPVP, it));
}
else if (it->second.getFormat() == "CI16")
{
::getData(dest + it->second.getByteOffset(), addedPVP.find(it->first)->second.getComplex<std::int64_t>());
::getData(dest + it->second.getByteOffset(), findComplex<cphd::zint64_t>(addedPVP, it));
}
else if (it->second.getFormat() == "CF8")
{
::getData(dest + it->second.getByteOffset(), addedPVP.find(it->first)->second.getComplex<float>());
::getData(dest + it->second.getByteOffset(), findComplex<cphd::zfloat>(addedPVP, it));
}
else if (it->second.getFormat() == "CF16")
{
::getData(dest + it->second.getByteOffset(), addedPVP.find(it->first)->second.getComplex<double>());
::getData(dest + it->second.getByteOffset(), findComplex<cphd::zdouble>(addedPVP, it));
}
else
{
Expand Down
Loading

0 comments on commit d0f21ca

Please sign in to comment.