CrossSIMD: Add Vec4S32::StoreS16Sat

A signed saturating narrow to 16 bits. Vec4U16::FromVec4S32 can't stand
in for it: it saturates unsigned on SSE and LSX and truncates on NEON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-11-09 17:29:39 +01:00
1 parent 463a85d8ab
commit 153d60c446
2 files changed
+20

No files matched your search

+12
View File
@@ -163,6 +163,8 @@ struct Vec4S32 {
void Store(int *dst) const { _mm_storeu_si128((__m128i *)dst, v); }
void Store2(int *dst) const { _mm_storel_epi64((__m128i *)dst, v); }
// Four signed 16-bit values, each lane clamped to -32768..32767.
void StoreS16Sat(int16_t *dst) const { _mm_storel_epi64((__m128i *)dst, _mm_packs_epi32(v, v)); }
void StoreAligned(int *dst) const { _mm_store_si128((__m128i *)dst, v);}
Vec4S32 SignBits32ToMask() {
@@ -684,6 +686,8 @@ struct Vec4S32 {
void Store(int *dst) const { vst1q_s32(dst, v); }
void Store2(int *dst) const { vst1_s32(dst, vget_low_s32(v)); }
// Four signed 16-bit values, each lane clamped to -32768..32767.
void StoreS16Sat(int16_t *dst) const { vst1_s16(dst, vqmovn_s32(v)); }
void StoreAligned(int *dst) const { vst1q_s32(dst, v); }
// Warning: Unlike on x86, this is a full 32-bit multiplication.
@@ -1276,6 +1280,8 @@ struct Vec4S32 {
void Store(int *dst) const { __lsx_vst(v, dst, 0); }
void Store2(int *dst) const { __lsx_vstelm_d(v, dst, 0, 0); }
// Four signed 16-bit values, each lane clamped to -32768..32767.
void StoreS16Sat(int16_t *dst) const { __lsx_vstelm_d(__lsx_vssrani_h_w(v, v, 0), dst, 0, 0); }
void StoreAligned(int *dst) const { __lsx_vst(v, dst, 0); }
// Warning: Unlike on x86, this is a full 32-bit multiplication.
@@ -1771,6 +1777,12 @@ struct Vec4S32 {
void Store(int *dst) const { memcpy(dst, v, sizeof(v)); }
void Store2(int *dst) const { memcpy(dst, v, sizeof(v[0]) * 2); }
// Four signed 16-bit values, each lane clamped to -32768..32767.
void StoreS16Sat(int16_t *dst) const {
for (int i = 0; i < 4; i++) {
dst[i] = (int16_t)(v[i] < -32768 ? -32768 : (v[i] > 32767 ? 32767 : v[i]));
}
}
void StoreAligned(int *dst) const { memcpy(dst, v, sizeof(v)); }
// Warning: Unlike on x86 SSE2, this is a full 32-bit multiplication.
+8
View File
@@ -92,6 +92,14 @@ static bool TestVec4S32() {
EXPECT_EQ_INT(result[i], a_values[i] * b_values[i]);
}
const int sat_values[4] = { 40000, -40000, -32768, 1234 };
const int16_t sat_expected[4] = { 32767, -32768, -32768, 1234 };
int16_t sat_result[4];
Vec4S32::Load(sat_values).StoreS16Sat(sat_result);
for (int i = 0; i < 4; i++) {
EXPECT_EQ_INT(sat_result[i], sat_expected[i]);
}
// Mul16 only promises correct results when both sides fit in 16 bits.
const int small_a[4] = { 3, -7, 300, -1 };
const int small_b[4] = { 5, 11, -2, 1000 };