You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A common request in stdsimd is being able to initialize static variables with architecture-specific vector types.
There are many ways to hack this out, but AFAICt all of them basically reduce to using an union to implement a mem::transmute clone whose only different from mem::transmute is that it is a const fn.
Since this is already possible today, we should IMO just go ahead and make mem::transmute a const fn.
Reacted by Scott Olson, Shing Tak Lam, Gui Andrade, Jaro Fietz, Michael Bradshaw, Eira Fransham, Grégoire Geis, Paul Ellenbogen, Tim Diekmann, Alexander Huszagh and 10 more
There are many ways to hack this out, but AFAICt all of them basically reduce to using an union to implement a mem::transmute clone whose only different from mem::transmute is that it is a const fn.
I'm ideologically opposed to the generality and special-ness of transmute anyway, so I'm not a big fan of extending it or making it more general in any way. For the SIMD use case specifically, I'd rather see safe reinterpreting methods/constructors, analogous to f32::{to,from}_bits.
Reacted by Jonas Schievink, scottmcm and gnzlbg
Reacted by Robin Lambertz, robsmith11, Michael Bradshaw, Stephen Touset, Nathan Wiebe Neufeldt, Sympatron GmbH and Markus Unterwaditzer
For the SIMD use case specifically, I'd rather see safe reinterpreting methods/constructors, analogous to f32::{to,from}_bits.
These methods call transmute under the covers, and if transmute is not a const fn they basically have to re-implement their own transmute function that is.
Yes, and I am fine with that. It's an acceptable trade off for not presenting const fn transmute to users.
Reacted by Michael Bradshaw, Stephen Touset, James Leitch, Nathan Wiebe Neufeldt, Mitchell Kember, Douman, ylxdzsw, Ryan Butler, Markus Unterwaditzer, Troy Hinckley and 2 more
Yes, and I am fine with that. It's an acceptable trade off for not presenting const fn transmute to users.
The stdsimd crate currently offers FromBits/IntoBits traits that do what the f32/f64::from_bits functions do, but currently can't be const because trait methods can't be const (we could make these inherent methods by using from_{type}_bits or similar).
The current problem with this approach is that it requires O(N^2) implementations. A Compatible<T> approach like the one discussed here would be better, but it would still need const fn in trait methods.
But these are issues for any interface that is more specific than transmute, right? We'll definitely want such interfaces, so those problems needs solving in any case.
This has now been implemented behind a feature gate: #53535
The tracking issue is #53605 . Please raise any objections to the stabilization there.
My personal stance is that if we do not stabilize it, but allow unions in const fn, then users will just write
unionTransmute<T,U>{t:T,u:U,}pubconstunsafefnmy_hacky_transmute<T,U>(t:T) -> U{Transmute{ t }.u}
and everything will be worse with that than with transmute being const fn and having a bunch of type checks (size checks and lints about statically known bad transmutes)
Reacted by Ralf Jung, Benjamin Saunders, Nathan Wiebe Neufeldt, Artur Sinila, raldone01 and ⭐️NINIKA⭐️
Activity
solson commentedon Mar 28, 2018
The union-in-const transmute trick is actually already stable on nightly (without
const fn, even), but only works onCopytypes (which may work for SIMD, right?). Here's an example: https://play.rust-lang.org/?gist=c4c0b2c82ce182243d506b24af7d2e1c&version=nightlygnzlbg commentedon Mar 28, 2018
@solson yes, that's what I meant with:
hanna-kruppe commentedon Mar 28, 2018
I'm ideologically opposed to the generality and special-ness of
transmuteanyway, so I'm not a big fan of extending it or making it more general in any way. For the SIMD use case specifically, I'd rather see safe reinterpreting methods/constructors, analogous tof32::{to,from}_bits.gnzlbg commentedon Mar 28, 2018
These methods call
transmuteunder the covers, and iftransmuteis not aconst fnthey basically have to re-implement their owntransmutefunction that is.hanna-kruppe commentedon Mar 28, 2018
Yes, and I am fine with that. It's an acceptable trade off for not presenting
const fn transmuteto users.gnzlbg commentedon Mar 28, 2018
The
stdsimdcrate currently offersFromBits/IntoBitstraits that do what thef32/f64::from_bitsfunctions do, but currently can't be const because trait methods can't be const (we could make these inherent methods by usingfrom_{type}_bitsor similar).The current problem with this approach is that it requires
O(N^2)implementations. ACompatible<T>approach like the one discussed here would be better, but it would still needconst fnin trait methods.hanna-kruppe commentedon Mar 28, 2018
But these are issues for any interface that is more specific than
transmute, right? We'll definitely want such interfaces, so those problems needs solving in any case.oli-obk commentedon Oct 2, 2018
This has now been implemented behind a feature gate: #53535
The tracking issue is #53605 . Please raise any objections to the stabilization there.
My personal stance is that if we do not stabilize it, but allow unions in const fn, then users will just write
and everything will be worse with that than with
transmutebeing const fn and having a bunch of type checks (size checks and lints about statically known bad transmutes)