Skip to content

Commit 7454872

Browse files
chore: allowing cast() function to allow implicity type conversion for
certain cases (like uint32_t to uint64_t) extras: 1. added more C++ headers 2. typecasting integer constants (for example, the MM_FROUND arguments) for type compatibility
1 parent 5383867 commit 7454872

File tree

1 file changed

+24
-6
lines changed
  • crates/intrinsic-test/src/common

1 file changed

+24
-6
lines changed

crates/intrinsic-test/src/common/gen_c.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ use super::intrinsic_helpers::IntrinsicTypeDefinition;
66

77
// The number of times each intrinsic will be called.
88
const PASSES: u32 = 20;
9-
const COMMON_HEADERS: [&str; 5] = ["iostream", "string", "cstring", "iomanip", "sstream"];
9+
const COMMON_HEADERS: [&str; 7] = [
10+
"iostream",
11+
"string",
12+
"cstring",
13+
"iomanip",
14+
"sstream",
15+
"type_traits",
16+
"cassert",
17+
];
1018

1119
pub fn generate_c_test_loop<T: IntrinsicTypeDefinition + Sized>(
1220
w: &mut impl std::io::Write,
@@ -48,9 +56,13 @@ pub fn generate_c_constraint_blocks<'a, T: IntrinsicTypeDefinition + 'a>(
4856
let ty = current.ty.c_type();
4957

5058
writeln!(w, "{indentation}{{")?;
59+
60+
// TODO: Move to actually specifying the enum value
61+
// instead of typecasting integers, for better clarity
62+
// of generated code.
5163
writeln!(
5264
w,
53-
"{body_indentation}const {ty} {} = {i};",
65+
"{body_indentation}const {ty} {} = ({ty}){i};",
5466
current.generate_name()
5567
)?;
5668

@@ -113,11 +125,17 @@ pub fn write_mod_cpp<T: IntrinsicTypeDefinition>(
113125
writeln!(
114126
w,
115127
r#"
128+
// T1 is the `To` type, T2 is the `From` type
116129
template<typename T1, typename T2> T1 cast(T2 x) {{
117-
static_assert(sizeof(T1) == sizeof(T2), "sizeof T1 and T2 must be the same");
118-
T1 ret{{}};
119-
memcpy(&ret, &x, sizeof(T1));
120-
return ret;
130+
if (std::is_convertible<T2, T1>::value) {{
131+
return x;
132+
}} else if (sizeof(T1) == sizeof(T2)) {{
133+
T1 ret{{}};
134+
memcpy(&ret, &x, sizeof(T1));
135+
return ret;
136+
}} else {{
137+
assert("T2 must either be convertable to T1, or have the same size as T1!");
138+
}}
121139
}}
122140
"#
123141
)?;

0 commit comments

Comments
 (0)