[Bug c/81759] New: Improve data tracking for _pext_u64 and __builtin_ffsll

bugzilla@poradnik-webmastera.com gcc-bugzilla@gcc.gnu.org
Mon Aug 7 21:40:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81759

            Bug ID: 81759
           Summary: Improve data tracking for _pext_u64 and
                    __builtin_ffsll
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla@poradnik-webmastera.com
  Target Milestone: ---

This simple function gets index of least significant byte with its MSB cleared:

#include <stdint.h>
#include "immintrin.h"

uint64_t get_first_msb_cleared(uint64_t val)
{
  uint64_t msbs = _pext_u64(val, 0x8080808080808080);
  uint64_t msb_index = __builtin_ffsll(~msbs) - 1;
  return msb_index;
}

Here is assembly generated by gcc 7.1 (according to https://godbolt.org/):
get_first_msb_cleared(unsigned long):
  movabs rax, -9187201950435737472
  pext rdi, rdi, rax
  mov rax, -1
  not rdi
  bsf rdi, rdi
  cmove rdi, rax
  movsx rax, edi
  ret

gcc could do better job here. In this case value returned by _pext_u64 has bits
9-64 set to zero, so after negation they will be set to 1. Because of this gcc
could skip "mov rax, -1" and "cmove rdi, rax", this code path is dead.


More information about the Gcc-bugs mailing list