]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/machine/OSByteOrder.h
f22f8e9e0a3467258093419fc29c1eaa45a96c07
[apple/xnu.git] / libkern / libkern / machine / OSByteOrder.h
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
25 *
26 * HISTORY
27 *
28 */
29
30
31 #ifndef _OS_OSBYTEORDERMACHINE_H
32 #define _OS_OSBYTEORDERMACHINE_H
33
34 #include <stdint.h>
35
36 #if !defined(OS_INLINE)
37 # define OS_INLINE static inline
38 #endif
39
40 /* Functions for byte reversed loads. */
41
42 OS_INLINE
43 uint16_t
44 OSReadSwapInt16(
45 volatile void * base,
46 uintptr_t offset
47 )
48 {
49 union sconv {
50 uint16_t us;
51 uint8_t uc[2];
52 } *inp, outv;
53 inp = (union sconv *)((uint8_t *)base + offset);
54 outv.uc[0] = inp->uc[1];
55 outv.uc[1] = inp->uc[0];
56 return (outv.us);
57 }
58
59 OS_INLINE
60 uint32_t
61 OSReadSwapInt32(
62 volatile void * base,
63 uintptr_t offset
64 )
65 {
66 union lconv {
67 uint32_t ul;
68 uint8_t uc[4];
69 } *inp, outv;
70 inp = (union lconv *)((uint8_t *)base + offset);
71 outv.uc[0] = inp->uc[3];
72 outv.uc[1] = inp->uc[2];
73 outv.uc[2] = inp->uc[1];
74 outv.uc[3] = inp->uc[0];
75 return (outv.ul);
76 }
77
78 OS_INLINE
79 uint64_t
80 OSReadSwapInt64(
81 volatile void * base,
82 uintptr_t offset
83 )
84 {
85 union llconv {
86 uint64_t ull;
87 uint8_t uc[8];
88 } *inp, outv;
89 inp = (union llconv *)((uint8_t *)base + offset);
90 outv.uc[0] = inp->uc[7];
91 outv.uc[1] = inp->uc[6];
92 outv.uc[2] = inp->uc[5];
93 outv.uc[3] = inp->uc[4];
94 outv.uc[4] = inp->uc[3];
95 outv.uc[5] = inp->uc[2];
96 outv.uc[6] = inp->uc[1];
97 outv.uc[7] = inp->uc[0];
98 return (outv.ull);
99 }
100
101 /* Functions for byte reversed stores. */
102
103 OS_INLINE
104 void
105 OSWriteSwapInt16(
106 volatile void * base,
107 uintptr_t offset,
108 uint16_t data
109 )
110 {
111 union sconv {
112 uint16_t us;
113 uint8_t uc[2];
114 } *inp, *outp;
115 inp = (union sconv *)((uint8_t *)base + offset);
116 outp = (union sconv *)&data;
117 outp->uc[0] = inp->uc[1];
118 outp->uc[1] = inp->uc[0];
119 }
120
121 OS_INLINE
122 void
123 OSWriteSwapInt32(
124 volatile void * base,
125 uintptr_t offset,
126 uint32_t data
127 )
128 {
129 union lconv {
130 uint32_t ul;
131 uint8_t uc[4];
132 } *inp, *outp;
133 inp = (union lconv *)((uint8_t *)base + offset);
134 outp = (union lconv *)&data;
135 outp->uc[0] = inp->uc[3];
136 outp->uc[1] = inp->uc[2];
137 outp->uc[2] = inp->uc[1];
138 outp->uc[3] = inp->uc[0];
139 }
140
141 OS_INLINE
142 void
143 OSWriteSwapInt64(
144 volatile void * base,
145 uintptr_t offset,
146 uint64_t data
147 )
148 {
149 union llconv {
150 uint64_t ull;
151 uint8_t uc[8];
152 } *inp, *outp;
153 inp = (union llconv *)((uint8_t *)base + offset);
154 outp = (union llconv *)&data;
155 outp->uc[0] = inp->uc[7];
156 outp->uc[1] = inp->uc[6];
157 outp->uc[2] = inp->uc[5];
158 outp->uc[3] = inp->uc[4];
159 outp->uc[4] = inp->uc[3];
160 outp->uc[5] = inp->uc[2];
161 outp->uc[6] = inp->uc[1];
162 outp->uc[7] = inp->uc[0];
163 }
164
165 /* Generic byte swapping functions. */
166
167 OS_INLINE
168 uint16_t
169 _OSSwapInt16(
170 uint16_t data
171 )
172 {
173 uint16_t temp = data;
174 return OSReadSwapInt16(&temp, 0);
175 }
176
177 OS_INLINE
178 uint32_t
179 _OSSwapInt32(
180 uint32_t data
181 )
182 {
183 uint32_t temp = data;
184 return OSReadSwapInt32(&temp, 0);
185 }
186
187 OS_INLINE
188 uint64_t
189 _OSSwapInt64(
190 uint64_t data
191 )
192 {
193 uint64_t temp = data;
194 return OSReadSwapInt64(&temp, 0);
195 }
196
197 #endif /* ! _OS_OSBYTEORDERMACHINE_H */