From 8166b9c5809b948b4dd38af5f775673a044aa152 Mon Sep 17 00:00:00 2001 From: sunyuechi Date: Mon, 16 Sep 2024 23:13:16 +0800 Subject: [PATCH] spa/support: implement RISCV V CPU detection --- meson.build | 15 +++++++++++++++ spa/include/spa/support/cpu.h | 3 +++ spa/plugins/support/cpu-riscv.c | 29 +++++++++++++++++++++++++++++ spa/plugins/support/cpu.c | 4 ++++ spa/plugins/support/meson.build | 10 +++++++++- 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 spa/plugins/support/cpu-riscv.c diff --git a/meson.build b/meson.build index f0559630f..cd7c37b28 100644 --- a/meson.build +++ b/meson.build @@ -177,6 +177,20 @@ elif cc.has_argument('-mfpu=neon') endif endif +have_rvv = false +if host_machine.cpu_family() == 'riscv64' + if cc.compiles(''' + int main() { + __asm__ __volatile__ ( + ".option arch, +v\nvsetivli zero, 0, e8, m1, ta, ma" + ); + } + ''', + name : 'riscv64 V Support') + have_rvv = true + endif +endif + libatomic = cc.find_library('atomic', required : false) test_8_byte_atomic = ''' @@ -237,6 +251,7 @@ if host_machine.endian() == 'big' endif check_headers = [ + ['sys/auxv.h', 'HAVE_SYS_AUXV_H'], ['sys/mount.h', 'HAVE_SYS_MOUNT_H'], ['sys/param.h', 'HAVE_SYS_PARAM_H'], ['sys/random.h', 'HAVE_SYS_RANDOM_H'], diff --git a/spa/include/spa/support/cpu.h b/spa/include/spa/support/cpu.h index 350dee78f..072b9be07 100644 --- a/spa/include/spa/support/cpu.h +++ b/spa/include/spa/support/cpu.h @@ -68,6 +68,9 @@ struct spa_cpu { struct spa_interface iface; }; #define SPA_CPU_FLAG_NEON (1 << 5) #define SPA_CPU_FLAG_ARMV8 (1 << 6) +/* RISCV specific */ +#define SPA_CPU_FLAG_RISCV_V (1 << 0) + #define SPA_CPU_FORCE_AUTODETECT ((uint32_t)-1) #define SPA_CPU_VM_NONE (0) diff --git a/spa/plugins/support/cpu-riscv.c b/spa/plugins/support/cpu-riscv.c new file mode 100644 index 000000000..e6ffa0ad7 --- /dev/null +++ b/spa/plugins/support/cpu-riscv.c @@ -0,0 +1,29 @@ +/* Spa */ +/* SPDX-FileCopyrightText: Copyright (c) 2023 Institue of Software Chinese Academy of Sciences (ISCAS). */ +/* SPDX-License-Identifier: MIT */ + +#ifdef HAVE_SYS_AUXV_H +#include +#define HWCAP_RV(letter) (1ul << ((letter) - 'A')) +#endif + +static int +riscv_init(struct impl *impl) +{ + uint32_t flags = 0; + +#ifdef HAVE_SYS_AUXV_H + const unsigned long hwcap = getauxval(AT_HWCAP); + if (hwcap & HWCAP_RV('V')) + flags |= SPA_CPU_FLAG_RISCV_V; +#endif + + impl->flags = flags; + + return 0; +} + +static int riscv_zero_denormals(void *object, bool enable) +{ + return 0; +} diff --git a/spa/plugins/support/cpu.c b/spa/plugins/support/cpu.c index 6db8278ec..b90391f37 100644 --- a/spa/plugins/support/cpu.c +++ b/spa/plugins/support/cpu.c @@ -64,6 +64,10 @@ static char *spa_cpu_read_file(const char *name, char *buffer, size_t len) #include "cpu-arm.c" #define init(t) arm_init(t) #define impl_cpu_zero_denormals arm_zero_denormals +# elif defined (__riscv) +#include "cpu-riscv.c" +#define init(t) riscv_init(t) +#define impl_cpu_zero_denormals riscv_zero_denormals # else #define init(t) #define impl_cpu_zero_denormals NULL diff --git a/spa/plugins/support/meson.build b/spa/plugins/support/meson.build index fc01036e3..05d98cda4 100644 --- a/spa/plugins/support/meson.build +++ b/spa/plugins/support/meson.build @@ -14,9 +14,17 @@ if have_sse simd_cargs += [sse_args, '-DHAVE_SSE'] endif +header_cargs = [] + +if host_machine.cpu_family() == 'riscv64' + if cdata.get('HAVE_SYS_AUXV_H') + header_cargs += ['-DHAVE_SYS_AUXV_H'] + endif +endif + spa_support_lib = shared_library('spa-support', spa_support_sources, - c_args : [ simd_cargs ], + c_args : [ simd_cargs, header_cargs ], dependencies : [ spa_dep, pthread_lib, epoll_shim_dep, mathlib ], install : true, install_dir : spa_plugindir / 'support')