]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/machine/OSByteOrder.h
xnu-792.6.76.tar.gz
[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 * 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
30 #ifndef _OS_OSBYTEORDERMACHINE_H
31 #define _OS_OSBYTEORDERMACHINE_H
32
33 #include <stdint.h>
34
35 #if !defined(OS_INLINE)
36 # define OS_INLINE static inline
37 #endif
38
39 /* Functions for byte reversed loads. */
40
41 OS_INLINE
42 uint16_t
43 OSReadSwapInt16(
44 volatile void * base,
45 uintptr_t offset
46 )
47 {
48 union sconv {
49 uint16_t us;
50 uint8_t uc[2];
51 } *inp, outv;
52 inp = (union sconv *)((uint8_t *)base + offset);
53 outv.uc[0] = inp->uc[1];
54 outv.uc[1] = inp->uc[0];
55 return (outv.us);
56 }
57
58 OS_INLINE
59 uint32_t
60 OSReadSwapInt32(
61 volatile void * base,
62 uintptr_t offset
63 )
64 {
65 union lconv {
66 uint32_t ul;
67 uint8_t uc[4];
68 } *inp, outv;
69 inp = (union lconv *)((uint8_t *)base + offset);
70 outv.uc[0] = inp->uc[3];
71 outv.uc[1] = inp->uc[2];
72 outv.uc[2] = inp->uc[1];
73 outv.uc[3] = inp->uc[0];
74 return (outv.ul);
75 }
76
77 OS_INLINE
78 uint64_t
79 OSReadSwapInt64(
80 volatile void * base,
81 uintptr_t offset
82 )
83 {
84 union llconv {
85 uint64_t ull;
86 uint8_t uc[8];
87 } *inp, outv;
88 inp = (union llconv *)((uint8_t *)base + offset);
89 outv.uc[0] = inp->uc[7];
90 outv.uc[1] = inp->uc[6];
91 outv.uc[2] = inp->uc[5];
92 outv.uc[3] = inp->uc[4];
93 outv.uc[4] = inp->uc[3];
94 outv.uc[5] = inp->uc[2];
95 outv.uc[6] = inp->uc[1];
96 outv.uc[7] = inp->uc[0];
97 return (outv.ull);
98 }
99
100 /* Functions for byte reversed stores. */
101
102 OS_INLINE
103 void
104 OSWriteSwapInt16(
105 volatile void * base,
106 uintptr_t offset,
107 uint16_t data
108 )
109 {
110 union sconv {
111 uint16_t us;
112 uint8_t uc[2];
113 } *inp, *outp;
114 inp = (union sconv *)((uint8_t *)base + offset);
115 outp = (union sconv *)&data;
116 outp->uc[0] = inp->uc[1];
117 outp->uc[1] = inp->uc[0];
118 }
119
120 OS_INLINE
121 void
122 OSWriteSwapInt32(
123 volatile void * base,
124 uintptr_t offset,
125 uint32_t data
126 )
127 {
128 union lconv {
129 uint32_t ul;
130 uint8_t uc[4];
131 } *inp, *outp;
132 inp = (union lconv *)((uint8_t *)base + offset);
133 outp = (union lconv *)&data;
134 outp->uc[0] = inp->uc[3];
135 outp->uc[1] = inp->uc[2];
136 outp->uc[2] = inp->uc[1];
137 outp->uc[3] = inp->uc[0];
138 }
139
140 OS_INLINE
141 void
142 OSWriteSwapInt64(
143 volatile void * base,
144 uintptr_t offset,
145 uint64_t data
146 )
147 {
148 union llconv {
149 uint64_t ull;
150 uint8_t uc[8];
151 } *inp, *outp;
152 inp = (union llconv *)((uint8_t *)base + offset);
153 outp = (union llconv *)&data;
154 outp->uc[0] = inp->uc[7];
155 outp->uc[1] = inp->uc[6];
156 outp->uc[2] = inp->uc[5];
157 outp->uc[3] = inp->uc[4];
158 outp->uc[4] = inp->uc[3];
159 outp->uc[5] = inp->uc[2];
160 outp->uc[6] = inp->uc[1];
161 outp->uc[7] = inp->uc[0];
162 }
163
164 /* Generic byte swapping functions. */
165
166 OS_INLINE
167 uint16_t
168 _OSSwapInt16(
169 uint16_t data
170 )
171 {
172 uint16_t temp = data;
173 return OSReadSwapInt16(&temp, 0);
174 }
175
176 OS_INLINE
177 uint32_t
178 _OSSwapInt32(
179 uint32_t data
180 )
181 {
182 uint32_t temp = data;
183 return OSReadSwapInt32(&temp, 0);
184 }
185
186 OS_INLINE
187 uint64_t
188 _OSSwapInt64(
189 uint64_t data
190 )
191 {
192 uint64_t temp = data;
193 return OSReadSwapInt64(&temp, 0);
194 }
195
196 #endif /* ! _OS_OSBYTEORDERMACHINE_H */