@@ -726,31 +726,19 @@ pub unsafe fn uninitialized<T>() -> T {
726
726
#[ rustc_const_unstable( feature = "const_swap" , issue = "83163" ) ]
727
727
#[ rustc_diagnostic_item = "mem_swap" ]
728
728
pub const fn swap < T > ( x : & mut T , y : & mut T ) {
729
- // NOTE(eddyb) SPIR-V's Logical addressing model doesn't allow for arbitrary
730
- // reinterpretation of values as (chunkable) byte arrays, and the loop in the
731
- // block optimization in `swap_slice` is hard to rewrite back
732
- // into the (unoptimized) direct swapping implementation, so we disable it.
733
- #[ cfg( not( any( target_arch = "spirv" ) ) ) ]
734
- {
735
- // For types that are larger multiples of their alignment, the simple way
736
- // tends to copy the whole thing to stack rather than doing it one part
737
- // at a time, so instead treat them as one-element slices and piggy-back
738
- // the slice optimizations that will split up the swaps.
739
- if const { size_of :: < T > ( ) / align_of :: < T > ( ) > 2 } {
740
- // SAFETY: exclusive references always point to one non-overlapping
741
- // element and are non-null and properly aligned.
742
- return unsafe { ptr:: swap_nonoverlapping ( x, y, 1 ) } ;
729
+ cfg_if ! {
730
+ // NOTE(eddyb) SPIR-V's Logical addressing model doesn't allow for arbitrary
731
+ // reinterpretation of values as (chunkable) byte arrays, and the loop in the
732
+ // block optimization in `typed_swap_many` is hard to rewrite back
733
+ // into the (unoptimized) direct swapping implementation, so we disable it.
734
+ if #[ cfg( any( target_arch = "spirv" ) ) ] {
735
+ swap_simple( x, y)
736
+ } else {
737
+ // SAFETY: `&mut` guarantees these are typed readable and writable
738
+ // as well as non-overlapping.
739
+ unsafe { ptr:: typed_swap( x, y) }
743
740
}
744
741
}
745
-
746
- // If a scalar consists of just a small number of alignment units, let
747
- // the codegen just swap those pieces directly, as it's likely just a
748
- // few instructions and anything else is probably overcomplicated.
749
- //
750
- // Most importantly, this covers primitives and simd types that tend to
751
- // have size=align where doing anything else can be a pessimization.
752
- // (This will also be used for ZSTs, though any solution works for them.)
753
- swap_simple ( x, y) ;
754
742
}
755
743
756
744
/// Same as [`swap`] semantically, but always uses the simple implementation.
0 commit comments