Skip to content

Commit

Permalink
STYLE: Replace old if by "constexpr if" in itk::Math implementation
Browse files Browse the repository at this point in the history
Enforced compile-time evaluation of conditions, when rounding and when doing
`CastWithRangeCheck`.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Jan 22, 2024
1 parent a8f034d commit 444acec
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Modules/Core/Common/include/itkMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ static constexpr float float_sqrteps = vnl_math::float_sqrteps;
template <typename TReturn, typename TInput> \
inline TReturn name(TInput x) \
{ \
\
if (sizeof(TReturn) <= 4) \
if constexpr (sizeof(TReturn) <= 4) \
{ \
return static_cast<TReturn>(Detail::name##_32(x)); \
} \
else if (sizeof(TReturn) <= 8) \
else if constexpr (sizeof(TReturn) <= 8) \
{ \
return static_cast<TReturn>(Detail::name##_64(x)); \
} \
Expand Down Expand Up @@ -219,14 +218,14 @@ CastWithRangeCheck(TInput x)
#endif // ITK_USE_CONCEPT_CHECKING

auto ret = static_cast<TReturn>(x);
if (sizeof(TReturn) > sizeof(TInput) &&
!(!itk::NumericTraits<TReturn>::is_signed && itk::NumericTraits<TInput>::is_signed))
if constexpr (sizeof(TReturn) > sizeof(TInput) &&
!(!itk::NumericTraits<TReturn>::is_signed && itk::NumericTraits<TInput>::is_signed))
{
// if the output type is bigger and we are not converting a signed
// integer to an unsigned integer then we have no problems
return ret;
}
else if (sizeof(TReturn) >= sizeof(TInput))
else if constexpr (sizeof(TReturn) >= sizeof(TInput))
{
if (itk::NumericTraits<TInput>::IsPositive(x) != itk::NumericTraits<TReturn>::IsPositive(ret))
{
Expand Down

0 comments on commit 444acec

Please sign in to comment.