Skip to content

Commit

Permalink
C++17: use if constexpr() construct
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 15, 2023
1 parent 2cec1c1 commit 15563f5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 138 deletions.
31 changes: 17 additions & 14 deletions alg/gdalpansharpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ void GDALPansharpenOperation::WeightedBroveyWithNoData(
WorkDataType noData, validValue;
GDALCopyWord(psOptions->dfNoData, noData);

if (!(std::numeric_limits<WorkDataType>::is_integer))
if constexpr (!(std::numeric_limits<WorkDataType>::is_integer))
validValue = static_cast<WorkDataType>(noData + 1e-5);
else if (noData == std::numeric_limits<WorkDataType>::min())
validValue = std::numeric_limits<WorkDataType>::min() + 1;
Expand Down Expand Up @@ -655,8 +655,11 @@ void GDALPansharpenOperation::WeightedBrovey3(
j];
WorkDataType nPansharpenedValue;
GDALCopyWord(nRawValue * dfFactor, nPansharpenedValue);
if (bHasBitDepth && nPansharpenedValue > nMaxValue)
nPansharpenedValue = nMaxValue;
if constexpr (bHasBitDepth)
{
if (nPansharpenedValue > nMaxValue)
nPansharpenedValue = nMaxValue;
}
GDALCopyWord(nPansharpenedValue, pDataBuf[i * nBandValues + j]);
}
}
Expand All @@ -673,15 +676,15 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
size_t nValues, size_t nBandValues, T nMaxValue) const
{
CPL_STATIC_ASSERT(NINPUT == 3 || NINPUT == 4);
CPL_STATIC_ASSERT(NOUTPUT == 3 || NOUTPUT == 4);
static_assert(NINPUT == 3 || NINPUT == 4);
static_assert(NOUTPUT == 3 || NOUTPUT == 4);
const XMMReg4Double w0 =
XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 0);
const XMMReg4Double w1 =
XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 1);
const XMMReg4Double w2 =
XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 2);
const XMMReg4Double w3 =
[[maybe_unused]] const XMMReg4Double w3 =
(NINPUT == 3)
? XMMReg4Double::Zero()
: XMMReg4Double::Load1ValHighAndLow(psOptions->padfWeights + 3);
Expand All @@ -703,7 +706,7 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
XMMReg4Double val2 = XMMReg4Double::Load4Val(pUpsampledSpectralBuffer +
2 * nBandValues + j);
XMMReg4Double val3;
if (NINPUT == 4 || NOUTPUT == 4)
if constexpr (NINPUT == 4 || NOUTPUT == 4)
{
val3 = XMMReg4Double::Load4Val(pUpsampledSpectralBuffer +
3 * nBandValues + j);
Expand All @@ -712,7 +715,7 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
pseudoPanchro += w0 * val0;
pseudoPanchro += w1 * val1;
pseudoPanchro += w2 * val2;
if (NINPUT == 4)
if constexpr (NINPUT == 4)
pseudoPanchro += w3 * val3;

/* Little trick to avoid use of ternary operator due to one of the
Expand All @@ -724,14 +727,14 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
val0 = XMMReg4Double::Min(val0 * factor, maxValue);
val1 = XMMReg4Double::Min(val1 * factor, maxValue);
val2 = XMMReg4Double::Min(val2 * factor, maxValue);
if (NOUTPUT == 4)
if constexpr (NOUTPUT == 4)
{
val3 = XMMReg4Double::Min(val3 * factor, maxValue);
}
val0.Store4Val(pDataBuf + 0 * nBandValues + j);
val1.Store4Val(pDataBuf + 1 * nBandValues + j);
val2.Store4Val(pDataBuf + 2 * nBandValues + j);
if (NOUTPUT == 4)
if constexpr (NOUTPUT == 4)
{
val3.Store4Val(pDataBuf + 3 * nBandValues + j);
}
Expand All @@ -746,13 +749,13 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
const T *pPanBuffer, const T *pUpsampledSpectralBuffer, T *pDataBuf,
size_t nValues, size_t nBandValues, T nMaxValue) const
{
// cppcheck-suppress knownConditionTrueFalse
CPLAssert(NINPUT == 3 || NINPUT == 4);
static_assert(NINPUT == 3 || NINPUT == 4);
const double dfw0 = psOptions->padfWeights[0];
const double dfw1 = psOptions->padfWeights[1];
const double dfw2 = psOptions->padfWeights[2];
// cppcheck-suppress knownConditionTrueFalse
const double dfw3 = (NINPUT == 3) ? 0 : psOptions->padfWeights[3];
[[maybe_unused]] const double dfw3 =
(NINPUT == 3) ? 0 : psOptions->padfWeights[3];
size_t j = 0; // Used after for.
for (; j + 1 < nValues; j += 2)
{
Expand All @@ -772,7 +775,7 @@ size_t GDALPansharpenOperation::WeightedBroveyPositiveWeightsInternal(
dfPseudoPanchro2 +=
dfw2 * pUpsampledSpectralBuffer[2 * nBandValues + j + 1];

if (NINPUT == 4)
if constexpr (NINPUT == 4)
{
dfPseudoPanchro +=
dfw3 * pUpsampledSpectralBuffer[3 * nBandValues + j];
Expand Down
8 changes: 4 additions & 4 deletions alg/gdalwarpkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5700,14 +5700,14 @@ static void GWKResampleNoMasksOrDstDensityOnlyThreadInternal(void *pData)
for (int iBand = 0; iBand < poWK->nBands; iBand++)
{
T value = 0;
if (eResample == GRA_NearestNeighbour)
if constexpr (eResample == GRA_NearestNeighbour)
{
value = reinterpret_cast<T *>(
poWK->papabySrcImage[iBand])[iSrcOffset];
}
else if (bUse4SamplesFormula)
else if constexpr (bUse4SamplesFormula)
{
if (eResample == GRA_Bilinear)
if constexpr (eResample == GRA_Bilinear)
GWKBilinearResampleNoMasks4SampleT(
poWK, iBand, padfX[iDstX] - poWK->nSrcXOff,
padfY[iDstX] - poWK->nSrcYOff, &value);
Expand Down Expand Up @@ -5774,7 +5774,7 @@ static void GWKResampleNoMasksOrDstDensityOnlyHas4SampleThread(void *pData)
{
GWKJobStruct *psJob = static_cast<GWKJobStruct *>(pData);
GDALWarpKernel *poWK = psJob->poWK;
CPLAssert(eResample == GRA_Bilinear || eResample == GRA_Cubic);
static_assert(eResample == GRA_Bilinear || eResample == GRA_Cubic);
const bool bUse4SamplesFormula =
poWK->dfXScale >= 0.95 && poWK->dfYScale >= 0.95;
if (bUse4SamplesFormula)
Expand Down
16 changes: 8 additions & 8 deletions gcore/gdal_priv_templates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ inline void GDALGetDataLimits(Tin &tMaxValue, Tin &tMinValue)
tMinValue = std::numeric_limits<Tin>::min();

// Compute the actual minimum value of Tout in terms of Tin.
if (std::numeric_limits<Tout>::is_signed &&
std::numeric_limits<Tout>::is_integer)
if constexpr (std::numeric_limits<Tout>::is_signed &&
std::numeric_limits<Tout>::is_integer)
{
// the minimum value is less than zero
if (std::numeric_limits<Tout>::digits <
std::numeric_limits<Tin>::digits ||
!std::numeric_limits<Tin>::is_integer)
if constexpr (std::numeric_limits<Tout>::digits <
std::numeric_limits<Tin>::digits ||
!std::numeric_limits<Tin>::is_integer)
{
// Tout is smaller than Tin, so we need to clamp values in input
// to the range of Tout's min/max values
Expand All @@ -71,12 +71,12 @@ inline void GDALGetDataLimits(Tin &tMaxValue, Tin &tMinValue)
tMaxValue = static_cast<Tin>(std::numeric_limits<Tout>::max());
}
}
else if (std::numeric_limits<Tout>::is_integer)
else if constexpr (std::numeric_limits<Tout>::is_integer)
{
// the output is unsigned, so we just need to determine the max
/* coverity[same_on_both_sides] */
if (std::numeric_limits<Tout>::digits <=
std::numeric_limits<Tin>::digits)
if constexpr (std::numeric_limits<Tout>::digits <=
std::numeric_limits<Tin>::digits)
{
// Tout is smaller than Tin, so we need to clamp the input values
// to the range of Tout's max
Expand Down
Loading

0 comments on commit 15563f5

Please sign in to comment.