Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Use std::abs, instead of doing if (x < 0) x = -x manually #4943

Conversation

N-Dekker
Copy link
Contributor

Aims to make the code clearer. Cases found by regular expressions like (\w+) = -\1; and \w+ \*= -1;

Removed old comments from inside two DistanceToLine member functions, saying that they are "trying to avoid an expensive fabs". std::abs isn't that expensive anymore nowadays.

@github-actions github-actions bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Registration Issues affecting the Registration module type:Style Style changes: no logic impact (indentation, comments, naming) area:Numerics Issues affecting the Numerics module labels Nov 10, 2024
Comment on lines -168 to +171
RealType difference = static_cast<RealType>(t) - test.GetCenterPixel();
if (NumericTraits<RealType>::IsNegative(difference))
{
difference = -difference;
}
auto minimumDifference = static_cast<OutputPixelType>(difference);
RealType difference = std::abs(static_cast<RealType>(t) - test.GetCenterPixel());
auto minimumDifference = static_cast<OutputPixelType>(difference);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thewtex Regarding our discussion at discourse So yes, ComparisonImageFilter does take the absolute value! (This pull request just aims to make that clearer. It does not change the behavior, as far as I can see.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@N-Dekker great!

Comment on lines -240 to +241
// trying to avoid an expensive fabs
double tolerance = 1.e-05 * num;
if (tolerance < 0.0)
{
tolerance = -tolerance;
}
double tolerance = std::abs(1.e-05 * num);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "trying to avoid an expensive fabs" appears to be introduced by commit cc69c19 "ENH: Added EvaluatePosition()...", Julien Jomier (@jjomier), Nov 16, 2004. fabs might have been expensive twenty years ago. But nowadays, it appears to be the other way around! When compiler optimization is enabled, fabs appears to be compiled to just a single machine instruction! While the manual approach (if (x < 0)...) appears to yield quite a few more machine instructions! Example at https://godbolt.org/z/M8e6x8ssh

double Call_fabs(double x)
{
    return fabs(x);
}

double Call_std_abs(double x)
{
    return std::abs(x);
}

double ManuallyEstimateAbs(double x)
{
    if (x < 0)
    {
        return -x;
    }
    return x;
}

Output:

Call_fabs(double):
        andpd   xmm0, XMMWORD PTR .LC0[rip]
        ret
Call_std_abs(double):
        andpd   xmm0, XMMWORD PTR .LC0[rip]
        ret
ManuallyEstimateAbs(double):
        pxor    xmm2, xmm2
        movapd  xmm3, xmm0
        movapd  xmm1, xmm0
        xorpd   xmm1, XMMWORD PTR .LC2[rip]
        cmpltsd xmm0, xmm2
        andpd   xmm1, xmm0
        andnpd  xmm0, xmm3
        orpd    xmm0, xmm1
        ret
.LC0:
        .long   -1
        .long   2147483647
        .long   0
        .long   0
.LC2:
        .long   0
        .long   -2147483648
        .long   0
        .long   0

Aims to make the code clearer. Cases found by regular expressions like
` (\w+) = -\1;` and ` \w+ \*= -1;`

Removed old comments from inside two `DistanceToLine` member functions, saying
that they are "trying to avoid an expensive fabs". `std::abs` isn't that
expensive anymore nowadays.
@N-Dekker N-Dekker force-pushed the Use-std-abs-instead-of-manual-if branch from ec2d28f to a1e7863 Compare November 10, 2024 14:23
@N-Dekker N-Dekker marked this pull request as ready for review November 10, 2024 22:33
@@ -329,11 +329,7 @@ FloatAlmostEqual(T x1,
return false;
}

typename Detail::FloatIEEE<T>::IntType ulps = FloatDifferenceULP(x1, x2);
Copy link
Member

@thewtex thewtex Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@N-Dekker Are you sure that this has the same behavior, in particular with -0 and 0?

Copy link
Contributor Author

@N-Dekker N-Dekker Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing 0 versus -0 to the attention, Matt. In this particular case, FloatDifferenceULP returns an integer type. For integer types 0 and -0 are bit-wise equal, in C++. So exactly the same behavior, in this particular case.

Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@thewtex thewtex merged commit 70df21d into InsightSoftwareConsortium:master Nov 11, 2024
15 checks passed
@N-Dekker N-Dekker deleted the Use-std-abs-instead-of-manual-if branch November 15, 2024 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Numerics Issues affecting the Numerics module area:Registration Issues affecting the Registration module type:Style Style changes: no logic impact (indentation, comments, naming) type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants