Skip to content

What should SIMD bitmasks look like? #126217

Open
@RalfJung

Description

@RalfJung

The portable-simd intrinsics simd_bitmask and simd_bitmask_select work with a bit-efficient representation of a SIMD vector of bool. Specifically they actually support two representations: as an integer of sufficient size, and as an array of u8.
However, the exact format of the array-based bitmask is endianess-dependent in subtle ways, so much so that portable-simd stopped using that format. The integer-based format is pretty simple (albeit still being endianess-dependent) but does not scale to vectors of arbitrary size.

IMO we should only have one format supported by the low-level intrinsics, and leave it to higher-level code like portable-simd to convert that to other formats if needed. That one format probably has to be the array-based one, since that is the only way to actually support vectors of arbitrary size. But what should that format look like? Currently it is endianess-dependent, and then portable-simd has to do some work to convert that into an endianess-independent format and back. Can we make the intrinsic directly use an endianess-independent format? (It is extremely rare for our intrinsics to behave in an endianess-dependent way.)

What are the key constraints the format has to satisfy? Without knowing those, here's a completely naive proposal:
the array must be big enough to contain at least as many bits as the vector has elements (but that's just a lower bound, arbitrarily bigger arrays are allowed), and then vector elements are mapped to bits in the array as follows: the vector element i is represented in array element i / 8, in the bit i % 8, where bits are indexed from most significant to least significant. So for instance the vector [-1, -1, -1, 0, 0, 0, 0, 0, 0, -1] becomes the bitmask [0b11100000, 0b01_000000], simply filling in the vector elements left-to right bit-for-bit and then padding with 0 until the array is full. I don't know if that format is any good -- probably it is not -- but it is certainly easy to explain. :)

Cc @calebzulawski @workingjubilee @programmerjake @rust-lang/opsem

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-SIMDArea: SIMD (Single Instruction Multiple Data)C-discussionCategory: Discussion or questions that doesn't represent real issues.PG-portable-simdProject group: Portable SIMD (https://github.com/rust-lang/project-portable-simd)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions