2 * Copyright (c) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #if defined(__i386__) || defined(__x86_64__)
27 #include <machine/cpu_capabilities.h>
30 #define PLAT_CAP_MASK (kHasMMX | kHasSSE | kHasSSE2 | kHasSSE3 | kCache32 | kCache64 | kCache128 | kFastThreadLocalStorage | kHasSupplementalSSE3 | k64Bit | kHasSSE4_1 | kHasSSE4_2 | kHasAES | kInOrderPipeline | kSlow | kUP)
32 inline static int __get_cpu_capabilities() {
33 return *((int32_t*)_COMM_PAGE_CPU_CAPABILITIES
);
36 inline static int num_cpus(int capabilities
) {
37 return (capabilities
& kNumCPUs
) >> kNumCPUsShift
;
40 /* TODO: If 8151810 is fixed (or full support for visibility("internal") is added), change this to visibility("internal") */
41 void __attribute__((visibility("hidden"))) *find_platform_function(const platfunc_descriptor
*descriptors
[]) {
42 int cap
= __get_cpu_capabilities(),
43 have_cpus
= num_cpus(cap
);
45 for (int i
= 0; descriptors
[i
] != 0; i
++) {
46 int musthave_cpus
= num_cpus(descriptors
[i
]->musthave
),
47 canthave_cpus
= num_cpus(descriptors
[i
]->canthave
);
48 uint32_t musthave
= descriptors
[i
]->musthave
& PLAT_CAP_MASK
,
49 canthave
= descriptors
[i
]->canthave
& PLAT_CAP_MASK
;
50 /* First check that the CPU supports all the features that are in musthave */
51 if ((musthave
& cap
) != musthave
)
53 /* Now check that the CPU doesn't have any of the features that are in canthave */
54 if ((canthave
& cap
) != 0)
56 /* Special case check for the number of CPUs. */
57 /* TODO: Should there be a way to specify < or > num cpus? */
58 if (musthave_cpus
!= 0 && have_cpus
!= musthave_cpus
)
60 if (canthave_cpus
!= 0 && have_cpus
== canthave_cpus
)
62 /* otherwise, we have found our preferred implementation */
63 return descriptors
[i
]->address
;
66 /* We didn't find an acceptable implementation. (bug in data structures!) */