| 1 | /* |
| 2 | * Copyright (c) 1999-2008 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_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. Please obtain a copy of the License at |
| 10 | * http://www.opensource.apple.com/apsl/ and read it before using this |
| 11 | * file. |
| 12 | * |
| 13 | * The Original Code and all software distributed under the License are |
| 14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 18 | * Please see the License for the specific language governing rights and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * @APPLE_LICENSE_HEADER_END@ |
| 22 | */ |
| 23 | /* |
| 24 | * Copyright (c) 1992 NeXT Computer, Inc. |
| 25 | * |
| 26 | * Byte ordering conversion. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #ifndef	_ARCHITECTURE_BYTE_ORDER_H_ |
| 31 | #define _ARCHITECTURE_BYTE_ORDER_H_ |
| 32 | |
| 33 | /* |
| 34 | * Please note that the byte ordering functions in this file are deprecated. |
| 35 | * A replacement API exists in libkern/OSByteOrder.h |
| 36 | */ |
| 37 | |
| 38 | #include <libkern/OSByteOrder.h> |
| 39 | |
| 40 | typedef unsigned long NXSwappedFloat; |
| 41 | typedef unsigned long long NXSwappedDouble; |
| 42 | |
| 43 | static __inline__ __attribute__((deprecated)) |
| 44 | unsigned short |
| 45 | NXSwapShort( |
| 46 | unsigned short inv |
| 47 | ) |
| 48 | { |
| 49 | return (unsigned short)OSSwapInt16((uint16_t)inv); |
| 50 | } |
| 51 | |
| 52 | static __inline__ __attribute__((deprecated)) |
| 53 | unsigned int |
| 54 | NXSwapInt( |
| 55 | unsigned int inv |
| 56 | ) |
| 57 | { |
| 58 | return (unsigned int)OSSwapInt32((uint32_t)inv); |
| 59 | } |
| 60 | |
| 61 | static __inline__ __attribute__((deprecated)) |
| 62 | unsigned long |
| 63 | NXSwapLong( |
| 64 | unsigned long inv |
| 65 | ) |
| 66 | { |
| 67 | return (unsigned long)OSSwapInt32((uint32_t)inv); |
| 68 | } |
| 69 | |
| 70 | static __inline__ __attribute__((deprecated)) |
| 71 | unsigned long long |
| 72 | NXSwapLongLong( |
| 73 | unsigned long long inv |
| 74 | ) |
| 75 | { |
| 76 | return (unsigned long long)OSSwapInt64((uint64_t)inv); |
| 77 | } |
| 78 | |
| 79 | static __inline__ __attribute__((deprecated)) |
| 80 | NXSwappedFloat |
| 81 | NXConvertHostFloatToSwapped(float x) |
| 82 | { |
| 83 | union fconv { |
| 84 | float number; |
| 85 | NXSwappedFloat sf; |
| 86 | } u; |
| 87 | u.number = x; |
| 88 | return u.sf; |
| 89 | } |
| 90 | |
| 91 | static __inline__ __attribute__((deprecated)) |
| 92 | float |
| 93 | NXConvertSwappedFloatToHost(NXSwappedFloat x) |
| 94 | { |
| 95 | union fconv { |
| 96 | float number; |
| 97 | NXSwappedFloat sf; |
| 98 | } u; |
| 99 | u.sf = x; |
| 100 | return u.number; |
| 101 | } |
| 102 | |
| 103 | static __inline__ __attribute__((deprecated)) |
| 104 | NXSwappedDouble |
| 105 | NXConvertHostDoubleToSwapped(double x) |
| 106 | { |
| 107 | union dconv { |
| 108 | double number; |
| 109 | NXSwappedDouble sd; |
| 110 | } u; |
| 111 | u.number = x; |
| 112 | return u.sd; |
| 113 | } |
| 114 | |
| 115 | static __inline__ __attribute__((deprecated)) |
| 116 | double |
| 117 | NXConvertSwappedDoubleToHost(NXSwappedDouble x) |
| 118 | { |
| 119 | union dconv { |
| 120 | double number; |
| 121 | NXSwappedDouble sd; |
| 122 | } u; |
| 123 | u.sd = x; |
| 124 | return u.number; |
| 125 | } |
| 126 | |
| 127 | static __inline__ __attribute__((deprecated)) |
| 128 | NXSwappedFloat |
| 129 | NXSwapFloat(NXSwappedFloat x) |
| 130 | { |
| 131 | return (NXSwappedFloat)OSSwapInt32((uint32_t)x); |
| 132 | } |
| 133 | |
| 134 | static __inline__ __attribute__((deprecated)) |
| 135 | NXSwappedDouble |
| 136 | NXSwapDouble(NXSwappedDouble x) |
| 137 | { |
| 138 | return (NXSwappedDouble)OSSwapInt64((uint64_t)x); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Identify the byte order |
| 143 | * of the current host. |
| 144 | */ |
| 145 | |
| 146 | enum NXByteOrder { |
| 147 | NX_UnknownByteOrder, |
| 148 | NX_LittleEndian, |
| 149 | NX_BigEndian |
| 150 | }; |
| 151 | |
| 152 | static __inline__ |
| 153 | enum NXByteOrder |
| 154 | NXHostByteOrder(void) |
| 155 | { |
| 156 | #if defined(__LITTLE_ENDIAN__) |
| 157 | return NX_LittleEndian; |
| 158 | #elif defined(__BIG_ENDIAN__) |
| 159 | return NX_BigEndian; |
| 160 | #else |
| 161 | return NX_UnknownByteOrder; |
| 162 | #endif |
| 163 | } |
| 164 | |
| 165 | static __inline__ __attribute__((deprecated)) |
| 166 | unsigned short |
| 167 | NXSwapBigShortToHost( |
| 168 | unsigned short	x |
| 169 | ) |
| 170 | { |
| 171 | return (unsigned short)OSSwapBigToHostInt16((uint16_t)x); |
| 172 | } |
| 173 | |
| 174 | static __inline__ __attribute__((deprecated)) |
| 175 | unsigned int |
| 176 | NXSwapBigIntToHost( |
| 177 | unsigned int	x |
| 178 | ) |
| 179 | { |
| 180 | return (unsigned int)OSSwapBigToHostInt32((uint32_t)x); |
| 181 | } |
| 182 | |
| 183 | static __inline__ __attribute__((deprecated)) |
| 184 | unsigned long |
| 185 | NXSwapBigLongToHost( |
| 186 | unsigned long	x |
| 187 | ) |
| 188 | { |
| 189 | return (unsigned long)OSSwapBigToHostInt32((uint32_t)x); |
| 190 | } |
| 191 | |
| 192 | static __inline__ __attribute__((deprecated)) |
| 193 | unsigned long long |
| 194 | NXSwapBigLongLongToHost( |
| 195 | unsigned long long	x |
| 196 | ) |
| 197 | { |
| 198 | return (unsigned long long)OSSwapBigToHostInt64((uint64_t)x); |
| 199 | } |
| 200 | |
| 201 | static __inline__ __attribute__((deprecated)) |
| 202 | double |
| 203 | NXSwapBigDoubleToHost( |
| 204 | NXSwappedDouble	x |
| 205 | ) |
| 206 | { |
| 207 | return NXConvertSwappedDoubleToHost((NXSwappedDouble)OSSwapBigToHostInt64((uint64_t)x)); |
| 208 | } |
| 209 | |
| 210 | static __inline__ __attribute__((deprecated)) |
| 211 | float |
| 212 | NXSwapBigFloatToHost( |
| 213 | NXSwappedFloat	x |
| 214 | ) |
| 215 | { |
| 216 | return NXConvertSwappedFloatToHost((NXSwappedFloat)OSSwapBigToHostInt32((uint32_t)x)); |
| 217 | } |
| 218 | |
| 219 | static __inline__ __attribute__((deprecated)) |
| 220 | unsigned short |
| 221 | NXSwapHostShortToBig( |
| 222 | unsigned short	x |
| 223 | ) |
| 224 | { |
| 225 | return (unsigned short)OSSwapHostToBigInt16((uint16_t)x); |
| 226 | } |
| 227 | |
| 228 | static __inline__ __attribute__((deprecated)) |
| 229 | unsigned int |
| 230 | NXSwapHostIntToBig( |
| 231 | unsigned int	x |
| 232 | ) |
| 233 | { |
| 234 | return (unsigned int)OSSwapHostToBigInt32((uint32_t)x); |
| 235 | } |
| 236 | |
| 237 | static __inline__ __attribute__((deprecated)) |
| 238 | unsigned long |
| 239 | NXSwapHostLongToBig( |
| 240 | unsigned long	x |
| 241 | ) |
| 242 | { |
| 243 | return (unsigned long)OSSwapHostToBigInt32((uint32_t)x); |
| 244 | } |
| 245 | |
| 246 | static __inline__ __attribute__((deprecated)) |
| 247 | unsigned long long |
| 248 | NXSwapHostLongLongToBig( |
| 249 | unsigned long long	x |
| 250 | ) |
| 251 | { |
| 252 | return (unsigned long long)OSSwapHostToBigInt64((uint64_t)x); |
| 253 | } |
| 254 | |
| 255 | static __inline__ __attribute__((deprecated)) |
| 256 | NXSwappedDouble |
| 257 | NXSwapHostDoubleToBig( |
| 258 | double	x |
| 259 | ) |
| 260 | { |
| 261 | return (NXSwappedDouble)OSSwapHostToBigInt64((uint64_t)NXConvertHostDoubleToSwapped(x)); |
| 262 | } |
| 263 | |
| 264 | static __inline__ __attribute__((deprecated)) |
| 265 | NXSwappedFloat |
| 266 | NXSwapHostFloatToBig( |
| 267 | float	x |
| 268 | ) |
| 269 | { |
| 270 | return (NXSwappedFloat)OSSwapHostToBigInt32((uint32_t)NXConvertHostFloatToSwapped(x)); |
| 271 | } |
| 272 | |
| 273 | static __inline__ __attribute__((deprecated)) |
| 274 | unsigned short |
| 275 | NXSwapLittleShortToHost( |
| 276 | unsigned short	x |
| 277 | ) |
| 278 | { |
| 279 | return (unsigned short)OSSwapLittleToHostInt16((uint16_t)x); |
| 280 | } |
| 281 | |
| 282 | static __inline__ __attribute__((deprecated)) |
| 283 | unsigned int |
| 284 | NXSwapLittleIntToHost( |
| 285 | unsigned int	x |
| 286 | ) |
| 287 | { |
| 288 | return (unsigned int)OSSwapLittleToHostInt32((uint32_t)x); |
| 289 | } |
| 290 | |
| 291 | static __inline__ __attribute__((deprecated)) |
| 292 | unsigned long |
| 293 | NXSwapLittleLongToHost( |
| 294 | unsigned long	x |
| 295 | ) |
| 296 | { |
| 297 | return (unsigned long)OSSwapLittleToHostInt32((uint32_t)x); |
| 298 | } |
| 299 | |
| 300 | static __inline__ __attribute__((deprecated)) |
| 301 | unsigned long long |
| 302 | NXSwapLittleLongLongToHost( |
| 303 | unsigned long long	x |
| 304 | ) |
| 305 | { |
| 306 | return (unsigned long long)OSSwapLittleToHostInt64((uint64_t)x); |
| 307 | } |
| 308 | |
| 309 | static __inline__ __attribute__((deprecated)) |
| 310 | double |
| 311 | NXSwapLittleDoubleToHost( |
| 312 | NXSwappedDouble	x |
| 313 | ) |
| 314 | { |
| 315 | return NXConvertSwappedDoubleToHost((NXSwappedDouble)OSSwapLittleToHostInt64((uint64_t)x)); |
| 316 | } |
| 317 | |
| 318 | static __inline__ __attribute__((deprecated)) |
| 319 | float |
| 320 | NXSwapLittleFloatToHost( |
| 321 | NXSwappedFloat	x |
| 322 | ) |
| 323 | { |
| 324 | return NXConvertSwappedFloatToHost((NXSwappedFloat)OSSwapLittleToHostInt32((uint32_t)x)); |
| 325 | } |
| 326 | |
| 327 | static __inline__ __attribute__((deprecated)) |
| 328 | unsigned short |
| 329 | NXSwapHostShortToLittle( |
| 330 | unsigned short	x |
| 331 | ) |
| 332 | { |
| 333 | return (unsigned short)OSSwapHostToLittleInt16((uint16_t)x); |
| 334 | } |
| 335 | |
| 336 | static __inline__ __attribute__((deprecated)) |
| 337 | unsigned int |
| 338 | NXSwapHostIntToLittle( |
| 339 | unsigned int	x |
| 340 | ) |
| 341 | { |
| 342 | return (unsigned int)OSSwapHostToLittleInt32((uint32_t)x); |
| 343 | } |
| 344 | |
| 345 | static __inline__ __attribute__((deprecated)) |
| 346 | unsigned long |
| 347 | NXSwapHostLongToLittle( |
| 348 | unsigned long	x |
| 349 | ) |
| 350 | { |
| 351 | return (unsigned long)OSSwapHostToLittleInt32((uint32_t)x); |
| 352 | } |
| 353 | |
| 354 | static __inline__ __attribute__((deprecated)) |
| 355 | unsigned long long |
| 356 | NXSwapHostLongLongToLittle( |
| 357 | unsigned long long	x |
| 358 | ) |
| 359 | { |
| 360 | return (unsigned long long)OSSwapHostToLittleInt64((uint64_t)x); |
| 361 | } |
| 362 | |
| 363 | static __inline__ __attribute__((deprecated)) |
| 364 | NXSwappedDouble |
| 365 | NXSwapHostDoubleToLittle( |
| 366 | double	x |
| 367 | ) |
| 368 | { |
| 369 | return (NXSwappedDouble)OSSwapHostToLittleInt64((uint64_t)NXConvertHostDoubleToSwapped(x)); |
| 370 | } |
| 371 | |
| 372 | static __inline__ __attribute__((deprecated)) |
| 373 | NXSwappedFloat |
| 374 | NXSwapHostFloatToLittle( |
| 375 | float	x |
| 376 | ) |
| 377 | { |
| 378 | return (NXSwappedFloat)OSSwapHostToLittleInt32((uint32_t)NXConvertHostFloatToSwapped(x)); |
| 379 | } |
| 380 | |
| 381 | #endif	/* _ARCHITECTURE_BYTE_ORDER_H_ */ |