]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/machine/OSByteOrder.h
ac25ccee88a286075b04851f55aec27fb361497b
[apple/xnu.git] / libkern / libkern / machine / OSByteOrder.h
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
30 *
31 * HISTORY
32 *
33 */
34
35
36 #ifndef _OS_OSBYTEORDERMACHINE_H
37 #define _OS_OSBYTEORDERMACHINE_H
38
39 #include <stdint.h>
40
41 #if !defined(OS_INLINE)
42 # define OS_INLINE static inline
43 #endif
44
45 /* Functions for byte reversed loads. */
46
47 OS_INLINE
48 uint16_t
49 OSReadSwapInt16(
50 volatile void * base,
51 uintptr_t offset
52 )
53 {
54 union sconv {
55 uint16_t us;
56 uint8_t uc[2];
57 } *inp, outv;
58 inp = (union sconv *)((uint8_t *)base + offset);
59 outv.uc[0] = inp->uc[1];
60 outv.uc[1] = inp->uc[0];
61 return (outv.us);
62 }
63
64 OS_INLINE
65 uint32_t
66 OSReadSwapInt32(
67 volatile void * base,
68 uintptr_t offset
69 )
70 {
71 union lconv {
72 uint32_t ul;
73 uint8_t uc[4];
74 } *inp, outv;
75 inp = (union lconv *)((uint8_t *)base + offset);
76 outv.uc[0] = inp->uc[3];
77 outv.uc[1] = inp->uc[2];
78 outv.uc[2] = inp->uc[1];
79 outv.uc[3] = inp->uc[0];
80 return (outv.ul);
81 }
82
83 OS_INLINE
84 uint64_t
85 OSReadSwapInt64(
86 volatile void * base,
87 uintptr_t offset
88 )
89 {
90 union llconv {
91 uint64_t ull;
92 uint8_t uc[8];
93 } *inp, outv;
94 inp = (union llconv *)((uint8_t *)base + offset);
95 outv.uc[0] = inp->uc[7];
96 outv.uc[1] = inp->uc[6];
97 outv.uc[2] = inp->uc[5];
98 outv.uc[3] = inp->uc[4];
99 outv.uc[4] = inp->uc[3];
100 outv.uc[5] = inp->uc[2];
101 outv.uc[6] = inp->uc[1];
102 outv.uc[7] = inp->uc[0];
103 return (outv.ull);
104 }
105
106 /* Functions for byte reversed stores. */
107
108 OS_INLINE
109 void
110 OSWriteSwapInt16(
111 volatile void * base,
112 uintptr_t offset,
113 uint16_t data
114 )
115 {
116 union sconv {
117 uint16_t us;
118 uint8_t uc[2];
119 } *inp, *outp;
120 inp = (union sconv *)((uint8_t *)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 uintptr_t offset,
131 uint32_t data
132 )
133 {
134 union lconv {
135 uint32_t ul;
136 uint8_t uc[4];
137 } *inp, *outp;
138 inp = (union lconv *)((uint8_t *)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 uintptr_t offset,
151 uint64_t data
152 )
153 {
154 union llconv {
155 uint64_t ull;
156 uint8_t uc[8];
157 } *inp, *outp;
158 inp = (union llconv *)((uint8_t *)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 /* Generic byte swapping functions. */
171
172 OS_INLINE
173 uint16_t
174 _OSSwapInt16(
175 uint16_t data
176 )
177 {
178 uint16_t temp = data;
179 return OSReadSwapInt16(&temp, 0);
180 }
181
182 OS_INLINE
183 uint32_t
184 _OSSwapInt32(
185 uint32_t data
186 )
187 {
188 uint32_t temp = data;
189 return OSReadSwapInt32(&temp, 0);
190 }
191
192 OS_INLINE
193 uint64_t
194 _OSSwapInt64(
195 uint64_t data
196 )
197 {
198 uint64_t temp = data;
199 return OSReadSwapInt64(&temp, 0);
200 }
201
202 #endif /* ! _OS_OSBYTEORDERMACHINE_H */