rust-lang / rust-lang/stdarch

Make constructors `const fn` where possible

Open
#906 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
694
Forks
336
Avg merge
5d 14h
Merged PRs (30d)
9

Description

I have a library which wraps SIMD types and it would be a nice feature if users were able to create const values with my wrapper types. There's currently no way of doing this in stable Rust.

It would be convenient if some constructor functions could be made const fn so we can create SIMD constants.

For example this is not currently possible:

const ONES : __m128 = unsafe { _mm_set1_ps(1.0) };

Internally functions like _mm_set1_ps are quite straight forward:

pub unsafe fn _mm_set1_ps(a: f32) -> __m128 {
  __m128(a, a, a, a)
}

__m128 is a repr(simd) tuple struct.

I would propose making all of the _mm_set* functions const fn and any other construction functions in stdarch for that matter.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the _mm_set* constructors and the __m128 repr(simd) tuple struct in stdarch. Check which construction functions can be declared const fn, then verify that the issue's const ONES example can create a SIMD constant on stable Rust. Done means the applicable constructors support const evaluation without changing unrelated APIs.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.