]> git.saurik.com Git - apple/libc.git/blob - gen/platfunc.c
Libc-825.24.tar.gz
[apple/libc.git] / gen / platfunc.c
1 /*
2 * Copyright (c) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if defined(__i386__) || defined(__x86_64__)
25
26 #include <stdlib.h>
27 #include <machine/cpu_capabilities.h>
28 #include "platfunc.h"
29
30 #define PLAT_CAP_MASK (kHasMMX | kHasSSE | kHasSSE2 | kHasSSE3 | kCache32 | kCache64 | kCache128 | kFastThreadLocalStorage | kHasSupplementalSSE3 | k64Bit | kHasSSE4_1 | kHasSSE4_2 | kHasAES | kInOrderPipeline | kSlow | kUP)
31
32 inline static int __get_cpu_capabilities() {
33 return *((int32_t*)_COMM_PAGE_CPU_CAPABILITIES);
34 }
35
36 inline static int num_cpus(int capabilities) {
37 return (capabilities & kNumCPUs) >> kNumCPUsShift;
38 }
39
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);
44
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)
52 continue;
53 /* Now check that the CPU doesn't have any of the features that are in canthave */
54 if ((canthave & cap) != 0)
55 continue;
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)
59 continue;
60 if (canthave_cpus != 0 && have_cpus == canthave_cpus)
61 continue;
62 /* otherwise, we have found our preferred implementation */
63 return descriptors[i]->address;
64 }
65
66 /* We didn't find an acceptable implementation. (bug in data structures!) */
67 return NULL;
68 }
69
70 #endif