]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/architecture/ppc/byte_order.h
xnu-792.6.61.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / architecture / ppc / byte_order.h
1 /*
2 * Copyright (c) 2000-2004 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) 1996 NeXT Software, Inc.
24 *
25 * Byte ordering conversion (for ppc).
26 */
27
28 static __inline__
29 unsigned short
30 NXSwapShort(
31 unsigned short inv
32 )
33 {
34 union sconv {
35 unsigned short us;
36 unsigned char uc[2];
37 } *inp, outv;
38
39 inp = (union sconv *)&inv;
40
41 outv.uc[0] = inp->uc[1];
42 outv.uc[1] = inp->uc[0];
43
44 return (outv.us);
45 }
46
47 static __inline__
48 unsigned int
49 NXSwapInt(
50 unsigned int inv
51 )
52 {
53 union iconv {
54 unsigned int ui;
55 unsigned char uc[4];
56 } *inp, outv;
57
58 inp = (union iconv *)&inv;
59
60 outv.uc[0] = inp->uc[3];
61 outv.uc[1] = inp->uc[2];
62 outv.uc[2] = inp->uc[1];
63 outv.uc[3] = inp->uc[0];
64
65 return (outv.ui);
66 }
67
68 static __inline__
69 unsigned long long
70 NXSwapLongLong(
71 unsigned long long inv
72 )
73 {
74 union llconv {
75 unsigned long long ull;
76 unsigned char uc[8];
77 } *inp, outv;
78
79 inp = (union llconv *)&inv;
80
81 outv.uc[0] = inp->uc[7];
82 outv.uc[1] = inp->uc[6];
83 outv.uc[2] = inp->uc[5];
84 outv.uc[3] = inp->uc[4];
85 outv.uc[4] = inp->uc[3];
86 outv.uc[5] = inp->uc[2];
87 outv.uc[6] = inp->uc[1];
88 outv.uc[7] = inp->uc[0];
89
90 return (outv.ull);
91 }
92
93 #if defined(__LP64__)
94
95 static __inline__
96 unsigned long
97 NXSwapLong(
98 unsigned long inv
99 )
100 {
101 union llconv {
102 unsigned long ul;
103 unsigned char uc[8];
104 } *inp, outv;
105
106 inp = (union llconv *)&inv;
107
108 outv.uc[0] = inp->uc[7];
109 outv.uc[1] = inp->uc[6];
110 outv.uc[2] = inp->uc[5];
111 outv.uc[3] = inp->uc[4];
112 outv.uc[4] = inp->uc[3];
113 outv.uc[5] = inp->uc[2];
114 outv.uc[6] = inp->uc[1];
115 outv.uc[7] = inp->uc[0];
116
117 return (outv.ul);
118 }
119
120 #else
121
122 static __inline__
123 unsigned long
124 NXSwapLong(
125 unsigned long inv
126 )
127 {
128 union lconv {
129 unsigned long ul;
130 unsigned char uc[4];
131 } *inp, outv;
132
133 inp = (union lconv *)&inv;
134
135 outv.uc[0] = inp->uc[3];
136 outv.uc[1] = inp->uc[2];
137 outv.uc[2] = inp->uc[1];
138 outv.uc[3] = inp->uc[0];
139
140 return (outv.ul);
141 }
142
143 #endif /* __LP64__ */
144
145 #ifndef KERNEL
146
147 static __inline__ NXSwappedFloat
148 NXConvertHostFloatToSwapped(float x)
149 {
150 union fconv {
151 float number;
152 NXSwappedFloat sf;
153 };
154 return ((union fconv *)&x)->sf;
155 }
156
157 static __inline__ float
158 NXConvertSwappedFloatToHost(NXSwappedFloat x)
159 {
160 union fconv {
161 float number;
162 NXSwappedFloat sf;
163 };
164 return ((union fconv *)&x)->number;
165 }
166
167 static __inline__ NXSwappedDouble
168 NXConvertHostDoubleToSwapped(double x)
169 {
170 union dconv {
171 double number;
172 NXSwappedDouble sd;
173 };
174 return ((union dconv *)&x)->sd;
175 }
176
177 static __inline__ double
178 NXConvertSwappedDoubleToHost(NXSwappedDouble x)
179 {
180 union dconv {
181 double number;
182 NXSwappedDouble sd;
183 };
184 return ((union dconv *)&x)->number;
185 }
186
187 static __inline__ NXSwappedFloat
188 NXSwapFloat(NXSwappedFloat x)
189 {
190 return NXSwapLong(x);
191 }
192
193 static __inline__ NXSwappedDouble
194 NXSwapDouble(NXSwappedDouble x)
195 {
196 return NXSwapLongLong(x);
197 }
198
199 #endif /* ! KERNEL */