]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSByteOrder.h
b0fd3dabc52f37f91ffe4d3d66a2d00b3a19b8e8
[apple/xnu.git] / libkern / libkern / OSByteOrder.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
27 *
28 * HISTORY
29 *
30 */
31
32 #ifndef _OS_OSBYTEORDER_H
33 #define _OS_OSBYTEORDER_H
34
35 #include <libkern/OSTypes.h>
36
37 #if defined(__ppc__)
38 #include <libkern/ppc/OSByteOrder.h>
39 #elif defined(__i386__)
40 #include <libkern/i386/OSByteOrder.h>
41 #else
42 #include <libkern/machine/OSByteOrder.h>
43 #endif
44
45 enum {
46 OSUnknownByteOrder,
47 OSLittleEndian,
48 OSBigEndian
49 };
50
51 OS_INLINE
52 UInt32
53 OSHostByteOrder(void) {
54 UInt32 x = (OSBigEndian << 24) | OSLittleEndian;
55 return (UInt32)*((UInt8 *)&x);
56 }
57
58 /* Macros for swapping constant values in the preprocessing stage. */
59 #define OSSwapConstInt16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
60
61 #define OSSwapConstInt32(x) ((OSSwapConstInt16(x) << 16) | \
62 (OSSwapConstInt16((x) >> 16)))
63
64 #define OSSwapConstInt64(x) ((OSSwapConstInt32(x) << 32) | \
65 (OSSwapConstInt32((x) >> 32)))
66
67 #if defined(__BIG_ENDIAN__)
68
69 /* Functions for loading big endian to host endianess. */
70
71 OS_INLINE
72 UInt
73 OSReadBigInt(
74 volatile void * base,
75 UInt offset
76 )
77 {
78 return *(volatile UInt *)((UInt8 *)base + offset);
79 }
80
81 OS_INLINE
82 UInt16
83 OSReadBigInt16(
84 volatile void * base,
85 UInt offset
86 )
87 {
88 return *(volatile UInt16 *)((UInt8 *)base + offset);
89 }
90
91 OS_INLINE
92 UInt32
93 OSReadBigInt32(
94 volatile void * base,
95 UInt offset
96 )
97 {
98 return *(volatile UInt32 *)((UInt8 *)base + offset);
99 }
100
101 OS_INLINE
102 UInt64
103 OSReadBigInt64(
104 volatile void * base,
105 UInt offset
106 )
107 {
108 return *(volatile UInt64 *)((UInt8 *)base + offset);
109 }
110
111 /* Functions for storing host endianess to big endian. */
112
113 OS_INLINE
114 void
115 OSWriteBigInt(
116 volatile void * base,
117 UInt offset,
118 UInt data
119 )
120 {
121 *(volatile UInt *)((UInt8 *)base + offset) = data;
122 }
123
124 OS_INLINE
125 void
126 OSWriteBigInt16(
127 volatile void * base,
128 UInt offset,
129 UInt16 data
130 )
131 {
132 *(volatile UInt16 *)((UInt8 *)base + offset) = data;
133 }
134
135 OS_INLINE
136 void
137 OSWriteBigInt32(
138 volatile void * base,
139 UInt offset,
140 UInt32 data
141 )
142 {
143 *(volatile UInt32 *)((UInt8 *)base + offset) = data;
144 }
145
146 OS_INLINE
147 void
148 OSWriteBigInt64(
149 volatile void * base,
150 UInt offset,
151 UInt64 data
152 )
153 {
154 *(volatile UInt64 *)((UInt8 *)base + offset) = data;
155 }
156
157 /* Functions for loading little endian to host endianess. */
158
159 OS_INLINE
160 UInt
161 OSReadLittleInt(
162 volatile void * base,
163 UInt offset
164 )
165 {
166 return OSReadSwapInt(base, offset);
167 }
168
169 OS_INLINE
170 UInt16
171 OSReadLittleInt16(
172 volatile void * base,
173 UInt offset
174 )
175 {
176 return OSReadSwapInt16(base, offset);
177 }
178
179 OS_INLINE
180 UInt32
181 OSReadLittleInt32(
182 volatile void * base,
183 UInt offset
184 )
185 {
186 return OSReadSwapInt32(base, offset);
187 }
188
189 OS_INLINE
190 UInt64
191 OSReadLittleInt64(
192 volatile void * base,
193 UInt offset
194 )
195 {
196 return OSReadSwapInt64(base, offset);
197 }
198
199 /* Functions for storing host endianess to little endian. */
200
201 OS_INLINE
202 void
203 OSWriteLittleInt(
204 volatile void * base,
205 UInt offset,
206 UInt data
207 )
208 {
209 OSWriteSwapInt(base, offset, data);
210 }
211
212 OS_INLINE
213 void
214 OSWriteLittleInt16(
215 volatile void * base,
216 UInt offset,
217 UInt16 data
218 )
219 {
220 OSWriteSwapInt16(base, offset, data);
221 }
222
223 OS_INLINE
224 void
225 OSWriteLittleInt32(
226 volatile void * base,
227 UInt offset,
228 UInt32 data
229 )
230 {
231 OSWriteSwapInt32(base, offset, data);
232 }
233
234 OS_INLINE
235 void
236 OSWriteLittleInt64(
237 volatile void * base,
238 UInt offset,
239 UInt64 data
240 )
241 {
242 OSWriteSwapInt64(base, offset, data);
243 }
244
245 /* Host endianess to big endian byte swapping macros for constants. */
246
247 #define OSSwapHostToBigConstInt16(x) (x)
248 #define OSSwapHostToBigConstInt32(x) (x)
249 #define OSSwapHostToBigConstInt64(x) (x)
250
251 /* Generic host endianess to big endian byte swapping functions. */
252
253 OS_INLINE
254 UInt
255 OSSwapHostToBigInt(
256 UInt data
257 )
258 {
259 return data;
260 }
261
262 OS_INLINE
263 UInt16
264 OSSwapHostToBigInt16(
265 UInt16 data
266 )
267 {
268 return data;
269 }
270
271 OS_INLINE
272 UInt32
273 OSSwapHostToBigInt32(
274 UInt32 data
275 )
276 {
277 return data;
278 }
279
280 OS_INLINE
281 UInt64
282 OSSwapHostToBigInt64(
283 UInt64 data
284 )
285 {
286 return data;
287 }
288
289 /* Host endianess to little endian byte swapping macros for constants. */
290
291 #define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x)
292 #define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x)
293 #define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x)
294
295 /* Generic host endianess to little endian byte swapping functions. */
296
297 OS_INLINE
298 UInt
299 OSSwapHostToLittleInt(
300 UInt data
301 )
302 {
303 return OSSwapInt(data);
304 }
305
306 OS_INLINE
307 UInt16
308 OSSwapHostToLittleInt16(
309 UInt16 data
310 )
311 {
312 return OSSwapInt16(data);
313 }
314
315 OS_INLINE
316 UInt32
317 OSSwapHostToLittleInt32(
318 UInt32 data
319 )
320 {
321 return OSSwapInt32(data);
322 }
323
324 OS_INLINE
325 UInt64
326 OSSwapHostToLittleInt64(
327 UInt64 data
328 )
329 {
330 return OSSwapInt64(data);
331 }
332
333 /* Big endian to host endianess byte swapping macros for constants. */
334
335 #define OSSwapBigToHostConstInt16(x) (x)
336 #define OSSwapBigToHostConstInt32(x) (x)
337 #define OSSwapBigToHostConstInt64(x) (x)
338
339 /* Generic big endian to host endianess byte swapping functions. */
340
341 OS_INLINE
342 UInt
343 OSSwapBigToHostInt(
344 UInt data
345 )
346 {
347 return data;
348 }
349
350 OS_INLINE
351 UInt16
352 OSSwapBigToHostInt16(
353 UInt16 data
354 )
355 {
356 return data;
357 }
358
359 OS_INLINE
360 UInt32
361 OSSwapBigToHostInt32(
362 UInt32 data
363 )
364 {
365 return data;
366 }
367
368 OS_INLINE
369 UInt64
370 OSSwapBigToHostInt64(
371 UInt64 data
372 )
373 {
374 return data;
375 }
376
377 /* Little endian to host endianess byte swapping macros for constants. */
378
379 #define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x)
380 #define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x)
381 #define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x)
382
383 /* Generic little endian to host endianess byte swapping functions. */
384
385 OS_INLINE
386 UInt
387 OSSwapLittleToHostInt(
388 UInt data
389 )
390 {
391 return OSSwapInt(data);
392 }
393
394 OS_INLINE
395 UInt16
396 OSSwapLittleToHostInt16(
397 UInt16 data
398 )
399 {
400 return OSSwapInt16(data);
401 }
402
403 OS_INLINE
404 UInt32
405 OSSwapLittleToHostInt32(
406 UInt32 data
407 )
408 {
409 return OSSwapInt32(data);
410 }
411
412 OS_INLINE
413 UInt64
414 OSSwapLittleToHostInt64(
415 UInt64 data
416 )
417 {
418 return OSSwapInt64(data);
419 }
420
421 #elif defined(__LITTLE_ENDIAN__)
422
423 /* Functions for loading big endian to host endianess. */
424
425 OS_INLINE
426 UInt
427 OSReadBigInt(
428 volatile void * base,
429 UInt offset
430 )
431 {
432 return OSReadSwapInt(base, offset);
433 }
434
435 OS_INLINE
436 UInt16
437 OSReadBigInt16(
438 volatile void * base,
439 UInt offset
440 )
441 {
442 return OSReadSwapInt16(base, offset);
443 }
444
445 OS_INLINE
446 UInt32
447 OSReadBigInt32(
448 volatile void * base,
449 UInt offset
450 )
451 {
452 return OSReadSwapInt32(base, offset);
453 }
454
455 OS_INLINE
456 UInt64
457 OSReadBigInt64(
458 volatile void * base,
459 UInt offset
460 )
461 {
462 return OSReadSwapInt64(base, offset);
463 }
464
465 /* Functions for storing host endianess to big endian. */
466
467 OS_INLINE
468 void
469 OSWriteBigInt(
470 volatile void * base,
471 UInt offset,
472 UInt data
473 )
474 {
475 OSWriteSwapInt(base, offset, data);
476 }
477
478 OS_INLINE
479 void
480 OSWriteBigInt16(
481 volatile void * base,
482 UInt offset,
483 UInt16 data
484 )
485 {
486 OSWriteSwapInt16(base, offset, data);
487 }
488
489 OS_INLINE
490 void
491 OSWriteBigInt32(
492 volatile void * base,
493 UInt offset,
494 UInt32 data
495 )
496 {
497 OSWriteSwapInt32(base, offset, data);
498 }
499
500 OS_INLINE
501 void
502 OSWriteBigInt64(
503 volatile void * base,
504 UInt offset,
505 UInt64 data
506 )
507 {
508 OSWriteSwapInt64(base, offset, data);
509 }
510
511 /* Functions for loading little endian to host endianess. */
512
513 OS_INLINE
514 UInt
515 OSReadLittleInt(
516 volatile void * base,
517 UInt offset
518 )
519 {
520 return *(volatile UInt *)((UInt8 *)base + offset);
521 }
522
523 OS_INLINE
524 UInt16
525 OSReadLittleInt16(
526 volatile void * base,
527 UInt offset
528 )
529 {
530 return *(volatile UInt16 *)((UInt8 *)base + offset);
531 }
532
533 OS_INLINE
534 UInt32
535 OSReadLittleInt32(
536 volatile void * base,
537 UInt offset
538 )
539 {
540 return *(volatile UInt32 *)((UInt8 *)base + offset);
541 }
542
543 OS_INLINE
544 UInt64
545 OSReadLittleInt64(
546 volatile void * base,
547 UInt offset
548 )
549 {
550 return *(volatile UInt64 *)((UInt8 *)base + offset);
551 }
552
553 /* Functions for storing host endianess to little endian. */
554
555 OS_INLINE
556 void
557 OSWriteLittleInt(
558 volatile void * base,
559 UInt offset,
560 UInt data
561 )
562 {
563 *(volatile UInt *)((UInt8 *)base + offset) = data;
564 }
565
566 OS_INLINE
567 void
568 OSWriteLittleInt16(
569 volatile void * base,
570 UInt offset,
571 UInt16 data
572 )
573 {
574 *(volatile UInt16 *)((UInt8 *)base + offset) = data;
575 }
576
577 OS_INLINE
578 void
579 OSWriteLittleInt32(
580 volatile void * base,
581 UInt offset,
582 UInt32 data
583 )
584 {
585 *(volatile UInt32 *)((UInt8 *)base + offset) = data;
586 }
587
588 OS_INLINE
589 void
590 OSWriteLittleInt64(
591 volatile void * base,
592 UInt offset,
593 UInt64 data
594 )
595 {
596 *(volatile UInt64 *)((UInt8 *)base + offset) = data;
597 }
598
599 /* Host endianess to big endian byte swapping macros for constants. */
600
601 #define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x)
602 #define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x)
603 #define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x)
604
605 /* Generic host endianess to big endian byte swapping functions. */
606
607 OS_INLINE
608 UInt
609 OSSwapHostToBigInt(
610 UInt data
611 )
612 {
613 return OSSwapInt(data);
614 }
615
616 OS_INLINE
617 UInt16
618 OSSwapHostToBigInt16(
619 UInt16 data
620 )
621 {
622 return OSSwapInt16(data);
623 }
624
625 OS_INLINE
626 UInt32
627 OSSwapHostToBigInt32(
628 UInt32 data
629 )
630 {
631 return OSSwapInt32(data);
632 }
633
634 OS_INLINE
635 UInt64
636 OSSwapHostToBigInt64(
637 UInt64 data
638 )
639 {
640 return OSSwapInt64(data);
641 }
642
643 /* Host endianess to little endian byte swapping macros for constants. */
644
645 #define OSSwapHostToLittleConstInt16(x) (x)
646 #define OSSwapHostToLittleConstInt32(x) (x)
647 #define OSSwapHostToLittleConstInt64(x) (x)
648
649 /* Generic host endianess to little endian byte swapping functions. */
650
651 OS_INLINE
652 UInt
653 OSSwapHostToLittleInt(
654 UInt data
655 )
656 {
657 return data;
658 }
659
660 OS_INLINE
661 UInt16
662 OSSwapHostToLittleInt16(
663 UInt16 data
664 )
665 {
666 return data;
667 }
668
669 OS_INLINE
670 UInt32
671 OSSwapHostToLittleInt32(
672 UInt32 data
673 )
674 {
675 return data;
676 }
677
678 OS_INLINE
679 UInt64
680 OSSwapHostToLittleInt64(
681 UInt64 data
682 )
683 {
684 return data;
685 }
686
687 /* Big endian to host endianess byte swapping macros for constants. */
688
689 #define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x)
690 #define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x)
691 #define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x)
692
693 /* Generic big endian to host endianess byte swapping functions. */
694
695 OS_INLINE
696 UInt
697 OSSwapBigToHostInt(
698 UInt data
699 )
700 {
701 return OSSwapInt(data);
702 }
703
704 OS_INLINE
705 UInt16
706 OSSwapBigToHostInt16(
707 UInt16 data
708 )
709 {
710 return OSSwapInt16(data);
711 }
712
713 OS_INLINE
714 UInt32
715 OSSwapBigToHostInt32(
716 UInt32 data
717 )
718 {
719 return OSSwapInt32(data);
720 }
721
722 OS_INLINE
723 UInt64
724 OSSwapBigToHostInt64(
725 UInt64 data
726 )
727 {
728 return OSSwapInt64(data);
729 }
730
731 /* Little endian to host endianess byte swapping macros for constants. */
732
733 #define OSSwapLittleToHostConstInt16(x) (x)
734 #define OSSwapLittleToHostConstInt32(x) (x)
735 #define OSSwapLittleToHostConstInt64(x) (x)
736
737 /* Generic little endian to host endianess byte swapping functions. */
738
739 OS_INLINE
740 UInt
741 OSSwapLittleToHostInt(
742 UInt data
743 )
744 {
745 return data;
746 }
747
748 OS_INLINE
749 UInt16
750 OSSwapLittleToHostInt16(
751 UInt16 data
752 )
753 {
754 return data;
755 }
756
757 OS_INLINE
758 UInt32
759 OSSwapLittleToHostInt32(
760 UInt32 data
761 )
762 {
763 return data;
764 }
765
766 OS_INLINE
767 UInt64
768 OSSwapLittleToHostInt64(
769 UInt64 data
770 )
771 {
772 return data;
773 }
774
775 #else
776 #error Unknown endianess.
777 #endif
778
779 #endif /* ! _OS_OSBYTEORDER_H */
780
781