[Openmp-commits] [PATCH] D28599: kmp_affinity: Fix check if specific bit is set

Jonas Hahnfeld via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu Jan 12 00:47:48 PST 2017


Hahnfeld created this revision.
Hahnfeld added reviewers: jlpeyton, AndreyChurbanov, tlwilmar.
Hahnfeld added a subscriber: openmp-commits.

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.


https://reviews.llvm.org/D28599

Files:
  runtime/src/kmp_affinity.cpp


Index: runtime/src/kmp_affinity.cpp
===================================================================
--- runtime/src/kmp_affinity.cpp
+++ runtime/src/kmp_affinity.cpp
@@ -1022,7 +1022,7 @@
         // The apic id and max threads per pkg come from cpuid(1).
         //
         __kmp_x86_cpuid(1, 0, &buf);
-        if (! (buf.edx >> 9) & 1) {
+        if (! ((buf.edx >> 9) & 1)) {
             __kmp_set_system_affinity(oldMask, TRUE);
             __kmp_free(threadInfo);
             KMP_CPU_FREE(oldMask);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28599.84085.patch
Type: text/x-patch
Size: 514 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20170112/e6cbcdcc/attachment.bin>


More information about the Openmp-commits mailing list