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

--- Comment #1 from Kang-Che Sung <Explorer09 at gmail dot com> ---
GCC can miss the optimization in some more cases:

```c
#include <stdint.h>
// 'src' is restrict-qualified. Modifying '*rem' won't touch '*src'.
void func3(const int32_t *restrict src, int32_t *quo, unsigned int *rem) {
    *rem = (unsigned int)*src % 4;
    *quo = (*src - (int)*rem) / 4;
}
void func3_b(const int32_t *restrict src, int32_t *quo, unsigned int *rem) {
    unsigned int tmp = (unsigned int)*src % 4;
    *quo = (*src - (int)tmp) / 4;
    *rem = (unsigned int)tmp;
}
void func3_c(const int32_t *restrict src, int32_t *quo, unsigned int *rem) {
    unsigned int tmp = (unsigned int)*src % 4;
    *quo = (*src - (int)tmp) / 4;
    *rem = (unsigned int)*src % 4;
}
```

https://godbolt.org/z/PWdcoaeaa

x86-64 gcc 15.2 with -Os option:

```assembly
func3:
        movl    (%rdi), %eax
        movq    %rdx, %rcx
        movl    %eax, %edx
        andl    $3, %edx
        subl    %edx, %eax
        movl    %edx, (%rcx)
        movl    $4, %ecx
        cltd
        idivl   %ecx
        movl    %eax, (%rsi)
        ret
func3_c:
        movl    (%rdi), %eax
        movl    %eax, %ecx
        andl    $3, %eax
        sarl    $2, %ecx
        movl    %ecx, (%rsi)
        movl    %eax, (%rdx)
        ret
```

Reply via email to