| 1 |
|
| /* src/simd/mds_simd_dispatch.c — runtime backend selection. |
| 2 |
|
| * |
| 3 |
|
| * Selection order: |
| 4 |
|
| * 1. If `mds_simd_force_scalar(1)` has been called, OR the env var |
| 5 |
|
| * MARKDOWN_SIMPLE_NO_SIMD is set to a truthy value, return scalar. |
| 6 |
|
| * 2. On aarch64: return NEON (mandatory ISA extension). |
| 7 |
|
| * 3. On x86_64: probe CPUID — AVX2 > SSE2/SSSE3 > scalar. |
| 8 |
|
| * (Stub builds without -DMDS_HAVE_* compile flags fall through, |
| 9 |
|
| * and never reach for CPUID at all.) |
| 10 |
|
| * 4. Otherwise scalar. |
| 11 |
|
| * |
| 12 |
|
| * The selected ops table is cached in a static after first call. |
| 13 |
|
| * `mds_simd_force_scalar()` invalidates the cache. |
| 14 |
|
| */ |
| 15 |
|
| #include "mds_simd.h" |
| 16 |
|
| |
| 17 |
|
| #include <stdlib.h> |
| 18 |
|
| #include <string.h> |
| 19 |
|
| |
| 20 |
|
| /* CPUID is only wanted when an x86 backend was actually compiled in - being |
| 21 |
|
| * on x86 is not the same question. GCC has only shipped cpuid.h since 4.3 |
| 22 |
|
| * (and __cpuid_count since 4.4), so keying the include off the architecture |
| 23 |
|
| * alone failed the whole build on FreeBSD 9, whose base cc is gcc 4.2.1, |
| 24 |
|
| * where the Makefile.PL probes had already declined both backends and there |
| 25 |
|
| * was nothing for CPUID to choose between. */ |
| 26 |
|
| #if defined(__has_include) |
| 27 |
|
| # if __has_include(<cpuid.h>) |
| 28 |
|
| # define MDS_HAVE_CPUID_H 1 |
| 29 |
|
| # endif |
| 30 |
|
| #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) |
| 31 |
|
| # define MDS_HAVE_CPUID_H 1 |
| 32 |
|
| #endif |
| 33 |
|
| |
| 34 |
|
| #if (defined(__x86_64__) || defined(_M_X64) || \ |
| 35 |
|
| defined(__i386__) || defined(_M_IX86)) && \ |
| 36 |
|
| (defined(MDS_HAVE_SSE2) || defined(MDS_HAVE_AVX2)) |
| 37 |
|
| # if defined(_MSC_VER) |
| 38 |
|
| # define MDS_SIMD_X86 1 |
| 39 |
|
| # include <intrin.h> |
| 40 |
|
| # elif defined(MDS_HAVE_CPUID_H) |
| 41 |
|
| # define MDS_SIMD_X86 1 |
| 42 |
|
| # include <cpuid.h> |
| 43 |
|
| # endif |
| 44 |
|
| /* No CPUID mechanism: the backend stays unselected and we run scalar, |
| 45 |
|
| * which is the same answer this file gives on any non-x86 host. */ |
| 46 |
|
| #endif |
| 47 |
|
| |
| 48 |
|
| static int s_force_scalar = 0; |
| 49 |
|
| static const mds_simd_ops* s_cached = 0; |
| 50 |
|
| static const char* s_cached_name = "scalar"; |
| 51 |
|
| |
| 52 |
737 |
| static int env_no_simd(void) |
| 53 |
|
| { |
| 54 |
737 |
| const char* v = getenv("MARKDOWN_SIMPLE_NO_SIMD"); |
| 55 |
737 |
▶ | if (!v || !*v) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !*v) return 0; |
1 |
736 |
737 |
| if (!v || !*v) return 0; |
0 |
1 |
1 |
|
| 56 |
1 |
▶ | if (v[0] == '0' && v[1] == '\0') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (v[0] == '0' && v[1] == '\0') return 0; |
0 |
1 |
1 |
| if (v[0] == '0' && v[1] == '\0') return 0; |
0 |
0 |
0 |
|
| 57 |
1 |
| return 1; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
|
| #ifdef MDS_SIMD_X86 |
| 61 |
2208 |
| static void cpuid_call(unsigned leaf, unsigned subleaf, unsigned regs[4]) |
| 62 |
|
| { |
| 63 |
|
| # if defined(_MSC_VER) |
| 64 |
|
| int r[4]; |
| 65 |
|
| __cpuidex(r, (int)leaf, (int)subleaf); |
| 66 |
|
| regs[0] = (unsigned)r[0]; regs[1] = (unsigned)r[1]; |
| 67 |
|
| regs[2] = (unsigned)r[2]; regs[3] = (unsigned)r[3]; |
| 68 |
|
| # else |
| 69 |
|
| unsigned a, b, c, d; |
| 70 |
2208 |
| __cpuid_count(leaf, subleaf, a, b, c, d); |
| 71 |
2208 |
| regs[0] = a; regs[1] = b; regs[2] = c; regs[3] = d; |
| 72 |
|
| # endif |
| 73 |
2208 |
| } |
| 74 |
|
| |
| 75 |
|
| /* XCR0, to learn whether the OS actually restores the YMM half on a context |
| 76 |
|
| * switch. Without this an AVX2-capable CPU under a kernel that never enabled |
| 77 |
|
| * XSAVE executes vpand with a silently truncated register. Spelled as its |
| 78 |
|
| * bytes because assemblers older than the instruction reject the mnemonic. */ |
| 79 |
736 |
| static unsigned xcr0_low(void) |
| 80 |
|
| { |
| 81 |
|
| # if defined(_MSC_VER) |
| 82 |
|
| return (unsigned)_xgetbv(0); |
| 83 |
|
| # else |
| 84 |
|
| unsigned a, d; |
| 85 |
736 |
| __asm__ __volatile__(".byte 0x0f, 0x01, 0xd0" |
| 86 |
|
| : "=a"(a), "=d"(d) : "c"(0)); |
| 87 |
|
| (void)d; |
| 88 |
736 |
| return a; |
| 89 |
|
| # endif |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| /* The "sse2" backend shuffles with pshufb, which is SSSE3, not SSE2: the |
| 93 |
|
| * name is the build flag's, not the ISA level's. Checking only the SSE2 bit |
| 94 |
|
| * selected it on an x86_64 CPU predating SSSE3 (K8, Prescott), where the |
| 95 |
|
| * first classified chunk is an illegal instruction. Check what it runs. */ |
| 96 |
0 |
| static int x86_has_ssse3(void) |
| 97 |
|
| { |
| 98 |
|
| unsigned r[4]; |
| 99 |
0 |
| cpuid_call(1, 0, r); |
| 100 |
0 |
▶ | if (!(r[3] & (1u << 26))) return 0; /* EDX bit 26: SSE2 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(r[3] & (1u << 26))) return 0; /* EDX bit 26: SSE2 */ |
0 |
0 |
0 |
|
| 101 |
0 |
| return (r[2] & (1u << 9)) != 0; /* ECX bit 9: SSSE3 */ |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| /* AVX2 needs three separate yeses: the CPU has AVX2 (and BMI2, which the |
| 105 |
|
| * backend's target attribute lets the compiler emit), the OS has turned on |
| 106 |
|
| * XSAVE, and XCR0 says the YMM state is among what it saves. */ |
| 107 |
736 |
| static int x86_has_avx2(void) |
| 108 |
|
| { |
| 109 |
|
| unsigned r[4]; |
| 110 |
736 |
| cpuid_call(1, 0, r); |
| 111 |
736 |
▶ | if (!(r[2] & (1u << 27))) return 0; /* ECX bit 27: OSXSAVE */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(r[2] & (1u << 27))) return 0; /* ECX bit 27: OSXSAVE */ |
0 |
736 |
736 |
|
| 112 |
736 |
▶ | if (!(r[2] & (1u << 28))) return 0; /* ECX bit 28: AVX */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(r[2] & (1u << 28))) return 0; /* ECX bit 28: AVX */ |
0 |
736 |
736 |
|
| 113 |
736 |
▶ | if ((xcr0_low() & 0x6u) != 0x6u) return 0; /* XMM | YMM saved */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((xcr0_low() & 0x6u) != 0x6u) return 0; /* XMM | YMM saved */ |
0 |
736 |
736 |
|
| 114 |
736 |
| cpuid_call(0, 0, r); |
| 115 |
736 |
▶ | if (r[0] < 7) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r[0] < 7) return 0; |
0 |
736 |
736 |
|
| 116 |
736 |
| cpuid_call(7, 0, r); |
| 117 |
736 |
▶ | if (!(r[1] & (1u << 5))) return 0; /* EBX bit 5: AVX2 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(r[1] & (1u << 5))) return 0; /* EBX bit 5: AVX2 */ |
0 |
736 |
736 |
|
| 118 |
736 |
| return (r[1] & (1u << 8)) != 0; /* EBX bit 8: BMI2 */ |
| 119 |
|
| } |
| 120 |
|
| #endif /* MDS_SIMD_X86 */ |
| 121 |
|
| |
| 122 |
1434 |
| static void pick(void) |
| 123 |
|
| { |
| 124 |
1434 |
▶ | if (s_force_scalar || env_no_simd()) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s_force_scalar || env_no_simd()) { |
737 |
697 |
1434 |
| if (s_force_scalar || env_no_simd()) { |
1 |
736 |
737 |
|
| 125 |
698 |
| s_cached = mds_simd_ops_scalar(); |
| 126 |
698 |
| s_cached_name = "scalar"; |
| 127 |
698 |
| return; |
| 128 |
|
| } |
| 129 |
|
| #ifdef MDS_HAVE_NEON |
| 130 |
|
| /* aarch64 always; on 32-bit ARM the build system gates this. */ |
| 131 |
|
| s_cached = mds_simd_ops_neon(); |
| 132 |
|
| s_cached_name = "neon"; |
| 133 |
|
| return; |
| 134 |
|
| #endif |
| 135 |
|
| #ifdef MDS_SIMD_X86 |
| 136 |
|
| # ifdef MDS_HAVE_AVX2 |
| 137 |
736 |
▶ | if (x86_has_avx2()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x86_has_avx2()) { |
736 |
0 |
736 |
|
| 138 |
736 |
| s_cached = mds_simd_ops_avx2(); |
| 139 |
736 |
| s_cached_name = "avx2"; |
| 140 |
736 |
| return; |
| 141 |
|
| } |
| 142 |
|
| # endif |
| 143 |
|
| # ifdef MDS_HAVE_SSE2 |
| 144 |
0 |
▶ | if (x86_has_ssse3()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x86_has_ssse3()) { |
0 |
0 |
0 |
|
| 145 |
0 |
| s_cached = mds_simd_ops_sse2(); |
| 146 |
0 |
| s_cached_name = "sse2"; |
| 147 |
0 |
| return; |
| 148 |
|
| } |
| 149 |
|
| # endif |
| 150 |
|
| #endif |
| 151 |
0 |
| s_cached = mds_simd_ops_scalar(); |
| 152 |
0 |
| s_cached_name = "scalar"; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
165298 |
| const mds_simd_ops* mds_simd_get(void) |
| 156 |
|
| { |
| 157 |
165298 |
▶ | if (!s_cached) pick(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s_cached) pick(); |
1429 |
163869 |
165298 |
|
| 158 |
165298 |
| return s_cached; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
5 |
| const char* mds_simd_backend(void) |
| 162 |
|
| { |
| 163 |
5 |
▶ | if (!s_cached) pick(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s_cached) pick(); |
5 |
0 |
5 |
|
| 164 |
5 |
| return s_cached_name; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
1394 |
| void mds_simd_force_scalar(int on) |
| 168 |
|
| { |
| 169 |
1394 |
| s_force_scalar = on ? 1 : 0; |
| 170 |
1394 |
| s_cached = 0; /* invalidate; next call to _get() re-picks */ |
| 171 |
1394 |
| s_cached_name = "scalar"; |
| 172 |
1394 |
| } |