1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#ifndef _OS_OSBYTEORDER_H
30#define _OS_OSBYTEORDER_H
31
32#include <stdint.h>
33
34#if defined(__GNUC__) && defined(__ppc__)
35#include <libkern/ppc/OSByteOrder.h>
36#elif defined(__GNUC__) && defined(__i386__)
37#include <libkern/i386/OSByteOrder.h>
38#else
39#include <libkern/machine/OSByteOrder.h>
40#endif
41
42enum {
43 OSUnknownByteOrder,
44 OSLittleEndian,
45 OSBigEndian
46};
47
48OS_INLINE
49int32_t
50OSHostByteOrder(void) {
51#if defined(__LITTLE_ENDIAN__)
52 return OSLittleEndian;
53#elif defined(__BIG_ENDIAN__)
54 return OSBigEndian;
55#else
56 return OSUnknownByteOrder;
57#endif
58}
59
60
61#define OSSwapConstInt16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \
62 (((uint16_t)(x) & 0x00ff) << 8))
63
64#define OSSwapConstInt32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \
65 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
66 (((uint32_t)(x) & 0x0000ff00) << 8) | \
67 (((uint32_t)(x) & 0x000000ff) << 24))
68
69#define OSSwapConstInt64(x) ((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
70 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
71 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
72 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
73 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
74 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
75 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
76 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))
77
78#if !defined(__GNUC__)
79#define __builtin_constant_p(x) (0)
80#endif
81
82#define OSSwapInt16(x) \
83 (__builtin_constant_p(x) ? OSSwapConstInt16(x) : _OSSwapInt16(x))
84
85#define OSSwapInt32(x) \
86 (__builtin_constant_p(x) ? OSSwapConstInt32(x) : _OSSwapInt32(x))
87
88#define OSSwapInt64(x) \
89 (__builtin_constant_p(x) ? OSSwapConstInt64(x) : _OSSwapInt64(x))
90
91#define OSReadBigInt(x, y) OSReadBigInt32(x, y)
92#define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z)
93#define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x)
94#define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x)
95#define OSReadLittleInt(x, y) OSReadLittleInt32(x, y)
96#define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z)
97#define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x)
98#define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x)
99
100#if defined(__BIG_ENDIAN__)
101
102
103
104OS_INLINE
105uint16_t
106OSReadBigInt16(
107 const volatile void * base,
108 uintptr_t offset
109)
110{
111 return *(volatile uint16_t *)((uintptr_t)base + offset);
112}
113
114OS_INLINE
115uint32_t
116OSReadBigInt32(
117 const volatile void * base,
118 uintptr_t offset
119)
120{
121 return *(volatile uint32_t *)((uintptr_t)base + offset);
122}
123
124OS_INLINE
125uint64_t
126OSReadBigInt64(
127 const volatile void * base,
128 uintptr_t offset
129)
130{
131 return *(volatile uint64_t *)((uintptr_t)base + offset);
132}
133
134
135
136OS_INLINE
137void
138OSWriteBigInt16(
139 volatile void * base,
140 uintptr_t offset,
141 uint16_t data
142)
143{
144 *(volatile uint16_t *)((uintptr_t)base + offset) = data;
145}
146
147OS_INLINE
148void
149OSWriteBigInt32(
150 volatile void * base,
151 uintptr_t offset,
152 uint32_t data
153)
154{
155 *(volatile uint32_t *)((uintptr_t)base + offset) = data;
156}
157
158OS_INLINE
159void
160OSWriteBigInt64(
161 volatile void * base,
162 uintptr_t offset,
163 uint64_t data
164)
165{
166 *(volatile uint64_t *)((uintptr_t)base + offset) = data;
167}
168
169
170
171OS_INLINE
172uint16_t
173OSReadLittleInt16(
174 volatile void * base,
175 uintptr_t offset
176)
177{
178 return OSReadSwapInt16(base, offset);
179}
180
181OS_INLINE
182uint32_t
183OSReadLittleInt32(
184 volatile void * base,
185 uintptr_t offset
186)
187{
188 return OSReadSwapInt32(base, offset);
189}
190
191OS_INLINE
192uint64_t
193OSReadLittleInt64(
194 volatile void * base,
195 uintptr_t offset
196)
197{
198 return OSReadSwapInt64(base, offset);
199}
200
201
202
203OS_INLINE
204void
205OSWriteLittleInt16(
206 volatile void * base,
207 uintptr_t offset,
208 uint16_t data
209)
210{
211 OSWriteSwapInt16(base, offset, data);
212}
213
214OS_INLINE
215void
216OSWriteLittleInt32(
217 volatile void * base,
218 uintptr_t offset,
219 uint32_t data
220)
221{
222 OSWriteSwapInt32(base, offset, data);
223}
224
225OS_INLINE
226void
227OSWriteLittleInt64(
228 volatile void * base,
229 uintptr_t offset,
230 uint64_t data
231)
232{
233 OSWriteSwapInt64(base, offset, data);
234}
235
236
237
238#define OSSwapHostToBigConstInt16(x) (x)
239#define OSSwapHostToBigConstInt32(x) (x)
240#define OSSwapHostToBigConstInt64(x) (x)
241
242
243
244OS_INLINE
245uint16_t
246OSSwapHostToBigInt16(
247 uint16_t data
248)
249{
250 return data;
251}
252
253OS_INLINE
254uint32_t
255OSSwapHostToBigInt32(
256 uint32_t data
257)
258{
259 return data;
260}
261
262OS_INLINE
263uint64_t
264OSSwapHostToBigInt64(
265 uint64_t data
266)
267{
268 return data;
269}
270
271
272
273#define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
274#define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
275#define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
276
277
278
279#define OSSwapHostToLittleInt16(x) OSSwapInt16(x)
280#define OSSwapHostToLittleInt32(x) OSSwapInt32(x)
281#define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
282
283
284
285#define OSSwapBigToHostConstInt16(x) (x)
286#define OSSwapBigToHostConstInt32(x) (x)
287#define OSSwapBigToHostConstInt64(x) (x)
288
289
290
291OS_INLINE
292uint16_t
293OSSwapBigToHostInt16(
294 uint16_t data
295)
296{
297 return data;
298}
299
300OS_INLINE
301uint32_t
302OSSwapBigToHostInt32(
303 uint32_t data
304)
305{
306 return data;
307}
308
309OS_INLINE
310uint64_t
311OSSwapBigToHostInt64(
312 uint64_t data
313)
314{
315 return data;
316}
317
318
319
320#define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
321#define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
322#define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
323
324
325
326#define OSSwapLittleToHostInt16(x) OSSwapInt16(x)
327#define OSSwapLittleToHostInt32(x) OSSwapInt32(x)
328#define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
329
330#elif defined(__LITTLE_ENDIAN__)
331
332
333
334OS_INLINE
335uint16_t
336OSReadBigInt16(
337 const volatile void * base,
338 uintptr_t offset
339)
340{
341 return OSReadSwapInt16(base, offset);
342}
343
344OS_INLINE
345uint32_t
346OSReadBigInt32(
347 const volatile void * base,
348 uintptr_t offset
349)
350{
351 return OSReadSwapInt32(base, offset);
352}
353
354OS_INLINE
355uint64_t
356OSReadBigInt64(
357 const volatile void * base,
358 uintptr_t offset
359)
360{
361 return OSReadSwapInt64(base, offset);
362}
363
364
365
366OS_INLINE
367void
368OSWriteBigInt16(
369 volatile void * base,
370 uintptr_t offset,
371 uint16_t data
372)
373{
374 OSWriteSwapInt16(base, offset, data);
375}
376
377OS_INLINE
378void
379OSWriteBigInt32(
380 volatile void * base,
381 uintptr_t offset,
382 uint32_t data
383)
384{
385 OSWriteSwapInt32(base, offset, data);
386}
387
388OS_INLINE
389void
390OSWriteBigInt64(
391 volatile void * base,
392 uintptr_t offset,
393 uint64_t data
394)
395{
396 OSWriteSwapInt64(base, offset, data);
397}
398
399
400
401OS_INLINE
402uint16_t
403OSReadLittleInt16(
404 const volatile void * base,
405 uintptr_t offset
406)
407{
408 return *(volatile uint16_t *)((uintptr_t)base + offset);
409}
410
411OS_INLINE
412uint32_t
413OSReadLittleInt32(
414 const volatile void * base,
415 uintptr_t offset
416)
417{
418 return *(volatile uint32_t *)((uintptr_t)base + offset);
419}
420
421OS_INLINE
422uint64_t
423OSReadLittleInt64(
424 const volatile void * base,
425 uintptr_t offset
426)
427{
428 return *(volatile uint64_t *)((uintptr_t)base + offset);
429}
430
431
432
433OS_INLINE
434void
435OSWriteLittleInt16(
436 volatile void * base,
437 uintptr_t offset,
438 uint16_t data
439)
440{
441 *(volatile uint16_t *)((uintptr_t)base + offset) = data;
442}
443
444OS_INLINE
445void
446OSWriteLittleInt32(
447 volatile void * base,
448 uintptr_t offset,
449 uint32_t data
450)
451{
452 *(volatile uint32_t *)((uintptr_t)base + offset) = data;
453}
454
455OS_INLINE
456void
457OSWriteLittleInt64(
458 volatile void * base,
459 uintptr_t offset,
460 uint64_t data
461)
462{
463 *(volatile uint64_t *)((uintptr_t)base + offset) = data;
464}
465
466
467
468#define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
469#define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
470#define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
471
472
473
474#define OSSwapHostToBigInt16(x) OSSwapInt16(x)
475#define OSSwapHostToBigInt32(x) OSSwapInt32(x)
476#define OSSwapHostToBigInt64(x) OSSwapInt64(x)
477
478
479
480#define OSSwapHostToLittleConstInt16(x) (x)
481#define OSSwapHostToLittleConstInt32(x) (x)
482#define OSSwapHostToLittleConstInt64(x) (x)
483
484
485
486OS_INLINE
487uint16_t
488OSSwapHostToLittleInt16(
489 uint16_t data
490)
491{
492 return data;
493}
494
495OS_INLINE
496uint32_t
497OSSwapHostToLittleInt32(
498 uint32_t data
499)
500{
501 return data;
502}
503
504OS_INLINE
505uint64_t
506OSSwapHostToLittleInt64(
507 uint64_t data
508)
509{
510 return data;
511}
512
513
514
515#define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
516#define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
517#define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
518
519
520
521#define OSSwapBigToHostInt16(x) OSSwapInt16(x)
522#define OSSwapBigToHostInt32(x) OSSwapInt32(x)
523#define OSSwapBigToHostInt64(x) OSSwapInt64(x)
524
525
526
527#define OSSwapLittleToHostConstInt16(x) (x)
528#define OSSwapLittleToHostConstInt32(x) (x)
529#define OSSwapLittleToHostConstInt64(x) (x)
530
531
532
533OS_INLINE
534uint16_t
535OSSwapLittleToHostInt16(
536 uint16_t data
537)
538{
539 return data;
540}
541
542OS_INLINE
543uint32_t
544OSSwapLittleToHostInt32(
545 uint32_t data
546)
547{
548 return data;
549}
550
551OS_INLINE
552uint64_t
553OSSwapLittleToHostInt64(
554 uint64_t data
555)
556{
557 return data;
558}
559
560#else
561#error Unknown endianess.
562#endif
563
564#endif
565
566
567