1 | // Copyright 2022 Google LLC |
---|---|
2 | // |
3 | // This source code is licensed under the BSD-style license found in the |
4 | // LICENSE file in the root directory of this source tree. |
5 | |
6 | #include <assert.h> |
7 | #include <stddef.h> |
8 | |
9 | #ifdef _WIN32 |
10 | #include <windows.h> |
11 | #else |
12 | #include <pthread.h> |
13 | #endif |
14 | |
15 | #include <xnnpack/common.h> |
16 | #include <xnnpack/config.h> |
17 | #include <xnnpack/lut.h> |
18 | |
19 | |
20 | static struct xnn_x8_lut_config x8_lut_config = {0}; |
21 | |
22 | #if XNN_PLATFORM_WINDOWS |
23 | static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT; |
24 | #else |
25 | static pthread_once_t init_guard = PTHREAD_ONCE_INIT; |
26 | #endif |
27 | |
28 | static void init_x8_lut_config(void) { |
29 | #if XNN_ARCH_ARM |
30 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__scalar_x4; |
31 | x8_lut_config.tile_size = 4; |
32 | #elif XNN_ARCH_ARM64 |
33 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__aarch64_neon_tbx128x4_x64; |
34 | x8_lut_config.tile_size = 64; |
35 | #elif XNN_ARCH_X86 || XNN_ARCH_X86_64 |
36 | const struct xnn_hardware_config* hardware_config = xnn_init_hardware_config(); |
37 | assert(hardware_config != NULL); |
38 | |
39 | if (!XNN_PLATFORM_MOBILE && hardware_config->use_x86_avx512skx) { |
40 | if (hardware_config->use_x86_avx512vbmi) { |
41 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__avx512vbmi_vpermx2b_x128; |
42 | x8_lut_config.tile_size = 128; |
43 | } else { |
44 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__avx512skx_vpshufb_x64; |
45 | x8_lut_config.tile_size = 64; |
46 | } |
47 | } else if (hardware_config->use_x86_avx2) { |
48 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__avx2_x128; |
49 | x8_lut_config.tile_size = 128; |
50 | } else if (hardware_config->use_x86_avx) { |
51 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__avx_x64; |
52 | x8_lut_config.tile_size = 64; |
53 | } else { |
54 | // Note: SSSE3 version is usually slower than scalar |
55 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__scalar_x4; |
56 | x8_lut_config.tile_size = 4; |
57 | } |
58 | #elif XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD |
59 | const struct xnn_hardware_config* hardware_config = xnn_init_hardware_config(); |
60 | assert(hardware_config != NULL); |
61 | |
62 | if (hardware_config->is_x86) { |
63 | #if XNN_ARCH_WASMRELAXEDSIMD |
64 | if (hardware_config->use_wasm_pshufb) { |
65 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__wasmpshufb_x32; |
66 | x8_lut_config.tile_size = 32; |
67 | } else { |
68 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__scalar_x1; |
69 | x8_lut_config.tile_size = 1; |
70 | } |
71 | #else |
72 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__scalar_x1; |
73 | x8_lut_config.tile_size = 1; |
74 | #endif |
75 | } else { |
76 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__wasmsimd_x32; |
77 | x8_lut_config.tile_size = 32; |
78 | } |
79 | #elif XNN_ARCH_WASM |
80 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__scalar_x1; |
81 | x8_lut_config.tile_size = 1; |
82 | #elif XNN_ARCH_RISCV |
83 | x8_lut_config.microkernel = xnn_x8_lut_ukernel__scalar_x4; |
84 | x8_lut_config.tile_size = 4; |
85 | #else |
86 | #error "Unsupported architecture" |
87 | #endif |
88 | } |
89 | |
90 | #if XNN_PLATFORM_WINDOWS |
91 | static BOOL CALLBACK init_x8_lut_config_windows(PINIT_ONCE init_once, PVOID parameter, PVOID* context) { |
92 | init_x8_lut_config(); |
93 | return TRUE; |
94 | } |
95 | #endif |
96 | |
97 | const struct xnn_x8_lut_config* xnn_init_x8_lut_config() { |
98 | const struct xnn_hardware_config* hardware_config = xnn_init_hardware_config(); |
99 | if (hardware_config == NULL) { |
100 | return NULL; |
101 | } |
102 | #if XNN_PLATFORM_WINDOWS |
103 | InitOnceExecuteOnce(&init_guard, &init_x8_lut_config_windows, NULL, NULL); |
104 | #else |
105 | pthread_once(&init_guard, &init_x8_lut_config); |
106 | #endif |
107 | return &x8_lut_config; |
108 | } |
109 |