]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/ppc/OSByteOrder.h
54b69b6a3ed67b0d7008f303ed8a1b92b5d16c05
[apple/xnu.git] / libkern / libkern / ppc / OSByteOrder.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
25 *
26 * HISTORY
27 *
28 */
29
30
31 #ifndef _OS_OSBYTEORDERPPC_H
32 #define _OS_OSBYTEORDERPPC_H
33
34 #include <stdint.h>
35
36 #if !defined(OS_INLINE)
37 # define OS_INLINE static inline
38 #endif
39
40 /* Functions for byte reversed loads. */
41
42 OS_INLINE
43 uint16_t
44 OSReadSwapInt16(
45 const volatile void * base,
46 uintptr_t offset
47 )
48 {
49 uint16_t result;
50 __asm__ volatile("lhbrx %0, %1, %2"
51 : "=r" (result)
52 : "b%" (base), "r" (offset)
53 : "memory");
54 return result;
55 }
56
57 OS_INLINE
58 uint32_t
59 OSReadSwapInt32(
60 const volatile void * base,
61 uintptr_t offset
62 )
63 {
64 uint32_t result;
65 __asm__ volatile("lwbrx %0, %1, %2"
66 : "=r" (result)
67 : "b%" (base), "r" (offset)
68 : "memory");
69 return result;
70 }
71
72 OS_INLINE
73 uint64_t
74 OSReadSwapInt64(
75 const volatile void * base,
76 uintptr_t offset
77 )
78 {
79 const volatile uint64_t * inp;
80 union ullc {
81 uint64_t ull;
82 uint32_t ul[2];
83 } outv;
84
85 inp = (const volatile uint64_t *)base;
86 outv.ul[0] = OSReadSwapInt32(inp, offset + 4);
87 outv.ul[1] = OSReadSwapInt32(inp, offset);
88 return outv.ull;
89 }
90
91 /* Functions for byte reversed stores. */
92
93 OS_INLINE
94 void
95 OSWriteSwapInt16(
96 volatile void * base,
97 uintptr_t offset,
98 uint16_t data
99 )
100 {
101 __asm__ volatile("sthbrx %0, %1, %2"
102 :
103 : "r" (data), "b%" (base), "r" (offset)
104 : "memory");
105 }
106
107 OS_INLINE
108 void
109 OSWriteSwapInt32(
110 volatile void * base,
111 uintptr_t offset,
112 uint32_t data
113 )
114 {
115 __asm__ volatile("stwbrx %0, %1, %2"
116 :
117 : "r" (data), "b%" (base), "r" (offset)
118 : "memory" );
119 }
120
121 OS_INLINE
122 void
123 OSWriteSwapInt64(
124 volatile void * base,
125 uintptr_t offset,
126 uint64_t data
127 )
128 {
129 volatile uint64_t * outp;
130 volatile union ullc {
131 uint64_t ull;
132 uint32_t ul[2];
133 } *inp;
134
135 outp = (volatile uint64_t *)base;
136 inp = (volatile union ullc *)&data;
137 OSWriteSwapInt32(outp, offset, inp->ul[1]);
138 OSWriteSwapInt32(outp, offset + 4, inp->ul[0]);
139 }
140
141 /* Generic byte swapping functions. */
142
143 OS_INLINE
144 uint16_t
145 _OSSwapInt16(
146 uint16_t data
147 )
148 {
149 uint16_t temp = data;
150 return OSReadSwapInt16(&temp, 0);
151 }
152
153 OS_INLINE
154 uint32_t
155 _OSSwapInt32(
156 uint32_t data
157 )
158 {
159 uint32_t temp = data;
160 return OSReadSwapInt32(&temp, 0);
161 }
162
163 OS_INLINE
164 uint64_t
165 _OSSwapInt64(
166 uint64_t data
167 )
168 {
169 uint64_t temp = data;
170 return OSReadSwapInt64(&temp, 0);
171 }
172
173 #endif /* ! _OS_OSBYTEORDERPPC_H */