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

            Bug ID: 124453
           Summary: suboptimal read of 64-bit bitfields
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

https://gcc.godbolt.org/z/9fPMqT77f:

```c
typedef __UINT64_TYPE__ uint64_t;

struct Value
  {
    uint64_t a :  1;
    uint64_t b :  4;
    uint64_t c : 59;
  };

int
ctz_b(const struct Value* p) 
  {
    struct Value val = *p;
    if(val.b == 0)
      return -1;
    return __builtin_ctz(val.b);
  }
```

GCC 15:

```
get_b:
        mov     rax, QWORD PTR [rdi]
        test    al, 30
        je      .L3
        shr     rax           # this just needs to be `shr eax`
        and     eax, 15
        rep bsf eax, eax
        ret
.L3:
        mov     eax, -1
        ret
```

GCC 16:

```
"get_b":
        mov     rdx, QWORD PTR [rdi]
        mov     rax, rdx      # should be `mov eax, edx`
        shr     rax           # likewise `shr eax`
        and     eax, 15
        rep bsf eax, eax
        and     edx, 30
        mov     edx, -1
        cmove   eax, edx
        ret
```

Reply via email to