Skip to content

Commit 0847ab2

Browse files
committed
Add RVV vector length
1 parent 1bc9d88 commit 0847ab2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

include/xsimd/config/xsimd_cpu_features_riscv.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#ifndef XSIMD_CPU_FEATURES_RISCV_HPP
1313
#define XSIMD_CPU_FEATURES_RISCV_HPP
1414

15+
#include <cstddef>
16+
#include <cstdint>
17+
1518
#include "./xsimd_config.hpp"
1619
#include "./xsimd_getauxval.hpp"
1720

@@ -24,16 +27,47 @@
2427

2528
namespace xsimd
2629
{
30+
namespace detail
31+
{
32+
using riscv_reg64_t = std::uint64_t;
33+
34+
/**
35+
* Return the RVV vector length in bytes.
36+
*
37+
* This does not require to be compiles with SVE, which should not
38+
* be done in a dynamic dispatch jump function.
39+
*
40+
* Safety: It is the user responsibility to first make sure that RVV is
41+
* available.
42+
*/
43+
inline riscv_reg64_t riscv_csrr_unsafe();
44+
}
45+
2746
class riscv_cpu_features : private linux_hwcap_backend_default
2847
{
2948
public:
3049
inline bool rvv() const noexcept;
50+
inline std::size_t rvv_size_bytes() const noexcept;
3151
};
3252

3353
/********************
3454
* Implementation *
3555
********************/
3656

57+
namespace detail
58+
{
59+
#if XSIMD_TARGET_RISCV && (defined(__GNUC__) || defined(__clang__))
60+
__attribute__((target("arch=+v"))) inline riscv_reg64_t riscv_csrr_unsafe()
61+
{
62+
riscv_reg64_t vlenb;
63+
__asm__ volatile("csrr %0, vlenb" : "=r"(vlenb));
64+
return vlenb;
65+
}
66+
#else
67+
inline riscv_reg64_t riscv_csrr_unsafe() { return 0; }
68+
#endif
69+
}
70+
3771
inline bool riscv_cpu_features::rvv() const noexcept
3872
{
3973
#if XSIMD_TARGET_RISCV && XSIMD_HAVE_LINUX_GETAUXVAL
@@ -47,6 +81,15 @@ namespace xsimd
4781
return false;
4882
#endif
4983
}
84+
85+
inline std::size_t riscv_cpu_features::rvv_size_bytes() const noexcept
86+
{
87+
if (rvv())
88+
{
89+
return detail::riscv_csrr_unsafe();
90+
}
91+
return 0;
92+
}
5093
}
5194

5295
#endif

test/test_cpu_features.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ TEST_CASE("[cpu_features] arm features from environment")
156156
CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_I8MM", cpu.i8mm());
157157
}
158158

159+
TEST_CASE("[cpu_features] risc-v implication chains")
160+
{
161+
xsimd::riscv_cpu_features cpu;
162+
163+
CHECK_IMPLICATION(cpu.rvv(), cpu.rvv_size_bytes() >= 128);
164+
}
165+
159166
TEST_CASE("[cpu_features] risc-v features from environment")
160167
{
161168
xsimd::riscv_cpu_features cpu;

0 commit comments

Comments
 (0)