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

            Bug ID: 91502
           Summary: suboptimal atomic_fetch_sub and atomic_fetch_add
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nruslan_devel at yahoo dot com
  Target Milestone: ---

I have not specified the gcc version; it seems like it applies to any version.

I have noticed that if I write code:

#include <stdatomic.h>

int func(_Atomic(int) *a)
{
        return (atomic_fetch_sub(a, 1) - 1 == 0);
}

gcc generates optimized code (gcc -O2):
func:
.LFB0:
        .cfi_startproc
        xorl    %eax, %eax
        lock subl       $1, (%rdi)
        sete    %al
        ret

But when I change the condition to <= 0, it does not work. Correct me if I am
wrong, but, I think, it should still be able to use sub:

#include <stdatomic.h>

int func(_Atomic(int) *a)
{
        return (atomic_fetch_sub(a, 1) - 1 <= 0);

}

func:
.LFB0:
        .cfi_startproc
        movl    $-1, %eax
        lock xaddl      %eax, (%rdi)
        cmpl    $1, %eax
        setle   %al
        movzbl  %al, %eax
        ret

Seems like the same problem exists for atomic_fetch_add as well.

Reply via email to