1 | /* |
2 | * Copyright (c) 2006 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 | #ifndef _OS__OSBYTEORDER_H |
30 | #define _OS__OSBYTEORDER_H |
31 | |
32 | /* |
33 | * This header is normally included from <libkern/OSByteOrder.h>. However, |
34 | * <sys/_endian.h> also includes this in the case of little-endian |
35 | * architectures, so that we can map OSByteOrder routines to the hton* and ntoh* |
36 | * macros. This results in the asymmetry below; we only include |
37 | * <libkern/arch/_OSByteOrder.h> for little-endian architectures. |
38 | */ |
39 | |
40 | #include <sys/_types.h> |
41 | |
42 | /* Macros for swapping constant values in the preprocessing stage. */ |
43 | #define __DARWIN_OSSwapConstInt16(x) \ |
44 | ((__uint16_t)((((__uint16_t)(x) & 0xff00) >> 8) | \ |
45 | (((__uint16_t)(x) & 0x00ff) << 8))) |
46 | |
47 | #define __DARWIN_OSSwapConstInt32(x) \ |
48 | ((__uint32_t)((((__uint32_t)(x) & 0xff000000) >> 24) | \ |
49 | (((__uint32_t)(x) & 0x00ff0000) >> 8) | \ |
50 | (((__uint32_t)(x) & 0x0000ff00) << 8) | \ |
51 | (((__uint32_t)(x) & 0x000000ff) << 24))) |
52 | |
53 | #define __DARWIN_OSSwapConstInt64(x) \ |
54 | ((__uint64_t)((((__uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \ |
55 | (((__uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \ |
56 | (((__uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \ |
57 | (((__uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \ |
58 | (((__uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \ |
59 | (((__uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \ |
60 | (((__uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \ |
61 | (((__uint64_t)(x) & 0x00000000000000ffULL) << 56))) |
62 | |
63 | #if defined(__GNUC__) |
64 | |
65 | #if defined(__i386__) || defined(__x86_64__) |
66 | #include <libkern/i386/_OSByteOrder.h> |
67 | #endif |
68 | |
69 | #if defined (__arm__) || defined(__arm64__) |
70 | #include <libkern/arm/OSByteOrder.h> |
71 | #endif |
72 | |
73 | |
74 | #define __DARWIN_OSSwapInt16(x) \ |
75 | ((__uint16_t)(__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt16(x) : _OSSwapInt16(x))) |
76 | |
77 | #define __DARWIN_OSSwapInt32(x) \ |
78 | (__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt32(x) : _OSSwapInt32(x)) |
79 | |
80 | #define __DARWIN_OSSwapInt64(x) \ |
81 | (__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt64(x) : _OSSwapInt64(x)) |
82 | |
83 | #else /* ! __GNUC__ */ |
84 | |
85 | #if defined(__i386__) || defined(__x86_64__) |
86 | |
87 | #if !defined(__DARWIN_OS_INLINE) |
88 | # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
89 | # define __DARWIN_OS_INLINE static inline |
90 | # elif defined(__MWERKS__) || defined(__cplusplus) |
91 | # define __DARWIN_OS_INLINE static inline |
92 | # else |
93 | # define __DARWIN_OS_INLINE static __inline__ |
94 | # endif |
95 | #endif |
96 | |
97 | __DARWIN_OS_INLINE |
98 | uint16_t |
99 | _OSSwapInt16( |
100 | uint16_t data |
101 | ) |
102 | { |
103 | return __DARWIN_OSSwapConstInt16(data); |
104 | } |
105 | |
106 | __DARWIN_OS_INLINE |
107 | uint32_t |
108 | _OSSwapInt32( |
109 | uint32_t data |
110 | ) |
111 | { |
112 | return __DARWIN_OSSwapConstInt32(data); |
113 | } |
114 | |
115 | __DARWIN_OS_INLINE |
116 | uint64_t |
117 | _OSSwapInt64( |
118 | uint64_t data |
119 | ) |
120 | { |
121 | return __DARWIN_OSSwapConstInt64(data); |
122 | } |
123 | #endif |
124 | |
125 | #define __DARWIN_OSSwapInt16(x) _OSSwapInt16(x) |
126 | |
127 | #define __DARWIN_OSSwapInt32(x) _OSSwapInt32(x) |
128 | |
129 | #define __DARWIN_OSSwapInt64(x) _OSSwapInt64(x) |
130 | |
131 | #endif /* __GNUC__ */ |
132 | |
133 | #endif /* ! _OS__OSBYTEORDER_H */ |
134 | |