]> git.saurik.com Git - apple/libc.git/blame - gen/platfunc.c
Libc-763.12.tar.gz
[apple/libc.git] / gen / platfunc.c
CommitLineData
1f2f436a
A
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 <platfunc.h>
28#include <machine/cpu_capabilities.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
32inline static int num_cpus(int capabilities) {
33 return (capabilities & kNumCPUs) >> kNumCPUsShift;
34}
35
36/* TODO: If 8151810 is fixed (or full support for visibility("internal") is added), change this to visibility("internal") */
37void __attribute__((visibility("hidden"))) *find_platform_function(const platfunc_descriptor *descriptors[]) {
38 int cap = _get_cpu_capabilities(),
39 have_cpus = num_cpus(cap);
40
41 for (int i = 0; descriptors[i] != 0; i++) {
42 int musthave_cpus = num_cpus(descriptors[i]->musthave),
43 canthave_cpus = num_cpus(descriptors[i]->canthave);
44 uint32_t musthave = descriptors[i]->musthave & PLAT_CAP_MASK,
45 canthave = descriptors[i]->canthave & PLAT_CAP_MASK;
46 /* First check that the CPU supports all the features that are in musthave */
47 if ((musthave & cap) != musthave)
48 continue;
49 /* Now check that the CPU doesn't have any of the features that are in canthave */
50 if ((canthave & cap) != 0)
51 continue;
52 /* Special case check for the number of CPUs. */
53 /* TODO: Should there be a way to specify < or > num cpus? */
54 if (musthave_cpus != 0 && have_cpus != musthave_cpus)
55 continue;
56 if (canthave_cpus != 0 && have_cpus == canthave_cpus)
57 continue;
58 /* otherwise, we have found our preferred implementation */
59 return descriptors[i]->address;
60 }
61
62 /* We didn't find an acceptable implementation. (bug in data structures!) */
63 return NULL;
64}
65
66#endif