]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/i386/OSByteOrder.h
5783957f00de7e843f4c43b05bf4dc3c31fe3de9
[apple/xnu.git] / libkern / libkern / i386 / OSByteOrder.h
1 /*
2 * Copyright (c) 1999-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_OSBYTEORDERI386_H
24 #define _OS_OSBYTEORDERI386_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 ((data << 8) | (data >> 8));
47 }
48
49 OS_INLINE
50 uint32_t
51 _OSSwapInt32(
52 uint32_t data
53 )
54 {
55 __asm__ ("bswap %0" : "+r" (data));
56 return data;
57 }
58
59 #if defined(__i386__)
60 OS_INLINE
61 uint64_t
62 _OSSwapInt64(
63 uint64_t data
64 )
65 {
66 __asm__ ("bswap %%eax\n\t"
67 "bswap %%edx\n\t"
68 "xchgl %%eax, %%edx"
69 : "+A" (data));
70 return data;
71 }
72 #elif defined(__x86_64__)
73 OS_INLINE
74 uint64_t
75 _OSSwapInt64(
76 uint64_t data
77 )
78 {
79 __asm__ ("bswap %0" : "+r" (data));
80 return data;
81 }
82 #else
83 #error Unknown architecture
84 #endif
85
86 /* Functions for byte reversed loads. */
87
88 OS_INLINE
89 uint16_t
90 OSReadSwapInt16(
91 const volatile void * base,
92 uintptr_t byteOffset
93 )
94 {
95 uint16_t result;
96
97 result = *(volatile uint16_t *)((uintptr_t)base + byteOffset);
98 return _OSSwapInt16(result);
99 }
100
101 OS_INLINE
102 uint32_t
103 OSReadSwapInt32(
104 const volatile void * base,
105 uintptr_t byteOffset
106 )
107 {
108 uint32_t result;
109
110 result = *(volatile uint32_t *)((uintptr_t)base + byteOffset);
111 return _OSSwapInt32(result);
112 }
113
114 OS_INLINE
115 uint64_t
116 OSReadSwapInt64(
117 const volatile void * base,
118 uintptr_t byteOffset
119 )
120 {
121 uint64_t result;
122
123 result = *(volatile uint64_t *)((uintptr_t)base + byteOffset);
124 return _OSSwapInt64(result);
125 }
126
127 /* Functions for byte reversed stores. */
128
129 OS_INLINE
130 void
131 OSWriteSwapInt16(
132 volatile void * base,
133 uintptr_t byteOffset,
134 uint16_t data
135 )
136 {
137 *(volatile uint16_t *)((uintptr_t)base + byteOffset) = _OSSwapInt16(data);
138 }
139
140 OS_INLINE
141 void
142 OSWriteSwapInt32(
143 volatile void * base,
144 uintptr_t byteOffset,
145 uint32_t data
146 )
147 {
148 *(volatile uint32_t *)((uintptr_t)base + byteOffset) = _OSSwapInt32(data);
149 }
150
151 OS_INLINE
152 void
153 OSWriteSwapInt64(
154 volatile void * base,
155 uintptr_t byteOffset,
156 uint64_t data
157 )
158 {
159 *(volatile uint64_t *)((uintptr_t)base + byteOffset) = _OSSwapInt64(data);
160 }
161
162 #endif /* ! _OS_OSBYTEORDERI386_H */