|
30 | 30 | #include "erasure_code.h" |
31 | 31 | #include "gf_vect_mul.h" |
32 | 32 |
|
| 33 | +#ifdef __ARM_FEATURE_SVE |
| 34 | +// If the compiler defines SVE intrinsics, include that header |
| 35 | +#include <arm_sve.h> |
| 36 | + |
| 37 | +#elif defined(__linux__) |
| 38 | +// Otherwise include these headers and define these constants as a fallback for Linux only |
| 39 | +#include <stddef.h> |
| 40 | +#include <sys/auxv.h> |
| 41 | +#include <sys/prctl.h> |
| 42 | +#ifndef PR_SVE_GET_VL |
| 43 | +#define PR_SVE_GET_VL 51 |
| 44 | +#endif |
| 45 | +#ifndef PR_SVE_VL_LEN_MASK |
| 46 | +#define PR_SVE_VL_LEN_MASK 0xffff |
| 47 | +#endif |
| 48 | + |
| 49 | +#endif |
| 50 | + |
| 51 | +static inline size_t |
| 52 | +get_sve_vector_length_bytes(void) |
| 53 | +{ |
| 54 | +#ifdef __ARM_FEATURE_SVE |
| 55 | + // Use intrinsic if available at compile time |
| 56 | + return svcntb(); |
| 57 | +#elif defined(__linux__) |
| 58 | + // Fall back to prctl on Linux |
| 59 | + long sve_vl = prctl(PR_SVE_GET_VL); |
| 60 | + if (sve_vl != -1) { |
| 61 | + return sve_vl & PR_SVE_VL_LEN_MASK; |
| 62 | + } |
| 63 | +#endif |
| 64 | + return 0; // Unknown or unavailable |
| 65 | +} |
| 66 | + |
33 | 67 | extern void |
34 | 68 | gf_vect_dot_prod_sve(int, int, unsigned char *, unsigned char **, unsigned char *); |
35 | 69 | extern void |
@@ -94,8 +128,16 @@ DEFINE_INTERFACE_DISPATCHER(ec_encode_data) |
94 | 128 | #if defined(__linux__) |
95 | 129 | unsigned long auxval = getauxval(AT_HWCAP); |
96 | 130 |
|
97 | | - if (auxval & HWCAP_SVE) |
| 131 | + if (auxval & HWCAP_SVE) { |
| 132 | + size_t vector_length = get_sve_vector_length_bytes(); |
| 133 | + |
| 134 | + // If 128-bit SVE (16 bytes), use NEON instead |
| 135 | + if (vector_length == 16 && (auxval & HWCAP_ASIMD)) { |
| 136 | + return ec_encode_data_neon; |
| 137 | + } |
| 138 | + |
98 | 139 | return ec_encode_data_sve; |
| 140 | + } |
99 | 141 | if (auxval & HWCAP_ASIMD) |
100 | 142 | return ec_encode_data_neon; |
101 | 143 | #elif defined(__APPLE__) |
|
0 commit comments