]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/ppc/OSByteOrder.h
28ff91dc21138b5b1ab74cb7c9203fd77eb16282
[apple/xnu.git] / libkern / libkern / ppc / OSByteOrder.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
27 *
28 * HISTORY
29 *
30 */
31
32
33 #ifndef _OS_OSBYTEORDERPPC_H
34 #define _OS_OSBYTEORDERPPC_H
35
36 #include <libkern/OSTypes.h>
37
38 /* Functions for byte reversed loads. */
39
40 OS_INLINE
41 UInt16
42 OSReadSwapInt16(
43 volatile void * base,
44 UInt offset
45 )
46 {
47 UInt16 result;
48 __asm__ volatile("lhbrx %0, %1, %2"
49 : "=r" (result)
50 : "b" (base), "r" (offset)
51 : "memory");
52 return result;
53 }
54
55 OS_INLINE
56 UInt32
57 OSReadSwapInt32(
58 volatile void * base,
59 UInt offset
60 )
61 {
62 UInt32 result;
63 __asm__ volatile("lwbrx %0, %1, %2"
64 : "=r" (result)
65 : "b" (base), "r" (offset)
66 : "memory");
67 return result;
68 }
69
70 OS_INLINE
71 UInt64
72 OSReadSwapInt64(
73 volatile void * base,
74 UInt offset
75 )
76 {
77 UInt64 * inp;
78 union ullc {
79 UInt64 ull;
80 UInt ul[2];
81 } outv;
82
83 inp = (UInt64 *)base;
84 outv.ul[0] = OSReadSwapInt32(inp, offset + 4);
85 outv.ul[1] = OSReadSwapInt32(inp, offset);
86 return outv.ull;
87 }
88
89 OS_INLINE
90 UInt
91 OSReadSwapInt(
92 volatile void * base,
93 UInt offset
94 )
95 {
96 UInt result;
97 __asm__ volatile("lwbrx %0, %1, %2"
98 : "=r" (result)
99 : "b" (base), "r" (offset)
100 : "memory");
101 return result;
102 }
103
104 /* Functions for byte reversed stores. */
105
106 OS_INLINE
107 void
108 OSWriteSwapInt16(
109 volatile void * base,
110 UInt offset,
111 UInt16 data
112 )
113 {
114 __asm__ volatile("sthbrx %0, %1, %2"
115 :
116 : "r" (data), "b" (base), "r" (offset)
117 : "memory");
118 }
119
120 OS_INLINE
121 void
122 OSWriteSwapInt32(
123 volatile void * base,
124 UInt offset,
125 UInt32 data
126 )
127 {
128 __asm__ volatile("stwbrx %0, %1, %2"
129 :
130 : "r" (data), "b" (base), "r" (offset)
131 : "memory" );
132 }
133
134 OS_INLINE
135 void
136 OSWriteSwapInt64(
137 volatile void * base,
138 UInt offset,
139 UInt64 data
140 )
141 {
142 UInt64 * outp;
143 union ullc {
144 UInt64 ull;
145 UInt ul[2];
146 } *inp;
147
148 outp = (UInt64 *)base;
149 inp = (union ullc *)&data;
150 OSWriteSwapInt32(outp, offset, inp->ul[1]);
151 OSWriteSwapInt32(outp, offset + 4, inp->ul[0]);
152 }
153
154 OS_INLINE
155 void
156 OSWriteSwapInt(
157 volatile void * base,
158 UInt offset,
159 UInt data
160 )
161 {
162 __asm__ volatile("stwbrx %0, %1, %2"
163 :
164 : "r" (data), "b" (base), "r" (offset)
165 : "memory" );
166 }
167
168 /* Generic byte swapping functions. */
169
170 OS_INLINE
171 UInt16
172 OSSwapInt16(
173 UInt16 data
174 )
175 {
176 UInt16 temp = data;
177 return OSReadSwapInt16(&temp, 0);
178 }
179
180 OS_INLINE
181 UInt32
182 OSSwapInt32(
183 UInt32 data
184 )
185 {
186 UInt32 temp = data;
187 return OSReadSwapInt32(&temp, 0);
188 }
189
190 OS_INLINE
191 UInt64
192 OSSwapInt64(
193 UInt64 data
194 )
195 {
196 UInt64 temp = data;
197 return OSReadSwapInt64(&temp, 0);
198 }
199
200 OS_INLINE
201 UInt
202 OSSwapInt(
203 UInt data
204 )
205 {
206 UInt temp = data;
207 return OSReadSwapInt(&temp, 0);
208 }
209
210 #endif /* ! _OS_OSBYTEORDERPPC_H */