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