Skip to content

Commit a5db4ea

Browse files
authored
replace some unions by transmute and make the rest repr(C) (#925)
1 parent 027680f commit a5db4ea

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

crates/core_arch/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
allow_internal_unstable,
4040
decl_macro
4141
)]
42-
#![cfg_attr(test, feature(test, abi_vectorcall, untagged_unions))]
42+
#![cfg_attr(test, feature(test, abi_vectorcall))]
4343
#![cfg_attr(all(test, target_arch = "wasm32"), feature(wasm_simd))]
4444
#![deny(clippy::missing_inline_in_public_items)]
4545
#![allow(

crates/core_arch/src/x86/test.rs

+10-35
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//! Utilities used in testing the x86 intrinsics
22
33
use crate::core_arch::x86::*;
4+
use std::mem::transmute;
45

56
#[target_feature(enable = "sse2")]
67
pub unsafe fn assert_eq_m128i(a: __m128i, b: __m128i) {
7-
union A {
8-
a: __m128i,
9-
b: [u64; 2],
10-
}
11-
assert_eq!(A { a }.b, A { a: b }.b)
8+
assert_eq!(transmute::<_, [u64; 2]>(a), transmute::<_, [u64; 2]>(b))
129
}
1310

1411
#[target_feature(enable = "sse2")]
@@ -20,11 +17,7 @@ pub unsafe fn assert_eq_m128d(a: __m128d, b: __m128d) {
2017

2118
#[target_feature(enable = "sse2")]
2219
pub unsafe fn get_m128d(a: __m128d, idx: usize) -> f64 {
23-
union A {
24-
a: __m128d,
25-
b: [f64; 2],
26-
};
27-
A { a }.b[idx]
20+
transmute::<_, [f64; 2]>(a)[idx]
2821
}
2922

3023
#[target_feature(enable = "sse")]
@@ -37,11 +30,7 @@ pub unsafe fn assert_eq_m128(a: __m128, b: __m128) {
3730

3831
#[target_feature(enable = "sse")]
3932
pub unsafe fn get_m128(a: __m128, idx: usize) -> f32 {
40-
union A {
41-
a: __m128,
42-
b: [f32; 4],
43-
};
44-
A { a }.b[idx]
33+
transmute::<_, [f32; 4]>(a)[idx]
4534
}
4635

4736
// not actually an intrinsic but useful in various tests as we proted from
@@ -53,11 +42,7 @@ pub unsafe fn _mm_setr_epi64x(a: i64, b: i64) -> __m128i {
5342

5443
#[target_feature(enable = "avx")]
5544
pub unsafe fn assert_eq_m256i(a: __m256i, b: __m256i) {
56-
union A {
57-
a: __m256i,
58-
b: [u64; 4],
59-
}
60-
assert_eq!(A { a }.b, A { a: b }.b)
45+
assert_eq!(transmute::<_, [u64; 4]>(a), transmute::<_, [u64; 4]>(b))
6146
}
6247

6348
#[target_feature(enable = "avx")]
@@ -70,11 +55,7 @@ pub unsafe fn assert_eq_m256d(a: __m256d, b: __m256d) {
7055

7156
#[target_feature(enable = "avx")]
7257
pub unsafe fn get_m256d(a: __m256d, idx: usize) -> f64 {
73-
union A {
74-
a: __m256d,
75-
b: [f64; 4],
76-
};
77-
A { a }.b[idx]
58+
transmute::<_, [f64; 4]>(a)[idx]
7859
}
7960

8061
#[target_feature(enable = "avx")]
@@ -87,11 +68,7 @@ pub unsafe fn assert_eq_m256(a: __m256, b: __m256) {
8768

8869
#[target_feature(enable = "avx")]
8970
pub unsafe fn get_m256(a: __m256, idx: usize) -> f32 {
90-
union A {
91-
a: __m256,
92-
b: [f32; 8],
93-
};
94-
A { a }.b[idx]
71+
transmute::<_, [f32; 8]>(a)[idx]
9572
}
9673

9774
// These intrinsics doesn't exist on x86 b/c it requires a 64-bit register,
@@ -101,6 +78,7 @@ mod x86_polyfill {
10178
use crate::core_arch::x86::*;
10279

10380
pub unsafe fn _mm_insert_epi64(a: __m128i, val: i64, idx: i32) -> __m128i {
81+
#[repr(C)]
10482
union A {
10583
a: __m128i,
10684
b: [i64; 2],
@@ -112,6 +90,7 @@ mod x86_polyfill {
11290

11391
#[target_feature(enable = "avx2")]
11492
pub unsafe fn _mm256_insert_epi64(a: __m256i, val: i64, idx: i32) -> __m256i {
93+
#[repr(C)]
11594
union A {
11695
a: __m256i,
11796
b: [i64; 4],
@@ -128,11 +107,7 @@ mod x86_polyfill {
128107
pub use self::x86_polyfill::*;
129108

130109
pub unsafe fn assert_eq_m512i(a: __m512i, b: __m512i) {
131-
union A {
132-
a: __m512i,
133-
b: [i32; 16],
134-
}
135-
assert_eq!(A { a }.b, A { a: b }.b)
110+
assert_eq!(transmute::<_, [i32; 16]>(a), transmute::<_, [i32; 16]>(b))
136111
}
137112

138113
pub unsafe fn assert_eq_m512(a: __m512, b: __m512) {

0 commit comments

Comments
 (0)