]>
Commit | Line | Data |
---|---|---|
5ba3f43e A |
1 | /* |
2 | * Copyright (c) 1999-2007 Apple Inc. All rights reserved. | |
3 | */ | |
4 | ||
5 | #ifndef _OS_OSBYTEORDERARM_H | |
6 | #define _OS_OSBYTEORDERARM_H | |
7 | ||
8 | #include <stdint.h> | |
9 | #include <arm/arch.h> /* for _ARM_ARCH_6 */ | |
5ba3f43e A |
10 | |
11 | /* Generic byte swapping functions. */ | |
12 | ||
f427ee49 | 13 | __DARWIN_OS_INLINE |
5ba3f43e A |
14 | uint16_t |
15 | _OSSwapInt16( | |
f427ee49 | 16 | uint16_t _data |
0a7de745 | 17 | ) |
5ba3f43e | 18 | { |
0a7de745 | 19 | /* Reduces to 'rev16' with clang */ |
f427ee49 | 20 | return (uint16_t)(_data << 8 | _data >> 8); |
5ba3f43e A |
21 | } |
22 | ||
f427ee49 | 23 | __DARWIN_OS_INLINE |
5ba3f43e A |
24 | uint32_t |
25 | _OSSwapInt32( | |
f427ee49 | 26 | uint32_t _data |
0a7de745 | 27 | ) |
5ba3f43e A |
28 | { |
29 | #if defined(__llvm__) | |
f427ee49 | 30 | _data = __builtin_bswap32(_data); |
5ba3f43e | 31 | #else |
0a7de745 | 32 | /* This actually generates the best code */ |
f427ee49 | 33 | _data = (((_data ^ (_data >> 16 | (_data << 16))) & 0xFF00FFFF) >> 8) ^ (_data >> 8 | _data << 24); |
5ba3f43e | 34 | #endif |
0a7de745 | 35 | |
f427ee49 | 36 | return _data; |
5ba3f43e A |
37 | } |
38 | ||
f427ee49 | 39 | __DARWIN_OS_INLINE |
5ba3f43e A |
40 | uint64_t |
41 | _OSSwapInt64( | |
f427ee49 | 42 | uint64_t _data |
0a7de745 | 43 | ) |
5ba3f43e A |
44 | { |
45 | #if defined(__llvm__) | |
f427ee49 | 46 | return __builtin_bswap64(_data); |
5ba3f43e | 47 | #else |
0a7de745 | 48 | union { |
f427ee49 A |
49 | uint64_t _ull; |
50 | uint32_t _ul[2]; | |
51 | } _u; | |
0a7de745 A |
52 | |
53 | /* This actually generates the best code */ | |
f427ee49 A |
54 | _u._ul[0] = (uint32_t)(_data >> 32); |
55 | _u._ul[1] = (uint32_t)(_data & 0xffffffff); | |
56 | _u._ul[0] = _OSSwapInt32(_u._ul[0]); | |
57 | _u._ul[1] = _OSSwapInt32(_u._ul[1]); | |
58 | return _u._ull; | |
5ba3f43e A |
59 | #endif |
60 | } | |
61 | ||
62 | /* Functions for byte reversed loads. */ | |
63 | ||
f427ee49 A |
64 | struct _OSUnalignedU16 { |
65 | volatile uint16_t __val; | |
66 | } __attribute__((__packed__)); | |
67 | ||
68 | struct _OSUnalignedU32 { | |
69 | volatile uint32_t __val; | |
70 | } __attribute__((__packed__)); | |
71 | ||
72 | struct _OSUnalignedU64 { | |
73 | volatile uint64_t __val; | |
74 | } __attribute__((__packed__)); | |
75 | ||
76 | #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) | |
77 | __DARWIN_OS_INLINE | |
78 | uint16_t | |
79 | _OSReadSwapInt16( | |
80 | const volatile void * _base, | |
81 | uintptr_t _offset | |
82 | ) | |
83 | { | |
84 | return _OSSwapInt16(((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val); | |
85 | } | |
86 | #else | |
87 | __DARWIN_OS_INLINE | |
5ba3f43e A |
88 | uint16_t |
89 | OSReadSwapInt16( | |
f427ee49 A |
90 | const volatile void * _base, |
91 | uintptr_t _offset | |
0a7de745 | 92 | ) |
5ba3f43e | 93 | { |
f427ee49 | 94 | return _OSSwapInt16(((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val); |
5ba3f43e | 95 | } |
f427ee49 | 96 | #endif |
5ba3f43e | 97 | |
f427ee49 A |
98 | #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) |
99 | __DARWIN_OS_INLINE | |
100 | uint32_t | |
101 | _OSReadSwapInt32( | |
102 | const volatile void * _base, | |
103 | uintptr_t _offset | |
104 | ) | |
105 | { | |
106 | return _OSSwapInt32(((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val); | |
107 | } | |
108 | #else | |
109 | __DARWIN_OS_INLINE | |
5ba3f43e A |
110 | uint32_t |
111 | OSReadSwapInt32( | |
f427ee49 A |
112 | const volatile void * _base, |
113 | uintptr_t _offset | |
0a7de745 | 114 | ) |
5ba3f43e | 115 | { |
f427ee49 | 116 | return _OSSwapInt32(((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val); |
5ba3f43e | 117 | } |
f427ee49 | 118 | #endif |
5ba3f43e | 119 | |
f427ee49 A |
120 | #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) |
121 | __DARWIN_OS_INLINE | |
122 | uint64_t | |
123 | _OSReadSwapInt64( | |
124 | const volatile void * _base, | |
125 | uintptr_t _offset | |
126 | ) | |
127 | { | |
128 | return _OSSwapInt64(((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val); | |
129 | } | |
130 | #else | |
131 | __DARWIN_OS_INLINE | |
5ba3f43e A |
132 | uint64_t |
133 | OSReadSwapInt64( | |
f427ee49 A |
134 | const volatile void * _base, |
135 | uintptr_t _offset | |
0a7de745 | 136 | ) |
5ba3f43e | 137 | { |
f427ee49 | 138 | return _OSSwapInt64(((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val); |
5ba3f43e | 139 | } |
f427ee49 | 140 | #endif |
5ba3f43e A |
141 | |
142 | /* Functions for byte reversed stores. */ | |
143 | ||
f427ee49 A |
144 | #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) |
145 | __DARWIN_OS_INLINE | |
146 | void | |
147 | _OSWriteSwapInt16( | |
148 | volatile void * _base, | |
149 | uintptr_t _offset, | |
150 | uint16_t _data | |
151 | ) | |
152 | { | |
153 | ((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt16(_data); | |
154 | } | |
155 | #else | |
156 | __DARWIN_OS_INLINE | |
5ba3f43e A |
157 | void |
158 | OSWriteSwapInt16( | |
f427ee49 A |
159 | volatile void * _base, |
160 | uintptr_t _offset, | |
161 | uint16_t _data | |
0a7de745 | 162 | ) |
5ba3f43e | 163 | { |
f427ee49 | 164 | ((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt16(_data); |
5ba3f43e | 165 | } |
f427ee49 | 166 | #endif |
5ba3f43e | 167 | |
f427ee49 A |
168 | #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) |
169 | __DARWIN_OS_INLINE | |
170 | void | |
171 | _OSWriteSwapInt32( | |
172 | volatile void * _base, | |
173 | uintptr_t _offset, | |
174 | uint32_t _data | |
175 | ) | |
176 | { | |
177 | ((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt32(_data); | |
178 | } | |
179 | #else | |
180 | __DARWIN_OS_INLINE | |
5ba3f43e A |
181 | void |
182 | OSWriteSwapInt32( | |
f427ee49 A |
183 | volatile void * _base, |
184 | uintptr_t _offset, | |
185 | uint32_t _data | |
0a7de745 | 186 | ) |
5ba3f43e | 187 | { |
f427ee49 | 188 | ((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt32(_data); |
5ba3f43e | 189 | } |
f427ee49 | 190 | #endif |
5ba3f43e | 191 | |
f427ee49 A |
192 | #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) |
193 | __DARWIN_OS_INLINE | |
194 | void | |
195 | _OSWriteSwapInt64( | |
196 | volatile void * _base, | |
197 | uintptr_t _offset, | |
198 | uint64_t _data | |
199 | ) | |
200 | { | |
201 | ((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt64(_data); | |
202 | } | |
203 | #else | |
204 | __DARWIN_OS_INLINE | |
5ba3f43e A |
205 | void |
206 | OSWriteSwapInt64( | |
f427ee49 A |
207 | volatile void * _base, |
208 | uintptr_t _offset, | |
209 | uint64_t _data | |
0a7de745 | 210 | ) |
5ba3f43e | 211 | { |
f427ee49 | 212 | ((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt64(_data); |
5ba3f43e | 213 | } |
f427ee49 | 214 | #endif |
5ba3f43e A |
215 | |
216 | #endif /* ! _OS_OSBYTEORDERARM_H */ |