]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/i386/OSByteOrder.h
0aa2270f16b6ea5f92219b932ac0f73327cdb93f
[apple/xnu.git] / libkern / libkern / i386 / 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 #ifndef _OS_OSBYTEORDERI386_H
33 #define _OS_OSBYTEORDERI386_H
34
35 #include <libkern/OSTypes.h>
36
37 /* Functions for byte reversed loads. */
38
39 OS_INLINE
40 UInt16
41 OSReadSwapInt16(
42 volatile void * base,
43 UInt offset
44 )
45 {
46 UInt16 result;
47
48 result = *(volatile UInt16 *)((UInt8 *)base + offset);
49 __asm__ volatile("xchgb %b0,%h0"
50 : "=q" (result)
51 : "0" (result));
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
64 result = *(volatile UInt32 *)((UInt8 *)base + offset);
65 __asm__ volatile("bswap %0"
66 : "=r" (result)
67 : "0" (result));
68 return result;
69 }
70
71 OS_INLINE
72 UInt64
73 OSReadSwapInt64(
74 volatile void * base,
75 UInt offset
76 )
77 {
78 UInt64 * inp;
79 union ullc {
80 UInt64 ull;
81 UInt ul[2];
82 } outv;
83
84 inp = (UInt64 *)base;
85 outv.ul[0] = OSReadSwapInt32(inp, offset + 4);
86 outv.ul[1] = OSReadSwapInt32(inp, offset);
87 return outv.ull;
88 }
89
90 OS_INLINE
91 UInt
92 OSReadSwapInt(
93 volatile void * base,
94 UInt offset
95 )
96 {
97 UInt result;
98
99 result = *(volatile UInt *)((UInt8 *)base + offset);
100 __asm__ volatile("bswap %0"
101 : "=r" (result)
102 : "0" (result));
103 return result;
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 __asm__ volatile("xchgb %b0,%h0"
117 : "=q" (data)
118 : "0" (data));
119 *(volatile UInt16 *)((UInt8 *)base + offset) = data;
120 }
121
122 OS_INLINE
123 void
124 OSWriteSwapInt32(
125 volatile void * base,
126 UInt offset,
127 UInt32 data
128 )
129 {
130 __asm__ volatile("bswap %0"
131 : "=r" (data)
132 : "0" (data));
133 *(volatile UInt32 *)((UInt8 *)base + offset) = data;
134 }
135
136 OS_INLINE
137 void
138 OSWriteSwapInt64(
139 volatile void * base,
140 UInt offset,
141 UInt64 data
142 )
143 {
144 UInt64 * outp;
145 union ullc {
146 UInt64 ull;
147 UInt ul[2];
148 } *inp;
149
150 outp = (UInt64 *)base;
151 inp = (union ullc *)&data;
152 OSWriteSwapInt32(outp, offset, inp->ul[1]);
153 OSWriteSwapInt32(outp, offset + 4, inp->ul[0]);
154 }
155
156 OS_INLINE
157 void
158 OSWriteSwapInt(
159 volatile void * base,
160 UInt offset,
161 UInt data
162 )
163 {
164 __asm__ volatile("bswap %0"
165 : "=r" (data)
166 : "0" (data));
167 *(volatile UInt *)((UInt8 *)base + offset) = data;
168 }
169
170 /* Generic byte swapping functions. */
171
172 OS_INLINE
173 UInt16
174 OSSwapInt16(
175 UInt16 data
176 )
177 {
178 UInt16 temp = data;
179 return OSReadSwapInt16(&temp, 0);
180 }
181
182 OS_INLINE
183 UInt32
184 OSSwapInt32(
185 UInt32 data
186 )
187 {
188 UInt32 temp = data;
189 return OSReadSwapInt32(&temp, 0);
190 }
191
192 OS_INLINE
193 UInt64
194 OSSwapInt64(
195 UInt64 data
196 )
197 {
198 UInt64 temp = data;
199 return OSReadSwapInt64(&temp, 0);
200 }
201
202 OS_INLINE
203 UInt
204 OSSwapInt(
205 UInt data
206 )
207 {
208 UInt temp = data;
209 return OSReadSwapInt(&temp, 0);
210 }
211
212 #endif /* ! _OS_OSBYTEORDERI386_H */