1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include "config.h"
28
29 #include <stdint.h>
30
31 #include "src/cpu.h"
32 #include "src/log.h"
33
34 #ifdef _WIN32
35 #include <windows.h>
36 #endif
37 #ifdef __APPLE__
38 #include <sys/sysctl.h>
39 #include <sys/types.h>
40 #endif
41 #if HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #if HAVE_PTHREAD_GETAFFINITY_NP
46 #include <pthread.h>
47 #if HAVE_PTHREAD_NP_H
48 #include <pthread_np.h>
49 #endif
50 #if defined(__FreeBSD__)
51 #define cpu_set_t cpuset_t
52 #endif
53 #endif
54
55 unsigned dav1d_cpu_flags = 0U;
56 unsigned dav1d_cpu_flags_mask = ~0U;
57
dav1d_init_cpu(void)58 COLD void dav1d_init_cpu(void) {
59 #if HAVE_ASM && !__has_feature(memory_sanitizer)
60 // memory sanitizer is inherently incompatible with asm
61 #if ARCH_AARCH64 || ARCH_ARM
62 dav1d_cpu_flags = dav1d_get_cpu_flags_arm();
63 #elif ARCH_LOONGARCH
64 dav1d_cpu_flags = dav1d_get_cpu_flags_loongarch();
65 #elif ARCH_PPC64LE
66 dav1d_cpu_flags = dav1d_get_cpu_flags_ppc();
67 #elif ARCH_RISCV
68 dav1d_cpu_flags = dav1d_get_cpu_flags_riscv();
69 #elif ARCH_X86
70 dav1d_cpu_flags = dav1d_get_cpu_flags_x86();
71 #endif
72 #endif
73 }
74
dav1d_set_cpu_flags_mask(const unsigned mask)75 COLD void dav1d_set_cpu_flags_mask(const unsigned mask) {
76 dav1d_cpu_flags_mask = mask;
77 }
78
dav1d_num_logical_processors(Dav1dContext * const c)79 COLD int dav1d_num_logical_processors(Dav1dContext *const c) {
80 #ifdef _WIN32
81 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
82 GROUP_AFFINITY affinity;
83 if (GetThreadGroupAffinity(GetCurrentThread(), &affinity)) {
84 int num_processors = 1;
85 while (affinity.Mask &= affinity.Mask - 1)
86 num_processors++;
87 return num_processors;
88 }
89 #else
90 SYSTEM_INFO system_info;
91 GetNativeSystemInfo(&system_info);
92 return system_info.dwNumberOfProcessors;
93 #endif
94 #elif HAVE_PTHREAD_GETAFFINITY_NP && defined(CPU_COUNT)
95 cpu_set_t affinity;
96 if (!pthread_getaffinity_np(pthread_self(), sizeof(affinity), &affinity))
97 return CPU_COUNT(&affinity);
98 #elif defined(__APPLE__)
99 int num_processors;
100 size_t length = sizeof(num_processors);
101 if (!sysctlbyname("hw.logicalcpu", &num_processors, &length, NULL, 0))
102 return num_processors;
103 #elif defined(_SC_NPROCESSORS_ONLN)
104 return (int)sysconf(_SC_NPROCESSORS_ONLN);
105 #endif
106 if (c)
107 dav1d_log(c, "Unable to detect thread count, defaulting to single-threaded mode\n");
108 return 1;
109 }
110