]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/machine/OSByteOrder.h
97f2a6fbd148d097af70f3b0c467befbc70a24f6
[apple/xnu.git] / libkern / libkern / machine / OSByteOrder.h
1 /*
2 * Copyright (c) 2000-2005 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 #ifndef _OS_OSBYTEORDERMACHINE_H
24 #define _OS_OSBYTEORDERMACHINE_H
25
26 #include <stdint.h>
27
28 #if !defined(OS_INLINE)
29 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
30 # define OS_INLINE static inline
31 # elif defined(__MWERKS__) || defined(__cplusplus)
32 # define OS_INLINE static inline
33 # else
34 # define OS_INLINE static __inline__
35 # endif
36 #endif
37
38 /* Generic byte swapping functions. */
39
40 OS_INLINE
41 uint16_t
42 _OSSwapInt16(
43 uint16_t data
44 )
45 {
46 return OSSwapConstInt16(data);
47 }
48
49 OS_INLINE
50 uint32_t
51 _OSSwapInt32(
52 uint32_t data
53 )
54 {
55 return OSSwapConstInt32(data);
56 }
57
58 OS_INLINE
59 uint64_t
60 _OSSwapInt64(
61 uint64_t data
62 )
63 {
64 return OSSwapConstInt64(data);
65 }
66
67 /* Functions for byte reversed loads. */
68
69 OS_INLINE
70 uint16_t
71 OSReadSwapInt16(
72 const volatile void * base,
73 uintptr_t byteOffset
74 )
75 {
76 uint16_t data = *(volatile uint16_t *)((uintptr_t)base + byteOffset);
77 return _OSSwapInt16(data);
78 }
79
80 OS_INLINE
81 uint32_t
82 OSReadSwapInt32(
83 const volatile void * base,
84 uintptr_t byteOffset
85 )
86 {
87 uint32_t data = *(volatile uint32_t *)((uintptr_t)base + byteOffset);
88 return _OSSwapInt32(data);
89 }
90
91 OS_INLINE
92 uint64_t
93 OSReadSwapInt64(
94 const volatile void * base,
95 uintptr_t byteOffset
96 )
97 {
98 uint64_t data = *(volatile uint64_t *)((uintptr_t)base + byteOffset);
99 return _OSSwapInt64(data);
100 }
101
102 /* Functions for byte reversed stores. */
103
104 OS_INLINE
105 void
106 OSWriteSwapInt16(
107 volatile void * base,
108 uintptr_t byteOffset,
109 uint16_t data
110 )
111 {
112 *(volatile uint16_t *)((uintptr_t)base + byteOffset) = _OSSwapInt16(data);
113 }
114
115 OS_INLINE
116 void
117 OSWriteSwapInt32(
118 volatile void * base,
119 uintptr_t byteOffset,
120 uint32_t data
121 )
122 {
123 *(volatile uint32_t *)((uintptr_t)base + byteOffset) = _OSSwapInt32(data);
124 }
125
126 OS_INLINE
127 void
128 OSWriteSwapInt64(
129 volatile void * base,
130 uintptr_t byteOffset,
131 uint64_t data
132 )
133 {
134 *(volatile uint64_t *)((uintptr_t)base + byteOffset) = _OSSwapInt64(data);
135 }
136
137 #endif /* ! _OS_OSBYTEORDERMACHINE_H */