This is an archive of the discontinued LLVM Phabricator instance.

kmp_affinity: Fix check if specific bit is set
ClosedPublic

Authored by Hahnfeld on Jan 12 2017, 12:47 AM.

Details

Summary

Clang 4.0 trunk warns:

warning: logical not is only applied to the left hand side of this bitwise operator [-Wlogical-not-parentheses]

This points to a potential bug if the code really wants to check if the single bit is not set: If for example (buf.edx >> 9) = 2 (has any bit set except the
least significant one), 'logical not' will return 0 which stays 0 after the 'bitwise and'.
To do this correctly we first need to evaluate the 'bitwise and'. In that case it returns 2 & 1 = 0 which after the 'logical not' evaluates to 1.

Diff Detail

Repository
rL LLVM

Event Timeline

Hahnfeld updated this revision to Diff 84085.Jan 12 2017, 12:47 AM
Hahnfeld retitled this revision from to kmp_affinity: Fix check if specific bit is set.
Hahnfeld updated this object.
Hahnfeld added a subscriber: openmp-commits.

Good point. My personal preference would be to use "== 0", rather than logical not, since I find that easier to read.
So

if (((buf.edx >> 9) & 1) == 0) {
    ...
}

But your fix is fine too.

Good point. My personal preference would be to use "== 0", rather than logical not, since I find that easier to read.
So

if (((buf.edx >> 9) & 1) == 0) {
    ...
}

But your fix is fine too.

I agree, that looks easier to read. Will do the change on commit once approved.

AndreyChurbanov accepted this revision.Jan 12 2017, 3:29 AM
AndreyChurbanov edited edge metadata.

LGTM, either yours or Jim's suggestion.
Thanks.

This revision is now accepted and ready to land.Jan 12 2017, 3:29 AM
This revision was automatically updated to reflect the committed changes.