Skip to content

[AArch64] Unnecessary addv for popcount(uint8_t) #158741

@Kmeakin

Description

@Kmeakin

https://godbolt.org/z/e9qPT9z81

C++ code

#include <cstdint>

auto popcount_u8(std::uint8_t x) { return __builtin_popcountg(x); }

Assembly (LLVM)

popcount_u8(unsigned char):
        and     w8, w0, #0xff
        fmov    s0, w8
        cnt     v0.8b, v0.8b
        addv    b0, v0.8b
        fmov    w0, s0
        ret

Assembly (GCC)

popcount_u8(unsigned char):
        and     w0, w0, 255
        fmov    d31, x0
        cnt     v31.8b, v31.8b
        smov    w0, v31.b[0]
        ret

As you can see from GCC's assembly, the addv is unecessary, because all the lanes except for the first will be zero.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions