]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/ppc/OSByteOrder.h
0a7e75a72fc74e384add42bd2529b179f5fff08e
[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 * 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 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
24 *
25 * HISTORY
26 *
27 */
28
29
30 #ifndef _OS_OSBYTEORDERPPC_H
31 #define _OS_OSBYTEORDERPPC_H
32
33 #include <libkern/OSBase.h>
34
35 /* Functions for byte reversed loads. */
36
37 OS_INLINE
38 UInt16
39 OSReadSwapInt16(
40 volatile void * base,
41 UInt offset
42 )
43 {
44 UInt16 result;
45 __asm__ volatile("lhbrx %0, %1, %2"
46 : "=r" (result)
47 : "b" (base), "r" (offset)
48 : "memory");
49 return result;
50 }
51
52 OS_INLINE
53 UInt32
54 OSReadSwapInt32(
55 volatile void * base,
56 UInt offset
57 )
58 {
59 UInt32 result;
60 __asm__ volatile("lwbrx %0, %1, %2"
61 : "=r" (result)
62 : "b" (base), "r" (offset)
63 : "memory");
64 return result;
65 }
66
67 OS_INLINE
68 UInt64
69 OSReadSwapInt64(
70 volatile void * base,
71 UInt offset
72 )
73 {
74 UInt64 * inp;
75 union ullc {
76 UInt64 ull;
77 UInt ul[2];
78 } outv;
79
80 inp = (UInt64 *)base;
81 outv.ul[0] = OSReadSwapInt32(inp, offset + 4);
82 outv.ul[1] = OSReadSwapInt32(inp, offset);
83 return outv.ull;
84 }
85
86 OS_INLINE
87 UInt
88 OSReadSwapInt(
89 volatile void * base,
90 UInt offset
91 )
92 {
93 UInt result;
94 __asm__ volatile("lwbrx %0, %1, %2"
95 : "=r" (result)
96 : "b" (base), "r" (offset)
97 : "memory");
98 return result;
99 }
100
101 /* Functions for byte reversed stores. */
102
103 OS_INLINE
104 void
105 OSWriteSwapInt16(
106 volatile void * base,
107 UInt offset,
108 UInt16 data
109 )
110 {
111 __asm__ volatile("sthbrx %0, %1, %2"
112 :
113 : "r" (data), "b" (base), "r" (offset)
114 : "memory");
115 }
116
117 OS_INLINE
118 void
119 OSWriteSwapInt32(
120 volatile void * base,
121 UInt offset,
122 UInt32 data
123 )
124 {
125 __asm__ volatile("stwbrx %0, %1, %2"
126 :
127 : "r" (data), "b" (base), "r" (offset)
128 : "memory" );
129 }
130
131 OS_INLINE
132 void
133 OSWriteSwapInt64(
134 volatile void * base,
135 UInt offset,
136 UInt64 data
137 )
138 {
139 UInt64 * outp;
140 union ullc {
141 UInt64 ull;
142 UInt ul[2];
143 } *inp;
144
145 outp = (UInt64 *)base;
146 inp = (union ullc *)&data;
147 OSWriteSwapInt32(outp, offset, inp->ul[1]);
148 OSWriteSwapInt32(outp, offset + 4, inp->ul[0]);
149 }
150
151 OS_INLINE
152 void
153 OSWriteSwapInt(
154 volatile void * base,
155 UInt offset,
156 UInt data
157 )
158 {
159 __asm__ volatile("stwbrx %0, %1, %2"
160 :
161 : "r" (data), "b" (base), "r" (offset)
162 : "memory" );
163 }
164
165 /* Generic byte swapping functions. */
166
167 OS_INLINE
168 UInt16
169 OSSwapInt16(
170 UInt16 data
171 )
172 {
173 UInt16 temp = data;
174 return OSReadSwapInt16(&temp, 0);
175 }
176
177 OS_INLINE
178 UInt32
179 OSSwapInt32(
180 UInt32 data
181 )
182 {
183 UInt32 temp = data;
184 return OSReadSwapInt32(&temp, 0);
185 }
186
187 OS_INLINE
188 UInt64
189 OSSwapInt64(
190 UInt64 data
191 )
192 {
193 UInt64 temp = data;
194 return OSReadSwapInt64(&temp, 0);
195 }
196
197 OS_INLINE
198 UInt
199 OSSwapInt(
200 UInt data
201 )
202 {
203 UInt temp = data;
204 return OSReadSwapInt(&temp, 0);
205 }
206
207 #endif /* ! _OS_OSBYTEORDERPPC_H */