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