<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - bad code for __builtin_parity on x86 and ARM"
   href="https://bugs.llvm.org/show_bug.cgi?id=47433">47433</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>bad code for __builtin_parity on x86 and ARM
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>FreeBSD
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>fuzxxl@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I have found that __builtin_parity generates poor code on ARM (soft float).

    extern int foo(int x) {
        return __builtin_parity(x);
    }

compiles to

        mov     r1, #85
        and     r1, r1, r0, lsr #1
        sub     r0, r0, r1
        movw    r1, #13107
        movt    r1, #13107
        and     r2, r1, r0, lsr #2
        and     r0, r0, r1
        add     r0, r0, r2
        movw    r1, #3855
        movt    r1, #271
        add     r0, r0, r0, lsr #4
        and     r0, r0, r1
        movw    r1, #257
        movt    r1, #257
        mul     r0, r0, r1
        ubfx    r0, r0, #24, #1
        bx      lr

which essentially performs a population count and then truncates to one bit. 
This seems awfully suboptimal.  Why not a series of shifts and xors?

        eor r0, r0, r0, lsr #16
        eor r0, r0, r0, lsr #8
        eor r0, r0, r0, lsr #4
        eor r0, r0, r0, lsr #2
        eor r0, r0, r0, lsr #1
        and r0, r0, #1

This seems a lot better.  The builtin could also recognise if the input has a
width of less than 32 bit and perform less reductions if possible.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>