Elffuss BraidHub

kikocis/ncnn public

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