[llvm-bugs] [Bug 26930] New: Incorrect optimisation of next power of 2 code.

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Mar 13 16:06:44 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=26930

            Bug ID: 26930
           Summary: Incorrect optimisation of next power of 2 code.
           Product: clang
           Version: 3.7
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: maandree at member.fsf.org
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

The following code is only executed correctly of compiled with -O0,
not if compiled with -O1, -O2, -O3, -Os, or -Oz.

#include <stddef.h>
int main(void)
{
    size_t i, x, n = 7;
    if ((n & (~n + 1)) != n) {
        n |= n >> 1;
        n |= n >> 2;
        n |= n >> 4;
        for (i = sizeof(n), x = 8; i; i >>= 1, x <<= 1)
            n |= n >> x;
        n += 1;
    }
    return n != 8;
}

If you run this code, the program's exit value should be 0 (n was
changed from 7 to 8), but if optimised it's exit value is 1 (n was
changed from 7 to something else than 8, 0 in my experience.)

The code shall change n to the next power of 2.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160313/e7c7c1a6/attachment.html>


More information about the llvm-bugs mailing list