Using CPU flags in C
Florian Weimer
fweimer@redhat.com
Tue Nov 18 18:42:00 GMT 2025
* jh:
> I don't really know where to look for this information.
> Is it possible to use the CPU status flags to communicate information
> between inline assembly and the normal C control flow?
> For example, an asm statement would set the Z flag and then the C code
> would jump (for example with beq or bne in the case of ARM) without
> using any additional variable and comparison instruction. Using a
> dummy variable is no issue if the generated code is just the
> conditional branch instruction.
Do you mean something like this?
commit 119d658ac2aad88e306b4a66c1717e5ebf86c73f
Author: Uros Bizjak <ubizjak@gmail.com>
Date: Fri Aug 29 09:05:23 2025 +0200
x86: Use flag output operands for inline asm in atomic-machine.h
Use the flag output constraints feature available in gcc 6+
("=@cc<cond>") instead of explicitly setting a boolean variable
with SETcc instruction. This approach decouples the instruction
that sets the flags from the code that consumes them, allowing
the compiler to create better code when working with flags users.
Instead of e.g.:
lock add %esi,(%rdi)
sets %sil
test %sil,%sil
jne <...>
the compiler now generates:
lock add %esi,(%rdi)
js <...>
No functional changes intended.
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: H.J.Lu <hjl.tools@gmail.com>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Florian Weimer <fweimer@redhat.com>
<https://sourceware.org/cgit/glibc/commit/?id=119d658ac2aad88e306b4a66c1717e5ebf86c73f>
Thanks,
Florian
More information about the Gcc-help
mailing list