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