]> git.saurik.com Git - apple/xnu.git/blame - libkern/libkern/machine/OSByteOrder.h
xnu-344.tar.gz
[apple/xnu.git] / libkern / libkern / machine / OSByteOrder.h
CommitLineData
1c79356b
A
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
9bccf70c 33#include <libkern/OSTypes.h>
1c79356b
A
34
35/* Functions for byte reversed loads. */
36
37OS_INLINE
38UInt16
39OSReadSwapInt16(
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
54OS_INLINE
55UInt32
56OSReadSwapInt32(
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
73OS_INLINE
74UInt64
75OSReadSwapInt64(
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
96OS_INLINE
97UInt
98OSReadSwapInt(
99 volatile void * base,
100 UInt offset
101)
102{
103 return (UInt)OSReadSwapInt32(base, offset);
104}
105
106/* Functions for byte reversed stores. */
107
108OS_INLINE
109void
110OSWriteSwapInt16(
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
126OS_INLINE
127void
128OSWriteSwapInt32(
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
146OS_INLINE
147void
148OSWriteSwapInt64(
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
170OS_INLINE
171void
172OSWriteSwapInt(
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
183OS_INLINE
184UInt16
185OSSwapInt16(
186 UInt16 data
187)
188{
189 UInt16 temp = data;
190 return OSReadSwapInt16(&temp, 0);
191}
192
193OS_INLINE
194UInt32
195OSSwapInt32(
196 UInt32 data
197)
198{
199 UInt32 temp = data;
200 return OSReadSwapInt32(&temp, 0);
201}
202
203OS_INLINE
204UInt64
205OSSwapInt64(
206 UInt64 data
207)
208{
209 UInt64 temp = data;
210 return OSReadSwapInt64(&temp, 0);
211}
212
213OS_INLINE
214UInt
215OSSwapInt(
216 UInt data
217)
218{
219 UInt temp = data;
220 return OSReadSwapInt(&temp, 0);
221}
222
223#endif /* ! _OS_OSBYTEORDERMACHINE_H */