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