From 153d60c446cf054bd9f75107c392a9dd2258dade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 9 Nov 2026 15:19:31 +0100 Subject: [PATCH] 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 --- Common/Math/CrossSIMD.h | 12 ++++++++++++ unittest/TestCrossSIMD.cpp | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/Common/Math/CrossSIMD.h b/Common/Math/CrossSIMD.h index afac6e71a0..ea05f9f9e3 100644 --- a/Common/Math/CrossSIMD.h +++ b/Common/Math/CrossSIMD.h @@ -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. diff --git a/unittest/TestCrossSIMD.cpp b/unittest/TestCrossSIMD.cpp index 3f37a8dd3f..250cc0ab44 100644 --- a/unittest/TestCrossSIMD.cpp +++ b/unittest/TestCrossSIMD.cpp @@ -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 };