core/stdarch/crates/core_arch/src/x86/
f16c.rs1use crate::core_arch::{simd::*, x86::*};
6
7#[cfg(test)]
8use stdarch_test::assert_instr;
9
10#[allow(improper_ctypes)]
11unsafe extern "unadjusted" {
12 #[link_name = "llvm.x86.vcvtph2ps.128"]
13 fn llvm_vcvtph2ps_128(a: i16x8) -> f32x4;
14 #[link_name = "llvm.x86.vcvtph2ps.256"]
15 fn llvm_vcvtph2ps_256(a: i16x8) -> f32x8;
16 #[link_name = "llvm.x86.vcvtps2ph.128"]
17 fn llvm_vcvtps2ph_128(a: f32x4, rounding: i32) -> i16x8;
18 #[link_name = "llvm.x86.vcvtps2ph.256"]
19 fn llvm_vcvtps2ph_256(a: f32x8, rounding: i32) -> i16x8;
20}
21
22#[inline]
28#[target_feature(enable = "f16c")]
29#[cfg_attr(test, assert_instr("vcvtph2ps"))]
30#[stable(feature = "x86_f16c_intrinsics", since = "1.68.0")]
31pub fn _mm_cvtph_ps(a: __m128i) -> __m128 {
32 unsafe { transmute(llvm_vcvtph2ps_128(transmute(a))) }
33}
34
35#[inline]
40#[target_feature(enable = "f16c")]
41#[cfg_attr(test, assert_instr("vcvtph2ps"))]
42#[stable(feature = "x86_f16c_intrinsics", since = "1.68.0")]
43pub fn _mm256_cvtph_ps(a: __m128i) -> __m256 {
44 unsafe { transmute(llvm_vcvtph2ps_256(transmute(a))) }
45}
46
47#[inline]
61#[target_feature(enable = "f16c")]
62#[cfg_attr(test, assert_instr("vcvtps2ph", IMM_ROUNDING = 0))]
63#[rustc_legacy_const_generics(1)]
64#[stable(feature = "x86_f16c_intrinsics", since = "1.68.0")]
65pub fn _mm_cvtps_ph<const IMM_ROUNDING: i32>(a: __m128) -> __m128i {
66 static_assert_uimm_bits!(IMM_ROUNDING, 3);
67 unsafe {
68 let a = a.as_f32x4();
69 let r = llvm_vcvtps2ph_128(a, IMM_ROUNDING);
70 transmute(r)
71 }
72}
73
74#[inline]
87#[target_feature(enable = "f16c")]
88#[cfg_attr(test, assert_instr("vcvtps2ph", IMM_ROUNDING = 0))]
89#[rustc_legacy_const_generics(1)]
90#[stable(feature = "x86_f16c_intrinsics", since = "1.68.0")]
91pub fn _mm256_cvtps_ph<const IMM_ROUNDING: i32>(a: __m256) -> __m128i {
92 static_assert_uimm_bits!(IMM_ROUNDING, 3);
93 unsafe {
94 let a = a.as_f32x8();
95 let r = llvm_vcvtps2ph_256(a, IMM_ROUNDING);
96 transmute(r)
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use crate::{core_arch::x86::*, mem::transmute};
103 use stdarch_test::simd_test;
104
105 const F16_ONE: i16 = 0x3c00;
106 const F16_TWO: i16 = 0x4000;
107 const F16_THREE: i16 = 0x4200;
108 const F16_FOUR: i16 = 0x4400;
109 const F16_FIVE: i16 = 0x4500;
110 const F16_SIX: i16 = 0x4600;
111 const F16_SEVEN: i16 = 0x4700;
112 const F16_EIGHT: i16 = 0x4800;
113
114 #[simd_test(enable = "f16c")]
115 unsafe fn test_mm_cvtph_ps() {
116 let a = _mm_set_epi16(0, 0, 0, 0, F16_ONE, F16_TWO, F16_THREE, F16_FOUR);
117 let r = _mm_cvtph_ps(a);
118 let e = _mm_set_ps(1.0, 2.0, 3.0, 4.0);
119 assert_eq_m128(r, e);
120 }
121
122 #[simd_test(enable = "f16c")]
123 unsafe fn test_mm256_cvtph_ps() {
124 let a = _mm_set_epi16(
125 F16_ONE, F16_TWO, F16_THREE, F16_FOUR, F16_FIVE, F16_SIX, F16_SEVEN, F16_EIGHT,
126 );
127 let r = _mm256_cvtph_ps(a);
128 let e = _mm256_set_ps(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
129 assert_eq_m256(r, e);
130 }
131
132 #[simd_test(enable = "f16c")]
133 unsafe fn test_mm_cvtps_ph() {
134 let a = _mm_set_ps(1.0, 2.0, 3.0, 4.0);
135 let r = _mm_cvtps_ph::<_MM_FROUND_CUR_DIRECTION>(a);
136 let e = _mm_set_epi16(0, 0, 0, 0, F16_ONE, F16_TWO, F16_THREE, F16_FOUR);
137 assert_eq_m128i(r, e);
138 }
139
140 #[simd_test(enable = "f16c")]
141 unsafe fn test_mm256_cvtps_ph() {
142 let a = _mm256_set_ps(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
143 let r = _mm256_cvtps_ph::<_MM_FROUND_CUR_DIRECTION>(a);
144 let e = _mm_set_epi16(
145 F16_ONE, F16_TWO, F16_THREE, F16_FOUR, F16_FIVE, F16_SIX, F16_SEVEN, F16_EIGHT,
146 );
147 assert_eq_m128i(r, e);
148 }
149}