From 60102bd795faf855e67a2f026797c16a25b4c42c Mon Sep 17 00:00:00 2001 From: yuanhecai Date: Tue, 25 Aug 2026 19:29:00 +0800 Subject: [PATCH] loongarch: Fix RGBToY SIMD Add missing RGBToYMatrixRow_Any_{LSX,LASX} and kAddY offset fix so RAW/RGB24 to YUV works on LoongArch. Change-Id: I4c990a3705d7f521fa6d0c9e7e01f1e36f81c379 Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/8292090 Reviewed-by: Wan-Teh Chang Reviewed-by: Frank Barchard Commit-Queue: Wan-Teh Chang --- include/libyuv/row.h | 1 + source/convert.cc | 24 ++++++++++++++++++++++++ source/convert_from_argb.cc | 8 ++++++++ source/row_any.cc | 4 ++++ source/row_lasx.cc | 10 +++++----- source/row_lsx.cc | 19 ++++++++++--------- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/include/libyuv/row.h b/include/libyuv/row.h index 9dd340fc2..b748709c9 100644 --- a/include/libyuv/row.h +++ b/include/libyuv/row.h @@ -797,6 +797,7 @@ extern "C" { #define HAS_YUY2TOUV422ROW_LSX #define HAS_YUY2TOYROW_LSX #define HAS_ARGBTOYMATRIXROW_LSX +#define HAS_RGBTOYMATRIXROW_LSX #endif #if !defined(LIBYUV_DISABLE_LSX) && defined(__loongarch_sx) diff --git a/source/convert.cc b/source/convert.cc index e3864eec9..497b7b2bc 100644 --- a/source/convert.cc +++ b/source/convert.cc @@ -3399,6 +3399,14 @@ static int RAWToI444Matrix(const uint8_t* src_raw, } } #endif +#if defined(HAS_RGBTOYMATRIXROW_LSX) + if (TestCpuFlag(kCpuHasLSX)) { + RGBToYMatrixRow = RGBToYMatrixRow_Any_LSX; + if (IS_ALIGNED(width, 16)) { + RGBToYMatrixRow = RGBToYMatrixRow_LSX; + } + } +#endif #if defined(HAS_RGBTOYMATRIXROW_LASX) if (TestCpuFlag(kCpuHasLASX)) { RGBToYMatrixRow = RGBToYMatrixRow_Any_LASX; @@ -3822,6 +3830,14 @@ int RGB24ToJ400(const uint8_t* src_rgb24, } } #endif +#if defined(HAS_RGBTOYMATRIXROW_LSX) + if (TestCpuFlag(kCpuHasLSX)) { + RGBToYMatrixRow = RGBToYMatrixRow_Any_LSX; + if (IS_ALIGNED(width, 16)) { + RGBToYMatrixRow = RGBToYMatrixRow_LSX; + } + } +#endif #if defined(HAS_RGBTOYMATRIXROW_LASX) if (TestCpuFlag(kCpuHasLASX)) { RGBToYMatrixRow = RGBToYMatrixRow_Any_LASX; @@ -3911,6 +3927,14 @@ int RAWToJ400(const uint8_t* src_raw, } } #endif +#if defined(HAS_RGBTOYMATRIXROW_LSX) + if (TestCpuFlag(kCpuHasLSX)) { + RGBToYMatrixRow = RGBToYMatrixRow_Any_LSX; + if (IS_ALIGNED(width, 16)) { + RGBToYMatrixRow = RGBToYMatrixRow_LSX; + } + } +#endif #if defined(HAS_RGBTOYMATRIXROW_LASX) if (TestCpuFlag(kCpuHasLASX)) { RGBToYMatrixRow = RGBToYMatrixRow_Any_LASX; diff --git a/source/convert_from_argb.cc b/source/convert_from_argb.cc index 8c5ca7e7a..c8834a16a 100644 --- a/source/convert_from_argb.cc +++ b/source/convert_from_argb.cc @@ -2306,6 +2306,14 @@ static int RAWToNVMatrix(const uint8_t* src_raw, } } #endif +#if defined(HAS_RGBTOYMATRIXROW_LSX) + if (TestCpuFlag(kCpuHasLSX)) { + RGBToYMatrixRow = RGBToYMatrixRow_Any_LSX; + if (IS_ALIGNED(width, 16)) { + RGBToYMatrixRow = RGBToYMatrixRow_LSX; + } + } +#endif #if defined(HAS_RGBTOYMATRIXROW_LASX) if (TestCpuFlag(kCpuHasLASX)) { RGBToYMatrixRow = RGBToYMatrixRow_Any_LASX; diff --git a/source/row_any.cc b/source/row_any.cc index 7b4c27f0c..8fe41b098 100644 --- a/source/row_any.cc +++ b/source/row_any.cc @@ -2349,8 +2349,12 @@ ANY11MC(ARGB4444ToYMatrixRow_Any_NEON, ARGB4444ToYMatrixRow_NEON, 2, 15) #ifdef HAS_ARGBTOYMATRIXROW_LSX ANY11MC(ARGBToYMatrixRow_Any_LSX, ARGBToYMatrixRow_LSX, 4, 15) #endif +#ifdef HAS_RGBTOYMATRIXROW_LSX +ANY11MC(RGBToYMatrixRow_Any_LSX, RGBToYMatrixRow_LSX, 3, 15) +#endif #ifdef HAS_ARGBTOYMATRIXROW_LASX ANY11MC(ARGBToYMatrixRow_Any_LASX, ARGBToYMatrixRow_LASX, 4, 31) +ANY11MC(RGBToYMatrixRow_Any_LASX, RGBToYMatrixRow_LASX, 3, 31) #endif #ifdef HAS_ARGBTOYMATRIXROW_RVV ANY11MC(ARGBToYMatrixRow_Any_RVV, ARGBToYMatrixRow_RVV, 4, 15) diff --git a/source/row_lasx.cc b/source/row_lasx.cc index b743f9dd4..7a09e1406 100644 --- a/source/row_lasx.cc +++ b/source/row_lasx.cc @@ -1933,10 +1933,10 @@ void ARGBToYMatrixRow_LASX(const uint8_t* src_argb, } void RGBToYMatrixRow_LASX(const uint8_t* src_rgba, - uint8_t* dst_y, - int width, - const struct ArgbConstants* c) { - int8_t shuff[128] = { + uint8_t* dst_y, + int width, + const struct ArgbConstants* c) { + static const int8_t shuff[128] = { 0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23, 0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23, 24, 26, 27, 29, 30, 0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, @@ -1949,7 +1949,7 @@ void RGBToYMatrixRow_LASX(const uint8_t* src_rgba, "xvldrepl.b $xr0, %3, 0 \n\t" // load rgbconstants "xvldrepl.b $xr1, %3, 1 \n\t" // load rgbconstants "xvldrepl.b $xr2, %3, 2 \n\t" // load rgbconstants - "xvldrepl.h $xr3, %3, 4 \n\t" // load rgbconstants + "xvldrepl.h $xr3, %3, 96 \n\t" // load rgbconstants "xvld $xr4, %4, 0 \n\t" // load shuff "xvld $xr5, %4, 32 \n\t" "xvld $xr6, %4, 64 \n\t" diff --git a/source/row_lsx.cc b/source/row_lsx.cc index ae747513c..c4547e5c9 100644 --- a/source/row_lsx.cc +++ b/source/row_lsx.cc @@ -2646,19 +2646,20 @@ void ARGBToYMatrixRow_LSX(const uint8_t* src_argb, } void RGBToYMatrixRow_LSX(const uint8_t* src_rgba, - uint8_t* dst_y, - int width, - const struct ArgbConstants* c) { - int8_t shuff[64] = {0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, - 20, 21, 23, 24, 26, 27, 29, 30, 0, 1, 3, 4, 6, - 7, 9, 10, 12, 13, 15, 1, 0, 4, 0, 7, 0, 10, - 0, 13, 0, 16, 0, 19, 0, 22, 0, 25, 0, 28, 0, - 31, 0, 2, 0, 5, 0, 8, 0, 11, 0, 14, 0}; + uint8_t* dst_y, + int width, + const struct ArgbConstants* c) { + static const int8_t shuff[64] = { + 0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23, + 24, 26, 27, 29, 30, 0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, + 1, 0, 4, 0, 7, 0, 10, 0, 13, 0, 16, 0, 19, 0, 22, 0, + 25, 0, 28, 0, 31, 0, 2, 0, 5, 0, 8, 0, 11, 0, 14, 0, + }; asm volatile( "vldrepl.b $vr0, %3, 0 \n\t" // load rgbconstants "vldrepl.b $vr1, %3, 1 \n\t" // load rgbconstants "vldrepl.b $vr2, %3, 2 \n\t" // load rgbconstants - "vldrepl.h $vr3, %3, 4 \n\t" // load rgbconstants + "vldrepl.h $vr3, %3, 96 \n\t" // load rgbconstants "vld $vr4, %4, 0 \n\t" // load shuff "vld $vr5, %4, 16 \n\t" "vld $vr6, %4, 32 \n\t"