From 710884346794c7807fc2f4a2d3a3ec0cb490b5c0 Mon Sep 17 00:00:00 2001 From: Filip Wasil Date: Thu, 8 May 2025 15:39:32 +0200 Subject: [PATCH] riscv: Fix the run-time checking of the RVV availability Signed-off-by: Cosmin Truta --- CMakeLists.txt | 6 +++--- contrib/riscv-rvv/linux.c | 4 ++-- pngpriv.h | 10 ++++++++-- riscv/riscv_init.c | 10 ++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d37967f5..e2fdceff7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -323,7 +323,7 @@ if(PNG_HARDWARE_OPTIMIZATIONS) list(FIND PNG_RISCV_RVV_POSSIBLE_VALUES ${PNG_RISCV_RVV} index) if(index EQUAL -1) message(FATAL_ERROR "PNG_RISCV_RVV must be one of [${PNG_RISCV_RVV_POSSIBLE_VALUES}]") - elseif(NOT ${PNG_RISCV_RVV} STREQUAL "off") + elseif(NOT PNG_RISCV_RVV STREQUAL "off") check_c_compiler_flag("-march=rv64gv1p0" COMPILER_SUPPORTS_RVV) if(NOT COMPILER_SUPPORTS_RVV) message(FATAL_ERROR "Compiler does not support -march=rv64gv1p0 option") @@ -331,9 +331,9 @@ if(PNG_HARDWARE_OPTIMIZATIONS) set(libpng_riscv_sources riscv/filter_rvv_intrinsics.c riscv/riscv_init.c) - if(${PNG_RISCV_RVV} STREQUAL "on") + if(PNG_RISCV_RVV STREQUAL "on") add_definitions(-DPNG_RISCV_RVV_OPT=2) - elseif(${PNG_RISCV_RVV} STREQUAL "check") + elseif(PNG_RISCV_RVV STREQUAL "check") add_definitions(-DPNG_RISCV_RVV_CHECK_SUPPORTED) endif() else() diff --git a/contrib/riscv-rvv/linux.c b/contrib/riscv-rvv/linux.c index dc022704d..9babc09be 100644 --- a/contrib/riscv-rvv/linux.c +++ b/contrib/riscv-rvv/linux.c @@ -12,8 +12,8 @@ * STATUS: SUPPORTED * BUG REPORTS: png-mng-implement@sourceforge.net * - * png_have_rvv implemented for Linux by reading the widely available - * pseudo-file /proc/cpuinfo. + * png_have_rvv implemented for Linux by looking for COMPAT_HWCAP_ISA_V + * via hardware capabilites API. * * This code is strict ANSI-C and is probably moderately portable; it does * however use and it assumes that /proc/cpuinfo is never localized. diff --git a/pngpriv.h b/pngpriv.h index ad30e8d93..cb44a6aec 100644 --- a/pngpriv.h +++ b/pngpriv.h @@ -146,13 +146,19 @@ #ifndef PNG_RISCV_RVV_OPT /* RISCV_RVV optimizations are being controlled by the compiler settings, * typically the target compiler will define __riscv but the rvv extension - * availability has to be explicitly stated + * availability has to be explicitly stated. This is why if no + * PNG_RISCV_RVV_OPT was defined then a runtime check will be executed. * - * To enable RISCV_RVV optimizations, and compile the + * To enable RISCV_RVV optimizations unconditionally, and compile the * associated code, pass --enable-riscv-rvv=yes or --enable-riscv-rvv=on * to configure or put -DPNG_RISCV_RVV_OPT=2 in CPPFLAGS. */ + +# if defined(__riscv) && defined(PNG_ALIGNED_MEMORY_SUPPORTED) +# define PNG_RISCV_RVV_OPT 1 +# else # define PNG_RISCV_RVV_OPT 0 +# endif #endif #if PNG_ARM_NEON_OPT > 0 diff --git a/riscv/riscv_init.c b/riscv/riscv_init.c index aae57a709..fe3ffbb2b 100644 --- a/riscv/riscv_init.c +++ b/riscv/riscv_init.c @@ -16,24 +16,22 @@ #include -#ifdef PNG_RISCV_RVV_CHECK_SUPPORTED -#include -#endif - #ifdef PNG_RISCV_RVV_CHECK_SUPPORTED /* Do run-time checks */ /* WARNING: it is strongly recommended that you do not build libpng with - * run-time checks for CPU features if at all possible. In the case of the + * run-time checks for CPU features if at all possible. In the case of the * RISC-V Vector instructions there is no processor-specific way of detecting * the presence of the required support, therefore run-time detection is * extremely OS specific. * * You may set the macro PNG_RISCV_RVV_FILE to the file name of file containing - * a fragment of C source code which defines the png_have_neon function. There + * a fragment of C source code which defines the png_have_rvv function. There * are a number of implementations in contrib/riscv-rvv, but the only one that * has partial support is contrib/riscv-rvv/linux.c - a generic Linux * implementation which reads /proc/cpuinfo. */ +#include + #ifndef PNG_RISCV_RVV_FILE # if defined(__linux__) # define PNG_RISCV_RVV_FILE "contrib/riscv-rvv/linux.c"