]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/i386/OSByteOrder.h
87b0a9b4810ce74bafa935b7b64c6f2d93c902dc
[apple/xnu.git] / libkern / libkern / i386 / OSByteOrder.h
1 /*
2 * Copyright (c) 1999-2003 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 #ifndef _OS_OSBYTEORDERI386_H
27 #define _OS_OSBYTEORDERI386_H
28
29 #include <stdint.h>
30
31 #if !defined(OS_INLINE)
32 # define OS_INLINE static inline
33 #endif
34
35 /* Generic byte swapping functions. */
36
37 OS_INLINE
38 uint16_t
39 _OSSwapInt16(
40 uint16_t data
41 )
42 {
43 __asm__ ("xchgb %b0, %h0" : "+q" (data));
44 return data;
45 }
46
47 OS_INLINE
48 uint32_t
49 _OSSwapInt32(
50 uint32_t data
51 )
52 {
53 __asm__ ("bswap %0" : "+r" (data));
54 return data;
55 }
56
57 OS_INLINE
58 uint64_t
59 _OSSwapInt64(
60 uint64_t data
61 )
62 {
63 union {
64 uint64_t ull;
65 uint32_t ul[2];
66 } u;
67
68 /* This actually generates the best code */
69 u.ul[0] = data >> 32;
70 u.ul[1] = data & 0xffffffff;
71 u.ul[0] = _OSSwapInt32(u.ul[0]);
72 u.ul[1] = _OSSwapInt32(u.ul[1]);
73 return u.ull;
74 }
75
76 /* Functions for byte reversed loads. */
77
78 OS_INLINE
79 uint16_t
80 OSReadSwapInt16(
81 volatile void * base,
82 uintptr_t offset
83 )
84 {
85 uint16_t result;
86
87 result = *(uint16_t *)((uintptr_t)base + offset);
88 return _OSSwapInt16(result);
89 }
90
91 OS_INLINE
92 uint32_t
93 OSReadSwapInt32(
94 volatile void * base,
95 uintptr_t offset
96 )
97 {
98 uint32_t result;
99
100 result = *(uint32_t *)((uintptr_t)base + offset);
101 return _OSSwapInt32(result);
102 }
103
104 OS_INLINE
105 uint64_t
106 OSReadSwapInt64(
107 volatile void * base,
108 uintptr_t offset
109 )
110 {
111 uint32_t * inp;
112 union ullc {
113 uint64_t ull;
114 uint32_t ul[2];
115 } outv;
116
117 inp = (uint32_t *)((uintptr_t)base + offset);
118 outv.ul[0] = inp[1];
119 outv.ul[1] = inp[0];
120 outv.ul[0] = _OSSwapInt32(outv.ul[0]);
121 outv.ul[1] = _OSSwapInt32(outv.ul[1]);
122 return outv.ull;
123 }
124
125 /* Functions for byte reversed stores. */
126
127 OS_INLINE
128 void
129 OSWriteSwapInt16(
130 volatile void * base,
131 uintptr_t offset,
132 uint16_t data
133 )
134 {
135 *(uint16_t *)((uintptr_t)base + offset) = _OSSwapInt16(data);
136 }
137
138 OS_INLINE
139 void
140 OSWriteSwapInt32(
141 volatile void * base,
142 uintptr_t offset,
143 uint32_t data
144 )
145 {
146 *(uint32_t *)((uintptr_t)base + offset) = _OSSwapInt32(data);
147 }
148
149 OS_INLINE
150 void
151 OSWriteSwapInt64(
152 volatile void * base,
153 uintptr_t offset,
154 uint64_t data
155 )
156 {
157 *(uint64_t *)((uintptr_t)base + offset) = _OSSwapInt64(data);
158 }
159
160 #endif /* ! _OS_OSBYTEORDERI386_H */