Skip to content

Missed optimization: per-byte comparisons are not always combined #117853

@purplesyringa

Description

@purplesyringa

Reproducer: https://godbolt.org/z/6caGGoo1e

int is_all_ones(unsigned char *p) {
    unsigned char a = p[0];
    unsigned char b = p[1];
    return a == 255 && b == 255;
}

I've expected this to compile to a single read + comparison on platforms that support unaligned accesses. Instead, this happened:

is_all_ones:
        movzx   ecx, byte ptr [rdi + 1]
        and     cl, byte ptr [rdi]
        xor     eax, eax
        cmp     cl, -1
        sete    al
        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