diff --git a/nnue/inference.c b/nnue/inference.c index 53094b0..982eda7 100644 --- a/nnue/inference.c +++ b/nnue/inference.c @@ -23,7 +23,10 @@ #define _GNU_SOURCE #include "inference.h" #include "bitboard.h" +#if defined(__x86_64__) || defined(__i386__) #include +#define RV_X86 1 +#endif #include #include #include @@ -80,6 +83,10 @@ typedef struct { static _Thread_local Frame frames[FRAMES]; static _Thread_local CacheEntry bucket_cache[2][32]; static _Thread_local int base_ply; +#ifdef __ANDROID__ +/* Android arm64 (Bionic) refuses executables whose TLS segment is aligned below 64 bytes. */ +__attribute__((used)) static _Thread_local _Alignas(64) char rv_tls_align_pad[64]; +#endif static _Thread_local uint64_t seen_old[(AUX + 63) / 64], seen_new[(AUX + 63) / 64]; static uint16_t square_offset[12][64], piece_span[12]; @@ -204,6 +211,7 @@ static inline void prefetch_rows(const char *base, size_t rowbytes, const RowOps for (int r = 0; r < o->nsub; r++) { const char *q = base + (size_t)o->sub[r] * rowbytes + off; for (size_t k = 0; k < span; k += 64) __builtin_prefetch(q + k, 0, 3); } } +#ifdef RV_X86 __attribute__((target("avx2"))) static void psqt_rows_avx2(int16_t *dst, const int16_t *src, const RowOps *o) { prefetch_rows((const char *)pw, WIDTH * 2, o, 0, 256); @@ -284,9 +292,12 @@ static void aux_rows_avx512(int16_t *dst, const int16_t *src, const RowOps *o) { } } +#endif + static void (*psqt_rows)(int16_t *, const int16_t *, const RowOps *) = psqt_rows_scalar; static void (*aux_rows)(int16_t *, const int16_t *, const RowOps *) = aux_rows_scalar; +#ifdef RV_X86 static uint64_t square_diff(const uint8_t *x, const uint8_t *y) { uint64_t m = 0; for (int i = 0; i < 4; i++) { @@ -296,6 +307,14 @@ static uint64_t square_diff(const uint8_t *x, const uint8_t *y) { return m; } +#else +static uint64_t square_diff(const uint8_t *x, const uint8_t *y) { + uint64_t m = 0; + for (int i = 0; i < 64; i++) m |= (uint64_t)(x[i] != y[i]) << i; + return m; +} +#endif + /* Piece-square accumulator from scratch (verification and fresh frames). */ static void psqt_scratch(Frame *f, int p) { uint32_t add[32]; @@ -448,6 +467,7 @@ static int finish_scalar(const int32_t *s, int bucket) { return (int)(y * 240.0f); } +#ifdef RV_X86 /* Eight-lane float head; the FMA order matches finish_scalar exactly. */ __attribute__((target("avx2,fma"))) static int finish_avx2(const int32_t *s, int bucket) { @@ -481,6 +501,8 @@ static int finish_avx2(const int32_t *s, int bucket) { return (int)((_mm_cvtss_f32(t) + b3[bucket]) * 240.0f); } +#endif + static inline int output_bucket(const Board *b) { return (popcount(b->occ_all) - 2) / 4; } /* Pairwise CReLU product of the summed accumulators, stm half first. */ @@ -495,6 +517,7 @@ static void activate_scalar(const Frame *f, const Board *b, uint8_t *x) { } } +#ifdef RV_X86 __attribute__((target("avx2"))) static void activate_avx2(const Frame *f, const Board *b, uint8_t *x) { const __m256i zero = _mm256_setzero_si256(), cap = _mm256_set1_epi16(255); @@ -517,6 +540,8 @@ static void activate_avx2(const Frame *f, const Board *b, uint8_t *x) { } } +#endif + static int forward_scalar(const Frame *f, const Board *b) { uint8_t x[WIDTH]; activate_scalar(f, b, x); @@ -527,6 +552,7 @@ static int forward_scalar(const Frame *f, const Board *b) { return finish_scalar(s, bucket); } +#ifdef RV_X86 /* Indices of the 4-byte input groups with any nonzero byte, ascending. */ __attribute__((target("avx2"))) static inline int nonzero_groups(const uint8_t *x, uint16_t *idx) { @@ -599,6 +625,8 @@ static int forward_vnni(const Frame *f, const Board *b) { return finish_avx2(sums, bucket); } +#endif + static int (*forward)(const Frame *, const Board *) = forward_scalar; /* ---- loading ---- */ @@ -666,6 +694,7 @@ bool net_load(const unsigned char *bytes, size_t count) { aux_rows = aux_rows_scalar; forward = forward_scalar; net_tier = "scalar"; +#ifdef RV_X86 if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma")) { psqt_rows = psqt_rows_avx2; aux_rows = aux_rows_avx2; @@ -678,6 +707,7 @@ bool net_load(const unsigned char *bytes, size_t count) { net_tier = "avx512"; if (__builtin_cpu_supports("avx512vnni")) { forward = forward_vnni; net_tier = "avx512-vnni"; } } +#endif for (int i = 0; i < FRAMES; i++) frames[i].ready = false; return true; }