Skip to content

Commit c824690

Browse files
feat: moved cast<T1, T2> to architecture-specific definitions
1 parent 9c1ec7d commit c824690

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

crates/intrinsic-test/src/arm/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ pub const POLY128_OSTREAM_DECL: &str = r#"
88
std::ostream& operator<<(std::ostream& os, poly128_t value);
99
std::ostream& operator<<(std::ostream& os, float16_t value);
1010
#endif
11+
12+
// T1 is the `To` type, T2 is the `From` type
13+
template<typename T1, typename T2> T1 cast(T2 x) {{
14+
static_assert(sizeof(T1) == sizeof(T2), "sizeof T1 and T2 must be the same");
15+
T1 ret{{}};
16+
memcpy(&ret, &x, sizeof(T1));
17+
return ret;
18+
}}
1119
"#;
1220

1321
pub const POLY128_OSTREAM_DEF: &str = r#"

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,6 @@ pub fn write_mod_cpp<T: IntrinsicTypeDefinition>(
122122

123123
writeln!(w, "{}", forward_declarations)?;
124124

125-
writeln!(
126-
w,
127-
r#"
128-
// T1 is the `To` type, T2 is the `From` type
129-
template<typename T1, typename T2> T1 cast(T2 x) {{
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-
}}
139-
}}
140-
"#
141-
)?;
142-
143125
for intrinsic in intrinsics {
144126
create_c_test_function(w, intrinsic)?;
145127
}

crates/intrinsic-test/src/x86/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ std::ostream& operator<<(std::ostream& os, __m512i value) {
269269
return os;
270270
}
271271
272+
// T1 is the `To` type, T2 is the `From` type
273+
template<typename T1, typename T2> T1 cast(T2 x) {{
274+
if (std::is_convertible<T2, T1>::value) {{
275+
return x;
276+
}} else if (sizeof(T1) == sizeof(T2)) {{
277+
T1 ret{{}};
278+
memcpy(&ret, &x, sizeof(T1));
279+
return ret;
280+
}} else {{
281+
assert("T2 must either be convertable to T1, or have the same size as T1!");
282+
}}
283+
}}
284+
272285
#define _mm512_extract_intrinsic_test_epi8(m, lane) \
273286
_mm_extract_epi8(_mm512_extracti64x2_epi64((m), (lane) / 16), (lane) % 16)
274287

0 commit comments

Comments
 (0)