Skip to content

Loop counting top bits set badly vectorized on X86 #45398

@nikic

Description

@nikic
Bugzilla Link 46053
Version trunk
OS Linux
CC @Dushistov,@efriedma-quic,@fhahn,@hsivonen,@RKSimon,@rotateright

Extended Description

Originally reported at: rust-lang/rust#72355.

A loop counting the number of bytes with the top bit set:

#include <stddef.h>
#include <stdint.h>

uint64_t count_non_ascii(unsigned *buffer, size_t len) {
    uint64_t count = 0;
    for (size_t i = 0; i < len; i++) {
        count += buffer[i] >= 0x80;
    }
    return count;
}

Gets vectorized to some complicated code: https://c.godbolt.org/z/jTU3rX

While this could be just movmsk + popcnt.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions