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