loong64: Use HWCAP instead of CPUCFG to detect LSX/LASX

Per the Software Development and Build Convention for LoongArch™
Architectures manual, on Linux we should use HWCAP instead of CPUCFG to
detect if LSX/LASX is available.  The reason is the kernel may be
configured to disable them, and CPUCFG cannot provide info about the
kernel support.

Change-Id: I3f1b23e6d4c91c7da81311fbbe294e36ff178121
Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/6772567
Reviewed-by: Frank Barchard <fbarchard@chromium.org>
Reviewed-by: Wan-Teh Chang <wtc@google.com>
This commit is contained in:
Xi Ruoyao 2025-07-19 14:36:11 +08:00 committed by Frank Barchard
parent 96134e95a7
commit dd9ced1c6d

View File

@ -23,7 +23,7 @@
#include <stdio.h> // For fopen() #include <stdio.h> // For fopen()
#include <string.h> #include <string.h>
#if defined(__linux__) && defined(__aarch64__) #if defined(__linux__) && (defined(__aarch64__) || defined(__loongarch__))
#include <sys/auxv.h> // For getauxval() #include <sys/auxv.h> // For getauxval()
#endif #endif
@ -388,21 +388,20 @@ LIBYUV_API SAFEBUFFERS int MipsCpuCaps(const char* cpuinfo_name) {
return flag; return flag;
} }
#define LOONGARCH_CFG2 0x2 #if defined(__loongarch__) && defined(__linux__)
#define LOONGARCH_CFG2_LSX (1 << 6) // Define hwcap values ourselves: building with an old auxv header where these
#define LOONGARCH_CFG2_LASX (1 << 7) // hwcap values are not defined should not prevent features from being enabled.
#define YUV_LOONGARCH_HWCAP_LSX (1 << 4)
#define YUV_LOONGARCH_HWCAP_LASX (1 << 5)
#if defined(__loongarch__)
LIBYUV_API SAFEBUFFERS int LoongarchCpuCaps(void) { LIBYUV_API SAFEBUFFERS int LoongarchCpuCaps(void) {
int flag = 0; int flag = 0;
uint32_t cfg2 = 0; unsigned long hwcap = getauxval(AT_HWCAP);
__asm__ volatile("cpucfg %0, %1 \n\t" : "+&r"(cfg2) : "r"(LOONGARCH_CFG2)); if (hwcap & YUV_LOONGARCH_HWCAP_LSX)
if (cfg2 & LOONGARCH_CFG2_LSX)
flag |= kCpuHasLSX; flag |= kCpuHasLSX;
if (cfg2 & LOONGARCH_CFG2_LASX) if (hwcap & YUV_LOONGARCH_HWCAP_LASX)
flag |= kCpuHasLASX; flag |= kCpuHasLASX;
return flag; return flag;
} }