Skip to content

Convert _mm_slli_epi{16,32,64} & _mm_srli_epi{16,32,64} to const generics #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/core_arch/src/x86/avx512bw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5201,7 +5201,7 @@ pub unsafe fn _mm256_maskz_slli_epi16(k: __mmask16, a: __m256i, imm8: u32) -> __
pub unsafe fn _mm_mask_slli_epi16(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_slli_epi16(a, $imm8)
_mm_slli_epi16::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -5218,7 +5218,7 @@ pub unsafe fn _mm_mask_slli_epi16(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_slli_epi16(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_slli_epi16(a, $imm8)
_mm_slli_epi16::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -5530,7 +5530,7 @@ pub unsafe fn _mm256_maskz_srli_epi16(k: __mmask16, a: __m256i, imm8: i32) -> __
pub unsafe fn _mm_mask_srli_epi16(src: __m128i, k: __mmask8, a: __m128i, imm8: i32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srli_epi16(a, $imm8)
_mm_srli_epi16::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -5547,7 +5547,7 @@ pub unsafe fn _mm_mask_srli_epi16(src: __m128i, k: __mmask8, a: __m128i, imm8: i
pub unsafe fn _mm_maskz_srli_epi16(k: __mmask8, a: __m128i, imm8: i32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srli_epi16(a, $imm8)
_mm_srli_epi16::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down
16 changes: 8 additions & 8 deletions crates/core_arch/src/x86/avx512f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18184,7 +18184,7 @@ pub unsafe fn _mm256_maskz_slli_epi32(k: __mmask8, a: __m256i, imm8: u32) -> __m
pub unsafe fn _mm_mask_slli_epi32(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_slli_epi32(a, $imm8)
_mm_slli_epi32::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -18201,7 +18201,7 @@ pub unsafe fn _mm_mask_slli_epi32(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_slli_epi32(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_slli_epi32(a, $imm8)
_mm_slli_epi32::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -18309,7 +18309,7 @@ pub unsafe fn _mm256_maskz_srli_epi32(k: __mmask8, a: __m256i, imm8: u32) -> __m
pub unsafe fn _mm_mask_srli_epi32(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srli_epi32(a, $imm8)
_mm_srli_epi32::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -18326,7 +18326,7 @@ pub unsafe fn _mm_mask_srli_epi32(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_srli_epi32(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srli_epi32(a, $imm8)
_mm_srli_epi32::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -18434,7 +18434,7 @@ pub unsafe fn _mm256_maskz_slli_epi64(k: __mmask8, a: __m256i, imm8: u32) -> __m
pub unsafe fn _mm_mask_slli_epi64(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_slli_epi64(a, $imm8)
_mm_slli_epi64::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -18451,7 +18451,7 @@ pub unsafe fn _mm_mask_slli_epi64(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_slli_epi64(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_slli_epi64(a, $imm8)
_mm_slli_epi64::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down Expand Up @@ -18559,7 +18559,7 @@ pub unsafe fn _mm256_maskz_srli_epi64(k: __mmask8, a: __m256i, imm8: u32) -> __m
pub unsafe fn _mm_mask_srli_epi64(src: __m128i, k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srli_epi64(a, $imm8)
_mm_srli_epi64::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand All @@ -18576,7 +18576,7 @@ pub unsafe fn _mm_mask_srli_epi64(src: __m128i, k: __mmask8, a: __m128i, imm8: u
pub unsafe fn _mm_maskz_srli_epi64(k: __mmask8, a: __m128i, imm8: u32) -> __m128i {
macro_rules! call {
($imm8:expr) => {
_mm_srli_epi64(a, $imm8)
_mm_srli_epi64::<$imm8>(a)
};
}
let shf = constify_imm8_sae!(imm8, call);
Expand Down
82 changes: 29 additions & 53 deletions crates/core_arch/src/x86/sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,11 @@ pub unsafe fn _mm_bsrli_si128(a: __m128i, imm8: i32) -> __m128i {
#[inline]
#[target_feature(enable = "sse2")]
#[cfg_attr(test, assert_instr(psllw, imm8 = 7))]
#[rustc_args_required_const(1)]
#[rustc_legacy_const_generics(1)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_slli_epi16(a: __m128i, imm8: i32) -> __m128i {
let a = a.as_i16x8();
macro_rules! call {
($imm8:expr) => {
transmute(pslliw(a, $imm8))
};
}
constify_imm8!(imm8, call)
pub unsafe fn _mm_slli_epi16<const imm8: i32>(a: __m128i) -> __m128i {
static_assert_imm8!(imm8);
transmute(pslliw(a.as_i16x8(), imm8))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a static_assert! to ensure the immediate is between 0 and 255.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact use static_assert_imm8 that was added in #1021.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added range checks using static_assert_imm8.

What do you use as a reference point, to make decision whether it should be hard error or not? The fact that it is documented as imm8? clang, gcc, and icc, as far as I can see accept arbitrary values there (in fact run-time ones as well).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We follow the intel intrinsics data which specifies that the argument is an 8-bit immediate. There is a separate intrinsic (_mm_sll_epi32) which allows run-time values.

<intrinsic tech="SSE2" vexEq="TRUE" name="_mm_slli_epi32">
	<type>Integer</type>
	<CPUID>SSE2</CPUID>
	<category>Shift</category>
	<return type="__m128i" varname="dst" etype="UI32"/>
	<parameter type="__m128i" varname="a" etype="UI32"/>
	<parameter type="int" varname="imm8" etype="IMM" immwidth="8"/>
	<description>Shift packed 32-bit integers in "a" left by "imm8" while shifting in zeros, and store the results in "dst".</description>
	<operation>
FOR j := 0 to 3
	i := j*32
	IF imm8[7:0] &gt; 31
		dst[i+31:i] := 0
	ELSE
		dst[i+31:i] := ZeroExtend32(a[i+31:i] &lt;&lt; imm8[7:0])
	FI
ENDFOR
	</operation>
	<instruction name="PSLLD" form="xmm, imm8" xed="PSLLD_XMMdq_IMMb"/>
	<header>emmintrin.h</header>
</intrinsic>

The presence of an appropriate static_assert! should probably be enforced by stdarch-verify, I'll look into implementing that.

}

/// Shifts packed 16-bit integers in `a` left by `count` while shifting in
Expand All @@ -533,16 +528,11 @@ pub unsafe fn _mm_sll_epi16(a: __m128i, count: __m128i) -> __m128i {
#[inline]
#[target_feature(enable = "sse2")]
#[cfg_attr(test, assert_instr(pslld, imm8 = 7))]
#[rustc_args_required_const(1)]
#[rustc_legacy_const_generics(1)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_slli_epi32(a: __m128i, imm8: i32) -> __m128i {
let a = a.as_i32x4();
macro_rules! call {
($imm8:expr) => {
transmute(psllid(a, $imm8))
};
}
constify_imm8!(imm8, call)
pub unsafe fn _mm_slli_epi32<const imm8: i32>(a: __m128i) -> __m128i {
static_assert_imm8!(imm8);
transmute(psllid(a.as_i32x4(), imm8))
}

/// Shifts packed 32-bit integers in `a` left by `count` while shifting in
Expand All @@ -563,16 +553,11 @@ pub unsafe fn _mm_sll_epi32(a: __m128i, count: __m128i) -> __m128i {
#[inline]
#[target_feature(enable = "sse2")]
#[cfg_attr(test, assert_instr(psllq, imm8 = 7))]
#[rustc_args_required_const(1)]
#[rustc_legacy_const_generics(1)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_slli_epi64(a: __m128i, imm8: i32) -> __m128i {
let a = a.as_i64x2();
macro_rules! call {
($imm8:expr) => {
transmute(pslliq(a, $imm8))
};
}
constify_imm8!(imm8, call)
pub unsafe fn _mm_slli_epi64<const imm8: i32>(a: __m128i) -> __m128i {
static_assert_imm8!(imm8);
transmute(pslliq(a.as_i64x2(), imm8))
}

/// Shifts packed 64-bit integers in `a` left by `count` while shifting in
Expand Down Expand Up @@ -713,16 +698,11 @@ unsafe fn _mm_srli_si128_impl(a: __m128i, imm8: i32) -> __m128i {
#[inline]
#[target_feature(enable = "sse2")]
#[cfg_attr(test, assert_instr(psrlw, imm8 = 1))]
#[rustc_args_required_const(1)]
#[rustc_legacy_const_generics(1)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_srli_epi16(a: __m128i, imm8: i32) -> __m128i {
let a = a.as_i16x8();
macro_rules! call {
($imm8:expr) => {
transmute(psrliw(a, $imm8))
};
}
constify_imm8!(imm8, call)
pub unsafe fn _mm_srli_epi16<const imm8: i32>(a: __m128i) -> __m128i {
static_assert_imm8!(imm8);
transmute(psrliw(a.as_i16x8(), imm8))
}

/// Shifts packed 16-bit integers in `a` right by `count` while shifting in
Expand All @@ -744,16 +724,11 @@ pub unsafe fn _mm_srl_epi16(a: __m128i, count: __m128i) -> __m128i {
#[inline]
#[target_feature(enable = "sse2")]
#[cfg_attr(test, assert_instr(psrld, imm8 = 8))]
#[rustc_args_required_const(1)]
#[rustc_legacy_const_generics(1)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_srli_epi32(a: __m128i, imm8: i32) -> __m128i {
let a = a.as_i32x4();
macro_rules! call {
($imm8:expr) => {
transmute(psrlid(a, $imm8))
};
}
constify_imm8!(imm8, call)
pub unsafe fn _mm_srli_epi32<const imm8: i32>(a: __m128i) -> __m128i {
static_assert_imm8!(imm8);
transmute(psrlid(a.as_i32x4(), imm8))
}

/// Shifts packed 32-bit integers in `a` right by `count` while shifting in
Expand All @@ -775,9 +750,10 @@ pub unsafe fn _mm_srl_epi32(a: __m128i, count: __m128i) -> __m128i {
#[inline]
#[target_feature(enable = "sse2")]
#[cfg_attr(test, assert_instr(psrlq, imm8 = 1))]
#[rustc_args_required_const(1)]
#[rustc_legacy_const_generics(1)]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _mm_srli_epi64(a: __m128i, imm8: i32) -> __m128i {
pub unsafe fn _mm_srli_epi64<const imm8: i32>(a: __m128i) -> __m128i {
static_assert_imm8!(imm8);
transmute(psrliq(a.as_i64x2(), imm8))
}

Expand Down Expand Up @@ -3439,7 +3415,7 @@ mod tests {
let a = _mm_setr_epi16(
0xFFFF as u16 as i16, 0x0FFF, 0x00FF, 0x000F, 0, 0, 0, 0,
);
let r = _mm_slli_epi16(a, 4);
let r = _mm_slli_epi16::<4>(a);

#[rustfmt::skip]
let e = _mm_setr_epi16(
Expand All @@ -3460,7 +3436,7 @@ mod tests {

#[simd_test(enable = "sse2")]
unsafe fn test_mm_slli_epi32() {
let r = _mm_slli_epi32(_mm_set1_epi32(0xFFFF), 4);
let r = _mm_slli_epi32::<4>(_mm_set1_epi32(0xFFFF));
assert_eq_m128i(r, _mm_set1_epi32(0xFFFF0));
}

Expand All @@ -3474,7 +3450,7 @@ mod tests {

#[simd_test(enable = "sse2")]
unsafe fn test_mm_slli_epi64() {
let r = _mm_slli_epi64(_mm_set1_epi64x(0xFFFFFFFF), 4);
let r = _mm_slli_epi64::<4>(_mm_set1_epi64x(0xFFFFFFFF));
assert_eq_m128i(r, _mm_set1_epi64x(0xFFFFFFFF0));
}

Expand Down Expand Up @@ -3563,7 +3539,7 @@ mod tests {
let a = _mm_setr_epi16(
0xFFFF as u16 as i16, 0x0FFF, 0x00FF, 0x000F, 0, 0, 0, 0,
);
let r = _mm_srli_epi16(a, 4);
let r = _mm_srli_epi16::<4>(a);
#[rustfmt::skip]
let e = _mm_setr_epi16(
0xFFF as u16 as i16, 0xFF as u16 as i16, 0xF, 0, 0, 0, 0, 0,
Expand All @@ -3582,7 +3558,7 @@ mod tests {

#[simd_test(enable = "sse2")]
unsafe fn test_mm_srli_epi32() {
let r = _mm_srli_epi32(_mm_set1_epi32(0xFFFF), 4);
let r = _mm_srli_epi32::<4>(_mm_set1_epi32(0xFFFF));
assert_eq_m128i(r, _mm_set1_epi32(0xFFF));
}

Expand All @@ -3596,7 +3572,7 @@ mod tests {

#[simd_test(enable = "sse2")]
unsafe fn test_mm_srli_epi64() {
let r = _mm_srli_epi64(_mm_set1_epi64x(0xFFFFFFFF), 4);
let r = _mm_srli_epi64::<4>(_mm_set1_epi64x(0xFFFFFFFF));
assert_eq_m128i(r, _mm_set1_epi64x(0xFFFFFFF));
}

Expand Down