Skip to content

Machines lacking AND-NOT should transform (a & x) | (~a & y) -> (a & (x ^ y)) ^ y #83637

@Validark

Description

@Validark

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions