xref: /aosp_15_r20/external/cpu_features/src/impl_x86_macos.c (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
1*eca53ba6SRoland Levillain // Copyright 2017 Google LLC
2*eca53ba6SRoland Levillain //
3*eca53ba6SRoland Levillain // Licensed under the Apache License, Version 2.0 (the "License");
4*eca53ba6SRoland Levillain // you may not use this file except in compliance with the License.
5*eca53ba6SRoland Levillain // You may obtain a copy of the License at
6*eca53ba6SRoland Levillain //
7*eca53ba6SRoland Levillain //    http://www.apache.org/licenses/LICENSE-2.0
8*eca53ba6SRoland Levillain //
9*eca53ba6SRoland Levillain // Unless required by applicable law or agreed to in writing, software
10*eca53ba6SRoland Levillain // distributed under the License is distributed on an "AS IS" BASIS,
11*eca53ba6SRoland Levillain // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eca53ba6SRoland Levillain // See the License for the specific language governing permissions and
13*eca53ba6SRoland Levillain // limitations under the License.
14*eca53ba6SRoland Levillain 
15*eca53ba6SRoland Levillain #include "cpu_features_macros.h"
16*eca53ba6SRoland Levillain 
17*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_ARCH_X86
18*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_OS_MACOS
19*eca53ba6SRoland Levillain 
20*eca53ba6SRoland Levillain #include "impl_x86__base_implementation.inl"
21*eca53ba6SRoland Levillain 
22*eca53ba6SRoland Levillain #if !defined(HAVE_SYSCTLBYNAME)
23*eca53ba6SRoland Levillain #error "Darwin needs support for sysctlbyname"
24*eca53ba6SRoland Levillain #endif
25*eca53ba6SRoland Levillain #include <sys/sysctl.h>
26*eca53ba6SRoland Levillain 
27*eca53ba6SRoland Levillain #if defined(CPU_FEATURES_MOCK_CPUID_X86)
28*eca53ba6SRoland Levillain extern bool GetDarwinSysCtlByName(const char*);
29*eca53ba6SRoland Levillain #else  // CPU_FEATURES_MOCK_CPUID_X86
GetDarwinSysCtlByName(const char * name)30*eca53ba6SRoland Levillain static bool GetDarwinSysCtlByName(const char* name) {
31*eca53ba6SRoland Levillain   int enabled;
32*eca53ba6SRoland Levillain   size_t enabled_len = sizeof(enabled);
33*eca53ba6SRoland Levillain   const int failure = sysctlbyname(name, &enabled, &enabled_len, NULL, 0);
34*eca53ba6SRoland Levillain   return failure ? false : enabled;
35*eca53ba6SRoland Levillain }
36*eca53ba6SRoland Levillain #endif
37*eca53ba6SRoland Levillain 
OverrideOsPreserves(OsPreserves * os_preserves)38*eca53ba6SRoland Levillain static void OverrideOsPreserves(OsPreserves* os_preserves) {
39*eca53ba6SRoland Levillain   // On Darwin AVX512 support is On-demand.
40*eca53ba6SRoland Levillain   // We have to query the OS instead of querying the Zmm save/restore state.
41*eca53ba6SRoland Levillain   // https://github.com/apple/darwin-xnu/blob/8f02f2a044b9bb1ad951987ef5bab20ec9486310/osfmk/i386/fpu.c#L173-L199
42*eca53ba6SRoland Levillain   os_preserves->avx512_registers = GetDarwinSysCtlByName("hw.optional.avx512f");
43*eca53ba6SRoland Levillain }
44*eca53ba6SRoland Levillain 
DetectFeaturesFromOs(X86Info * info,X86Features * features)45*eca53ba6SRoland Levillain static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
46*eca53ba6SRoland Levillain   (void)info;
47*eca53ba6SRoland Levillain   // Handling Darwin platform through sysctlbyname.
48*eca53ba6SRoland Levillain   features->sse = GetDarwinSysCtlByName("hw.optional.sse");
49*eca53ba6SRoland Levillain   features->sse2 = GetDarwinSysCtlByName("hw.optional.sse2");
50*eca53ba6SRoland Levillain   features->sse3 = GetDarwinSysCtlByName("hw.optional.sse3");
51*eca53ba6SRoland Levillain   features->ssse3 = GetDarwinSysCtlByName("hw.optional.supplementalsse3");
52*eca53ba6SRoland Levillain   features->sse4_1 = GetDarwinSysCtlByName("hw.optional.sse4_1");
53*eca53ba6SRoland Levillain   features->sse4_2 = GetDarwinSysCtlByName("hw.optional.sse4_2");
54*eca53ba6SRoland Levillain }
55*eca53ba6SRoland Levillain 
56*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_OS_MACOS
57*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_ARCH_X86
58