Skip to content

Commit 5d360a0

Browse files
feat: merging changes related to f16 formatting
1 parent a4f084e commit 5d360a0

File tree

7 files changed

+104
-6
lines changed

7 files changed

+104
-6
lines changed

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,93 @@ pub const POLY128_OSTREAM_DEF: &str = r#"std::ostream& operator<<(std::ostream&
2121
return os;
2222
}"#;
2323

24+
// Format f16 values (and vectors containing them) in a way that is consistent with C.
25+
pub const F16_FORMATTING_DEF: &str = r#"
26+
/// Used to continue `Debug`ging SIMD types as `MySimd(1, 2, 3, 4)`, as they
27+
/// were before moving to array-based simd.
28+
#[inline]
29+
fn debug_simd_finish<T: core::fmt::Debug, const N: usize>(
30+
formatter: &mut core::fmt::Formatter<'_>,
31+
type_name: &str,
32+
array: &[T; N],
33+
) -> core::fmt::Result {
34+
core::fmt::Formatter::debug_tuple_fields_finish(
35+
formatter,
36+
type_name,
37+
&core::array::from_fn::<&dyn core::fmt::Debug, N, _>(|i| &array[i]),
38+
)
39+
}
40+
41+
#[repr(transparent)]
42+
struct Hex<T>(T);
43+
44+
impl<T: DebugHexF16> core::fmt::Debug for Hex<T> {
45+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46+
<T as DebugHexF16>::fmt(&self.0, f)
47+
}
48+
}
49+
50+
fn debug_f16<T: DebugHexF16>(x: T) -> impl core::fmt::Debug {
51+
Hex(x)
52+
}
53+
54+
trait DebugHexF16 {
55+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
56+
}
57+
58+
impl DebugHexF16 for f16 {
59+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
60+
write!(f, "{:#06x?}", self.to_bits())
61+
}
62+
}
63+
64+
impl DebugHexF16 for float16x4_t {
65+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66+
let array = unsafe { core::mem::transmute::<_, [Hex<f16>; 4]>(*self) };
67+
debug_simd_finish(f, "float16x4_t", &array)
68+
}
69+
}
70+
71+
impl DebugHexF16 for float16x8_t {
72+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73+
let array = unsafe { core::mem::transmute::<_, [Hex<f16>; 8]>(*self) };
74+
debug_simd_finish(f, "float16x8_t", &array)
75+
}
76+
}
77+
78+
impl DebugHexF16 for float16x4x2_t {
79+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
80+
debug_simd_finish(f, "float16x4x2_t", &[Hex(self.0), Hex(self.1)])
81+
}
82+
}
83+
impl DebugHexF16 for float16x4x3_t {
84+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
85+
debug_simd_finish(f, "float16x4x3_t", &[Hex(self.0), Hex(self.1), Hex(self.2)])
86+
}
87+
}
88+
impl DebugHexF16 for float16x4x4_t {
89+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
90+
debug_simd_finish(f, "float16x4x4_t", &[Hex(self.0), Hex(self.1), Hex(self.2), Hex(self.3)])
91+
}
92+
}
93+
94+
impl DebugHexF16 for float16x8x2_t {
95+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
96+
debug_simd_finish(f, "float16x8x2_t", &[Hex(self.0), Hex(self.1)])
97+
}
98+
}
99+
impl DebugHexF16 for float16x8x3_t {
100+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
101+
debug_simd_finish(f, "float16x8x3_t", &[Hex(self.0), Hex(self.1), Hex(self.2)])
102+
}
103+
}
104+
impl DebugHexF16 for float16x8x4_t {
105+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
106+
debug_simd_finish(f, "float16x8x4_t", &[Hex(self.0), Hex(self.1), Hex(self.2), Hex(self.3)])
107+
}
108+
}
109+
"#;
110+
24111
pub const AARCH_CONFIGURATIONS: &str = r#"
25112
#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]
26113
#![cfg_attr(target_arch = "arm", feature(stdarch_aarch32_crc32))]
@@ -30,5 +117,6 @@ pub const AARCH_CONFIGURATIONS: &str = r#"
30117
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_sha3))]
31118
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_sm4))]
32119
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_ftts))]
120+
#![feature(fmt_helpers_for_derive)]
33121
#![feature(stdarch_neon_f16)]
34122
"#;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
8787
)
8888
}
8989

