From 707cd64b8a942de7fcc66575619c9b5d25d94104 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Thu, 8 Aug 2024 09:54:51 +0200 Subject: [PATCH] [v1.05][ARM] Another try at properly implement SVE detection: move it to a separate file --- Makefile | 10 +++++++--- src/arm/midr.c | 16 +--------------- src/arm/sve.c | 16 ++++++++++++++++ src/arm/sve.h | 6 ++++++ 4 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 src/arm/sve.c create mode 100644 src/arm/sve.h diff --git a/Makefile b/Makefile index 8c3b9e6..a4e5d13 100644 --- a/Makefile +++ b/Makefile @@ -38,13 +38,14 @@ ifneq ($(OS),Windows_NT) CFLAGS += -DARCH_PPC -std=gnu99 -fstack-protector-all -Wno-language-extension-token else ifeq ($(arch), $(filter $(arch), arm aarch64_be aarch64 arm64 armv8b armv8l armv7l armv6l)) SRC_DIR=src/arm/ - SOURCE += $(COMMON_SRC) $(SRC_DIR)midr.c $(SRC_DIR)uarch.c $(SRC_COMMON)soc.c $(SRC_DIR)soc.c $(SRC_COMMON)pci.c $(SRC_DIR)udev.c + SOURCE += $(COMMON_SRC) $(SRC_DIR)midr.c $(SRC_DIR)uarch.c $(SRC_COMMON)soc.c $(SRC_DIR)soc.c $(SRC_COMMON)pci.c $(SRC_DIR)udev.c sve.o HEADERS += $(COMMON_HDR) $(SRC_DIR)midr.h $(SRC_DIR)uarch.h $(SRC_COMMON)soc.h $(SRC_DIR)soc.h $(SRC_COMMON)pci.h $(SRC_DIR)udev.c $(SRC_DIR)socs.h CFLAGS += -DARCH_ARM -Wno-unused-parameter -std=c99 -fstack-protector-all - is_sve_flag_supported := $(shell touch foo.c && $(CC) -march=armv8-a+sve -c foo.c -o foo.o &> /dev/null && echo 'yes'; rm -f foo.c foo.o) + # Check if the compiler supports -march=armv8-a+sve. We will use it (if supported) to compile SVE detection code later + is_sve_flag_supported := $(shell $(CC) -march=armv8-a+sve -c $(SRC_DIR)sve.c -o sve_test.o 2> /dev/null && echo 'yes'; rm -f sve_test.o) ifeq ($(is_sve_flag_supported), yes) - CFLAGS += -march=armv8-a+sve + SVE_FLAGS += -march=armv8-a+sve endif ifeq ($(os), Darwin) @@ -96,6 +97,9 @@ freq_avx.o: Makefile $(SRC_DIR)freq/freq_avx.c $(SRC_DIR)freq/freq_avx.h $(SRC_D freq_avx512.o: Makefile $(SRC_DIR)freq/freq_avx512.c $(SRC_DIR)freq/freq_avx512.h $(SRC_DIR)freq/freq.h $(CC) $(CFLAGS) $(SANITY_FLAGS) -c -mavx512f -pthread $(SRC_DIR)freq/freq_avx512.c -o $@ +sve.o: Makefile $(SRC_DIR)sve.c $(SRC_DIR)sve.h + $(CC) $(CFLAGS) $(SANITY_FLAGS) $(SVE_FLAGS) -c $(SRC_DIR)sve.c -o $@ + $(OUTPUT): Makefile $(SOURCE) $(HEADERS) ifeq ($(GIT_VERSION),"") $(CC) $(CFLAGS) $(SANITY_FLAGS) $(SOURCE) -o $(OUTPUT) diff --git a/src/arm/midr.c b/src/arm/midr.c index 44f25d1..5874e46 100644 --- a/src/arm/midr.c +++ b/src/arm/midr.c @@ -19,6 +19,7 @@ #include "udev.h" #include "midr.h" #include "uarch.h" +#include "sve.h" bool cores_are_equal(int c1pos, int c2pos, uint32_t* midr_array, int32_t* freq_array) { return midr_array[c1pos] == midr_array[c2pos] && freq_array[c1pos] == freq_array[c2pos]; @@ -142,21 +143,6 @@ void init_cpu_info(struct cpuInfo* cpu) { cpu->next_cpu = NULL; } -// https://learn.arm.com/learning-paths/servers-and-cloud-computing/sve/sve_basics/#:~:text=Using%20a%20text%20editor%20of%20your%20choice%2C%20copy,svcntb%28%29%29%3B%20%7D%20This%20program%20prints%20the%20vector%20length -static inline uint32_t sve_cntb(void) { - #ifdef __ARM_FEATURE_SVE - uint32_t x0 = 0; - __asm volatile("cntb %0" - : "=r"(x0)); - printf("cntb=%d\n", x0); - return x0; - #else - printBug("sve_cntb: SVE not enabled by the compiler"); - return 0; - #endif -} - - // We assume all cpus share the same hardware // capabilities but I'm not sure it is always // true... diff --git a/src/arm/sve.c b/src/arm/sve.c new file mode 100644 index 0000000..cb8217f --- /dev/null +++ b/src/arm/sve.c @@ -0,0 +1,16 @@ +#include +#include "../common/global.h" + +// https://learn.arm.com/learning-paths/servers-and-cloud-computing/sve/sve_basics/#:~:text=Using%20a%20text%20editor%20of%20your%20choice%2C%20copy,svcntb%28%29%29%3B%20%7D%20This%20program%20prints%20the%20vector%20length +uint64_t sve_cntb(void) { + #ifdef __ARM_FEATURE_SVE + uint64_t x0 = 0; + __asm volatile("cntb %0" + : "=r"(x0)); + printf("cntb=%ld\n", x0); + return x0; + #else + printBug("sve_cntb: SVE not enabled by the compiler"); + return 0; + #endif +} diff --git a/src/arm/sve.h b/src/arm/sve.h new file mode 100644 index 0000000..ddb3f70 --- /dev/null +++ b/src/arm/sve.h @@ -0,0 +1,6 @@ +#ifndef __SVE_DETECTION__ +#define __SVE_DETECTION__ + +uint64_t sve_cntb(void); + +#endif