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