| 1 |
|
| #include "stencil.h" |
| 2 |
|
| |
| 3 |
|
| stencil_dispatch_t stencil_dispatch; |
| 4 |
|
| |
| 5 |
|
| /* Scalar fallbacks. Phase 02 supplies the SWAR/SIMD implementations and |
| 6 |
|
| * registers them here; the selection pattern is fixed from day one. */ |
| 7 |
|
| |
| 8 |
73719 |
| static const char *stencil_scan_scalar(const char *p, const char *end) |
| 9 |
|
| { |
| 10 |
|
| const char *hit; |
| 11 |
73719 |
▶ | if (p >= end) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= end) |
1985 |
71734 |
73719 |
|
| 12 |
1985 |
| return end; |
| 13 |
71734 |
| hit = (const char *)memchr(p, '{', (size_t)(end - p)); |
| 14 |
71734 |
▶ | return hit ? hit : end; |
Branch: 1 branch
| Branch | True | False | Total |
| return hit ? hit : end; |
71287 |
447 |
71734 |
|
| 15 |
|
| } |
| 16 |
|
| |
| 17 |
|
| /* cpuid.h first shipped in gcc 4.3; older x86 gccs (FreeBSD 9's base |
| 18 |
|
| * gcc 4.2.1) define __GNUC__ but lack the header, so without the version |
| 19 |
|
| * check caps stays 0 and the scalar paths are used there. */ |
| 20 |
|
| #if (defined(__x86_64__) || defined(__i386__) || defined(_M_X64)) \ |
| 21 |
|
| && defined(__GNUC__) \ |
| 22 |
|
| && (defined(__clang__) \ |
| 23 |
|
| || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) |
| 24 |
|
| # include <cpuid.h> |
| 25 |
|
| # define STENCIL_X86_CPUID 1 |
| 26 |
54 |
| static uint32_t stencil_x86_caps(void) |
| 27 |
|
| { |
| 28 |
54 |
| uint32_t caps = 0; |
| 29 |
|
| unsigned int a, b, c, d; |
| 30 |
54 |
| unsigned int max = __get_cpuid_max(0, 0); |
| 31 |
54 |
▶ | if (max >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (max >= 1) { |
54 |
0 |
54 |
|
| 32 |
54 |
| __cpuid_count(1, 0, a, b, c, d); |
| 33 |
54 |
▶ | if (d & (1u << 26)) caps |= STENCIL_CAP_SSE2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d & (1u << 26)) caps |= STENCIL_CAP_SSE2; |
54 |
0 |
54 |
|
| 34 |
54 |
▶ | if (c & (1u << 20)) caps |= STENCIL_CAP_SSE42; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c & (1u << 20)) caps |= STENCIL_CAP_SSE42; |
54 |
0 |
54 |
|
| 35 |
|
| } |
| 36 |
54 |
▶ | if (max >= 7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (max >= 7) { |
54 |
0 |
54 |
|
| 37 |
54 |
| __cpuid_count(7, 0, a, b, c, d); |
| 38 |
54 |
▶ | if (b & (1u << 5)) caps |= STENCIL_CAP_AVX2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b & (1u << 5)) caps |= STENCIL_CAP_AVX2; |
54 |
0 |
54 |
|
| 39 |
|
| } |
| 40 |
54 |
| return caps; |
| 41 |
|
| } |
| 42 |
|
| #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) |
| 43 |
|
| /* MSVC: the same leaves through <intrin.h>. Only the SSE2 variant is ever |
| 44 |
|
| * compiled there (the others need __attribute__((target))), but the CPU |
| 45 |
|
| * is still asked so a build with STENCIL_HAVE_SSE2 actually uses it. */ |
| 46 |
|
| # include <intrin.h> |
| 47 |
|
| # define STENCIL_X86_CPUID 1 |
| 48 |
|
| static uint32_t stencil_x86_caps(void) |
| 49 |
|
| { |
| 50 |
|
| uint32_t caps = 0; |
| 51 |
|
| int r[4]; |
| 52 |
|
| unsigned int max; |
| 53 |
|
| __cpuid(r, 0); |
| 54 |
|
| max = (unsigned int)r[0]; |
| 55 |
|
| if (max >= 1) { |
| 56 |
|
| __cpuidex(r, 1, 0); |
| 57 |
|
| if (r[3] & (1 << 26)) caps |= STENCIL_CAP_SSE2; |
| 58 |
|
| if (r[2] & (1 << 20)) caps |= STENCIL_CAP_SSE42; |
| 59 |
|
| } |
| 60 |
|
| if (max >= 7) { |
| 61 |
|
| __cpuidex(r, 7, 0); |
| 62 |
|
| if (r[1] & (1 << 5)) caps |= STENCIL_CAP_AVX2; |
| 63 |
|
| } |
| 64 |
|
| return caps; |
| 65 |
|
| } |
| 66 |
|
| #endif |
| 67 |
|
| |
| 68 |
54 |
| void stencil_boot(pTHX) |
| 69 |
|
| { |
| 70 |
54 |
| uint32_t caps = 0; |
| 71 |
|
| const char *force; |
| 72 |
|
| PERL_UNUSED_CONTEXT; |
| 73 |
|
| |
| 74 |
|
| #ifdef STENCIL_HAVE_COMPUTED_GOTO |
| 75 |
54 |
| caps |= STENCIL_CAP_COMPUTED_GOTO; |
| 76 |
|
| #endif |
| 77 |
|
| #ifdef STENCIL_HAVE_BUILTIN_EXPECT |
| 78 |
54 |
| caps |= STENCIL_CAP_BUILTIN_EXPECT; |
| 79 |
|
| #endif |
| 80 |
|
| |
| 81 |
|
| /* SIMD: a capability is advertised only when the variant was both |
| 82 |
|
| * compiled in (STENCIL_HAVE_*) and supported by this CPU. */ |
| 83 |
|
| { |
| 84 |
54 |
| uint32_t cpu = 0; |
| 85 |
|
| #ifdef STENCIL_X86_CPUID |
| 86 |
54 |
| cpu = stencil_x86_caps(); |
| 87 |
|
| #endif |
| 88 |
|
| #ifdef STENCIL_HAVE_NEON |
| 89 |
|
| cpu |= STENCIL_CAP_NEON; /* baseline on arm64 */ |
| 90 |
|
| #endif |
| 91 |
|
| #ifndef STENCIL_HAVE_SSE2 |
| 92 |
|
| cpu &= ~STENCIL_CAP_SSE2; |
| 93 |
|
| #endif |
| 94 |
|
| #ifndef STENCIL_HAVE_SSE42 |
| 95 |
|
| cpu &= ~STENCIL_CAP_SSE42; |
| 96 |
|
| #endif |
| 97 |
|
| #ifndef STENCIL_HAVE_AVX2 |
| 98 |
|
| cpu &= ~STENCIL_CAP_AVX2; |
| 99 |
|
| #endif |
| 100 |
54 |
| caps |= cpu; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| /* Force the portable paths - kept in shipped code so exotic hardware |
| 104 |
|
| * effectively tests both variants. */ |
| 105 |
54 |
| force = PerlEnv_getenv("STENCIL_FORCE_SWAR"); |
| 106 |
54 |
▶ | if (force && *force && !(*force == '0' && !force[1])) |
Branch: 4 branches
| Branch | True | False | Total |
| if (force && *force && !(*force == '0' && !force[1])) |
1 |
53 |
54 |
| if (force && *force && !(*force == '0' && !force[1])) |
1 |
0 |
1 |
| if (force && *force && !(*force == '0' && !force[1])) |
0 |
1 |
1 |
| if (force && *force && !(*force == '0' && !force[1])) |
0 |
0 |
0 |
|
| 107 |
1 |
| caps &= ~(STENCIL_CAP_SSE2 | STENCIL_CAP_SSE42 |
| 108 |
|
| | STENCIL_CAP_AVX2 | STENCIL_CAP_NEON); |
| 109 |
|
| |
| 110 |
54 |
| force = PerlEnv_getenv("STENCIL_FORCE_SWITCH"); |
| 111 |
54 |
| stencil_dispatch.force_switch = |
| 112 |
54 |
▶ | force && *force && !(*force == '0' && !force[1]); |
Branch: 4 branches
| Branch | True | False | Total |
| force && *force && !(*force == '0' && !force[1]); |
0 |
54 |
54 |
| force && *force && !(*force == '0' && !force[1]); |
0 |
0 |
0 |
| force && *force && !(*force == '0' && !force[1]); |
0 |
0 |
0 |
| force && *force && !(*force == '0' && !force[1]); |
0 |
0 |
0 |
|
| 113 |
|
| |
| 114 |
54 |
| stencil_dispatch.caps = caps; |
| 115 |
|
| |
| 116 |
|
| /* scan: libc memchr is already vectorised on every platform we |
| 117 |
|
| * care about; revisit in phase 03 only if profiling demands. */ |
| 118 |
54 |
| stencil_dispatch.scan = stencil_scan_scalar; |
| 119 |
|
| |
| 120 |
|
| /* escape: best compiled-in variant the CPU supports; SWAR baseline |
| 121 |
|
| * is always available. Selection happens exactly once. */ |
| 122 |
54 |
| stencil_dispatch.escape = stencil_escape_swar; |
| 123 |
|
| #ifdef STENCIL_HAVE_AVX2 |
| 124 |
54 |
▶ | if (caps & STENCIL_CAP_AVX2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (caps & STENCIL_CAP_AVX2) |
53 |
1 |
54 |
|
| 125 |
53 |
| stencil_dispatch.escape = stencil_escape_avx2; |
| 126 |
|
| else |
| 127 |
|
| #endif |
| 128 |
|
| #ifdef STENCIL_HAVE_SSE2 |
| 129 |
1 |
▶ | if (caps & STENCIL_CAP_SSE2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (caps & STENCIL_CAP_SSE2) |
0 |
1 |
1 |
|
| 130 |
0 |
| stencil_dispatch.escape = stencil_escape_sse2; |
| 131 |
|
| else |
| 132 |
|
| #endif |
| 133 |
|
| #ifdef STENCIL_HAVE_NEON |
| 134 |
|
| if (caps & STENCIL_CAP_NEON) |
| 135 |
|
| stencil_dispatch.escape = stencil_escape_neon; |
| 136 |
|
| else |
| 137 |
|
| #endif |
| 138 |
|
| ; /* SWAR baseline stands */ |
| 139 |
54 |
| } |