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