90-
pub fn generate_loop_c(
90+
fn generate_loop_c(
9191
&self,
9292
indentation: Indentation,
9393
additional: &str,
@@ -108,7 +108,7 @@ impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
108108
)
109109
}
110110

111-
pub fn generate_loop_rust(
111+
fn generate_loop_rust(
112112
&self,
113113
indentation: Indentation,
114114
additional: &str,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::common::gen_rust::compile_rust;
1313
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
1414
use crate::common::intrinsic_helpers::{BaseIntrinsicTypeDefinition, TypeKind};
1515
use crate::common::write_file::{write_c_testfiles, write_rust_testfiles};
16-
use config::{AARCH_CONFIGURATIONS, POLY128_OSTREAM_DEF, build_notices};
16+
use config::{AARCH_CONFIGURATIONS, F16_FORMATTING_DEF, POLY128_OSTREAM_DEF, build_notices};
1717
use json_parser::get_neon_intrinsics;
1818

1919
pub struct ArmArchitectureTest {
@@ -91,6 +91,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
9191
.collect::<Vec<_>>(),
9292
final_target,
9393
&build_notices("// "),
94+
F16_FORMATTING_DEF,
9495
AARCH_CONFIGURATIONS,
9596
);
9697

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::process::Command;
77

88
pub fn generate_rust_program(
99
notices: &str,
10+
definitions: &str,
1011
configurations: &str,
1112
arch_definition: &str,
1213
arglists: &str,
@@ -17,6 +18,7 @@ pub fn generate_rust_program(
1718
#![feature(link_llvm_intrinsics)]
1819
#![feature(f16)]
1920
{configurations}
21+
{definitions}
2022
#![allow(non_upper_case_globals)]
2123
use core_arch::arch::{arch_definition}::*;
2224

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ where
202202
}
203203
}
204204

205-
fn generate_rust_program(&self, target: &str, notice: &str, cfg: &str) -> String {
205+
fn generate_rust_program(
206+
&self,
207+
target: &str,
208+
notice: &str,
209+
definitions: &str,
210+
cfg: &str,
211+
) -> String {
206212
let arguments = self.arguments();
207213
let constraints = arguments
208214
.iter()
@@ -212,6 +218,7 @@ where
212218
let indentation = Indentation::default();
213219
generate_rust_program(
214220
notice,
221+
definitions,
215222
cfg,
216223
target,
217224
self.arguments()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn write_rust_testfiles<T: IntrinsicTypeDefinition>(
3232
intrinsics: Vec<&dyn IntrinsicDefinition<T>>,
3333
rust_target: &str,
3434
notice: &str,
35+
definitions: &str,
3536
cfg: &str,
3637
) -> Vec<String> {
3738
let intrinsics_name_list = intrinsics
@@ -41,7 +42,7 @@ pub fn write_rust_testfiles<T: IntrinsicTypeDefinition>(
4142
let filename_mapping = create_rust_filenames(&intrinsics_name_list);
4243

4344
intrinsics.iter().for_each(|i| {
44-
let rust_code = i.generate_rust_program(rust_target, notice, cfg);
45+
let rust_code = i.generate_rust_program(rust_target, notice, definitions, cfg);
4546
match filename_mapping.get(&i.name()) {
4647
Some(filename) => write_file(filename, rust_code),
4748
None => {}

crates/intrinsic-test/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(slice_partition_dedup)]
21
#[macro_use]
32
extern crate log;
43

0 commit comments

Comments
 (0)