Skip to content

Commit d9c89b6

Browse files
authored
Merge pull request #44 from afonso360/riscv-rvv
Add Distance functions for RISC-V Vector extensions
2 parents b9b1f73 + 9d3d39c commit d9c89b6

File tree

5 files changed

+1391
-2
lines changed

5 files changed

+1391
-2
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ else # linux
9696
STRIP = strip --strip-unneeded $@
9797
endif
9898

99+
# For RISC-V pass through the arch string directly to march
100+
ifneq (,$(findstring rv64,$(ARCH)))
101+
LDFLAGS += -march=$(ARCH)
102+
CFLAGS += -march=$(ARCH)
103+
endif
104+
99105
# Windows .def file generation
100106
$(DEF_FILE):
101107
ifeq ($(PLATFORM),windows)

src/distance-cpu.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "distance-sse2.h"
1818
#include "distance-avx2.h"
1919
#include "distance-avx512.h"
20+
#include "distance-rvv.h"
2021

2122
const char *distance_backend_name = "CPU";
2223
distance_function_t dispatch_distance_table[VECTOR_DISTANCE_MAX][VECTOR_TYPE_MAX] = {0};
@@ -828,7 +829,14 @@ float bit1_distance_hamming_cpu (const void *v1, const void *v2, int n) {
828829
x86_cpuid(1, 0, &eax, &ebx, &ecx, &edx);
829830
return (edx & (1 << 26)) != 0; // SSE2
830831
}
831-
832+
#elif defined(__riscv) || defined(__riscv__)
833+
#include <sys/auxv.h>
834+
#define ISA_V_HWCAP (1 << ('v' - 'a'))
835+
836+
bool cpu_supports_rvv (void) {
837+
unsigned long hw_cap = getauxval(AT_HWCAP);
838+
return (hw_cap & ISA_V_HWCAP) != 0;
839+
}
832840
#else
833841
// For ARM (NEON is always present on aarch64, runtime detection rarely needed)
834842
#if defined(__aarch64__) || defined(__ARM_NEON) || defined(__ARM_NEON__)
@@ -844,7 +852,7 @@ float bit1_distance_hamming_cpu (const void *v1, const void *v2, int n) {
844852
#include <sys/auxv.h>
845853
#include <asm/hwcap.h>
846854
bool cpu_supports_neon (void) {
847-
#ifdef AT_HWCAP
855+
#if defined(AT_HWCAP) && defined(HWCAP_NEON)
848856
return (getauxval(AT_HWCAP) & HWCAP_NEON) != 0;
849857
#else
850858
return false;
@@ -919,6 +927,10 @@ void init_distance_functions (bool force_cpu) {
919927
if (cpu_supports_neon()) {
920928
init_distance_functions_neon();
921929
}
930+
#elif defined(__riscv) || defined(__riscv__)
931+
if (cpu_supports_rvv()) {
932+
init_distance_functions_rvv();
933+
}
922934
#endif
923935
}
924936

0 commit comments

Comments
 (0)