Skip to content

Provide a way to const-initialize vendor-specific vector types #48745

@sfackler

Description

@sfackler

The platform independent simd types have constructors which are const fns, so they can be used in things like lookup tables: https://github.com/raw/sfackler/stream-vbyte64/ea0d5b0afdf97f31473fafbf33d460fbbb313785/src/tables.rs.

However, the same is not the case for the vendor-specific types like __m256i. There are platform intrinsics which initialize those types (e.g. _mm256_setr_epi32 is equivalent to i32x8::new) but those are not const fns.

One way to work around this is by type-punning through unions:

#![feature(stdsimd)]

use std::arch::x86_64::*;

#[repr(C)]
union Pun {
    a: __m256i,
    b: [u32; 8],
}

static FOO: Pun = Pun { b: [1, 2, 3, 4, 5, 6, 7, 8] };

fn main() {
    let x = unsafe { _mm256_extract_epi32(FOO.a, 3) };
    println!("{}", x);
}

This kind of union is pretty common in SIMD-related C code I've seen, and I think well defined behavior from what the unions RFC describes. Is this the "right" way to do this? Can/should we make functions like _mm256_set_epi32 const?

cc @alexcrichton

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-SIMDArea: SIMD (Single Instruction Multiple Data)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions