https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127832
Bug ID: 127832
Summary: (a&1)?-1:1 could be better
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Target: aarch64
Take:
```
signed src(signed x)
{
return ((x&1)<<31)>>31 | 1;
}
signed tgt(signed x)
{
return (x&1) ? -1 : 1;
}
signed tgt1(signed x)
{
signed x1 = ~x;
signed x2 = x1 & 1;
return -1 + (x2<<1);
}
signed tgt2(signed x)
{
signed x1 = x;
signed x2 = x1 & 1;
return 1 - (x2<<1);
}
```
These are all different ways to express `(x&1) ? -1 : 1`. They all produce
different code.
src produces the best code for aarch64.
While for riscv, src and tgt produce the same code and is the best.