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