]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSCrossEndian.h
0131455d1abf00396c6622a8c248bb6207f92a85
[apple/xnu.git] / libkern / libkern / OSCrossEndian.h
1 /*
2 * Copyright (c) 2000-2005 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 #if __ppc__
56
57 static __inline__ int _OSRosettaCheck(void)
58 {
59 int isCrossEndian;
60
61 __asm__ ( "b 0f\n"
62 " .long 0x14400004\n"
63 " li %0,1\n"
64 "0:"
65 : "=r" (isCrossEndian) : "0" (0)
66 );
67
68 return isCrossEndian;
69 }
70
71 #else
72
73 static __inline__ int _OSRosettaCheck(void) { return 0; }
74
75 #endif
76
77 #define IF_ROSETTA() if (__builtin_expect(_OSRosettaCheck(), 0) )
78
79 #define ROSETTA_ONLY(exprs) \
80 do { \
81 IF_ROSETTA() { \
82 exprs \
83 } \
84 } while(0)
85
86 #endif /* _LIBKERN_OSCROSSENDIAN_H */