Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 71.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "cpu.h"
16
17#include "platform.h"
18
19#include <limits.h>
20#ifndef __wasi__
21#include <setjmp.h>
22#include <signal.h>
23#endif // __wasi__
24#include <stdio.h>
25#include <string.h>
26
27#ifdef _OPENMP
28#if NCNN_SIMPLEOMP
29#include "simpleomp.h"
30#else
31#include <omp.h>
32#endif
33#endif
34
35#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
36#ifdef _MSC_VER
37#include <intrin.h> // __cpuid()
38#include <immintrin.h> // _xgetbv()
39#endif
40#if defined(__clang__) || defined(__GNUC__)
41#include <cpuid.h> // __get_cpuid() and __cpuid_count()
42#endif
43#endif
44
45#ifdef __EMSCRIPTEN__
46#include <emscripten/threading.h>
47#endif
48
49#if defined _WIN32
50#define WIN32_LEAN_AND_MEAN
51#include <windows.h>
52#endif
53
54#if defined __ANDROID__ || defined __linux__
55#if defined __ANDROID__
56#if __ANDROID_API__ >= 18
57#include <sys/auxv.h> // getauxval()
58#endif
59#include <sys/system_properties.h> // __system_property_get()
60#include <dlfcn.h>
61#endif
62#include <ctype.h>
63#include <stdint.h>
64#include <fcntl.h>
65#include <sys/stat.h>
66#include <sys/syscall.h>
67#include <unistd.h>
68#endif
69
70#if __APPLE__
71#include <mach/mach.h>
72#include <mach/machine.h>
73#include <mach/thread_act.h>
74#include <sys/sysctl.h>
75#include <sys/types.h>
76#include <unistd.h>
77#include "TargetConditionals.h"
78#if TARGET_OS_IPHONE
79#define __IOS__ 1
80#endif
81// define missing cpu model for old sdk
82#ifndef CPUFAMILY_ARM_HURRICANE
83#define CPUFAMILY_ARM_HURRICANE 0x67ceee93
84#endif
85// A11
86#ifndef CPUFAMILY_ARM_MONSOON_MISTRAL
87#define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
88#endif
89// A12
90#ifndef CPUFAMILY_ARM_VORTEX_TEMPEST
91#define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
92#endif
93// A13
94#ifndef CPUFAMILY_ARM_LIGHTNING_THUNDER
95#define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
96#endif
97// A14 / M1
98#ifndef CPUFAMILY_ARM_FIRESTORM_ICESTORM
99#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
100#endif
101// A15 / M2
102#ifndef CPUFAMILY_ARM_AVALANCHE_BLIZZARD
103#define CPUFAMILY_ARM_AVALANCHE_BLIZZARD 0xda33d83d
104#endif
105// A16
106#ifndef CPUFAMILY_ARM_EVEREST_SAWTOOTH
107#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
108#endif
109// A17
110#ifndef CPUFAMILY_ARM_COLL
111#define CPUFAMILY_ARM_COLL 0x2876f5b5
112#endif
113// M3
114#ifndef CPUFAMILY_ARM_IBIZA
115#define CPUFAMILY_ARM_IBIZA 0xfa33415e
116#endif
117// M3 Pro
118#ifndef CPUFAMILY_ARM_LOBOS
119#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
120#endif
121// M3 Max
122#ifndef CPUFAMILY_ARM_PALMA
123#define CPUFAMILY_ARM_PALMA 0x72015832
124#endif
125#endif // __APPLE__
126
127#if defined(__SSE3__)
128#include <immintrin.h>
129#endif
130
131#if (defined _WIN32 && (__aarch64__ || __arm__))
132#define RUAPU_IMPLEMENTATION
133#include "ruapu.h"
134#endif
135
136// topology info
137static int g_cpucount;
138static int g_physical_cpucount;
139static int g_powersave;
140static ncnn::CpuSet g_cpu_affinity_mask_all;
141static ncnn::CpuSet g_cpu_affinity_mask_little;
142static ncnn::CpuSet g_cpu_affinity_mask_big;
143
144// isa info
145#if defined _WIN32
146#if __aarch64__
147static int g_cpu_support_arm_asimdhp;
148static int g_cpu_support_arm_cpuid;
149static int g_cpu_support_arm_asimddp;
150static int g_cpu_support_arm_asimdfhm;
151static int g_cpu_support_arm_bf16;
152static int g_cpu_support_arm_i8mm;
153static int g_cpu_support_arm_sve;
154static int g_cpu_support_arm_sve2;
155static int g_cpu_support_arm_svebf16;
156static int g_cpu_support_arm_svei8mm;
157static int g_cpu_support_arm_svef32mm;
158#elif __arm__
159static int g_cpu_support_arm_edsp;
160static int g_cpu_support_arm_neon;
161static int g_cpu_support_arm_vfpv4;
162#endif // __aarch64__ || __arm__
163#elif defined __ANDROID__ || defined __linux__
164static unsigned int g_hwcaps;
165static unsigned int g_hwcaps2;
166#elif __APPLE__
167static unsigned int g_hw_cpufamily;
168static cpu_type_t g_hw_cputype;
169static cpu_subtype_t g_hw_cpusubtype;
170#if __aarch64__
171static int g_hw_optional_arm_FEAT_FP16;
172static int g_hw_optional_arm_FEAT_DotProd;
173static int g_hw_optional_arm_FEAT_FHM;
174static int g_hw_optional_arm_FEAT_BF16;
175static int g_hw_optional_arm_FEAT_I8MM;
176#endif // __aarch64__
177#endif
178
179#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
180static int g_cpu_support_x86_avx;
181static int g_cpu_support_x86_fma;
182static int g_cpu_support_x86_xop;
183static int g_cpu_support_x86_f16c;
184static int g_cpu_support_x86_avx2;
185static int g_cpu_support_x86_avx_vnni;
186static int g_cpu_support_x86_avx512;
187static int g_cpu_support_x86_avx512_vnni;
188static int g_cpu_support_x86_avx512_bf16;
189static int g_cpu_support_x86_avx512_fp16;
190#endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
191
192static int g_cpu_level2_cachesize;
193static int g_cpu_level3_cachesize;
194
195// misc info
196#if defined __ANDROID__ || defined __linux__
197#if __aarch64__
198static int g_cpu_is_arm_a53_a55;
199#endif // __aarch64__
200#endif // defined __ANDROID__ || defined __linux__
201
202static bool is_being_debugged()
203{
204#if defined _WIN32
205 return IsDebuggerPresent();
206#elif defined __ANDROID__ || defined __linux__
207 // https://stackoverflow.com/questions/3596781/how-to-detect-if-the-current-process-is-being-run-by-gdb
208 int status_fd = open("/proc/self/status", O_RDONLY);
209 if (status_fd == -1)
210 return false;
211
212 char buf[4096];
213 ssize_t num_read = read(status_fd, buf, sizeof(buf) - 1);
214 close(status_fd);
215
216 if (num_read <= 0)
217 return false;
218
219 buf[num_read] = '\0';
220 const char tracerPidString[] = "TracerPid:";
221 const char* tracer_pid_ptr = strstr(buf, tracerPidString);
222 if (!tracer_pid_ptr)
223 return false;
224
225 for (const char* ch = tracer_pid_ptr + sizeof(tracerPidString) - 1; ch <= buf + num_read; ++ch)
226 {
227 if (isspace(*ch))
228 continue;
229
230 return isdigit(*ch) != 0 && *ch != '0';
231 }
232
233 return false;
234#elif defined __APPLE__
235 // https://stackoverflow.com/questions/2200277/detecting-debugger-on-mac-os-x
236 struct kinfo_proc info;
237 info.kp_proc.p_flag = 0;
238
239 int mib[4];
240 mib[0] = CTL_KERN;
241 mib[1] = KERN_PROC;
242 mib[2] = KERN_PROC_PID;
243 mib[3] = getpid();
244
245 size_t size = sizeof(info);
246 sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
247
248 return ((info.kp_proc.p_flag & P_TRACED) != 0);
249#else
250 // unknown platform :(
251 fprintf(stderr, "unknown platform!\n");
252 return false;
253#endif
254}
255
256#if defined __ANDROID__ || defined __linux__
257
258#define AT_HWCAP 16
259#define AT_HWCAP2 26
260
261#if __aarch64__
262// from arch/arm64/include/uapi/asm/hwcap.h
263#define HWCAP_ASIMD (1 << 1)
264#define HWCAP_ASIMDHP (1 << 10)
265#define HWCAP_CPUID (1 << 11)
266#define HWCAP_ASIMDDP (1 << 20)
267#define HWCAP_SVE (1 << 22)
268#define HWCAP_ASIMDFHM (1 << 23)
269#define HWCAP2_SVE2 (1 << 1)
270#define HWCAP2_SVEI8MM (1 << 9)
271#define HWCAP2_SVEF32MM (1 << 10)
272#define HWCAP2_SVEBF16 (1 << 12)
273#define HWCAP2_I8MM (1 << 13)
274#define HWCAP2_BF16 (1 << 14)
275#else
276// from arch/arm/include/uapi/asm/hwcap.h
277#define HWCAP_EDSP (1 << 7)
278#define HWCAP_NEON (1 << 12)
279#define HWCAP_VFPv4 (1 << 16)
280#endif
281
282#if __mips__
283// from arch/mips/include/uapi/asm/hwcap.h
284#define HWCAP_MIPS_MSA (1 << 1)
285#define HWCAP_LOONGSON_MMI (1 << 11)
286#endif
287
288#if __loongarch64
289// from arch/loongarch/include/uapi/asm/hwcap.h
290#define HWCAP_LOONGARCH_LSX (1 << 4)
291#define HWCAP_LOONGARCH_LASX (1 << 5)
292#endif
293
294#if __riscv
295// from arch/riscv/include/uapi/asm/hwcap.h
296#define COMPAT_HWCAP_ISA_F (1 << ('F' - 'A'))
297#define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A'))
298#endif
299
300#if defined __ANDROID__
301// Probe the system's C library for a 'getauxval' function and call it if
302// it exits, or return 0 for failure. This function is available since API
303// level 18.
304//
305// Note that getauxval() can't really be re-implemented here, because
306// its implementation does not parse /proc/self/auxv. Instead it depends
307// on values that are passed by the kernel at process-init time to the
308// C runtime initialization layer.
309static unsigned int get_elf_hwcap_from_getauxval(unsigned int type)
310{
311#if __ANDROID_API__ >= 18
312 unsigned int hwcap = getauxval(type);
313 if (hwcap)
314 return hwcap;
315#endif
316
317 typedef unsigned long getauxval_func_t(unsigned long);
318
319 dlerror();
320 void* libc_handle = dlopen("libc.so", RTLD_NOW);
321 if (!libc_handle)
322 {
323 NCNN_LOGE("dlopen libc.so failed %s", dlerror());
324 return 0;
325 }
326
327 unsigned int result = 0;
328 getauxval_func_t* func = (getauxval_func_t*)dlsym(libc_handle, "getauxval");
329 if (!func)
330 {
331 NCNN_LOGE("dlsym getauxval failed");
332 }
333 else
334 {
335 // Note: getauxval() returns 0 on failure. Doesn't touch errno.
336 result = (unsigned int)(*func)(type);
337 }
338 dlclose(libc_handle);
339
340 return result;
341}
342#endif // defined __ANDROID__
343
344// extract the ELF HW capabilities bitmap from /proc/self/auxv
345static unsigned int get_elf_hwcap_from_proc_self_auxv(unsigned int type)
346{
347 FILE* fp = fopen("/proc/self/auxv", "rb");
348 if (!fp)
349 {
350 NCNN_LOGE("fopen /proc/self/auxv failed");
351 return 0;
352 }
353
354#if __aarch64__ || __mips64 || __riscv_xlen == 64 || __loongarch64
355 struct
356 {
357 uint64_t tag;
358 uint64_t value;
359 } entry;
360#else
361 struct
362 {
363 unsigned int tag;
364 unsigned int value;
365 } entry;
366
367#endif
368
369 unsigned int result = 0;
370 while (!feof(fp))
371 {
372 int nread = fread((char*)&entry, sizeof(entry), 1, fp);
373 if (nread != 1)
374 break;
375
376 if (entry.tag == 0 && entry.value == 0)
377 break;
378
379 if (entry.tag == type)
380 {
381 result = entry.value;
382 break;
383 }
384 }
385
386 fclose(fp);
387
388 return result;
389}
390
391static unsigned int get_elf_hwcap(unsigned int type)
392{
393 unsigned int hwcap = 0;
394
395#if defined __ANDROID__
396 hwcap = get_elf_hwcap_from_getauxval(type);
397#endif
398
399 if (!hwcap)
400 hwcap = get_elf_hwcap_from_proc_self_auxv(type);
401
402#if defined __ANDROID__
403#if __aarch64__
404 if (type == AT_HWCAP)
405 {
406 // samsung exynos9810 on android pre-9 incorrectly reports armv8.2
407 // for little cores, but big cores only support armv8.0
408 // drop all armv8.2 features used by ncnn for preventing SIGILLs
409 // ref https://reviews.llvm.org/D114523
410 char arch[PROP_VALUE_MAX];
411 int len = __system_property_get("ro.arch", arch);
412 if (len > 0 && strncmp(arch, "exynos9810", 10) == 0)
413 {
414 hwcap &= ~HWCAP_ASIMDHP;
415 hwcap &= ~HWCAP_ASIMDDP;
416 }
417 }
418#endif // __aarch64__
419#endif // defined __ANDROID__
420
421 return hwcap;
422}
423#endif // defined __ANDROID__ || defined __linux__
424
425#if __APPLE__
426static unsigned int get_hw_cpufamily()
427{
428 unsigned int value = 0;
429 size_t len = sizeof(value);
430 sysctlbyname("hw.cpufamily", &value, &len, NULL, 0);
431 return value;
432}
433
434static cpu_type_t get_hw_cputype()
435{
436 cpu_type_t value = 0;
437 size_t len = sizeof(value);
438 sysctlbyname("hw.cputype", &value, &len, NULL, 0);
439 return value;
440}
441
442static cpu_subtype_t get_hw_cpusubtype()
443{
444 cpu_subtype_t value = 0;
445 size_t len = sizeof(value);
446 sysctlbyname("hw.cpusubtype", &value, &len, NULL, 0);
447 return value;
448}
449
450static int get_hw_capability(const char* cap)
451{
452 int64_t value = 0;
453 size_t len = sizeof(value);
454 sysctlbyname(cap, &value, &len, NULL, 0);
455 return value;
456}
457#endif // __APPLE__
458
459#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
460static inline void x86_cpuid(int level, unsigned int out[4])
461{
462#if defined(_MSC_VER) && !defined(__clang__)
463 __cpuid((int*)out, level);
464#elif defined(__clang__) || defined(__GNUC__)
465 __get_cpuid(level, out, out + 1, out + 2, out + 3);
466#else
467 NCNN_LOGE("x86_cpuid is unknown for current compiler");
468 out[0] = 0;
469 out[1] = 0;
470 out[2] = 0;
471 out[3] = 0;
472#endif
473}
474
475static inline void x86_cpuid_sublevel(int level, int sublevel, unsigned int out[4])
476{
477#if defined(_MSC_VER)
478 __cpuidex((int*)out, level, sublevel);
479#elif defined(__clang__) || defined(__GNUC__)
480 __cpuid_count(level, sublevel, out[0], out[1], out[2], out[3]);
481#else
482 NCNN_LOGE("x86_cpuid_sublevel is unknown for current compiler");
483 out[0] = 0;
484 out[1] = 0;
485 out[2] = 0;
486 out[3] = 0;
487#endif
488}
489
490static inline int x86_get_xcr0()
491{
492#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
493 return _xgetbv(0);
494#elif defined(__i386__) || defined(__x86_64__)
495 int xcr0 = 0;
496 asm(".byte 0x0f, 0x01, 0xd0"
497 : "=a"(xcr0)
498 : "c"(0)
499 : "%edx");
500 return xcr0;
501#else
502 NCNN_LOGE("x86_get_xcr0 is unknown for current compiler");
503 return 0xffffffff; // assume it will work
504#endif
505}
506
507static int get_cpu_support_x86_avx()
508{
509 unsigned int cpu_info[4] = {0};
510 x86_cpuid(0, cpu_info);
511
512 int nIds = cpu_info[0];
513 if (nIds < 1)
514 return 0;
515
516 x86_cpuid(1, cpu_info);
517 // check AVX XSAVE OSXSAVE
518 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
519 return 0;
520
521 // check XSAVE enabled by kernel
522 if ((x86_get_xcr0() & 6) != 6)
523 return 0;
524
525 return 1;
526}
527
528static int get_cpu_support_x86_fma()
529{
530 unsigned int cpu_info[4] = {0};
531 x86_cpuid(0, cpu_info);
532
533 int nIds = cpu_info[0];
534 if (nIds < 7)
535 return 0;
536
537 x86_cpuid(1, cpu_info);
538 // check AVX XSAVE OSXSAVE
539 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
540 return 0;
541
542 // check XSAVE enabled by kernel
543 if ((x86_get_xcr0() & 6) != 6)
544 return 0;
545
546 return cpu_info[2] & (1u << 12);
547}
548
549static int get_cpu_support_x86_xop()
550{
551 unsigned int cpu_info[4] = {0};
552 x86_cpuid(0x80000000, cpu_info);
553
554 if (cpu_info[0] < 0x80000001)
555 return 0;
556
557 x86_cpuid(0x80000001, cpu_info);
558
559 return cpu_info[2] & (1u << 11);
560}
561
562static int get_cpu_support_x86_f16c()
563{
564 unsigned int cpu_info[4] = {0};
565 x86_cpuid(0, cpu_info);
566
567 int nIds = cpu_info[0];
568 if (nIds < 1)
569 return 0;
570
571 x86_cpuid(1, cpu_info);
572
573 return cpu_info[2] & (1u << 29);
574}
575
576static int get_cpu_support_x86_avx2()
577{
578 unsigned int cpu_info[4] = {0};
579 x86_cpuid(0, cpu_info);
580
581 int nIds = cpu_info[0];
582 if (nIds < 7)
583 return 0;
584
585 x86_cpuid(1, cpu_info);
586 // check AVX XSAVE OSXSAVE
587 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
588 return 0;
589
590 // check XSAVE enabled by kernel
591 if ((x86_get_xcr0() & 6) != 6)
592 return 0;
593
594 x86_cpuid_sublevel(7, 0, cpu_info);
595 return cpu_info[1] & (1u << 5);
596}
597
598static int get_cpu_support_x86_avx_vnni()
599{
600 unsigned int cpu_info[4] = {0};
601 x86_cpuid(0, cpu_info);
602
603 int nIds = cpu_info[0];
604 if (nIds < 7)
605 return 0;
606
607 x86_cpuid(1, cpu_info);
608 // check AVX XSAVE OSXSAVE
609 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
610 return 0;
611
612 // check XSAVE enabled by kernel
613 if ((x86_get_xcr0() & 6) != 6)
614 return 0;
615
616 x86_cpuid_sublevel(7, 1, cpu_info);
617 return cpu_info[0] & (1u << 4);
618}
619
620static int get_cpu_support_x86_avx512()
621{
622#if __APPLE__
623 return get_hw_capability("hw.optional.avx512f")
624 && get_hw_capability("hw.optional.avx512bw")
625 && get_hw_capability("hw.optional.avx512cd")
626 && get_hw_capability("hw.optional.avx512dq")
627 && get_hw_capability("hw.optional.avx512vl");
628#else
629 unsigned int cpu_info[4] = {0};
630 x86_cpuid(0, cpu_info);
631
632 int nIds = cpu_info[0];
633 if (nIds < 7)
634 return 0;
635
636 x86_cpuid(1, cpu_info);
637 // check AVX XSAVE OSXSAVE
638 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
639 return 0;
640
641 // check XSAVE enabled by kernel
642 if ((x86_get_xcr0() & 6) != 6)
643 return 0;
644
645 // check avx512 XSAVE enabled by kernel
646 if ((x86_get_xcr0() & 0xe0) != 0xe0)
647 return 0;
648
649 x86_cpuid_sublevel(7, 0, cpu_info);
650 return (cpu_info[1] & (1u << 16)) && (cpu_info[1] & (1u << 17)) && (cpu_info[1] & (1u << 28)) && (cpu_info[1] & (1u << 30)) && (cpu_info[1] & (1u << 31));
651#endif
652}
653
654static int get_cpu_support_x86_avx512_vnni()
655{
656#if __APPLE__
657 return get_hw_capability("hw.optional.avx512vnni");
658#else
659 unsigned int cpu_info[4] = {0};
660 x86_cpuid(0, cpu_info);
661
662 int nIds = cpu_info[0];
663 if (nIds < 7)
664 return 0;
665
666 x86_cpuid(1, cpu_info);
667 // check AVX XSAVE OSXSAVE
668 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
669 return 0;
670
671 // check XSAVE enabled by kernel
672 if ((x86_get_xcr0() & 6) != 6)
673 return 0;
674
675 // check avx512 XSAVE enabled by kernel
676 if ((x86_get_xcr0() & 0xe0) != 0xe0)
677 return 0;
678
679 x86_cpuid_sublevel(7, 0, cpu_info);
680 return cpu_info[2] & (1u << 11);
681#endif
682}
683
684static int get_cpu_support_x86_avx512_bf16()
685{
686#if __APPLE__
687 return get_hw_capability("hw.optional.avx512bf16");
688#else
689 unsigned int cpu_info[4] = {0};
690 x86_cpuid(0, cpu_info);
691
692 int nIds = cpu_info[0];
693 if (nIds < 7)
694 return 0;
695
696 x86_cpuid(1, cpu_info);
697 // check AVX XSAVE OSXSAVE
698 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
699 return 0;
700
701 // check XSAVE enabled by kernel
702 if ((x86_get_xcr0() & 6) != 6)
703 return 0;
704
705 x86_cpuid_sublevel(7, 1, cpu_info);
706 return cpu_info[0] & (1u << 5);
707#endif
708}
709
710static int get_cpu_support_x86_avx512_fp16()
711{
712#if __APPLE__
713 return get_hw_capability("hw.optional.avx512fp16");
714#else
715 unsigned int cpu_info[4] = {0};
716 x86_cpuid(0, cpu_info);
717
718 int nIds = cpu_info[0];
719 if (nIds < 7)
720 return 0;
721
722 x86_cpuid(1, cpu_info);
723 // check AVX XSAVE OSXSAVE
724 if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
725 return 0;
726
727 // check XSAVE enabled by kernel
728 if ((x86_get_xcr0() & 6) != 6)
729 return 0;
730
731 // check avx512 XSAVE enabled by kernel
732 if ((x86_get_xcr0() & 0xe0) != 0xe0)
733 return 0;
734
735 x86_cpuid_sublevel(7, 0, cpu_info);
736 return cpu_info[3] & (1u << 23);
737#endif
738}
739#endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
740
741static int get_cpucount()
742{
743 int count = 0;
744#ifdef __EMSCRIPTEN__
745 if (emscripten_has_threading_support())
746 count = emscripten_num_logical_cores();
747 else
748 count = 1;
749#elif defined _WIN32
750 SYSTEM_INFO system_info;
751 GetSystemInfo(&system_info);
752 count = system_info.dwNumberOfProcessors;
753#elif defined __ANDROID__ || defined __linux__
754 // get cpu count from /proc/cpuinfo
755 FILE* fp = fopen("/proc/cpuinfo", "rb");
756 if (!fp)
757 return 1;
758
759 char line[1024];
760 while (!feof(fp))
761 {
762 char* s = fgets(line, 1024, fp);
763 if (!s)
764 break;
765
766 if (memcmp(line, "processor", 9) == 0)
767 {
768 count++;
769 }
770 }
771
772 fclose(fp);
773#elif __APPLE__
774 size_t len = sizeof(count);
775 sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
776#else
777#ifdef _OPENMP
778 count = omp_get_max_threads();
779#else
780 count = 1;
781#endif // _OPENMP
782#endif
783
784 if (count < 1)
785 count = 1;
786
787 return count;
788}
789
790#if defined __ANDROID__ || defined __linux__
791static int get_thread_siblings(int cpuid)
792{
793 char path[256];
794 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpuid);
795
796 FILE* fp = fopen(path, "rb");
797 if (!fp)
798 return -1;
799
800 int thread_siblings = -1;
801 int nscan = fscanf(fp, "%x", &thread_siblings);
802 if (nscan != 1)
803 {
804 // ignore
805 }
806
807 fclose(fp);
808
809 return thread_siblings;
810}
811#endif // defined __ANDROID__ || defined __linux__
812
813static int get_physical_cpucount()
814{
815 int count = 0;
816#if defined _WIN32
817 typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
818 LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
819 if (glpi == NULL)
820 {
821 NCNN_LOGE("GetLogicalProcessorInformation is not supported");
822 return g_cpucount;
823 }
824
825 DWORD return_length = 0;
826 glpi(NULL, &return_length);
827
828 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
829 glpi(buffer, &return_length);
830
831 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
832 DWORD byte_offset = 0;
833 while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
834 {
835 if (ptr->Relationship == RelationProcessorCore)
836 {
837 count++;
838 }
839
840 byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
841 ptr++;
842 }
843
844 free(buffer);
845#elif defined __ANDROID__ || defined __linux__
846 std::vector<int> thread_set;
847 for (int i = 0; i < g_cpucount; i++)
848 {
849 int thread_siblings = get_thread_siblings(i);
850 if (thread_siblings == -1)
851 {
852 // ignore malformed one
853 continue;
854 }
855
856 bool thread_siblings_exists = false;
857 for (size_t j = 0; j < thread_set.size(); j++)
858 {
859 if (thread_set[j] == thread_siblings)
860 {
861 thread_siblings_exists = true;
862 break;
863 }
864 }
865
866 if (!thread_siblings_exists)
867 {
868 thread_set.push_back(thread_siblings);
869 count++;
870 }
871 }
872#elif __APPLE__
873 size_t len = sizeof(count);
874 sysctlbyname("hw.physicalcpu_max", &count, &len, NULL, 0);
875#else
876 count = g_cpucount;
877#endif
878
879 if (count > g_cpucount)
880 count = g_cpucount;
881
882 return count;
883}
884
885#if defined __ANDROID__ || defined __linux__
886static int get_data_cache_size(int cpuid, int level)
887{
888 char path[256];
889
890 // discover sysfs cache entry
891 int indexid = -1;
892 for (int i = 0;; i++)
893 {
894 // check level
895 {
896 sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/level", cpuid, i);
897 FILE* fp = fopen(path, "rb");
898 if (!fp)
899 break;
900
901 int cache_level = -1;
902 int nscan = fscanf(fp, "%d", &cache_level);
903 fclose(fp);
904 if (nscan != 1 || cache_level != level)
905 continue;
906 }
907
908 // check type
909 {
910 sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/type", cpuid, i);
911 FILE* fp = fopen(path, "rb");
912 if (!fp)
913 break;
914
915 char type[32];
916 int nscan = fscanf(fp, "%31s", type);
917 fclose(fp);
918 if (nscan != 1 || (strcmp(type, "Data") != 0 && strcmp(type, "Unified") != 0))
919 continue;
920 }
921
922 indexid = i;
923 break;
924 }
925
926 if (indexid == -1)
927 {
928 // no sysfs entry
929 return 0;
930 }
931
932 // get size
933 int cache_size_K = 0;
934 {
935 sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/size", cpuid, indexid);
936 FILE* fp = fopen(path, "rb");
937 if (!fp)
938 return 0;
939
940 int nscan = fscanf(fp, "%dK", &cache_size_K);
941 fclose(fp);
942 if (nscan != 1)
943 {
944 NCNN_LOGE("fscanf cache_size_K error %d", nscan);
945 return 0;
946 }
947 }
948
949 // parse shared_cpu_map mask
950 ncnn::CpuSet shared_cpu_map;
951 {
952 sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/shared_cpu_map", cpuid, indexid);
953 FILE* fp = fopen(path, "rb");
954 if (!fp)
955 return 0;
956
957 char shared_cpu_map_str[256];
958 int nscan = fscanf(fp, "%255s", shared_cpu_map_str);
959 fclose(fp);
960 if (nscan != 1)
961 {
962 NCNN_LOGE("fscanf shared_cpu_map error %d", nscan);
963 return 0;
964 }
965
966 int len = strlen(shared_cpu_map_str);
967
968 if (shared_cpu_map_str[0] == '0' && shared_cpu_map_str[1] == 'x')
969 {
970 // skip leading 0x
971 len -= 2;
972 }
973
974 int ci = 0;
975 for (int i = len - 1; i >= 0; i--)
976 {
977 char x = shared_cpu_map_str[i];
978 if (x & 1) shared_cpu_map.enable(ci + 0);
979 if (x & 2) shared_cpu_map.enable(ci + 1);
980 if (x & 4) shared_cpu_map.enable(ci + 2);
981 if (x & 8) shared_cpu_map.enable(ci + 3);
982
983 ci += 4;
984 }
985 }
986
987 if (shared_cpu_map.num_enabled() == 1)
988 return cache_size_K * 1024;
989
990 // resolve physical cpu count in the shared_cpu_map
991 int shared_physical_cpu_count = 0;
992 {
993 std::vector<int> thread_set;
994 for (int i = 0; i < g_cpucount; i++)
995 {
996 if (!shared_cpu_map.is_enabled(i))
997 continue;
998
999 int thread_siblings = get_thread_siblings(i);
1000 if (thread_siblings == -1)
1001 {
1002 // ignore malformed one
1003 continue;
1004 }
1005
1006 bool thread_siblings_exists = false;
1007 for (size_t j = 0; j < thread_set.size(); j++)
1008 {
1009 if (thread_set[j] == thread_siblings)
1010 {
1011 thread_siblings_exists = true;
1012 break;
1013 }
1014 }
1015
1016 if (!thread_siblings_exists)
1017 {
1018 thread_set.push_back(thread_siblings);
1019 shared_physical_cpu_count++;
1020 }
1021 }
1022 }
1023
1024 // return per-physical-core cache size with 4K aligned
1025 cache_size_K = (cache_size_K / shared_physical_cpu_count + 3) / 4 * 4;
1026
1027 return cache_size_K * 1024;
1028}
1029
1030static int get_big_cpu_data_cache_size(int level)
1031{
1032 if (g_cpu_affinity_mask_big.num_enabled() == 0)
1033 {
1034 // smp cpu
1035 return get_data_cache_size(0, level);
1036 }
1037
1038 for (int i = 0; i < g_cpucount; i++)
1039 {
1040 if (g_cpu_affinity_mask_big.is_enabled(i))
1041 {
1042 return get_data_cache_size(i, level);
1043 }
1044 }
1045
1046 // should never reach here, fallback to cpu0
1047 return get_data_cache_size(0, level);
1048}
1049#endif // defined __ANDROID__ || defined __linux__
1050
1051static int get_cpu_level2_cachesize()
1052{
1053 int size = 0;
1054#if defined _WIN32
1055 typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1056 LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
1057 if (glpi != NULL)
1058 {
1059 DWORD return_length = 0;
1060 glpi(NULL, &return_length);
1061
1062 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
1063 glpi(buffer, &return_length);
1064
1065 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
1066 DWORD byte_offset = 0;
1067 while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
1068 {
1069 if (ptr->Relationship == RelationCache)
1070 {
1071 PCACHE_DESCRIPTOR Cache = &ptr->Cache;
1072 if (Cache->Level == 2)
1073 {
1074 size = std::max(size, (int)Cache->Size);
1075 }
1076 }
1077
1078 byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1079 ptr++;
1080 }
1081
1082 free(buffer);
1083 }
1084#elif defined __ANDROID__ || defined __linux__
1085 size = get_big_cpu_data_cache_size(2);
1086#if defined(_SC_LEVEL2_CACHE_SIZE)
1087 if (size <= 0)
1088 size = sysconf(_SC_LEVEL2_CACHE_SIZE);
1089#endif
1090#elif __APPLE__
1091 // perflevel 0 is the higher performance cluster
1092 int cpusperl2 = get_hw_capability("hw.perflevel0.cpusperl2");
1093 int l2cachesize = get_hw_capability("hw.perflevel0.l2cachesize");
1094 size = cpusperl2 > 1 ? l2cachesize / cpusperl2 : l2cachesize;
1095#endif
1096
1097 // fallback to a common value
1098 if (size <= 0)
1099 {
1100#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
1101 size = 64 * 1024;
1102 if (g_cpu_support_x86_avx)
1103 size = 128 * 1024;
1104 if (g_cpu_support_x86_avx2)
1105 size = 256 * 1024;
1106 if (g_cpu_support_x86_avx512)
1107 size = 1024 * 1024;
1108#elif __aarch64__
1109 size = 256 * 1024;
1110#elif __arm__
1111 size = 128 * 1024;
1112#else
1113 // is 64k still too large here ?
1114 size = 64 * 1024;
1115#endif
1116 }
1117
1118 return size;
1119}
1120
1121static int get_cpu_level3_cachesize()
1122{
1123 int size = 0;
1124#if defined _WIN32
1125 typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1126 LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
1127 if (glpi != NULL)
1128 {
1129 DWORD return_length = 0;
1130 glpi(NULL, &return_length);
1131
1132 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
1133 glpi(buffer, &return_length);
1134
1135 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
1136 DWORD byte_offset = 0;
1137 while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
1138 {
1139 if (ptr->Relationship == RelationCache)
1140 {
1141 PCACHE_DESCRIPTOR Cache = &ptr->Cache;
1142 if (Cache->Level == 3)
1143 {
1144 size = std::max(size, (int)Cache->Size);
1145 }
1146 }
1147
1148 byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1149 ptr++;
1150 }
1151
1152 free(buffer);
1153 }
1154#elif defined __ANDROID__ || defined __linux__
1155 size = get_big_cpu_data_cache_size(3);
1156#if defined(_SC_LEVEL3_CACHE_SIZE)
1157 if (size <= 0)
1158 size = sysconf(_SC_LEVEL3_CACHE_SIZE);
1159#endif
1160#elif __APPLE__
1161 // perflevel 0 is the higher performance cluster
1162 // get the size shared among all cpus
1163 size = get_hw_capability("hw.perflevel0.l3cachesize");
1164#endif
1165
1166 // l3 cache size can be zero
1167
1168 return size;
1169}
1170
1171#if defined _WIN32
1172static ncnn::CpuSet get_smt_cpu_mask()
1173{
1174 ncnn::CpuSet smt_cpu_mask;
1175
1176 typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1177 LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
1178 if (glpi == NULL)
1179 {
1180 NCNN_LOGE("GetLogicalProcessorInformation is not supported");
1181 return smt_cpu_mask;
1182 }
1183
1184 DWORD return_length = 0;
1185 glpi(NULL, &return_length);
1186
1187 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
1188 glpi(buffer, &return_length);
1189
1190 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
1191 DWORD byte_offset = 0;
1192 while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
1193 {
1194 if (ptr->Relationship == RelationProcessorCore)
1195 {
1196 ncnn::CpuSet smt_set;
1197 smt_set.mask = ptr->ProcessorMask;
1198 if (smt_set.num_enabled() > 1)
1199 {
1200 // this core is smt
1201 smt_cpu_mask.mask |= smt_set.mask;
1202 }
1203 }
1204
1205 byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1206 ptr++;
1207 }
1208
1209 free(buffer);
1210
1211 return smt_cpu_mask;
1212}
1213
1214static std::vector<int> get_max_freq_mhz()
1215{
1216 typedef struct _PROCESSOR_POWER_INFORMATION
1217 {
1218 ULONG Number;
1219 ULONG MaxMhz;
1220 ULONG CurrentMhz;
1221 ULONG MhzLimit;
1222 ULONG MaxIdleState;
1223 ULONG CurrentIdleState;
1224 } PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
1225
1226 HMODULE powrprof = LoadLibrary(TEXT("powrprof.dll"));
1227
1228 typedef LONG(WINAPI * LPFN_CNPI)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
1229 LPFN_CNPI cnpi = (LPFN_CNPI)GetProcAddress(powrprof, "CallNtPowerInformation");
1230 if (cnpi == NULL)
1231 {
1232 NCNN_LOGE("CallNtPowerInformation is not supported");
1233 FreeLibrary(powrprof);
1234 return std::vector<int>(g_cpucount, 0);
1235 }
1236
1237 DWORD return_length = sizeof(PROCESSOR_POWER_INFORMATION) * g_cpucount;
1238 PPROCESSOR_POWER_INFORMATION buffer = (PPROCESSOR_POWER_INFORMATION)malloc(return_length);
1239
1240 cnpi(ProcessorInformation, NULL, 0, buffer, return_length);
1241
1242 std::vector<int> ret;
1243 for (int i = 0; i < g_cpucount; i++)
1244 {
1245 ULONG max_mhz = buffer[i].MaxMhz;
1246 ret.push_back(max_mhz);
1247 }
1248
1249 free(buffer);
1250 FreeLibrary(powrprof);
1251 return ret;
1252}
1253
1254static int set_sched_affinity(const ncnn::CpuSet& thread_affinity_mask)
1255{
1256 DWORD_PTR prev_mask = SetThreadAffinityMask(GetCurrentThread(), thread_affinity_mask.mask);
1257 if (prev_mask == 0)
1258 {
1259 NCNN_LOGE("SetThreadAffinityMask failed %d", GetLastError());
1260 return -1;
1261 }
1262
1263 return 0;
1264}
1265#endif // defined _WIN32
1266
1267#if defined __ANDROID__ || defined __linux__
1268static int get_max_freq_khz(int cpuid)
1269{
1270 // first try, for all possible cpu
1271 char path[256];
1272 sprintf(path, "/sys/devices/system/cpu/cpufreq/stats/cpu%d/time_in_state", cpuid);
1273
1274 FILE* fp = fopen(path, "rb");
1275
1276 if (!fp)
1277 {
1278 // second try, for online cpu
1279 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpuid);
1280 fp = fopen(path, "rb");
1281
1282 if (fp)
1283 {
1284 int max_freq_khz = 0;
1285 while (!feof(fp))
1286 {
1287 int freq_khz = 0;
1288 int nscan = fscanf(fp, "%d %*d", &freq_khz);
1289 if (nscan != 1)
1290 break;
1291
1292 if (freq_khz > max_freq_khz)
1293 max_freq_khz = freq_khz;
1294 }
1295
1296 fclose(fp);
1297
1298 if (max_freq_khz != 0)
1299 return max_freq_khz;
1300
1301 fp = NULL;
1302 }
1303
1304 if (!fp)
1305 {
1306 // third try, for online cpu
1307 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpuid);
1308 fp = fopen(path, "rb");
1309
1310 if (!fp)
1311 return -1;
1312
1313 int max_freq_khz = -1;
1314 int nscan = fscanf(fp, "%d", &max_freq_khz);
1315 if (nscan != 1)
1316 {
1317 NCNN_LOGE("fscanf cpuinfo_max_freq error %d", nscan);
1318 }
1319 fclose(fp);
1320
1321 return max_freq_khz;
1322 }
1323 }
1324
1325 int max_freq_khz = 0;
1326 while (!feof(fp))
1327 {
1328 int freq_khz = 0;
1329 int nscan = fscanf(fp, "%d %*d", &freq_khz);
1330 if (nscan != 1)
1331 break;
1332
1333 if (freq_khz > max_freq_khz)
1334 max_freq_khz = freq_khz;
1335 }
1336
1337 fclose(fp);
1338
1339 return max_freq_khz;
1340}
1341
1342static bool is_smt_cpu(int cpuid)
1343{
1344 // https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/stable/sysfs-devices-system-cpu#L68-72
1345 char path[256];
1346 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_cpus_list", cpuid);
1347
1348 FILE* fp = fopen(path, "rb");
1349
1350 if (!fp)
1351 {
1352 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpuid);
1353 fp = fopen(path, "rb");
1354
1355 if (!fp)
1356 return false;
1357 }
1358
1359 bool is_smt = false;
1360 while (!feof(fp))
1361 {
1362 char ch = fgetc(fp);
1363 if (ch == ',' || ch == '-')
1364 {
1365 is_smt = true;
1366 break;
1367 }
1368 }
1369
1370 fclose(fp);
1371
1372 return is_smt;
1373}
1374
1375static int set_sched_affinity(const ncnn::CpuSet& thread_affinity_mask)
1376{
1377 // set affinity for thread
1378#if defined(__BIONIC__) && !defined(__OHOS__)
1379 pid_t pid = gettid();
1380#else
1381 pid_t pid = syscall(SYS_gettid);
1382#endif
1383
1384 int syscallret = syscall(__NR_sched_setaffinity, pid, sizeof(cpu_set_t), &thread_affinity_mask.cpu_set);
1385 if (syscallret)
1386 {
1387 NCNN_LOGE("syscall error %d", syscallret);
1388 return -1;
1389 }
1390
1391 return 0;
1392}
1393#endif // defined __ANDROID__ || defined __linux__
1394
1395#if __APPLE__
1396static int set_sched_affinity(const ncnn::CpuSet& thread_affinity_mask)
1397{
1398 // https://developer.apple.com/library/archive/releasenotes/Performance/RN-AffinityAPI/index.html
1399 // http://www.hybridkernel.com/2015/01/18/binding_threads_to_cores_osx.html
1400 // https://gist.github.com/Coneko/4234842
1401
1402 // This is a quite outdated document. Apple will not allow developers to set CPU affinity.
1403 // In OS X 10.5 it worked, later it became a suggestion to OS X, then in 10.10 or so (as well in later ones), macOS will ignore any affinity settings.
1404 // see https://github.com/Tencent/ncnn/pull/2335#discussion_r528233919 --- AmeAkio
1405
1406 int affinity_tag = THREAD_AFFINITY_TAG_NULL;
1407 for (int i = 0; i < (int)sizeof(thread_affinity_mask.policy) * 8; i++)
1408 {
1409 if (thread_affinity_mask.is_enabled(i))
1410 {
1411 affinity_tag = i + 1;
1412 break;
1413 }
1414 }
1415
1416 mach_port_t tid = pthread_mach_thread_np(pthread_self());
1417
1418 thread_affinity_policy_data_t policy_data;
1419 policy_data.affinity_tag = affinity_tag;
1420 int ret = thread_policy_set(tid, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy_data, THREAD_AFFINITY_POLICY_COUNT);
1421 if (ret && ret != KERN_NOT_SUPPORTED)
1422 {
1423 NCNN_LOGE("thread_policy_set error %d", ret);
1424 return -1;
1425 }
1426
1427 return 0;
1428}
1429#endif // __APPLE__
1430
1431static void initialize_cpu_thread_affinity_mask(ncnn::CpuSet& mask_all, ncnn::CpuSet& mask_little, ncnn::CpuSet& mask_big)
1432{
1433 mask_all.disable_all();
1434 for (int i = 0; i < g_cpucount; i++)
1435 {
1436 mask_all.enable(i);
1437 }
1438
1439#if defined _WIN32
1440 // get max freq mhz for all cores
1441 int max_freq_mhz_min = INT_MAX;
1442 int max_freq_mhz_max = 0;
1443 std::vector<int> cpu_max_freq_mhz = get_max_freq_mhz();
1444 for (int i = 0; i < g_cpucount; i++)
1445 {
1446 int max_freq_mhz = cpu_max_freq_mhz[i];
1447
1448 // NCNN_LOGE("%d max freq = %d khz", i, max_freq_mhz);
1449
1450 if (max_freq_mhz > max_freq_mhz_max)
1451 max_freq_mhz_max = max_freq_mhz;
1452 if (max_freq_mhz < max_freq_mhz_min)
1453 max_freq_mhz_min = max_freq_mhz;
1454 }
1455
1456 int max_freq_mhz_medium = (max_freq_mhz_min + max_freq_mhz_max) / 2;
1457 if (max_freq_mhz_medium == max_freq_mhz_max)
1458 {
1459 mask_little.disable_all();
1460 mask_big = mask_all;
1461 return;
1462 }
1463
1464 ncnn::CpuSet smt_cpu_mask = get_smt_cpu_mask();
1465
1466 for (int i = 0; i < g_cpucount; i++)
1467 {
1468 if (smt_cpu_mask.is_enabled(i))
1469 {
1470 // always treat smt core as big core
1471 mask_big.enable(i);
1472 continue;
1473 }
1474
1475 if (cpu_max_freq_mhz[i] < max_freq_mhz_medium)
1476 mask_little.enable(i);
1477 else
1478 mask_big.enable(i);
1479 }
1480#elif defined __ANDROID__ || defined __linux__
1481 int max_freq_khz_min = INT_MAX;
1482 int max_freq_khz_max = 0;
1483 std::vector<int> cpu_max_freq_khz(g_cpucount);
1484 for (int i = 0; i < g_cpucount; i++)
1485 {
1486 int max_freq_khz = get_max_freq_khz(i);
1487
1488 // NCNN_LOGE("%d max freq = %d khz", i, max_freq_khz);
1489
1490 cpu_max_freq_khz[i] = max_freq_khz;
1491
1492 if (max_freq_khz > max_freq_khz_max)
1493 max_freq_khz_max = max_freq_khz;
1494 if (max_freq_khz < max_freq_khz_min)
1495 max_freq_khz_min = max_freq_khz;
1496 }
1497
1498 int max_freq_khz_medium = (max_freq_khz_min + max_freq_khz_max) / 2;
1499 if (max_freq_khz_medium == max_freq_khz_max)
1500 {
1501 mask_little.disable_all();
1502 mask_big = mask_all;
1503 return;
1504 }
1505
1506 for (int i = 0; i < g_cpucount; i++)
1507 {
1508 if (is_smt_cpu(i))
1509 {
1510 // always treat smt core as big core
1511 mask_big.enable(i);
1512 continue;
1513 }
1514
1515 if (cpu_max_freq_khz[i] < max_freq_khz_medium)
1516 mask_little.enable(i);
1517 else
1518 mask_big.enable(i);
1519 }
1520#elif __APPLE__
1521 int nperflevels = get_hw_capability("hw.nperflevels");
1522 if (nperflevels == 1)
1523 {
1524 // smp models
1525 mask_little.disable_all();
1526 mask_big = mask_all;
1527 }
1528 else
1529 {
1530 // two or more clusters, level0 is the high-performance cluster
1531 int perflevel0_logicalcpu = get_hw_capability("hw.perflevel0.logicalcpu_max");
1532 for (int i = 0; i < perflevel0_logicalcpu; i++)
1533 {
1534 mask_big.enable(i);
1535 }
1536 for (int i = perflevel0_logicalcpu; i < g_cpucount; i++)
1537 {
1538 mask_little.enable(i);
1539 }
1540 }
1541#else
1542 // TODO implement me for other platforms
1543 mask_little.disable_all();
1544 mask_big = mask_all;
1545#endif
1546}
1547
1548#if defined __ANDROID__ || defined __linux__
1549#if __aarch64__
1550union midr_info_t
1551{
1552 struct __attribute__((packed))
1553 {
1554 unsigned int revision : 4;
1555 unsigned int part : 12;
1556 unsigned int architecture : 4;
1557 unsigned int variant : 4;
1558 unsigned int implementer : 8;
1559 };
1560 unsigned int midr;
1561
1562 midr_info_t(unsigned int _midr)
1563 : midr(_midr)
1564 {
1565 }
1566};
1567
1568static unsigned int get_midr_from_sysfs(int cpuid)
1569{
1570 char path[256];
1571 sprintf(path, "/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1", cpuid);
1572
1573 FILE* fp = fopen(path, "rb");
1574 if (!fp)
1575 return 0;
1576
1577 unsigned int midr_el1 = 0;
1578 int nscan = fscanf(fp, "%x", &midr_el1);
1579 if (nscan != 1)
1580 {
1581 // ignore
1582 }
1583
1584 fclose(fp);
1585
1586 return midr_el1;
1587}
1588
1589static int get_midr_from_proc_cpuinfo(std::vector<unsigned int>& midrs)
1590{
1591 FILE* fp = fopen("/proc/cpuinfo", "rb");
1592 if (!fp)
1593 return -1;
1594
1595 midrs.resize(g_cpucount, 0);
1596
1597 int cpuid = -1;
1598 midr_info_t midr_info(0);
1599
1600 char line[1024];
1601 while (!feof(fp))
1602 {
1603 char* s = fgets(line, 1024, fp);
1604 if (!s)
1605 break;
1606
1607 if (memcmp(line, "processor", 9) == 0)
1608 {
1609 // processor : 4
1610 int id = -1;
1611 int nscan = sscanf(line, "%*[^:]: %d", &id);
1612 if (nscan != 1)
1613 continue;
1614
1615 if (cpuid >= 0 && cpuid < g_cpucount)
1616 {
1617 if (midr_info.midr == 0)
1618 {
1619 // shared midr
1620 midrs[cpuid] = (unsigned int)-1;
1621 }
1622 else
1623 {
1624 // save midr and reset
1625 midrs[cpuid] = midr_info.midr;
1626 for (int i = 0; i < g_cpucount; i++)
1627 {
1628 if (midrs[i] == (unsigned int)-1)
1629 midrs[i] = midr_info.midr;
1630 }
1631 }
1632
1633 midr_info.midr = 0;
1634 }
1635
1636 cpuid = id;
1637 }
1638
1639 if (cpuid == -1)
1640 continue;
1641
1642 if (memcmp(line, "CPU implementer", 15) == 0)
1643 {
1644 // CPU implementer : 0x51
1645 unsigned int id = 0;
1646 int nscan = sscanf(line, "%*[^:]: %x", &id);
1647 if (nscan != 1)
1648 continue;
1649
1650 midr_info.implementer = id;
1651 }
1652 else if (memcmp(line, "CPU architecture", 16) == 0)
1653 {
1654 // CPU architecture: 8
1655 int id = 0;
1656 int nscan = sscanf(line, "%*[^:]: %d", &id);
1657 if (nscan != 1)
1658 continue;
1659
1660 midr_info.architecture = id;
1661 }
1662 else if (memcmp(line, "CPU variant", 11) == 0)
1663 {
1664 // CPU variant : 0xd
1665 int id = 0;
1666 int nscan = sscanf(line, "%*[^:]: %x", &id);
1667 if (nscan != 1)
1668 continue;
1669
1670 midr_info.variant = id;
1671 }
1672 else if (memcmp(line, "CPU part", 8) == 0)
1673 {
1674 // CPU part : 0x804
1675 int id = 0;
1676 int nscan = sscanf(line, "%*[^:]: %x", &id);
1677 if (nscan != 1)
1678 continue;
1679
1680 midr_info.part = id;
1681 }
1682 else if (memcmp(line, "CPU revision", 12) == 0)
1683 {
1684 // CPU revision : 14
1685 int id = 0;
1686 int nscan = sscanf(line, "%*[^:]: %d", &id);
1687 if (nscan != 1)
1688 continue;
1689
1690 midr_info.revision = id;
1691 }
1692 }
1693
1694 fclose(fp);
1695
1696 if (cpuid >= 0 && cpuid < g_cpucount)
1697 {
1698 if (midr_info.midr == 0)
1699 {
1700 // shared midr
1701 midrs[cpuid] = (unsigned int)-1;
1702 }
1703 else
1704 {
1705 // save midr and reset
1706 midrs[cpuid] = midr_info.midr;
1707 for (int i = 0; i < g_cpucount; i++)
1708 {
1709 if (midrs[i] == (unsigned int)-1)
1710 midrs[i] = midr_info.midr;
1711 }
1712 }
1713
1714 midr_info.midr = 0;
1715 }
1716
1717 // /proc/cpuinfo may only report little/online cores on old kernel
1718 if (g_cpu_affinity_mask_big.num_enabled() == g_cpucount)
1719 {
1720 // assign the remaining unknown midrs for smp cpu
1721 for (int i = 0; i < g_cpucount; i++)
1722 {
1723 if (midrs[i] == 0)
1724 midrs[i] = midr_info.midr;
1725 }
1726 }
1727 else
1728 {
1729 // clear the big core midrs for hmp cpu if they are the same as little cores
1730 unsigned int little_midr = 0;
1731 for (int i = 0; i < g_cpucount; i++)
1732 {
1733 if (g_cpu_affinity_mask_little.is_enabled(i))
1734 {
1735 little_midr = midrs[i];
1736 break;
1737 }
1738 }
1739
1740 for (int i = 0; i < g_cpucount; i++)
1741 {
1742 if (g_cpu_affinity_mask_big.is_enabled(i))
1743 {
1744 if (midrs[i] == little_midr)
1745 {
1746 midrs[i] = 0;
1747 }
1748 }
1749 }
1750 }
1751
1752 return 0;
1753}
1754
1755// return midr for the current running core
1756static unsigned int get_midr_from_register()
1757{
1758 uint64_t midr;
1759 asm volatile("mrs %0, MIDR_EL1"
1760 : "=r"(midr));
1761
1762 return (unsigned int)midr;
1763}
1764
1765static int get_sched_affinity(ncnn::CpuSet& thread_affinity_mask)
1766{
1767 // get affinity for thread
1768#if defined(__BIONIC__) && !defined(__OHOS__)
1769 pid_t pid = gettid();
1770#else
1771 pid_t pid = syscall(SYS_gettid);
1772#endif
1773
1774 thread_affinity_mask.disable_all();
1775
1776 int syscallret = syscall(__NR_sched_getaffinity, pid, sizeof(cpu_set_t), &thread_affinity_mask.cpu_set);
1777 if (syscallret)
1778 {
1779 // handle get error silently
1780 return -1;
1781 }
1782
1783 return 0;
1784}
1785
1786static int midr_is_a53_a55(unsigned int midr)
1787{
1788 // 0x 41 ? f d03 ? = arm cortex-a53
1789 // 0x 51 ? f 801 ? = qcom kryo200 a53
1790 // 0x 41 ? f d04 ? = arm cortex-a35
1791 // 0x 41 ? f d05 ? = arm cortex-a55
1792 // 0x 51 ? f 803 ? = qcom kryo300 a55
1793 // 0x 51 ? f 805 ? = qcom kryo400 a55
1794
1795 midr_info_t midr_info(midr);
1796
1797 return (midr_info.implementer == 0x41 && midr_info.part == 0xd03)
1798 || (midr_info.implementer == 0x51 && midr_info.part == 0x801)
1799 || (midr_info.implementer == 0x41 && midr_info.part == 0xd04)
1800 || (midr_info.implementer == 0x41 && midr_info.part == 0xd05)
1801 || (midr_info.implementer == 0x51 && midr_info.part == 0x803)
1802 || (midr_info.implementer == 0x51 && midr_info.part == 0x805);
1803}
1804
1805static int detect_cpu_is_arm_a53_a55()
1806{
1807 int a53_a55_cpu_count = 0;
1808
1809 // first try, iterate /sys/devices/system/cpu/cpuX/regs/identification/midr_el1
1810 bool sysfs_midr = true;
1811 for (int i = 0; i < g_cpucount; i++)
1812 {
1813 unsigned int midr = 0;
1814
1815 // for kernel 4.7+
1816 midr = get_midr_from_sysfs(i);
1817 if (midr == 0)
1818 {
1819 sysfs_midr = false;
1820 break;
1821 }
1822
1823 if (midr_is_a53_a55(midr))
1824 {
1825 a53_a55_cpu_count++;
1826 }
1827 }
1828
1829 if (!sysfs_midr)
1830 {
1831 // second try, collect midr from /proc/cpuinfo
1832 std::vector<unsigned int> midrs;
1833 int ret = get_midr_from_proc_cpuinfo(midrs);
1834 if (ret == 0 && (int)midrs.size() == g_cpucount)
1835 {
1836 for (int i = 0; i < g_cpucount; i++)
1837 {
1838 if (midr_is_a53_a55(midrs[i]))
1839 {
1840 a53_a55_cpu_count++;
1841 }
1842 }
1843 }
1844 else
1845 {
1846 // third try, assume all aarch64 little cores are a53/a55
1847 a53_a55_cpu_count = g_cpu_affinity_mask_little.num_enabled();
1848 }
1849 }
1850
1851 if (a53_a55_cpu_count == 0)
1852 return 0; // all non a53/a55
1853
1854 if (a53_a55_cpu_count == g_cpucount)
1855 return 1; // all a53/a55
1856
1857 // little cores are a53/a55
1858 return 2;
1859}
1860#endif // __aarch64__
1861#endif // defined __ANDROID__ || defined __linux__
1862
1863// the initialization
1864static void initialize_global_cpu_info()
1865{
1866 g_cpucount = get_cpucount();
1867 g_physical_cpucount = get_physical_cpucount();
1868 g_powersave = 0;
1869 initialize_cpu_thread_affinity_mask(g_cpu_affinity_mask_all, g_cpu_affinity_mask_little, g_cpu_affinity_mask_big);
1870
1871#if (defined _WIN32 && (__aarch64__ || __arm__))
1872 if (!is_being_debugged())
1873 {
1874 ruapu_init();
1875 }
1876#endif
1877
1878#if defined _WIN32
1879#if __aarch64__
1880 g_cpu_support_arm_cpuid = ruapu_supports("cpuid");
1881 g_cpu_support_arm_asimdhp = ruapu_supports("asimdhp") || IsProcessorFeaturePresent(43); // dp implies hp
1882 g_cpu_support_arm_asimddp = ruapu_supports("asimddp") || IsProcessorFeaturePresent(43); // 43 is PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
1883 g_cpu_support_arm_asimdfhm = ruapu_supports("asimdfhm");
1884 g_cpu_support_arm_bf16 = ruapu_supports("bf16");
1885 g_cpu_support_arm_i8mm = ruapu_supports("i8mm");
1886 g_cpu_support_arm_sve = ruapu_supports("sve");
1887 g_cpu_support_arm_sve2 = ruapu_supports("sve2");
1888 g_cpu_support_arm_svebf16 = ruapu_supports("svebf16");
1889 g_cpu_support_arm_svei8mm = ruapu_supports("svei8mm");
1890 g_cpu_support_arm_svef32mm = ruapu_supports("svef32mm");
1891#elif __arm__
1892 g_cpu_support_arm_edsp = ruapu_supports("edsp");
1893 g_cpu_support_arm_neon = 1; // all modern windows arm devices have neon
1894 g_cpu_support_arm_vfpv4 = ruapu_supports("vfpv4");
1895#endif // __aarch64__ || __arm__
1896#elif defined __ANDROID__ || defined __linux__
1897 g_hwcaps = get_elf_hwcap(AT_HWCAP);
1898 g_hwcaps2 = get_elf_hwcap(AT_HWCAP2);
1899#elif __APPLE__
1900 g_hw_cpufamily = get_hw_cpufamily();
1901 g_hw_cputype = get_hw_cputype();
1902 g_hw_cpusubtype = get_hw_cpusubtype();
1903#if __aarch64__
1904 g_hw_optional_arm_FEAT_FP16 = get_hw_capability("hw.optional.arm.FEAT_FP16");
1905 g_hw_optional_arm_FEAT_DotProd = get_hw_capability("hw.optional.arm.FEAT_DotProd");
1906 g_hw_optional_arm_FEAT_FHM = get_hw_capability("hw.optional.arm.FEAT_FHM");
1907 g_hw_optional_arm_FEAT_BF16 = get_hw_capability("hw.optional.arm.FEAT_BF16");
1908 g_hw_optional_arm_FEAT_I8MM = get_hw_capability("hw.optional.arm.FEAT_I8MM");
1909#endif // __aarch64__
1910#endif
1911
1912#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
1913 g_cpu_support_x86_avx = get_cpu_support_x86_avx();
1914 g_cpu_support_x86_fma = get_cpu_support_x86_fma();
1915 g_cpu_support_x86_xop = get_cpu_support_x86_xop();
1916 g_cpu_support_x86_f16c = get_cpu_support_x86_f16c();
1917 g_cpu_support_x86_avx2 = get_cpu_support_x86_avx2();
1918 g_cpu_support_x86_avx_vnni = get_cpu_support_x86_avx_vnni();
1919 g_cpu_support_x86_avx512 = get_cpu_support_x86_avx512();
1920 g_cpu_support_x86_avx512_vnni = get_cpu_support_x86_avx512_vnni();
1921 g_cpu_support_x86_avx512_bf16 = get_cpu_support_x86_avx512_bf16();
1922 g_cpu_support_x86_avx512_fp16 = get_cpu_support_x86_avx512_fp16();
1923#endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
1924
1925 g_cpu_level2_cachesize = get_cpu_level2_cachesize();
1926 g_cpu_level3_cachesize = get_cpu_level3_cachesize();
1927
1928#if defined __ANDROID__ || defined __linux__
1929#if __aarch64__
1930 g_cpu_is_arm_a53_a55 = detect_cpu_is_arm_a53_a55();
1931#endif // __aarch64__
1932#endif // defined __ANDROID__ || defined __linux__
1933}
1934
1935static int g_cpu_info_initialized = 0;
1936
1937static inline void try_initialize_global_cpu_info()
1938{
1939 if (!g_cpu_info_initialized)
1940 {
1941 initialize_global_cpu_info();
1942 g_cpu_info_initialized = 1;
1943 }
1944}
1945
1946namespace ncnn {
1947
1948#if defined _WIN32
1949CpuSet::CpuSet()
1950{
1951 disable_all();
1952}
1953
1954void CpuSet::enable(int cpu)
1955{
1956 mask |= ((ULONG_PTR)1 << cpu);
1957}
1958
1959void CpuSet::disable(int cpu)
1960{
1961 mask &= ~((ULONG_PTR)1 << cpu);
1962}
1963
1964void CpuSet::disable_all()
1965{
1966 mask = 0;
1967}
1968
1969bool CpuSet::is_enabled(int cpu) const
1970{
1971 return mask & ((ULONG_PTR)1 << cpu);
1972}
1973
1974int CpuSet::num_enabled() const
1975{
1976 int num_enabled = 0;
1977 for (int i = 0; i < (int)sizeof(mask) * 8; i++)
1978 {
1979 if (is_enabled(i))
1980 num_enabled++;
1981 }
1982
1983 return num_enabled;
1984}
1985#elif defined __ANDROID__ || defined __linux__
1986CpuSet::CpuSet()
1987{
1988 disable_all();
1989}
1990
1991void CpuSet::enable(int cpu)
1992{
1993 CPU_SET(cpu, &cpu_set);
1994}
1995
1996void CpuSet::disable(int cpu)
1997{
1998 CPU_CLR(cpu, &cpu_set);
1999}
2000
2001void CpuSet::disable_all()
2002{
2003 CPU_ZERO(&cpu_set);
2004}
2005
2006bool CpuSet::is_enabled(int cpu) const
2007{
2008 return CPU_ISSET(cpu, &cpu_set);
2009}
2010
2011int CpuSet::num_enabled() const
2012{
2013 int num_enabled = 0;
2014 for (int i = 0; i < (int)sizeof(cpu_set_t) * 8; i++)
2015 {
2016 if (is_enabled(i))
2017 num_enabled++;
2018 }
2019
2020 return num_enabled;
2021}
2022#elif __APPLE__
2023CpuSet::CpuSet()
2024{
2025 disable_all();
2026}
2027
2028void CpuSet::enable(int cpu)
2029{
2030 policy |= ((unsigned int)1 << cpu);
2031}
2032
2033void CpuSet::disable(int cpu)
2034{
2035 policy &= ~((unsigned int)1 << cpu);
2036}
2037
2038void CpuSet::disable_all()
2039{
2040 policy = 0;
2041}
2042
2043bool CpuSet::is_enabled(int cpu) const
2044{
2045 return policy & ((unsigned int)1 << cpu);
2046}
2047
2048int CpuSet::num_enabled() const
2049{
2050 int num_enabled = 0;
2051 for (int i = 0; i < (int)sizeof(policy) * 8; i++)
2052 {
2053 if (is_enabled(i))
2054 num_enabled++;
2055 }
2056
2057 return num_enabled;
2058}
2059#else
2060CpuSet::CpuSet()
2061{
2062}
2063
2064void CpuSet::enable(int /* cpu */)
2065{
2066}
2067
2068void CpuSet::disable(int /* cpu */)
2069{
2070}
2071
2072void CpuSet::disable_all()
2073{
2074}
2075
2076bool CpuSet::is_enabled(int /* cpu */) const
2077{
2078 return true;
2079}
2080
2081int CpuSet::num_enabled() const
2082{
2083 return get_cpu_count();
2084}
2085#endif
2086
2087int cpu_support_arm_edsp()
2088{
2089 try_initialize_global_cpu_info();
2090#if __arm__ && !__aarch64__
2091#if defined _WIN32
2092 return g_cpu_support_arm_edsp;
2093#elif defined __ANDROID__ || defined __linux__
2094 return g_hwcaps & HWCAP_EDSP;
2095#elif __APPLE__
2096 return g_hw_cputype == CPU_TYPE_ARM;
2097#else
2098 return 0;
2099#endif
2100#else
2101 return 0;
2102#endif
2103}
2104
2105int cpu_support_arm_neon()
2106{
2107 try_initialize_global_cpu_info();
2108#if __aarch64__
2109 return 1;
2110#elif __arm__
2111#if defined _WIN32
2112 return g_cpu_support_arm_neon;
2113#elif defined __ANDROID__ || defined __linux__
2114 return g_hwcaps & HWCAP_NEON;
2115#elif __APPLE__
2116 return g_hw_cputype == CPU_TYPE_ARM && g_hw_cpusubtype > CPU_SUBTYPE_ARM_V7;
2117#else
2118 return 0;
2119#endif
2120#else
2121 return 0;
2122#endif
2123}
2124
2125int cpu_support_arm_vfpv4()
2126{
2127 try_initialize_global_cpu_info();
2128#if __aarch64__
2129 return 1;
2130#elif __arm__
2131#if defined _WIN32
2132 return g_cpu_support_arm_vfpv4;
2133#elif defined __ANDROID__ || defined __linux__
2134 return g_hwcaps & HWCAP_VFPv4;
2135#elif __APPLE__
2136 return g_hw_cputype == CPU_TYPE_ARM && g_hw_cpusubtype > CPU_SUBTYPE_ARM_V7S;
2137#else
2138 return 0;
2139#endif
2140#else
2141 return 0;
2142#endif
2143}
2144
2145int cpu_support_arm_asimdhp()
2146{
2147 try_initialize_global_cpu_info();
2148#if __aarch64__
2149#if defined _WIN32
2150 return g_cpu_support_arm_asimdhp;
2151#elif defined __ANDROID__ || defined __linux__
2152 return g_hwcaps & HWCAP_ASIMDHP;
2153#elif __APPLE__
2154 return g_hw_optional_arm_FEAT_FP16
2155 || g_hw_cpufamily == CPUFAMILY_ARM_MONSOON_MISTRAL
2156 || g_hw_cpufamily == CPUFAMILY_ARM_VORTEX_TEMPEST
2157 || g_hw_cpufamily == CPUFAMILY_ARM_LIGHTNING_THUNDER
2158 || g_hw_cpufamily == CPUFAMILY_ARM_FIRESTORM_ICESTORM
2159 || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
2160 || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
2161 || g_hw_cpufamily == CPUFAMILY_ARM_COLL
2162 || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
2163 || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
2164 || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
2165#else
2166 return 0;
2167#endif
2168#else
2169 return 0;
2170#endif
2171}
2172
2173int cpu_support_arm_cpuid()
2174{
2175 try_initialize_global_cpu_info();
2176#if __aarch64__
2177#if defined _WIN32
2178 return g_cpu_support_arm_cpuid;
2179#elif defined __ANDROID__ || defined __linux__
2180 return g_hwcaps & HWCAP_CPUID;
2181#elif __APPLE__
2182 return 0;
2183#else
2184 return 0;
2185#endif
2186#else
2187 return 0;
2188#endif
2189}
2190
2191int cpu_support_arm_asimddp()
2192{
2193 try_initialize_global_cpu_info();
2194#if __aarch64__
2195#if defined _WIN32
2196 return g_cpu_support_arm_asimddp;
2197#elif defined __ANDROID__ || defined __linux__
2198 return g_hwcaps & HWCAP_ASIMDDP;
2199#elif __APPLE__
2200 return g_hw_optional_arm_FEAT_DotProd
2201 || g_hw_cpufamily == CPUFAMILY_ARM_LIGHTNING_THUNDER
2202 || g_hw_cpufamily == CPUFAMILY_ARM_FIRESTORM_ICESTORM
2203 || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
2204 || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
2205 || g_hw_cpufamily == CPUFAMILY_ARM_COLL
2206 || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
2207 || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
2208 || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
2209#else
2210 return 0;
2211#endif
2212#else
2213 return 0;
2214#endif
2215}
2216
2217int cpu_support_arm_asimdfhm()
2218{
2219 try_initialize_global_cpu_info();
2220#if __aarch64__
2221#if defined _WIN32
2222 return g_cpu_support_arm_asimdfhm;
2223#elif defined __ANDROID__ || defined __linux__
2224 return g_hwcaps & HWCAP_ASIMDFHM;
2225#elif __APPLE__
2226 return g_hw_optional_arm_FEAT_FHM
2227 || g_hw_cpufamily == CPUFAMILY_ARM_LIGHTNING_THUNDER
2228 || g_hw_cpufamily == CPUFAMILY_ARM_FIRESTORM_ICESTORM
2229 || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
2230 || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
2231 || g_hw_cpufamily == CPUFAMILY_ARM_COLL
2232 || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
2233 || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
2234 || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
2235#else
2236 return 0;
2237#endif
2238#else
2239 return 0;
2240#endif
2241}
2242
2243int cpu_support_arm_bf16()
2244{
2245 try_initialize_global_cpu_info();
2246#if __aarch64__
2247#if defined _WIN32
2248 return g_cpu_support_arm_bf16;
2249#elif defined __ANDROID__ || defined __linux__
2250 return g_hwcaps2 & HWCAP2_BF16;
2251#elif __APPLE__
2252 return g_hw_optional_arm_FEAT_BF16
2253 || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
2254 || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
2255 || g_hw_cpufamily == CPUFAMILY_ARM_COLL
2256 || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
2257 || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
2258 || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
2259#else
2260 return 0;
2261#endif
2262#else
2263 return 0;
2264#endif
2265}
2266
2267int cpu_support_arm_i8mm()
2268{
2269 try_initialize_global_cpu_info();
2270#if __aarch64__
2271#if defined _WIN32
2272 return g_cpu_support_arm_i8mm;
2273#elif defined __ANDROID__ || defined __linux__
2274 return g_hwcaps2 & HWCAP2_I8MM;
2275#elif __APPLE__
2276 return g_hw_optional_arm_FEAT_I8MM
2277 || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
2278 || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
2279 || g_hw_cpufamily == CPUFAMILY_ARM_COLL
2280 || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
2281 || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
2282 || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
2283#else
2284 return 0;
2285#endif
2286#else
2287 return 0;
2288#endif
2289}
2290
2291int cpu_support_arm_sve()
2292{
2293 try_initialize_global_cpu_info();
2294#if __aarch64__
2295#if defined _WIN32
2296 return g_cpu_support_arm_sve;
2297#elif defined __ANDROID__ || defined __linux__
2298 return g_hwcaps & HWCAP_SVE;
2299#elif __APPLE__
2300 return 0; // no known apple cpu support armv8.6 sve
2301#else
2302 return 0;
2303#endif
2304#else
2305 return 0;
2306#endif
2307}
2308
2309int cpu_support_arm_sve2()
2310{
2311 try_initialize_global_cpu_info();
2312#if __aarch64__
2313#if defined _WIN32
2314 return g_cpu_support_arm_sve2;
2315#elif defined __ANDROID__ || defined __linux__
2316 return g_hwcaps2 & HWCAP2_SVE2;
2317#elif __APPLE__
2318 return 0; // no known apple cpu support armv8.6 sve2
2319#else
2320 return 0;
2321#endif
2322#else
2323 return 0;
2324#endif
2325}
2326
2327int cpu_support_arm_svebf16()
2328{
2329 try_initialize_global_cpu_info();
2330#if __aarch64__
2331#if defined _WIN32
2332 return g_cpu_support_arm_svebf16;
2333#elif defined __ANDROID__ || defined __linux__
2334 return g_hwcaps2 & HWCAP2_SVEBF16;
2335#elif __APPLE__
2336 return 0; // no known apple cpu support armv8.6 svebf16
2337#else
2338 return 0;
2339#endif
2340#else
2341 return 0;
2342#endif
2343}
2344
2345int cpu_support_arm_svei8mm()
2346{
2347 try_initialize_global_cpu_info();
2348#if __aarch64__
2349#if defined _WIN32
2350 return g_cpu_support_arm_svei8mm;
2351#elif defined __ANDROID__ || defined __linux__
2352 return g_hwcaps2 & HWCAP2_SVEI8MM;
2353#elif __APPLE__
2354 return 0; // no known apple cpu support armv8.6 svei8mm
2355#else
2356 return 0;
2357#endif
2358#else
2359 return 0;
2360#endif
2361}
2362
2363int cpu_support_arm_svef32mm()
2364{
2365 try_initialize_global_cpu_info();
2366#if __aarch64__
2367#if defined _WIN32
2368 return g_cpu_support_arm_svef32mm;
2369#elif defined __ANDROID__ || defined __linux__
2370 return g_hwcaps2 & HWCAP2_SVEF32MM;
2371#elif __APPLE__
2372 return 0; // no known apple cpu support armv8.6 svef32mm
2373#else
2374 return 0;
2375#endif
2376#else
2377 return 0;
2378#endif
2379}
2380
2381int cpu_support_x86_avx()
2382{
2383 try_initialize_global_cpu_info();
2384#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2385 return g_cpu_support_x86_avx;
2386#else
2387 return 0;
2388#endif
2389}
2390
2391int cpu_support_x86_fma()
2392{
2393 try_initialize_global_cpu_info();
2394#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2395 return g_cpu_support_x86_fma;
2396#else
2397 return 0;
2398#endif
2399}
2400
2401int cpu_support_x86_xop()
2402{
2403 try_initialize_global_cpu_info();
2404#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2405 return g_cpu_support_x86_xop;
2406#else
2407 return 0;
2408#endif
2409}
2410
2411int cpu_support_x86_f16c()
2412{
2413 try_initialize_global_cpu_info();
2414#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2415 return g_cpu_support_x86_f16c;
2416#else
2417 return 0;
2418#endif
2419}
2420
2421int cpu_support_x86_avx2()
2422{
2423 try_initialize_global_cpu_info();
2424#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2425 return g_cpu_support_x86_avx2;
2426#else
2427 return 0;
2428#endif
2429}
2430
2431int cpu_support_x86_avx_vnni()
2432{
2433 try_initialize_global_cpu_info();
2434#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2435 return g_cpu_support_x86_avx_vnni;
2436#else
2437 return 0;
2438#endif
2439}
2440
2441int cpu_support_x86_avx512()
2442{
2443 try_initialize_global_cpu_info();
2444#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2445 return g_cpu_support_x86_avx512;
2446#else
2447 return 0;
2448#endif
2449}
2450
2451int cpu_support_x86_avx512_vnni()
2452{
2453 try_initialize_global_cpu_info();
2454#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2455 return g_cpu_support_x86_avx512_vnni;
2456#else
2457 return 0;
2458#endif
2459}
2460
2461int cpu_support_x86_avx512_bf16()
2462{
2463 try_initialize_global_cpu_info();
2464#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2465 return g_cpu_support_x86_avx512_bf16;
2466#else
2467 return 0;
2468#endif
2469}
2470
2471int cpu_support_x86_avx512_fp16()
2472{
2473 try_initialize_global_cpu_info();
2474#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
2475 return g_cpu_support_x86_avx512_fp16;
2476#else
2477 return 0;
2478#endif
2479}
2480
2481int cpu_support_mips_msa()
2482{
2483 try_initialize_global_cpu_info();
2484#if defined __ANDROID__ || defined __linux__
2485#if __mips__
2486 return g_hwcaps & HWCAP_MIPS_MSA;
2487#else
2488 return 0;
2489#endif
2490#else
2491 return 0;
2492#endif
2493}
2494
2495int cpu_support_loongarch_lsx()
2496{
2497 try_initialize_global_cpu_info();
2498#if defined __ANDROID__ || defined __linux__
2499#if __loongarch64
2500 return g_hwcaps & HWCAP_LOONGARCH_LSX;
2501#else
2502 return 0;
2503#endif
2504#else
2505 return 0;
2506#endif
2507}
2508
2509int cpu_support_loongarch_lasx()
2510{
2511 try_initialize_global_cpu_info();
2512#if defined __ANDROID__ || defined __linux__
2513#if __loongarch64
2514 return g_hwcaps & HWCAP_LOONGARCH_LASX;
2515#else
2516 return 0;
2517#endif
2518#else
2519 return 0;
2520#endif
2521}
2522
2523int cpu_support_loongson_mmi()
2524{
2525 try_initialize_global_cpu_info();
2526#if defined __ANDROID__ || defined __linux__
2527#if __mips__
2528 return g_hwcaps & HWCAP_LOONGSON_MMI;
2529#else
2530 return 0;
2531#endif
2532#else
2533 return 0;
2534#endif
2535}
2536
2537int cpu_support_riscv_v()
2538{
2539 try_initialize_global_cpu_info();
2540#if defined __ANDROID__ || defined __linux__
2541#if __riscv
2542 return g_hwcaps & COMPAT_HWCAP_ISA_V;
2543#else
2544 return 0;
2545#endif
2546#else
2547 return 0;
2548#endif
2549}
2550
2551int cpu_support_riscv_zfh()
2552{
2553 try_initialize_global_cpu_info();
2554#if defined __ANDROID__ || defined __linux__
2555#if __riscv
2556 // v + f does not imply zfh, but how to discover zfh properly ?
2557 // upstream issue https://github.com/riscv/riscv-isa-manual/issues/414
2558 return g_hwcaps & COMPAT_HWCAP_ISA_V && g_hwcaps & COMPAT_HWCAP_ISA_F;
2559#else
2560 return 0;
2561#endif
2562#else
2563 return 0;
2564#endif
2565}
2566
2567int cpu_riscv_vlenb()
2568{
2569 try_initialize_global_cpu_info();
2570#if __riscv
2571 if (!cpu_support_riscv_v())
2572 return 0;
2573
2574 int a = 0;
2575 asm volatile(
2576 ".word 0xc22026f3 \n" // csrr a3, vlenb
2577 "mv %0, a3 \n"
2578 : "=r"(a)
2579 :
2580 : "memory", "a3");
2581 return a;
2582#else
2583 return 0;
2584#endif
2585}
2586
2587int get_cpu_count()
2588{
2589 try_initialize_global_cpu_info();
2590 return g_cpucount;
2591}
2592
2593int get_little_cpu_count()
2594{
2595 try_initialize_global_cpu_info();
2596 return get_cpu_thread_affinity_mask(1).num_enabled();
2597}
2598
2599int get_big_cpu_count()
2600{
2601 try_initialize_global_cpu_info();
2602 int big_cpu_count = get_cpu_thread_affinity_mask(2).num_enabled();
2603 return big_cpu_count ? big_cpu_count : g_cpucount;
2604}
2605
2606int get_physical_cpu_count()
2607{
2608 try_initialize_global_cpu_info();
2609 return g_physical_cpucount;
2610}
2611
2612int get_physical_little_cpu_count()
2613{
2614 try_initialize_global_cpu_info();
2615 if (g_physical_cpucount == g_cpucount)
2616 return get_little_cpu_count();
2617
2618 return g_physical_cpucount * 2 - g_cpucount;
2619}
2620
2621int get_physical_big_cpu_count()
2622{
2623 try_initialize_global_cpu_info();
2624 if (g_physical_cpucount == g_cpucount)
2625 return get_big_cpu_count();
2626
2627 return g_cpucount - g_physical_cpucount;
2628}
2629
2630int get_cpu_level2_cache_size()
2631{
2632 try_initialize_global_cpu_info();
2633 return g_cpu_level2_cachesize;
2634}
2635
2636int get_cpu_level3_cache_size()
2637{
2638 try_initialize_global_cpu_info();
2639 return g_cpu_level3_cachesize;
2640}
2641
2642int get_cpu_powersave()
2643{
2644 try_initialize_global_cpu_info();
2645 return g_powersave;
2646}
2647
2648int set_cpu_powersave(int powersave)
2649{
2650 try_initialize_global_cpu_info();
2651 if (powersave < 0 || powersave > 2)
2652 {
2653 NCNN_LOGE("powersave %d not supported", powersave);
2654 return -1;
2655 }
2656
2657 const CpuSet& thread_affinity_mask = get_cpu_thread_affinity_mask(powersave);
2658
2659 int ret = set_cpu_thread_affinity(thread_affinity_mask);
2660 if (ret != 0)
2661 return ret;
2662
2663 g_powersave = powersave;
2664
2665 return 0;
2666}
2667
2668const CpuSet& get_cpu_thread_affinity_mask(int powersave)
2669{
2670 try_initialize_global_cpu_info();
2671 if (powersave == 0)
2672 return g_cpu_affinity_mask_all;
2673
2674 if (powersave == 1)
2675 return g_cpu_affinity_mask_little;
2676
2677 if (powersave == 2)
2678 return g_cpu_affinity_mask_big;
2679
2680 NCNN_LOGE("powersave %d not supported", powersave);
2681
2682 // fallback to all cores anyway
2683 return g_cpu_affinity_mask_all;
2684}
2685
2686int set_cpu_thread_affinity(const CpuSet& thread_affinity_mask)
2687{
2688 try_initialize_global_cpu_info();
2689#if defined __ANDROID__ || defined __linux__ || defined _WIN32
2690#ifdef _OPENMP
2691 int num_threads = thread_affinity_mask.num_enabled();
2692
2693 // set affinity for each thread
2694 set_omp_num_threads(num_threads);
2695 std::vector<int> ssarets(num_threads, 0);
2696 #pragma omp parallel for num_threads(num_threads)
2697 for (int i = 0; i < num_threads; i++)
2698 {
2699 ssarets[i] = set_sched_affinity(thread_affinity_mask);
2700 }
2701 for (int i = 0; i < num_threads; i++)
2702 {
2703 if (ssarets[i] != 0)
2704 return -1;
2705 }
2706#else
2707 int ssaret = set_sched_affinity(thread_affinity_mask);
2708 if (ssaret != 0)
2709 return -1;
2710#endif
2711
2712 return 0;
2713#elif __APPLE__
2714
2715#ifdef _OPENMP
2716 int num_threads = thread_affinity_mask.num_enabled();
2717
2718 // set affinity for each thread
2719 set_omp_num_threads(num_threads);
2720 std::vector<int> ssarets(num_threads, 0);
2721 #pragma omp parallel for num_threads(num_threads)
2722 for (int i = 0; i < num_threads; i++)
2723 {
2724 // assign one core for each thread
2725 int core = -1 - i;
2726 for (int j = 0; j < (int)sizeof(thread_affinity_mask.policy) * 8; j++)
2727 {
2728 if (thread_affinity_mask.is_enabled(j))
2729 {
2730 if (core == -1)
2731 {
2732 core = j;
2733 break;
2734 }
2735 else
2736 {
2737 core++;
2738 }
2739 }
2740 }
2741 CpuSet this_thread_affinity_mask;
2742 if (core != -1 - i)
2743 {
2744 this_thread_affinity_mask.enable(core);
2745 }
2746
2747 ssarets[i] = set_sched_affinity(this_thread_affinity_mask);
2748 }
2749 for (int i = 0; i < num_threads; i++)
2750 {
2751 if (ssarets[i] != 0)
2752 return -1;
2753 }
2754#else
2755 int ssaret = set_sched_affinity(thread_affinity_mask);
2756 if (ssaret != 0)
2757 return -1;
2758#endif
2759
2760 return 0;
2761#else
2762 // TODO
2763 (void)thread_affinity_mask;
2764 return -1;
2765#endif
2766}
2767
2768int is_current_thread_running_on_a53_a55()
2769{
2770 try_initialize_global_cpu_info();
2771#if defined __ANDROID__ || defined __linux__
2772#if __aarch64__
2773 if (g_cpu_is_arm_a53_a55 == 0)
2774 return 0; // all non a53/a55
2775
2776 if (g_cpu_is_arm_a53_a55 == 1)
2777 return 1; // all a53/a55
2778
2779 if (g_powersave == 2)
2780 return 0; // big clusters
2781
2782 if (g_powersave == 1)
2783 return 1; // little clusters
2784
2785 // little cores are a53/a55
2786
2787 // use cpuid for retrieving midr since kernel 4.7+
2788 if (cpu_support_arm_cpuid())
2789 {
2790 unsigned int midr = get_midr_from_register();
2791 if (midr)
2792 return midr_is_a53_a55(midr);
2793 }
2794
2795 // check if affinity cpuid is in the little ones
2796 CpuSet thread_cs;
2797 int ret = get_sched_affinity(thread_cs);
2798 if (ret != 0)
2799 {
2800 // no affinity capability
2801 return 0;
2802 }
2803
2804 const CpuSet& little_cs = get_cpu_thread_affinity_mask(1);
2805 for (int i = 0; i < g_cpucount; i++)
2806 {
2807 if (!thread_cs.is_enabled(i))
2808 continue;
2809
2810 if (!little_cs.is_enabled(i))
2811 return 0;
2812 }
2813
2814 // all affinity cpuids are little core
2815 return 1;
2816#else
2817 return 0;
2818#endif // __aarch64__
2819#else
2820 return 0;
2821#endif // defined __ANDROID__ || defined __linux__
2822}
2823
2824int get_omp_num_threads()
2825{
2826#ifdef _OPENMP
2827 return omp_get_num_threads();
2828#else
2829 return 1;
2830#endif
2831}
2832
2833void set_omp_num_threads(int num_threads)
2834{
2835#ifdef _OPENMP
2836 omp_set_num_threads(num_threads);
2837#else
2838 (void)num_threads;
2839#endif
2840}
2841
2842int get_omp_dynamic()
2843{
2844#ifdef _OPENMP
2845 return omp_get_dynamic();
2846#else
2847 return 0;
2848#endif
2849}
2850
2851void set_omp_dynamic(int dynamic)
2852{
2853#ifdef _OPENMP
2854 omp_set_dynamic(dynamic);
2855#else
2856 (void)dynamic;
2857#endif
2858}
2859
2860int get_omp_thread_num()
2861{
2862#ifdef _OPENMP
2863 return omp_get_thread_num();
2864#else
2865 return 0;
2866#endif
2867}
2868
2869int get_kmp_blocktime()
2870{
2871#if defined(_OPENMP) && (__clang__ || defined(_OPENMP_LLVM_RUNTIME))
2872 return kmp_get_blocktime();
2873#else
2874 return 0;
2875#endif
2876}
2877
2878void set_kmp_blocktime(int time_ms)
2879{
2880#if defined(_OPENMP) && (__clang__ || defined(_OPENMP_LLVM_RUNTIME))
2881 kmp_set_blocktime(time_ms);
2882#else
2883 (void)time_ms;
2884#endif
2885}
2886
2887static ncnn::ThreadLocalStorage tls_flush_denormals;
2888
2889int get_flush_denormals()
2890{
2891#if defined(__SSE3__)
2892 return (int)reinterpret_cast<size_t>(tls_flush_denormals.get());
2893#else
2894 return 0;
2895#endif
2896}
2897
2898int set_flush_denormals(int flush_denormals)
2899{
2900 if (flush_denormals < 0 || flush_denormals > 3)
2901 {
2902 NCNN_LOGE("denormals_zero %d not supported", flush_denormals);
2903 return -1;
2904 }
2905#if defined(__SSE3__)
2906 if (flush_denormals == 0)
2907 {
2908 _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
2909 _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
2910 }
2911 else if (flush_denormals == 1)
2912 {
2913 _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
2914 _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
2915 }
2916 else if (flush_denormals == 2)
2917 {
2918 _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
2919 _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
2920 }
2921 else if (flush_denormals == 3)
2922 {
2923 _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
2924 _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
2925 }
2926
2927 tls_flush_denormals.set(reinterpret_cast<void*>((size_t)flush_denormals));
2928 return 0;
2929#else
2930 return 0;
2931#endif
2932}
2933
2934} // namespace ncnn
2935
2936#if defined __ANDROID__ && defined(_OPENMP) && __clang__
2937#ifdef __cplusplus
2938extern "C" {
2939#endif
2940void __wrap___kmp_affinity_determine_capable(const char* /*env_var*/)
2941{
2942 // the internal affinity routines in llvm openmp call abort on __NR_sched_getaffinity / __NR_sched_setaffinity fails
2943 // ref KMPNativeAffinity::get_system_affinity/set_system_affinity in openmp/runtime/src/kmp_affinity.h
2944 // and cpu core goes offline in powersave mode on android, which triggers abort
2945 // ATM there is no known api for controlling the abort behavior
2946 // override __kmp_affinity_determine_capable with empty body to disable affinity regardless of KMP_AFFINITY env_var
2947 // ugly hack works >.< --- nihui
2948}
2949#ifdef __cplusplus
2950} // extern "C"
2951#endif
2952#endif