Skip to content

Commit f33f566

Browse files
jacobbramleyAmanieu
authored andcommitted
Clean up intrinsic-test literals.
- Ensure that C literals don't rely on undefined overflow behaviour. - We don't need to use 'as' casts, so remove them. - We weren't using allow(overflowing_literals), so remove it. - Format FP bit values as hex. This simplifies the test input initialisers in the generated files, making them shorter and easier to debug.
1 parent 6e7965d commit f33f566

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

crates/intrinsic-test/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ fn generate_rust_program(notices: &str, intrinsic: &Intrinsic, a32: bool) -> Str
174174
#![cfg_attr(target_arch = "aarch64", feature(stdarch_neon_sha3))]
175175
#![cfg_attr(target_arch = "aarch64", feature(stdarch_neon_sm4))]
176176
#![cfg_attr(target_arch = "aarch64", feature(stdarch_neon_ftts))]
177-
#![allow(overflowing_literals)]
178177
#![allow(non_upper_case_globals)]
179178
use core_arch::arch::{target_arch}::*;
180179

crates/intrinsic-test/src/types.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,38 @@ impl IntrinsicType {
305305
match self {
306306
IntrinsicType::Ptr { child, .. } => child.populate_random(loads, language),
307307
IntrinsicType::Type {
308-
bit_len: Some(bit_len),
309-
kind: TypeKind::Int | TypeKind::UInt | TypeKind::Poly,
308+
bit_len: Some(bit_len @ (8 | 16 | 32 | 64)),
309+
kind: kind @ (TypeKind::Int | TypeKind::UInt | TypeKind::Poly),
310310
simd_len,
311311
vec_len,
312312
..
313313
} => {
314-
let (prefix, as_type, suffix) = match language {
315-
&Language::Rust => ("[", format!(" as {}", self.rust_scalar_type()), "]"),
316-
&Language::C => ("{", "".into(), "}"),
314+
let (prefix, suffix) = match language {
315+
&Language::Rust => ("[", "]"),
316+
&Language::C => ("{", "}"),
317317
};
318318
format!(
319319
"{prefix}{body}{suffix}",
320320
body = (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1) + loads - 1))
321-
.format_with(", ", |i, fmt| fmt(&format_args!(
322-
"{src}{as_type}",
323-
src = value_for_array(*bit_len, i)
324-
)))
321+
.format_with(", ", |i, fmt| {
322+
let src = value_for_array(*bit_len, i);
323+
assert!(src == 0 || src.ilog2() < *bit_len);
324+
if *kind == TypeKind::Int && (src >> (*bit_len - 1)) != 0 {
325+
// `src` is a two's complement representation of a negative value.
326+
let mask = !0u64 >> (64 - *bit_len);
327+
let ones_compl = src ^ mask;
328+
let twos_compl = ones_compl + 1;
329+
if (twos_compl == src) && (language == &Language::C) {
330+
// `src` is INT*_MIN. C requires `-0x7fffffff - 1` to avoid
331+
// undefined literal overflow behaviour.
332+
fmt(&format_args!("-{ones_compl:#x} - 1"))
333+
} else {
334+
fmt(&format_args!("-{twos_compl:#x}"))
335+
}
336+
} else {
337+
fmt(&format_args!("{src:#x}"))
338+
}
339+
})
325340
)
326341
}
327342
IntrinsicType::Type {
@@ -342,7 +357,7 @@ impl IntrinsicType {
342357
"{prefix}{body}{suffix}",
343358
body = (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1) + loads - 1))
344359
.format_with(", ", |i, fmt| fmt(&format_args!(
345-
"{cast_prefix}{src}{cast_suffix}",
360+
"{cast_prefix}{src:#x}{cast_suffix}",
346361
src = value_for_array(*bit_len, i)
347362
)))
348363
)

crates/intrinsic-test/src/values.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
/// Gets a hex constant value for a single value in the argument values array in a determistic way
1+
/// Get a single value for an argument values array in a determistic way.
22
/// * `bits`: The number of bits for the type, only 8, 16, 32, 64 are valid values
33
/// * `index`: The position in the array we are generating for
4-
pub fn value_for_array(bits: u32, index: u32) -> String {
4+
pub fn value_for_array(bits: u32, index: u32) -> u64 {
55
let index = index as usize;
6-
7-
if bits == 8 {
8-
format!("{:#X}", VALUES_8[index % VALUES_8.len()])
9-
} else if bits == 16 {
10-
format!("{:#X}", VALUES_16[index % VALUES_16.len()])
11-
} else if bits == 32 {
12-
format!("{:#X}", VALUES_32[index % VALUES_32.len()])
13-
} else if bits == 64 {
14-
format!("{:#X}", VALUES_64[index % VALUES_64.len()])
15-
} else {
16-
panic!("Unknown size: {bits}");
6+
match bits {
7+
8 => VALUES_8[index % VALUES_8.len()].into(),
8+
16 => VALUES_16[index % VALUES_16.len()].into(),
9+
32 => VALUES_32[index % VALUES_32.len()].into(),
10+
64 => VALUES_64[index % VALUES_64.len()].into(),
11+
_ => unimplemented!("value_for_array(bits: {bits}, ..)"),
1712
}
1813
}
1914

0 commit comments

Comments
 (0)