Skip to content

Commit 4ea608d

Browse files
gnzlbgalexcrichton
authored andcommitted
endian-dependent conversions to/from tuples tests (#400)
1 parent 3879a74 commit 4ea608d

File tree

1 file changed

+97
-14
lines changed

1 file changed

+97
-14
lines changed

crates/coresimd/tests/endian_tests.rs

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ fn endian_bitcasts() {
2424
8, 9, 10, 11, 12, 13, 14, 15,
2525
);
2626
let t: i16x8 = unsafe { mem::transmute(x) };
27-
if cfg!(target_endian = "little") {
28-
let t_el = i16x8::new(256, 770, 1284, 1798, 2312, 2826, 3340, 3854);
29-
assert_eq!(t, t_el);
30-
} else if cfg!(target_endian = "big") {
31-
let t_be = i16x8::new(1, 515, 1029, 1543, 2057, 2571, 3085, 3599);
32-
assert_eq!(t, t_be);
33-
}
27+
let e: i16x8 = if cfg!(target_endian = "little") {
28+
i16x8::new(256, 770, 1284, 1798, 2312, 2826, 3340, 3854)
29+
} else {
30+
i16x8::new(1, 515, 1029, 1543, 2057, 2571, 3085, 3599)
31+
};
32+
assert_eq!(t, e);
3433
}
3534

3635
#[test]
@@ -61,13 +60,12 @@ fn endian_load_and_stores() {
6160
slice::from_raw_parts_mut(&mut y as *mut _ as *mut i8, 16)
6261
});
6362

64-
if cfg!(target_endian = "little") {
65-
let e: [i16; 8] = [256, 770, 1284, 1798, 2312, 2826, 3340, 3854];
66-
assert_eq!(y, e);
67-
} else if cfg!(target_endian = "big") {
68-
let e: [i16; 8] = [1, 515, 1029, 1543, 2057, 2571, 3085, 3599];
69-
assert_eq!(y, e);
70-
}
63+
let e: [i16; 8] = if cfg!(target_endian = "little") {
64+
[256, 770, 1284, 1798, 2312, 2826, 3340, 3854]
65+
} else {
66+
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
67+
};
68+
assert_eq!(y, e);
7169

7270
let z = i8x16::load_unaligned(unsafe {
7371
slice::from_raw_parts(&y as *const _ as *const i8, 16)
@@ -124,6 +122,24 @@ fn endian_array_union() {
124122
);
125123
let z = unsafe { B { data: y }.vec };
126124
assert_eq!(z, e);
125+
126+
union C {
127+
data: [i16; 8],
128+
vec: i8x16,
129+
}
130+
#[cfg_attr(rustfmt, rustfmt_skip)]
131+
let x = i8x16::new(
132+
0, 1, 2, 3, 4, 5, 6, 7,
133+
8, 9, 10, 11, 12, 13, 14, 15,
134+
);
135+
let x: [i16; 8] = unsafe { C { vec: x }.data };
136+
137+
let e: [i16; 8] = if cfg!(target_endian = "little") {
138+
[256, 770, 1284, 1798, 2312, 2826, 3340, 3854]
139+
} else {
140+
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
141+
};
142+
assert_eq!(x, e);
127143
}
128144

129145
#[test]
@@ -192,4 +208,71 @@ fn endian_tuple_access() {
192208
7, 6, 5, 4, 3, 2, 1, 0
193209
);
194210
assert_eq!(e, z);
211+
212+
#[cfg_attr(rustfmt, rustfmt_skip)]
213+
type I16x8T = (i16, i16, i16, i16, i16, i16, i16, i16);
214+
union C {
215+
data: I16x8T,
216+
vec: i8x16,
217+
}
218+
219+
#[cfg_attr(rustfmt, rustfmt_skip)]
220+
let x = i8x16::new(
221+
0, 1, 2, 3, 4, 5, 6, 7,
222+
8, 9, 10, 11, 12, 13, 14, 15,
223+
);
224+
let x: I16x8T = unsafe { C { vec: x }.data };
225+
226+
let e: [i16; 8] = if cfg!(target_endian = "little") {
227+
[256, 770, 1284, 1798, 2312, 2826, 3340, 3854]
228+
} else {
229+
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
230+
};
231+
assert_eq!(x.0, e[0]);
232+
assert_eq!(x.1, e[1]);
233+
assert_eq!(x.2, e[2]);
234+
assert_eq!(x.3, e[3]);
235+
assert_eq!(x.4, e[4]);
236+
assert_eq!(x.5, e[5]);
237+
assert_eq!(x.6, e[6]);
238+
assert_eq!(x.7, e[7]);
239+
240+
// Without repr(C) this produces total garbage.
241+
// FIXME: investigate more, this is maybe due to
242+
// to tuple field reordering to minimize padding.
243+
#[cfg_attr(rustfmt, rustfmt_skip)]
244+
#[repr(C)]
245+
#[derive(Copy ,Clone)]
246+
pub struct Tup(pub i8, pub i8, pub i16, pub i8, pub i8, pub i16,
247+
pub i8, pub i8, pub i16, pub i8, pub i8, pub i16);
248+
249+
union D {
250+
data: Tup,
251+
vec: i8x16,
252+
}
253+
254+
#[cfg_attr(rustfmt, rustfmt_skip)]
255+
let x = i8x16::new(
256+
0, 1, 2, 3, 4, 5, 6, 7,
257+
8, 9, 10, 11, 12, 13, 14, 15,
258+
);
259+
let x: Tup = unsafe { D { vec: x }.data };
260+
261+
let e: [i16; 12] = if cfg!(target_endian = "little") {
262+
[0, 1, 770, 4, 5, 1798, 8, 9, 2826, 12, 13, 3854]
263+
} else {
264+
[0, 1, 515, 4, 5, 1543, 8, 9, 2571, 12, 13, 3599]
265+
};
266+
assert_eq!(x.0 as i16, e[0]);
267+
assert_eq!(x.1 as i16, e[1]);
268+
assert_eq!(x.2 as i16, e[2]);
269+
assert_eq!(x.3 as i16, e[3]);
270+
assert_eq!(x.4 as i16, e[4]);
271+
assert_eq!(x.5 as i16, e[5]);
272+
assert_eq!(x.6 as i16, e[6]);
273+
assert_eq!(x.7 as i16, e[7]);
274+
assert_eq!(x.8 as i16, e[8]);
275+
assert_eq!(x.9 as i16, e[9]);
276+
assert_eq!(x.10 as i16, e[10]);
277+
assert_eq!(x.11 as i16, e[11]);
195278
}

0 commit comments

Comments
 (0)