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