[llvm-bugs] [Bug 43513] New: Missing 32 - __builtin_popcount(x | -x) -> __builtin_ctz(x)

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Sep 30 13:07:06 PDT 2019


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

            Bug ID: 43513
           Summary: Missing 32 - __builtin_popcount(x | -x) ->
                    __builtin_ctz(x)
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Transformation Utilities
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

-O3 -march=haswell

int foo(int i) {
    return i == 0 ? 32 : __builtin_ctz(i);
    // return __builtin_ctz(i);
}

int food(int i) {
    return 32 - __builtin_popcount(i | -i);
}

int foodd(int i) {
    return __builtin_popcount(~i & (i-1));
}

foo(int):                                # @foo(int)
        tzcnt   eax, edi
        ret
food(int):                               # @food(int)
        mov     eax, edi
        neg     eax
        or      eax, edi
        popcnt  ecx, eax
        mov     eax, 32
        sub     eax, ecx
        ret

foodd(int):                              # @foodd(int)
        lea     eax, [rdi - 1]
        andn    eax, edi, eax
        popcnt  eax, eax
        ret

https://godbolt.org/z/iNeY43

-- 
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/20190930/21366423/attachment.html>


More information about the llvm-bugs mailing list