Open
Description
Machines lacking an AND-NOT instruction should transform select
into select2
. Godbolt link
#include <stdint.h>
uint64_t select(uint64_t a, uint64_t x, uint64_t y) {
return (a & x) | (~a & y);
}
uint64_t select2(uint64_t a, uint64_t x, uint64_t y) {
return (a & (x ^ y)) ^ y;
}
RISC-V emit:
select: # @select
and a1, a1, a0
not a0, a0
and a0, a0, a2
or a0, a0, a1
ret
select2: # @select2
xor a1, a1, a2
and a0, a0, a1
xor a0, a0, a2
ret