]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/i386/OSByteOrder.h
49336d991331d10843c7ceaf8d31a8d28ba3d946
[apple/xnu.git] / libkern / libkern / i386 / OSByteOrder.h
1 /*
2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _OS_OSBYTEORDERI386_H
30 #define _OS_OSBYTEORDERI386_H
31
32 #include <stdint.h>
33
34 #if !defined(OS_INLINE)
35 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
36 # define OS_INLINE static inline
37 # elif defined(__MWERKS__) || defined(__cplusplus)
38 # define OS_INLINE static inline
39 # else
40 # define OS_INLINE static __inline__
41 # endif
42 #endif
43
44 /* Generic byte swapping functions. */
45
46 OS_INLINE
47 uint16_t
48 _OSSwapInt16(
49 uint16_t data
50 )
51 {
52 return ((data << 8) | (data >> 8));
53 }
54
55 OS_INLINE
56 uint32_t
57 _OSSwapInt32(
58 uint32_t data
59 )
60 {
61 __asm__ ("bswap %0" : "+r" (data));
62 return data;
63 }
64
65 #if defined(__i386__)
66 OS_INLINE
67 uint64_t
68 _OSSwapInt64(
69 uint64_t data
70 )
71 {
72 __asm__ ("bswap %%eax\n\t"
73 "bswap %%edx\n\t"
74 "xchgl %%eax, %%edx"
75 : "+A" (data));
76 return data;
77 }
78 #elif defined(__x86_64__)
79 OS_INLINE
80 uint64_t
81 _OSSwapInt64(
82 uint64_t data
83 )
84 {
85 __asm__ ("bswap %0" : "+r" (data));
86 return data;
87 }
88 #else
89 #error Unknown architecture
90 #endif
91
92 /* Functions for byte reversed loads. */
93
94 OS_INLINE
95 uint16_t
96 OSReadSwapInt16(
97 const volatile void * base,
98 uintptr_t byteOffset
99 )
100 {
101 uint16_t result;
102
103 result = *(volatile uint16_t *)((uintptr_t)base + byteOffset);
104 return _OSSwapInt16(result);
105 }
106
107 OS_INLINE
108 uint32_t
109 OSReadSwapInt32(
110 const volatile void * base,
111 uintptr_t byteOffset
112 )
113 {
114 uint32_t result;
115
116 result = *(volatile uint32_t *)((uintptr_t)base + byteOffset);
117 return _OSSwapInt32(result);
118 }
119
120 OS_INLINE
121 uint64_t
122 OSReadSwapInt64(
123 const volatile void * base,
124 uintptr_t byteOffset
125 )
126 {
127 uint64_t result;
128
129 result = *(volatile uint64_t *)((uintptr_t)base + byteOffset);
130 return _OSSwapInt64(result);
131 }
132
133 /* Functions for byte reversed stores. */
134
135 OS_INLINE
136 void
137 OSWriteSwapInt16(
138 volatile void * base,
139 uintptr_t byteOffset,
140 uint16_t data
141 )
142 {
143 *(volatile uint16_t *)((uintptr_t)base + byteOffset) = _OSSwapInt16(data);
144 }
145
146 OS_INLINE
147 void
148 OSWriteSwapInt32(
149 volatile void * base,
150 uintptr_t byteOffset,
151 uint32_t data
152 )
153 {
154 *(volatile uint32_t *)((uintptr_t)base + byteOffset) = _OSSwapInt32(data);
155 }
156
157 OS_INLINE
158 void
159 OSWriteSwapInt64(
160 volatile void * base,
161 uintptr_t byteOffset,
162 uint64_t data
163 )
164 {
165 *(volatile uint64_t *)((uintptr_t)base + byteOffset) = _OSSwapInt64(data);
166 }
167
168 #endif /* ! _OS_OSBYTEORDERI386_H */