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