]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSCrossEndian.h
0b1e5aa1868160b4c3a71d17f736de29257803c8
[apple/xnu.git] / libkern / libkern / OSCrossEndian.h
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * This private header exports 3 APIs.
25 * _OSRosettaCheck() - An inline function that returns true if we are
26 * currently running under Rosetta.
27 * IF_ROSETTA() - Which is used to as a regular conditional
28 * expression that is true only if the current
29 * code is executing in the Rosetta
30 * translation space.
31 * ROSETTA_ONLY(exprs) - Which is used to create a block code that only
32 * executes if we are running in Rosetta.
33 *
34 * for example
35 *
36 * IF_ROSETTA() {
37 * // Do Cross endian swapping of input data
38 * outdata = OSSwap??(indata);
39 * }
40 * else {
41 * // Do straight through
42 * outdata = indata;
43 * }
44 *
45 * outdata = indata;
46 * ROSETTA_ONLY(
47 * // Do Cross endian swapping of input data
48 * outdata = OSSwap??(outdata);
49 * );
50 */
51
52 #ifndef _LIBKERN_OSCROSSENDIAN_H
53 #define _LIBKERN_OSCROSSENDIAN_H
54
55 #include <sys/sysctl.h>
56
57 #if __ppc__
58
59 static __inline__ int
60 _OSRosettaCheck(void)
61 {
62 int isCrossEndian = 0;
63 int val = 0;
64 size_t size = sizeof val;
65
66 if (sysctlbyname("sysctl.proc_native", &val, &size, NULL, 0) == -1)
67 isCrossEndian = 0;
68 else
69 isCrossEndian = val ? 0 : 1;
70
71 return isCrossEndian;
72 }
73
74 #else /* __ppc__ */
75
76 static __inline__ int _OSRosettaCheck(void) { return 0; }
77
78 #endif /* __ppc__ */
79
80 #define IF_ROSETTA() if (__builtin_expect(_OSRosettaCheck(), 0) )
81
82 #define ROSETTA_ONLY(exprs) \
83 do { \
84 IF_ROSETTA() { \
85 exprs \
86 } \
87 } while(0)
88
89 #endif /* _LIBKERN_OSCROSSENDIAN_H */