]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/machine/OSByteOrder.h
b85d69b9ac5abff5ba5fca2702158b68053bbd21
[apple/xnu.git] / libkern / libkern / machine / 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_OSBYTEORDERMACHINE_H
34 #define _OS_OSBYTEORDERMACHINE_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 union sconv {
48 UInt16 us;
49 UInt8 uc[2];
50 } *inp, outv;
51 inp = (union sconv *)((UInt8 *)base + offset);
52 outv.uc[0] = inp->uc[1];
53 outv.uc[1] = inp->uc[0];
54 return (outv.us);
55 }
56
57 OS_INLINE
58 UInt32
59 OSReadSwapInt32(
60 volatile void * base,
61 UInt offset
62 )
63 {
64 union lconv {
65 UInt32 ul;
66 UInt8 uc[4];
67 } *inp, outv;
68 inp = (union lconv *)((UInt8 *)base + offset);
69 outv.uc[0] = inp->uc[3];
70 outv.uc[1] = inp->uc[2];
71 outv.uc[2] = inp->uc[1];
72 outv.uc[3] = inp->uc[0];
73 return (outv.ul);
74 }
75
76 OS_INLINE
77 UInt64
78 OSReadSwapInt64(
79 volatile void * base,
80 UInt offset
81 )
82 {
83 union llconv {
84 UInt64 ull;
85 UInt8 uc[8];
86 } *inp, outv;
87 inp = (union llconv *)((UInt8 *)base + offset);
88 outv.uc[0] = inp->uc[7];
89 outv.uc[1] = inp->uc[6];
90 outv.uc[2] = inp->uc[5];
91 outv.uc[3] = inp->uc[4];
92 outv.uc[4] = inp->uc[3];
93 outv.uc[5] = inp->uc[2];
94 outv.uc[6] = inp->uc[1];
95 outv.uc[7] = inp->uc[0];
96 return (outv.ull);
97 }
98
99 OS_INLINE
100 UInt
101 OSReadSwapInt(
102 volatile void * base,
103 UInt offset
104 )
105 {
106 return (UInt)OSReadSwapInt32(base, offset);
107 }
108
109 /* Functions for byte reversed stores. */
110
111 OS_INLINE
112 void
113 OSWriteSwapInt16(
114 volatile void * base,
115 UInt offset,
116 UInt16 data
117 )
118 {
119 union sconv {
120 UInt16 us;
121 UInt8 uc[2];
122 } *inp, *outp;
123 inp = (union sconv *)((UInt8 *)base + offset);
124 outp = (union sconv *)&data;
125 outp->uc[0] = inp->uc[1];
126 outp->uc[1] = inp->uc[0];
127 }
128
129 OS_INLINE
130 void
131 OSWriteSwapInt32(
132 volatile void * base,
133 UInt offset,
134 UInt32 data
135 )
136 {
137 union lconv {
138 UInt32 ul;
139 UInt8 uc[4];
140 } *inp, *outp;
141 inp = (union lconv *)((UInt8 *)base + offset);
142 outp = (union lconv *)&data;
143 outp->uc[0] = inp->uc[3];
144 outp->uc[1] = inp->uc[2];
145 outp->uc[2] = inp->uc[1];
146 outp->uc[3] = inp->uc[0];
147 }
148
149 OS_INLINE
150 void
151 OSWriteSwapInt64(
152 volatile void * base,
153 UInt offset,
154 UInt64 data
155 )
156 {
157 union llconv {
158 UInt64 ull;
159 UInt8 uc[8];
160 } *inp, *outp;
161 inp = (union llconv *)((UInt8 *)base + offset);
162 outp = (union llconv *)&data;
163 outp->uc[0] = inp->uc[7];
164 outp->uc[1] = inp->uc[6];
165 outp->uc[2] = inp->uc[5];
166 outp->uc[3] = inp->uc[4];
167 outp->uc[4] = inp->uc[3];
168 outp->uc[5] = inp->uc[2];
169 outp->uc[6] = inp->uc[1];
170 outp->uc[7] = inp->uc[0];
171 }
172
173 OS_INLINE
174 void
175 OSWriteSwapInt(
176 volatile void * base,
177 UInt offset,
178 UInt data
179 )
180 {
181 OSWriteSwapInt32(base, offset, (UInt32)data);
182 }
183
184 /* Generic byte swapping functions. */
185
186 OS_INLINE
187 UInt16
188 OSSwapInt16(
189 UInt16 data
190 )
191 {
192 UInt16 temp = data;
193 return OSReadSwapInt16(&temp, 0);
194 }
195
196 OS_INLINE
197 UInt32
198 OSSwapInt32(
199 UInt32 data
200 )
201 {
202 UInt32 temp = data;
203 return OSReadSwapInt32(&temp, 0);
204 }
205
206 OS_INLINE
207 UInt64
208 OSSwapInt64(
209 UInt64 data
210 )
211 {
212 UInt64 temp = data;
213 return OSReadSwapInt64(&temp, 0);
214 }
215
216 OS_INLINE
217 UInt
218 OSSwapInt(
219 UInt data
220 )
221 {
222 UInt temp = data;
223 return OSReadSwapInt(&temp, 0);
224 }
225
226 #endif /* ! _OS_OSBYTEORDERMACHINE_H */