From f451a4de09eac5533f6da3cbc194e0416984713b Mon Sep 17 00:00:00 2001 From: Filip Wasil Date: Fri, 27 Jun 2025 13:00:56 +0200 Subject: [PATCH] riscv: Simplify the runtime check to always be present Signed-off-by: Cosmin Truta --- CMakeLists.txt | 9 +++--- configure.ac | 16 ++--------- riscv/riscv_init.c | 68 ++++------------------------------------------ 3 files changed, 12 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f802c40a8..3afd703b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -322,9 +322,9 @@ if(PNG_HARDWARE_OPTIMIZATIONS) # Set definitions and sources for RISC-V. if(PNG_TARGET_ARCHITECTURE MATCHES "^(riscv)") include(CheckCCompilerFlag) - set(PNG_RISCV_RVV_POSSIBLE_VALUES check on off) + set(PNG_RISCV_RVV_POSSIBLE_VALUES on off) set(PNG_RISCV_RVV "off" - CACHE STRING "Enable RISC-V Vector optimizations: check|on|off; off is default") + CACHE STRING "Enable RISC-V Vector optimizations: on|off; off is default") set_property(CACHE PNG_RISCV_RVV PROPERTY STRINGS ${PNG_RISCV_RVV_POSSIBLE_VALUES}) list(FIND PNG_RISCV_RVV_POSSIBLE_VALUES ${PNG_RISCV_RVV} index) @@ -354,9 +354,8 @@ if(PNG_HARDWARE_OPTIMIZATIONS) riscv/riscv_init.c) if(PNG_RISCV_RVV STREQUAL "on") add_definitions(-DPNG_RISCV_RVV_OPT=2) - elseif(PNG_RISCV_RVV STREQUAL "check") - add_definitions(-DPNG_RISCV_RVV_CHECK_SUPPORTED) - add_definitions(-DPNG_RISCV_RVV_OPT=1) + else() + add_definitions(-DPNG_RISCV_RVV_OPT=0) endif() else() add_definitions(-DPNG_RISCV_RVV_OPT=0) diff --git a/configure.ac b/configure.ac index ac963ff62..2cfee9cc3 100644 --- a/configure.ac +++ b/configure.ac @@ -679,8 +679,7 @@ AS_HELP_STRING([[[--enable-riscv-rvv]]], [Enable RISC-V Vector optimizations: =no/off, check, api, yes/on:] [no/off: disable the optimizations; check: use internal checking code] [api: disable by default, enable by a call to png_set_option] - [yes/on: turn on unconditionally.] - [If not specified: determined by the compiler.]), + [yes/on: turn on. If not specified: determined by the compiler.]), [case "$enableval" in no|off) # disable the default enabling on __riscv systems: @@ -688,20 +687,11 @@ AS_HELP_STRING([[[--enable-riscv-rvv]]], [Disable RISC-V Vector optimizations]) # Prevent inclusion of the platform-specific files below: enable_riscv_rvv=no ;; - check) - AC_DEFINE([PNG_RISCV_RVV_CHECK_SUPPORTED], [], - [Check for RISC-V Vector support at run-time]) - AC_MSG_WARN([--enable-riscv-rvv Please check contrib/riscv-rvv/README file] - [for the list of supported OSes.]);; - api) - AC_DEFINE([PNG_RISCV_RVV_API_SUPPORTED], [], - [Turn on RISC-V Vector optimizations at run-time]);; yes|on) AC_DEFINE([PNG_RISCV_RVV_OPT], [2], [Enable RISC-V Vector optimizations]) - AC_MSG_WARN([--enable-riscv-rvv: please specify 'check' or 'api', if] - [you want the optimizations unconditionally pass e.g. '-march=rv64gv1p0'] - [to the compiler.]);; + AC_MSG_WARN([--enable-riscv-rvv:] + [if you want the optimizations pass e.g. '-march=rv64gv1p0' to the compiler.]);; *) AC_MSG_ERROR([--enable-riscv-rvv=${enable_riscv_rvv}: invalid value]) esac]) diff --git a/riscv/riscv_init.c b/riscv/riscv_init.c index d3b91a53b..f3d92150d 100644 --- a/riscv/riscv_init.c +++ b/riscv/riscv_init.c @@ -16,20 +16,6 @@ #include -#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 - * 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_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 @@ -44,7 +30,6 @@ static int png_have_rvv(png_structp png_ptr); #ifdef PNG_RISCV_RVV_FILE # include PNG_RISCV_RVV_FILE #endif -#endif /* PNG_RISCV_RVV_CHECK_SUPPORTED */ #ifndef PNG_ALIGNED_MEMORY_SUPPORTED # error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED" @@ -53,59 +38,16 @@ static int png_have_rvv(png_structp png_ptr); void png_init_filter_functions_rvv(png_structp pp, unsigned int bpp) { - /* The switch statement is compiled in for RISCV_RVV_API, the call to - * png_have_rvv is compiled in for RISCV_RVV_CHECK. If both are - * defined the check is only performed if the API has not set the VECTOR - * option on or off explicitly. In this case the check controls what - * happens. - */ png_debug(1, "in png_init_filter_functions_rvv"); -#ifdef PNG_RISCV_RVV_API_SUPPORTED - switch ((pp->options >> PNG_RISCV_RVV) & 3) - { - case PNG_OPTION_UNSET: - /* Allow the run-time check to execute if it has been enabled - - * thus both API and CHECK can be turned on. If it isn't supported - * this case will fall through to the 'default' below, which just - * returns. - */ -#endif /* PNG_RISCV_RVV_API_SUPPORTED */ -#ifdef PNG_RISCV_RVV_CHECK_SUPPORTED - { - static volatile sig_atomic_t no_rvv = -1; /* not checked */ - if (no_rvv < 0) - no_rvv = !png_have_rvv(pp); + static volatile sig_atomic_t no_rvv = -1; /* not checked */ - if (no_rvv) - return; - } -#ifdef PNG_RISCV_RVV_API_SUPPORTED - break; -#endif -#endif /* PNG_RISCV_RVV_CHECK_SUPPORTED */ + if (no_rvv < 0) + no_rvv = !png_have_rvv(pp); -#ifdef PNG_RISCV_RVV_API_SUPPORTED - default: /* OFF or INVALID */ - return; + if (no_rvv) + return; - case PNG_OPTION_ON: - /* Option turned on */ - break; - } -#endif /* PNG_RISCV_RVV_API_SUPPORTED */ - - /* IMPORTANT: any new external functions used here must be declared using - * PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the - * 'prefix' option to configure works: - * - * ./configure --with-libpng-prefix=foobar_ - * - * Verify you have got this right by running the above command, doing a build - * and examining pngprefix.h; it must contain a #define for every external - * function you add. (Notice that this happens automatically for the - * initialization function.) - */ pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_rvv; if (bpp == 3)