Skip to content

Endian-dependent type-punning from vectors to tuples and arrays #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 97 additions & 14 deletions crates/coresimd/tests/endian_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ fn endian_bitcasts() {
8, 9, 10, 11, 12, 13, 14, 15,
);
let t: i16x8 = unsafe { mem::transmute(x) };
if cfg!(target_endian = "little") {
let t_el = i16x8::new(256, 770, 1284, 1798, 2312, 2826, 3340, 3854);
assert_eq!(t, t_el);
} else if cfg!(target_endian = "big") {
let t_be = i16x8::new(1, 515, 1029, 1543, 2057, 2571, 3085, 3599);
assert_eq!(t, t_be);
}
let e: i16x8 = if cfg!(target_endian = "little") {
i16x8::new(256, 770, 1284, 1798, 2312, 2826, 3340, 3854)
} else {
i16x8::new(1, 515, 1029, 1543, 2057, 2571, 3085, 3599)
};
assert_eq!(t, e);
}

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

if cfg!(target_endian = "little") {
let e: [i16; 8] = [256, 770, 1284, 1798, 2312, 2826, 3340, 3854];
assert_eq!(y, e);
} else if cfg!(target_endian = "big") {
let e: [i16; 8] = [1, 515, 1029, 1543, 2057, 2571, 3085, 3599];
assert_eq!(y, e);
}
let e: [i16; 8] = if cfg!(target_endian = "little") {
[256, 770, 1284, 1798, 2312, 2826, 3340, 3854]
} else {
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
};
assert_eq!(y, e);

let z = i8x16::load_unaligned(unsafe {
slice::from_raw_parts(&y as *const _ as *const i8, 16)
Expand Down Expand Up @@ -124,6 +122,24 @@ fn endian_array_union() {
);
let z = unsafe { B { data: y }.vec };
assert_eq!(z, e);

union C {
data: [i16; 8],
vec: i8x16,
}
#[cfg_attr(rustfmt, rustfmt_skip)]
let x = i8x16::new(
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
);
let x: [i16; 8] = unsafe { C { vec: x }.data };

let e: [i16; 8] = if cfg!(target_endian = "little") {
[256, 770, 1284, 1798, 2312, 2826, 3340, 3854]
} else {
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
};
assert_eq!(x, e);
}

#[test]
Expand Down Expand Up @@ -192,4 +208,71 @@ fn endian_tuple_access() {
7, 6, 5, 4, 3, 2, 1, 0
);
assert_eq!(e, z);

#[cfg_attr(rustfmt, rustfmt_skip)]
type I16x8T = (i16, i16, i16, i16, i16, i16, i16, i16);
union C {
data: I16x8T,
vec: i8x16,
}

#[cfg_attr(rustfmt, rustfmt_skip)]
let x = i8x16::new(
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
);
let x: I16x8T = unsafe { C { vec: x }.data };

let e: [i16; 8] = if cfg!(target_endian = "little") {
[256, 770, 1284, 1798, 2312, 2826, 3340, 3854]
} else {
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
};
assert_eq!(x.0, e[0]);
assert_eq!(x.1, e[1]);
assert_eq!(x.2, e[2]);
assert_eq!(x.3, e[3]);
assert_eq!(x.4, e[4]);
assert_eq!(x.5, e[5]);
assert_eq!(x.6, e[6]);
assert_eq!(x.7, e[7]);

// Without repr(C) this produces total garbage.
// FIXME: investigate more, this is maybe due to
// to tuple field reordering to minimize padding.
#[cfg_attr(rustfmt, rustfmt_skip)]
#[repr(C)]
#[derive(Copy ,Clone)]
pub struct Tup(pub i8, pub i8, pub i16, pub i8, pub i8, pub i16,
pub i8, pub i8, pub i16, pub i8, pub i8, pub i16);

union D {
data: Tup,
vec: i8x16,
}

#[cfg_attr(rustfmt, rustfmt_skip)]
let x = i8x16::new(
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
);
let x: Tup = unsafe { D { vec: x }.data };

let e: [i16; 12] = if cfg!(target_endian = "little") {
[0, 1, 770, 4, 5, 1798, 8, 9, 2826, 12, 13, 3854]
} else {
[0, 1, 515, 4, 5, 1543, 8, 9, 2571, 12, 13, 3599]
};
assert_eq!(x.0 as i16, e[0]);
assert_eq!(x.1 as i16, e[1]);
assert_eq!(x.2 as i16, e[2]);
assert_eq!(x.3 as i16, e[3]);
assert_eq!(x.4 as i16, e[4]);
assert_eq!(x.5 as i16, e[5]);
assert_eq!(x.6 as i16, e[6]);
assert_eq!(x.7 as i16, e[7]);
assert_eq!(x.8 as i16, e[8]);
assert_eq!(x.9 as i16, e[9]);
assert_eq!(x.10 as i16, e[10]);
assert_eq!(x.11 as i16, e[11]);
}