| 1 | // SPDX-License-Identifier: MIT |
| 2 | // Copyright (c) 2024 nihui (https://github.com/nihui) |
| 3 | // Copyright (c) 2024 kernelbin (https://github.com/kernelbin) |
| 4 | // |
| 5 | // ruapu --- detect cpu isa features with single-file |
| 6 | |
| 7 | #ifndef RUAPU_H |
| 8 | #define RUAPU_H |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | void ruapu_init(); |
| 15 | |
| 16 | int ruapu_supports(const char* isa); |
| 17 | |
| 18 | const char* const* ruapu_rua(); |
| 19 | |
| 20 | #ifdef RUAPU_IMPLEMENTATION |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | typedef void (*ruapu_some_inst)(); |
| 26 | |
| 27 | #if defined _WIN32 |
| 28 | |
| 29 | #include <windows.h> |
| 30 | #include <setjmp.h> |
| 31 | |
| 32 | #if defined (_MSC_VER) // MSVC |
| 33 | static int ruapu_detect_isa(ruapu_some_inst some_inst) |
| 34 | { |
| 35 | int g_ruapu_sigill_caught = 0; |
| 36 | |
| 37 | __try |
| 38 | { |
| 39 | some_inst(); |
| 40 | } |
| 41 | __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION ? |
| 42 | EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) |
| 43 | { |
| 44 | g_ruapu_sigill_caught = 1; |
| 45 | } |
| 46 | |
| 47 | return g_ruapu_sigill_caught ? 0 : 1; |
| 48 | } |
| 49 | #else |
| 50 | static int g_ruapu_sigill_caught = 0; |
| 51 | static jmp_buf g_ruapu_jmpbuf; |
| 52 | |
| 53 | static LONG CALLBACK ruapu_catch_sigill(struct _EXCEPTION_POINTERS* ExceptionInfo) |
| 54 | { |
| 55 | if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) |
| 56 | { |
| 57 | g_ruapu_sigill_caught = 1; |
| 58 | longjmp(g_ruapu_jmpbuf, -1); |
| 59 | } |
| 60 | |
| 61 | return EXCEPTION_CONTINUE_SEARCH; |
| 62 | } |
| 63 | |
| 64 | static int ruapu_detect_isa(ruapu_some_inst some_inst) |
| 65 | { |
| 66 | g_ruapu_sigill_caught = 0; |
| 67 | |
| 68 | PVOID eh = AddVectoredExceptionHandler(1, ruapu_catch_sigill); |
| 69 | |
| 70 | if (setjmp(g_ruapu_jmpbuf) == 0) |
| 71 | { |
| 72 | some_inst(); |
| 73 | } |
| 74 | |
| 75 | RemoveVectoredExceptionHandler(eh); |
| 76 | |
| 77 | return g_ruapu_sigill_caught ? 0 : 1; |
| 78 | } |
| 79 | #endif // WINAPI_FAMILY == WINAPI_FAMILY_APP |
| 80 | |
| 81 | #elif defined __ANDROID__ || defined __linux__ || defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __DragonFly__ || defined __sun__ |
| 82 | #include <signal.h> |
| 83 | #include <setjmp.h> |
| 84 | |
| 85 | static int g_ruapu_sigill_caught = 0; |
| 86 | static sigjmp_buf g_ruapu_jmpbuf; |
| 87 | |
| 88 | static void ruapu_catch_sigill(int signo, siginfo_t* si, void* data) |
| 89 | { |
| 90 | (void)signo; |
| 91 | (void)si; |
| 92 | (void)data; |
| 93 | |
| 94 | g_ruapu_sigill_caught = 1; |
| 95 | siglongjmp(g_ruapu_jmpbuf, -1); |
| 96 | } |
| 97 | |
| 98 | static int ruapu_detect_isa(ruapu_some_inst some_inst) |
| 99 | { |
| 100 | g_ruapu_sigill_caught = 0; |
| 101 | |
| 102 | struct sigaction sa = { 0 }; |
| 103 | struct sigaction old_sa; |
| 104 | sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; |
| 105 | sa.sa_sigaction = ruapu_catch_sigill; |
| 106 | sigaction(SIGILL, &sa, &old_sa); |
| 107 | |
| 108 | if (sigsetjmp(g_ruapu_jmpbuf, 1) == 0) |
| 109 | { |
| 110 | some_inst(); |
| 111 | } |
| 112 | |
| 113 | sigaction(SIGILL, &old_sa, NULL); |
| 114 | |
| 115 | return g_ruapu_sigill_caught ? 0 : 1; |
| 116 | } |
| 117 | |
| 118 | #elif defined __SYTERKIT__ |
| 119 | |
| 120 | #include <mmu.h> |
| 121 | |
| 122 | static int g_ruapu_sigill_caught = 0; |
| 123 | |
| 124 | void arm32_do_undefined_instruction(struct arm_regs_t *regs) |
| 125 | { |
| 126 | g_ruapu_sigill_caught = 1; |
| 127 | regs->pc += 4; |
| 128 | } |
| 129 | |
| 130 | static int ruapu_detect_isa(ruapu_some_inst some_inst) |
| 131 | { |
| 132 | g_ruapu_sigill_caught = 0; |
| 133 | some_inst(); |
| 134 | return g_ruapu_sigill_caught ? 0 : 1; |
| 135 | } |
| 136 | |
| 137 | #endif // defined _WIN32 || defined __ANDROID__ || defined __linux__ || defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __DragonFly__ || defined __sun__ || defined __SYTERKIT__ |
| 138 | |
| 139 | #if defined _WIN32 |
| 140 | |
| 141 | #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64) |
| 142 | #ifdef _MSC_VER |
| 143 | #define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned char ruapu_some_##isa[] = { __VA_ARGS__, 0xc3 }; |
| 144 | #else |
| 145 | #define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned char ruapu_some_##isa[] = { __VA_ARGS__, 0xc3 }; |
| 146 | #endif |
| 147 | |
| 148 | #elif __aarch64__ || defined(_M_ARM64) |
| 149 | #ifdef _MSC_VER |
| 150 | #define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned int ruapu_some_##isa[] = { __VA_ARGS__, 0xd65f03c0 }; |
| 151 | #else |
| 152 | #define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned int ruapu_some_##isa[] = { __VA_ARGS__, 0xd65f03c0 }; |
| 153 | #endif |
| 154 | |
| 155 | #elif __arm__ || defined(_M_ARM) |
| 156 | #if __thumb__ |
| 157 | #ifdef _MSC_VER |
| 158 | #define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned short ruapu_some_##isa[] = { __VA_ARGS__, 0x4770 }; |
| 159 | #else |
| 160 | #define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned short ruapu_some_##isa[] = { __VA_ARGS__, 0x4770 }; |
| 161 | #endif |
| 162 | #else |
| 163 | #ifdef _MSC_VER |
| 164 | #define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned int ruapu_some_##isa[] = { __VA_ARGS__, 0xe12fff1e }; |
| 165 | #else |
| 166 | #define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned int ruapu_some_##isa[] = { __VA_ARGS__, 0xe12fff1e }; |
| 167 | #endif |
| 168 | #endif |
| 169 | |
| 170 | #endif |
| 171 | |
| 172 | #else // defined _WIN32 |
| 173 | |
| 174 | #if defined(__i386__) || defined(__x86_64__) || __s390x__ |
| 175 | #define RUAPU_INSTCODE(isa, ...) static void ruapu_some_##isa() { asm volatile(".byte " #__VA_ARGS__ : : : ); } |
| 176 | #elif __aarch64__ || __arm__ || __mips__ || __riscv || __loongarch__ |
| 177 | #if __thumb__ |
| 178 | #define RUAPU_INSTCODE(isa, ...) static void ruapu_some_##isa() { asm volatile(".short " #__VA_ARGS__ : : : ); } |
| 179 | #else |
| 180 | #define RUAPU_INSTCODE(isa, ...) static void ruapu_some_##isa() { asm volatile(".word " #__VA_ARGS__ : : : ); } |
| 181 | #endif |
| 182 | #elif __powerpc__ |
| 183 | #define RUAPU_INSTCODE(isa, ...) static void ruapu_some_##isa() { asm volatile(".long " #__VA_ARGS__ : : : ); } |
| 184 | #endif |
| 185 | |
| 186 | #endif // defined _WIN32 |
| 187 | |
| 188 | #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64) |
| 189 | RUAPU_INSTCODE(mmx, 0x0f, 0xdb, 0xc0, 0x0f, 0x77) // pand mm0,mm0 + emms |
| 190 | RUAPU_INSTCODE(sse, 0x0f, 0x54, 0xc0) // andps xmm0,xmm0 |
| 191 | RUAPU_INSTCODE(sse2, 0x66, 0x0f, 0xfe, 0xc0) // paddd xmm0,xmm0 |
| 192 | RUAPU_INSTCODE(sse3, 0xf2, 0x0f, 0x7c, 0xc0) // haddps xmm0,xmm0 |
| 193 | RUAPU_INSTCODE(ssse3, 0x66, 0x0f, 0x38, 0x06, 0xc0) // phsubd xmm0,xmm0 |
| 194 | RUAPU_INSTCODE(sse41, 0x66, 0x0f, 0x38, 0x3d, 0xc0) // pmaxsd xmm0,xmm0 |
| 195 | RUAPU_INSTCODE(sse42, 0x66, 0x0f, 0x38, 0x37, 0xc0) // pcmpgtq xmm0,xmm0 |
| 196 | RUAPU_INSTCODE(sse4a, 0x66, 0x0f, 0x79, 0xc0) // extrq xmm0,xmm0 |
| 197 | RUAPU_INSTCODE(xop, 0x8f, 0xe8, 0x78, 0xb6, 0xc0, 0x00) // vpmadcswd xmm0,xmm0,xmm0,xmm0 |
| 198 | RUAPU_INSTCODE(avx, 0xc5, 0xfc, 0x54, 0xc0) // vandps ymm0,ymm0,ymm0 |
| 199 | RUAPU_INSTCODE(f16c, 0xc4, 0xe2, 0x7d, 0x13, 0xc0) // vcvtph2ps ymm0,xmm0 |
| 200 | RUAPU_INSTCODE(fma, 0xc4, 0xe2, 0x7d, 0x98, 0xc0) // vfmadd132ps ymm0,ymm0,ymm0 |
| 201 | RUAPU_INSTCODE(fma4, 0xc4, 0xe3, 0xfd, 0x68, 0xc0, 0x00) // vfmaddps ymm0,ymm0,ymm0,ymm0 |
| 202 | RUAPU_INSTCODE(avx2, 0xc5, 0xfd, 0xfe, 0xc0) // vpaddd ymm0,ymm0,ymm0 |
| 203 | RUAPU_INSTCODE(avx512f, 0x62, 0xf1, 0x7c, 0x48, 0x58, 0xc0) // vaddps zmm0,zmm0,zmm0 |
| 204 | RUAPU_INSTCODE(avx512bw, 0x62, 0xf1, 0x7d, 0x48, 0xfd, 0xc0) // vpaddw zmm0,zmm0,zmm0 |
| 205 | RUAPU_INSTCODE(avx512cd, 0x62, 0xf2, 0xfd, 0x48, 0x44, 0xc0) // vplzcntq zmm0,zmm0 |
| 206 | RUAPU_INSTCODE(avx512dq, 0x62, 0xf1, 0x7c, 0x48, 0x54, 0xc0) // vandps zmm0,zmm0,zmm0 |
| 207 | RUAPU_INSTCODE(avx512vl, 0x62, 0xf2, 0xfd, 0x28, 0x1f, 0xc0) // vpabsq ymm0,ymm0 |
| 208 | RUAPU_INSTCODE(avx512vnni, 0x62, 0xf2, 0x7d, 0x48, 0x52, 0xc0) // vpdpwssd zmm0,zmm0,zmm0 |
| 209 | RUAPU_INSTCODE(avx512bf16, 0x62, 0xf2, 0x7e, 0x48, 0x52, 0xc0) // vdpbf16ps zmm0,zmm0,zmm0 |
| 210 | RUAPU_INSTCODE(avx512ifma, 0x62, 0xf2, 0xfd, 0x48, 0xb4, 0xc0) // vpmadd52luq zmm0,zmm0,zmm0 |
| 211 | RUAPU_INSTCODE(avx512vbmi, 0x62, 0xf2, 0x7d, 0x48, 0x75, 0xc0) // vpermi2b zmm0,zmm0,zmm0 |
| 212 | RUAPU_INSTCODE(avx512vbmi2, 0x62, 0xf2, 0x7d, 0x48, 0x71, 0xc0) // vpshldvd zmm0,zmm0,zmm0 |
| 213 | RUAPU_INSTCODE(avx512fp16, 0x62, 0xf6, 0x7d, 0x48, 0x98, 0xc0) // vfmadd132ph zmm0,zmm0,zmm0 |
| 214 | // TODO:avx512pf, vgatherpf1dps DWORD PTR [esp+zmm0*1]{k1} |
| 215 | RUAPU_INSTCODE(avx512er, 0x62, 0xf2, 0xfd, 0x48, 0xc8, 0xc0) //vexp2pd zmm0,zmm0 |
| 216 | RUAPU_INSTCODE(avx5124fmaps, 0x67, 0x62, 0xf2, 0x7f, 0x48, 0x9a, 0x04, 0x24) //v4fmaddps zmm0,zmm0,XMMWORD PTR [esp] |
| 217 | RUAPU_INSTCODE(avx5124vnniw, 0x67, 0x62, 0xf2, 0x7f, 0x48, 0x52, 0x04, 0x24) //vp4dpwssd zmm0,zmm0,XMMWORD PTR [esp] |
| 218 | RUAPU_INSTCODE(avxvnni, 0xc4, 0xe2, 0x7d, 0x52, 0xc0) // vpdpwssd ymm0,ymm0,ymm0 |
| 219 | RUAPU_INSTCODE(avxvnniint8, 0xc4, 0xe2, 0x7f, 0x50, 0xc0) // vpdpbssd ymm0,ymm0,ymm0 |
| 220 | RUAPU_INSTCODE(avxvnniint16, 0xc4, 0xe2, 0x7e, 0xd2, 0xc0) // vpdpwsud ymm0,ymm0,ymm0 |
| 221 | RUAPU_INSTCODE(avxifma, 0xc4, 0xe2, 0xfd, 0xb4, 0xc0) // vpmadd52luq ymm0,ymm0,ymm0 |
| 222 | RUAPU_INSTCODE(amxfp16, 0xc4, 0xe2, 0x7b, 0x5c, 0xd1) // tdpfp16ps %tmm0, %tmm1, %tmm2 |
| 223 | RUAPU_INSTCODE(amxbf16, 0xc4, 0xe2, 0x7a, 0x5c, 0xd1) // tdpbf16ps %tmm0, %tmm1, %tmm2 |
| 224 | RUAPU_INSTCODE(amxint8, 0xc4, 0xe2, 0x7b, 0x5e, 0xd1) // tdpbssd %tmm0, %tmm1, %tmm2 |
| 225 | RUAPU_INSTCODE(amxtile, 0xc4, 0xe2, 0x7a, 0x49, 0xc0) // tilezero %tmm0 |
| 226 | |
| 227 | #elif __aarch64__ || defined(_M_ARM64) |
| 228 | RUAPU_INSTCODE(neon, 0x4e20d400) // fadd v0.4s,v0.4s,v0.4s |
| 229 | RUAPU_INSTCODE(vfpv4, 0x1f000000) // fmadd s0,s0,s0,s0 |
| 230 | RUAPU_INSTCODE(lse, 0xf82083e0, 0xf82083e0) // swp x0,x0,[sp] + swp x0,x0,[sp] |
| 231 | RUAPU_INSTCODE(cpuid, 0xd5380000) // mrs x0,midr_el1 |
| 232 | RUAPU_INSTCODE(asimdrdm, 0x6e808400) // sqrdmlah v0.4s,v0.4s,v0.4s |
| 233 | RUAPU_INSTCODE(asimdhp, 0x0e401400) // fadd v0.4h,v0.4h,v0.4h |
| 234 | RUAPU_INSTCODE(asimddp, 0x4e809400) // sdot v0.4h,v0.16b,v0.16b |
| 235 | RUAPU_INSTCODE(asimdfhm, 0x4e20ec00) // fmlal v0.4s,v0.4h,v0.4h |
| 236 | RUAPU_INSTCODE(bf16, 0x6e40ec00) // bfmmla v0.4h,v0.8h,v0.8h |
| 237 | RUAPU_INSTCODE(i8mm, 0x4e80a400) // smmla v0.4h,v0.16b,v0.16b |
| 238 | RUAPU_INSTCODE(frint, 0x4e21e800) // frint32z v0.4s,v0.4s |
| 239 | RUAPU_INSTCODE(jscvt, 0x1e7e0000) // fjcvtzs w0,d0 |
| 240 | RUAPU_INSTCODE(fcma, 0x6e80c400) // fcmla v0.4s,v0.4s,v0.4s,#0 |
| 241 | RUAPU_INSTCODE(mte, 0xd96003e0) // ldg x0,[sp] |
| 242 | RUAPU_INSTCODE(mte2, 0xd9e003e0) // ldgm x0,[sp] |
| 243 | RUAPU_INSTCODE(sve, 0x65608000) // fmad z0.h,p0/m,z0.h,z0.h |
| 244 | RUAPU_INSTCODE(sve2, 0x44405000) // smlslb z0.h,z0.b,z0.b |
| 245 | RUAPU_INSTCODE(svebf16, 0x6460e400) // bfmmla z0.s,z0.h,z0.h |
| 246 | RUAPU_INSTCODE(svei8mm, 0x45009800) // smmla z0.s,z0.b,z0.b |
| 247 | RUAPU_INSTCODE(svef32mm, 0x64a0e400) // fmmla z0.s,z0.s,z0.s |
| 248 | RUAPU_INSTCODE(svef64mm, 0x64e0e400) // fmmla z0.d,z0.d,z0.d |
| 249 | RUAPU_INSTCODE(sme, 0x80800000) // fmopa za0.s,p0/m,p0/m,z0.s,z0.s |
| 250 | RUAPU_INSTCODE(smef16f16, 0x81800008) // fmopa za0.h,p0/m,p0/m,z0.h,z0.h |
| 251 | RUAPU_INSTCODE(smef64f64, 0x80c00000) // fmopa za0.d,p0/m,p0/m,z0.d,z0.d |
| 252 | RUAPU_INSTCODE(smei64i64, 0xa0c00000) // smopa za0.d,p0/m,p0/m,z0.h,z0.h |
| 253 | RUAPU_INSTCODE(pmull, 0x0e20e000) // pmull v0.8h,v0.8b,v0.8b |
| 254 | RUAPU_INSTCODE(crc32, 0x1ac04000) // crc32b w0,w0,w0 |
| 255 | RUAPU_INSTCODE(aes, 0x4e285800) // aesd v0.16b,v0.16b |
| 256 | RUAPU_INSTCODE(sha1, 0x5e280800) // sha1h s0,s0 |
| 257 | RUAPU_INSTCODE(sha2, 0x5e004000) // sha256h q0,q0,v0.4s |
| 258 | RUAPU_INSTCODE(sha3, 0xce000000) // eor3 v0.16b, v0.16b, v0.16b, v0.16b |
| 259 | RUAPU_INSTCODE(sha512, 0xce608000) // sha512h q0, q0, v0.2d |
| 260 | RUAPU_INSTCODE(sm3, 0xce60c000) // sm3partw1 v0.4s, v0.4s, v0.4s |
| 261 | RUAPU_INSTCODE(sm4, 0xcec08400) // sm4e v0.4s, v0.4s |
| 262 | RUAPU_INSTCODE(svepmull, 0x45006800) // pmullb z0.q,z0.d,z0.d |
| 263 | RUAPU_INSTCODE(svebitperm, 0x4500b000) // bext z0.b,z0.b,z0.b |
| 264 | RUAPU_INSTCODE(sveaes, 0x4522e400) // aesd z0.b,z0.b,z0.b |
| 265 | RUAPU_INSTCODE(svesha3, 0x4520f400) // rax1 z0.d,z0.d,z0.d |
| 266 | RUAPU_INSTCODE(svesm4, 0x4523e000) // sm4e z0.s,z0.s,z0.s |
| 267 | RUAPU_INSTCODE(amx, 0x00201220) // amx setup |
| 268 | |
| 269 | |
| 270 | #elif __arm__ || defined(_M_ARM) |
| 271 | #if __thumb__ |
| 272 | RUAPU_INSTCODE(half, 0xf8bd, 0x0000) // ldrh r0,[sp] |
| 273 | RUAPU_INSTCODE(edsp, 0xfb20, 0x0000) // smlad r0,r0,r0,r0 |
| 274 | RUAPU_INSTCODE(neon, 0xef00, 0x0d40) // vadd.f32 q0,q0,q0 |
| 275 | RUAPU_INSTCODE(vfpv4, 0xeea0, 0x0a00) // vfma.f32 s0,s0,s0 |
| 276 | RUAPU_INSTCODE(idiv, 0x2003, 0xfb90, 0xf0f0) // movs r0,#3 + sdiv r0,r0,r0 |
| 277 | #else |
| 278 | RUAPU_INSTCODE(half, 0xe1dd00b0) // ldrh r0,[sp] |
| 279 | RUAPU_INSTCODE(edsp, 0xe7000010) // smlad r0,r0,r0,r0 |
| 280 | RUAPU_INSTCODE(neon, 0xf2000d40) // vadd.f32 q0,q0,q0 |
| 281 | RUAPU_INSTCODE(vfpv4, 0xeea00a00) // vfma.f32 s0,s0,s0 |
| 282 | RUAPU_INSTCODE(idiv, 0xe3a00003, 0xe710f010) // movs r0,#3 + sdiv r0,r0,r0 |
| 283 | #endif |
| 284 | |
| 285 | #elif __mips__ |
| 286 | RUAPU_INSTCODE(msa, 0x7900001b) // fmadd.w $w0,$w0,$w0 |
| 287 | RUAPU_INSTCODE(mmi, 0x4b60000e) // pmaddhw $f0,$f0 |
| 288 | RUAPU_INSTCODE(sx, 0xef48001e) // __lsx_vffloor_w |
| 289 | RUAPU_INSTCODE(asx, 0xec40001d) // __lasx_xfmadd_w |
| 290 | RUAPU_INSTCODE(msa2, 0x78000008) // __msa2_vperm_b |
| 291 | RUAPU_INSTCODE(crypto, 0x78010017) // __crypto_aes128_dec |
| 292 | |
| 293 | #elif __powerpc__ |
| 294 | RUAPU_INSTCODE(vsx, 0x104210c0) // vaddudm v2,v2,v2 |
| 295 | |
| 296 | #elif __s390x__ |
| 297 | RUAPU_INSTCODE(zvector, 0xe7, 0x11, 0x12, 0x00, 0x10, 0x8f) // vfmasb v1,v1,v1,v1 |
| 298 | |
| 299 | #elif __loongarch__ |
| 300 | RUAPU_INSTCODE(lsx, 0x700b0000) //vadd.w vr0, vr0, vr0 |
| 301 | RUAPU_INSTCODE(lasx, 0x740b0000) //xvadd.w xr0, xr0, xr0 |
| 302 | |
| 303 | #elif __riscv |
| 304 | RUAPU_INSTCODE(i, 0x00a50533) // add a0,a0,a0 |
| 305 | RUAPU_INSTCODE(m, 0x00200513, 0x02a50533, 0x02a54533) // addi a0,x0,2 mul a0,a0,a0 div a0,a0,a0 |
| 306 | RUAPU_INSTCODE(a, 0x100122af, 0x185122af) // lr.w t0,(sp) + sc.w t0,t0,(sp) |
| 307 | RUAPU_INSTCODE(f, 0x10a57553) // fmul.s fa0,fa0,fa0 |
| 308 | RUAPU_INSTCODE(d, 0x12a57553) // fmul.d fa0,fa0,fa0 |
| 309 | RUAPU_INSTCODE(c, 0x0001952a) // add a0,a0,a0 + nop |
| 310 | RUAPU_INSTCODE(zba, 0x20a52533) // sh1add a0,a0,a0 |
| 311 | RUAPU_INSTCODE(zbb, 0x60451513) // sext.b a0,a0,a0 |
| 312 | RUAPU_INSTCODE(zbc, 0x0aa52533) // clmulr a0,a0,a0 |
| 313 | RUAPU_INSTCODE(zbs, 0x48a51533) // bclr a0,a0,a0 |
| 314 | RUAPU_INSTCODE(zbkb, 0x08a54533) // pack a0,a0,a0 |
| 315 | RUAPU_INSTCODE(zbkc, 0x0aa53533) // clmulh a0,a0,a0 |
| 316 | RUAPU_INSTCODE(zbkx, 0x28a52533) // xperm.n a0,a0,a0 |
| 317 | RUAPU_INSTCODE(zfa, 0xf0108053) // fli.s ft0, min |
| 318 | RUAPU_INSTCODE(zfbfmin, 0x44807053) // fcvt.bf16.s ft0,ft0 |
| 319 | RUAPU_INSTCODE(zfh, 0x04007053); // fadd.hs ft0, ft0, ft0 |
| 320 | RUAPU_INSTCODE(zfhmin, 0xe4000553) // fmv.x.h a0, ft0 |
| 321 | RUAPU_INSTCODE(zicond, 0x0ea55533) // czero.eqz a0,a0,a0 |
| 322 | RUAPU_INSTCODE(zicsr, 0xc0102573); // csrr a0, time |
| 323 | RUAPU_INSTCODE(zifencei, 0x0000100f); // fence.i |
| 324 | RUAPU_INSTCODE(zmmul, 0x02a50533) // mul a0,a0,a0 |
| 325 | |
| 326 | RUAPU_INSTCODE(xtheadba, 0x00a5150b) // th.addsl a0,a0,a0,#0 |
| 327 | RUAPU_INSTCODE(xtheadbb, 0x1005150b) // th.srri a0,a0,#0 |
| 328 | RUAPU_INSTCODE(xtheadbs, 0x8805150b) // th.tst a0,a0,#0 |
| 329 | RUAPU_INSTCODE(xtheadcondmov, 0x40a5150b) // th.mveqz a0,a0,a0 |
| 330 | RUAPU_INSTCODE(xtheadfmemidx, 0x40a1650b) // th.flrw a0,sp,a0,#0 |
| 331 | RUAPU_INSTCODE(xtheadfmv, 0xc005150b) // th.fmv.x.hw a0,fa0 |
| 332 | RUAPU_INSTCODE(xtheadmac, 0x20a5150b) // th.mula a0,a0,a0 |
| 333 | RUAPU_INSTCODE(xtheadmemidx, 0x1801450b) // th.lbia a0,(sp),#0,#0 |
| 334 | RUAPU_INSTCODE(xtheadmempair, 0xe0a1450b) // th.lwd a0,a0,(sp),#0,3 |
| 335 | RUAPU_INSTCODE(xtheadsync, 0x0180000b) // th.sync |
| 336 | RUAPU_INSTCODE(xtheadvdot, 0x8000600b) // th.vmaqa.vv v0,v0,v0 |
| 337 | |
| 338 | // RVV 1.0 support |
| 339 | // unimp (csrrw x0, cycle, x0) |
| 340 | #define RUAPU_RV_TRAP() asm volatile(".align 2\n.word 0xc0001073") |
| 341 | // vcsr is only defined in rvv 1.0, which doesn't exist in rvv 0.7.1 or xtheadvector. |
| 342 | // csrr x0, vcsr |
| 343 | #define RUAPU_RVV1P0_AVAIL() asm volatile(".align 2\n.word 0x00f02573") |
| 344 | // csrr res, vlenb |
| 345 | #define RUAPU_DETECT_ZVL(len) static void ruapu_some_zvl##len##b() { \ |
| 346 | RUAPU_RVV1P0_AVAIL(); \ |
| 347 | intptr_t res; \ |
| 348 | asm volatile(".align 2\n.insn i 0x73, 0x2, %0, x0, -990" : "=r"(res)); \ |
| 349 | if (res < len/8) RUAPU_RV_TRAP(); \ |
| 350 | } |
| 351 | RUAPU_DETECT_ZVL(32) |
| 352 | RUAPU_DETECT_ZVL(64) |
| 353 | RUAPU_DETECT_ZVL(128) |
| 354 | RUAPU_DETECT_ZVL(256) |
| 355 | RUAPU_DETECT_ZVL(512) |
| 356 | RUAPU_DETECT_ZVL(1024) |
| 357 | #undef RUAPU_DETECT_ZVL |
| 358 | // vsetvl res, zero, vtype |
| 359 | // check vill bits after vsetvl |
| 360 | #define RUAPU_RVV_INSTCODE(isa, vtype, ...) static void ruapu_some_##isa() { \ |
| 361 | RUAPU_RVV1P0_AVAIL(); \ |
| 362 | intptr_t res; \ |
| 363 | asm volatile(".align 2\n.insn r 0x57, 0x7, 0x40, %0, x0, %1" : "=r"(res) : "r"(vtype)); \ |
| 364 | if (res < 0) RUAPU_RV_TRAP(); \ |
| 365 | asm volatile(".align 2\n.word " #__VA_ARGS__ ); \ |
| 366 | } |
| 367 | |
| 368 | RUAPU_RVV_INSTCODE(zvbb, 0, 0x4a862257) // vclz.v v4, v8 with SEW = 8 |
| 369 | RUAPU_RVV_INSTCODE(zvbc, 0, 0x32842257) // vclmul.vv v4, v8, v8 with SEW = 8 |
| 370 | RUAPU_RVV_INSTCODE(zvfh, 8, 0x02841257) // vfadd.vv v4, v8, v8 with SEW = 16 |
| 371 | RUAPU_RVV_INSTCODE(zvfhmin, 8, 0x4a8a1257) // vfncvt.f.f.v v4, v8 with SEW = 16 |
| 372 | RUAPU_RVV_INSTCODE(zvfbfmin, 8, 0x4a8e9257) // vfncvtbf16.f.f.w v4, v8 with SEW = 16 |
| 373 | RUAPU_RVV_INSTCODE(zvfbfwma, 8, 0xee855257) // vfwmaccbf16.vf v4, fa0, v8 with SEW = 16 |
| 374 | RUAPU_RVV_INSTCODE(zvkb, 0, 0x56860257) // vrol.vv v4, v8, v12 with SEW = 8 |
| 375 | RUAPU_RVV_INSTCODE(v, 24, 0x22842257) // vaaddu.vv v4, v8, v8 with SEW = 64 |
| 376 | |
| 377 | #undef RUAPU_RVV_INSTCODE |
| 378 | #undef RUAPU_RV_TRAP |
| 379 | #undef RUAPU_RVV1P0_AVAIL |
| 380 | #endif |
| 381 | |
| 382 | #undef RUAPU_INSTCODE |
| 383 | |
| 384 | struct ruapu_isa_entry |
| 385 | { |
| 386 | const char* isa; |
| 387 | ruapu_some_inst inst; |
| 388 | }; |
| 389 | |
| 390 | #define RUAPU_ISAENTRY(isa) { #isa, (ruapu_some_inst)(void*)ruapu_some_##isa }, |
| 391 | |
| 392 | struct ruapu_isa_entry g_ruapu_isa_map[] = { |
| 393 | |
| 394 | #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64) |
| 395 | RUAPU_ISAENTRY(mmx) |
| 396 | RUAPU_ISAENTRY(sse) |
| 397 | RUAPU_ISAENTRY(sse2) |
| 398 | RUAPU_ISAENTRY(sse3) |
| 399 | RUAPU_ISAENTRY(ssse3) |
| 400 | RUAPU_ISAENTRY(sse41) |
| 401 | RUAPU_ISAENTRY(sse42) |
| 402 | RUAPU_ISAENTRY(sse4a) |
| 403 | RUAPU_ISAENTRY(xop) |
| 404 | RUAPU_ISAENTRY(avx) |
| 405 | RUAPU_ISAENTRY(f16c) |
| 406 | RUAPU_ISAENTRY(fma) |
| 407 | RUAPU_ISAENTRY(fma4) |
| 408 | RUAPU_ISAENTRY(avx2) |
| 409 | RUAPU_ISAENTRY(avx512f) |
| 410 | RUAPU_ISAENTRY(avx512bw) |
| 411 | RUAPU_ISAENTRY(avx512cd) |
| 412 | RUAPU_ISAENTRY(avx512dq) |
| 413 | RUAPU_ISAENTRY(avx512vl) |
| 414 | RUAPU_ISAENTRY(avx512vnni) |
| 415 | RUAPU_ISAENTRY(avx512bf16) |
| 416 | RUAPU_ISAENTRY(avx512ifma) |
| 417 | RUAPU_ISAENTRY(avx512vbmi) |
| 418 | RUAPU_ISAENTRY(avx512vbmi2) |
| 419 | RUAPU_ISAENTRY(avx512fp16) |
| 420 | // TODO:avx512pf |
| 421 | RUAPU_ISAENTRY(avx512er) |
| 422 | RUAPU_ISAENTRY(avx5124fmaps) |
| 423 | RUAPU_ISAENTRY(avx5124vnniw) |
| 424 | RUAPU_ISAENTRY(avxvnni) |
| 425 | RUAPU_ISAENTRY(avxvnniint8) |
| 426 | RUAPU_ISAENTRY(avxvnniint16) |
| 427 | RUAPU_ISAENTRY(avxifma) |
| 428 | RUAPU_ISAENTRY(amxfp16) |
| 429 | RUAPU_ISAENTRY(amxbf16) |
| 430 | RUAPU_ISAENTRY(amxint8) |
| 431 | RUAPU_ISAENTRY(amxtile) |
| 432 | |
| 433 | #elif __aarch64__ || defined(_M_ARM64) |
| 434 | RUAPU_ISAENTRY(neon) |
| 435 | RUAPU_ISAENTRY(vfpv4) |
| 436 | RUAPU_ISAENTRY(lse) |
| 437 | RUAPU_ISAENTRY(cpuid) |
| 438 | RUAPU_ISAENTRY(asimdrdm) |
| 439 | RUAPU_ISAENTRY(asimdhp) |
| 440 | RUAPU_ISAENTRY(asimddp) |
| 441 | RUAPU_ISAENTRY(asimdfhm) |
| 442 | RUAPU_ISAENTRY(bf16) |
| 443 | RUAPU_ISAENTRY(i8mm) |
| 444 | RUAPU_ISAENTRY(frint) |
| 445 | RUAPU_ISAENTRY(jscvt) |
| 446 | RUAPU_ISAENTRY(fcma) |
| 447 | RUAPU_ISAENTRY(mte) |
| 448 | RUAPU_ISAENTRY(mte2) |
| 449 | RUAPU_ISAENTRY(sve) |
| 450 | RUAPU_ISAENTRY(sve2) |
| 451 | RUAPU_ISAENTRY(svebf16) |
| 452 | RUAPU_ISAENTRY(svei8mm) |
| 453 | RUAPU_ISAENTRY(svef32mm) |
| 454 | RUAPU_ISAENTRY(svef64mm) |
| 455 | RUAPU_ISAENTRY(sme) |
| 456 | RUAPU_ISAENTRY(smef16f16) |
| 457 | RUAPU_ISAENTRY(smef64f64) |
| 458 | RUAPU_ISAENTRY(smei64i64) |
| 459 | RUAPU_ISAENTRY(pmull) |
| 460 | RUAPU_ISAENTRY(crc32) |
| 461 | RUAPU_ISAENTRY(aes) |
| 462 | RUAPU_ISAENTRY(sha1) |
| 463 | RUAPU_ISAENTRY(sha2) |
| 464 | RUAPU_ISAENTRY(sha3) |
| 465 | RUAPU_ISAENTRY(sha512) |
| 466 | RUAPU_ISAENTRY(sm3) |
| 467 | RUAPU_ISAENTRY(sm4) |
| 468 | RUAPU_ISAENTRY(svepmull) |
| 469 | RUAPU_ISAENTRY(svebitperm) |
| 470 | RUAPU_ISAENTRY(sveaes) |
| 471 | RUAPU_ISAENTRY(svesha3) |
| 472 | RUAPU_ISAENTRY(svesm4) |
| 473 | RUAPU_ISAENTRY(amx) |
| 474 | |
| 475 | #elif __arm__ || defined(_M_ARM) |
| 476 | RUAPU_ISAENTRY(half) |
| 477 | RUAPU_ISAENTRY(edsp) |
| 478 | RUAPU_ISAENTRY(neon) |
| 479 | RUAPU_ISAENTRY(vfpv4) |
| 480 | RUAPU_ISAENTRY(idiv) |
| 481 | |
| 482 | #elif __mips__ |
| 483 | RUAPU_ISAENTRY(msa) |
| 484 | RUAPU_ISAENTRY(mmi) |
| 485 | RUAPU_ISAENTRY(sx) |
| 486 | RUAPU_ISAENTRY(asx) |
| 487 | RUAPU_ISAENTRY(msa2) |
| 488 | RUAPU_ISAENTRY(crypto) |
| 489 | |
| 490 | #elif __powerpc__ |
| 491 | RUAPU_ISAENTRY(vsx) |
| 492 | |
| 493 | #elif __s390x__ |
| 494 | RUAPU_ISAENTRY(zvector) |
| 495 | |
| 496 | #elif __loongarch__ |
| 497 | RUAPU_ISAENTRY(lsx) |
| 498 | RUAPU_ISAENTRY(lasx) |
| 499 | |
| 500 | #elif __riscv |
| 501 | RUAPU_ISAENTRY(i) |
| 502 | RUAPU_ISAENTRY(m) |
| 503 | RUAPU_ISAENTRY(a) |
| 504 | RUAPU_ISAENTRY(f) |
| 505 | RUAPU_ISAENTRY(d) |
| 506 | RUAPU_ISAENTRY(c) |
| 507 | RUAPU_ISAENTRY(v) |
| 508 | RUAPU_ISAENTRY(zba) |
| 509 | RUAPU_ISAENTRY(zbb) |
| 510 | RUAPU_ISAENTRY(zbc) |
| 511 | RUAPU_ISAENTRY(zbs) |
| 512 | RUAPU_ISAENTRY(zbkb) |
| 513 | RUAPU_ISAENTRY(zbkc) |
| 514 | RUAPU_ISAENTRY(zbkx) |
| 515 | RUAPU_ISAENTRY(zfa) |
| 516 | RUAPU_ISAENTRY(zfbfmin) |
| 517 | RUAPU_ISAENTRY(zfh) |
| 518 | RUAPU_ISAENTRY(zfhmin) |
| 519 | RUAPU_ISAENTRY(zicond) |
| 520 | RUAPU_ISAENTRY(zicsr) |
| 521 | RUAPU_ISAENTRY(zifencei) |
| 522 | RUAPU_ISAENTRY(zmmul) |
| 523 | RUAPU_ISAENTRY(zvbb) |
| 524 | RUAPU_ISAENTRY(zvbc) |
| 525 | RUAPU_ISAENTRY(zvfh) |
| 526 | RUAPU_ISAENTRY(zvfhmin) |
| 527 | RUAPU_ISAENTRY(zvfbfmin) |
| 528 | RUAPU_ISAENTRY(zvfbfwma) |
| 529 | RUAPU_ISAENTRY(zvkb) |
| 530 | RUAPU_ISAENTRY(zvl32b) |
| 531 | RUAPU_ISAENTRY(zvl64b) |
| 532 | RUAPU_ISAENTRY(zvl128b) |
| 533 | RUAPU_ISAENTRY(zvl256b) |
| 534 | RUAPU_ISAENTRY(zvl512b) |
| 535 | RUAPU_ISAENTRY(zvl1024b) |
| 536 | |
| 537 | RUAPU_ISAENTRY(xtheadba) |
| 538 | RUAPU_ISAENTRY(xtheadbb) |
| 539 | RUAPU_ISAENTRY(xtheadbs) |
| 540 | RUAPU_ISAENTRY(xtheadcondmov) |
| 541 | RUAPU_ISAENTRY(xtheadfmemidx) |
| 542 | RUAPU_ISAENTRY(xtheadfmv) |
| 543 | RUAPU_ISAENTRY(xtheadmac) |
| 544 | RUAPU_ISAENTRY(xtheadmemidx) |
| 545 | RUAPU_ISAENTRY(xtheadmempair) |
| 546 | RUAPU_ISAENTRY(xtheadsync) |
| 547 | RUAPU_ISAENTRY(xtheadvdot) |
| 548 | |
| 549 | #elif __openrisc__ |
| 550 | RUAPU_ISAENTRY(orbis32) |
| 551 | RUAPU_ISAENTRY(orbis64) |
| 552 | RUAPU_ISAENTRY(orfpx32) |
| 553 | RUAPU_ISAENTRY(orfpx64) |
| 554 | RUAPU_ISAENTRY(orvdx64) |
| 555 | |
| 556 | #endif |
| 557 | }; |
| 558 | |
| 559 | #undef RUAPU_ISAENTRY |
| 560 | |
| 561 | const char* g_ruapu_isa_supported[sizeof(g_ruapu_isa_map) / sizeof(g_ruapu_isa_map[0]) + 1] = { 0 }; |
| 562 | |
| 563 | #if defined __openrisc__ |
| 564 | static void ruapu_detect_openrisc_isa() |
| 565 | { |
| 566 | uint32_t value; |
| 567 | uint16_t addr = U(0x0000); |
| 568 | asm volatile ("l.mfspr %0, r0, %1" : "=r" (value) : "K" (addr)); |
| 569 | size_t j = 0; |
| 570 | for (size_t i = 0; i < sizeof(g_ruapu_isa_map) / sizeof(g_ruapu_isa_map[0]); i++) |
| 571 | { |
| 572 | int capable = ((value) >> (5 + i)) & 0x1; |
| 573 | if (capable) |
| 574 | { |
| 575 | g_ruapu_isa_supported[j] = g_ruapu_isa_map[i].isa; |
| 576 | j++; |
| 577 | } |
| 578 | } |
| 579 | g_ruapu_isa_supported[j] = 0; |
| 580 | } |
| 581 | #endif |
| 582 | |
| 583 | void ruapu_init() |
| 584 | { |
| 585 | #if defined _WIN32 || defined __ANDROID__ || defined __linux__ || defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __DragonFly__ || defined __sun__ || defined __SYTERKIT__ |
| 586 | size_t j = 0; |
| 587 | for (size_t i = 0; i < sizeof(g_ruapu_isa_map) / sizeof(g_ruapu_isa_map[0]); i++) |
| 588 | { |
| 589 | int capable = ruapu_detect_isa(g_ruapu_isa_map[i].inst); |
| 590 | if (capable) |
| 591 | { |
| 592 | g_ruapu_isa_supported[j] = g_ruapu_isa_map[i].isa; |
| 593 | j++; |
| 594 | } |
| 595 | } |
| 596 | g_ruapu_isa_supported[j] = 0; |
| 597 | #elif defined __openrisc__ |
| 598 | ruapu_detect_openrisc_isa(); |
| 599 | #else |
| 600 | // initialize g_ruapu_isa_map for baremetal here, default all zero |
| 601 | // there is still ruapu_some_XYZ() functions available |
| 602 | // but you have to work out your own signal handling |
| 603 | #warning ruapu does not support your baremetal os yet |
| 604 | #endif |
| 605 | } |
| 606 | |
| 607 | int ruapu_supports(const char* isa) |
| 608 | { |
| 609 | const char* const* isa_supported = g_ruapu_isa_supported; |
| 610 | while (*isa_supported) |
| 611 | { |
| 612 | if (strcmp(*isa_supported, isa) == 0) |
| 613 | return 1; |
| 614 | |
| 615 | isa_supported++; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | const char* const* ruapu_rua() |
| 622 | { |
| 623 | return g_ruapu_isa_supported; |
| 624 | } |
| 625 | |
| 626 | #endif // RUAPU_IMPLEMENTATION |
| 627 | |
| 628 | #ifdef __cplusplus |
| 629 | } |
| 630 | #endif |
| 631 | |
| 632 | #endif // RUAPU_H |