xref: /xnu-12377.61.12/libkern/libkern/_OSByteOrder.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2006-2023 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 #include <sys/_types.h>
33 
34 /* Macros for swapping constant values in the preprocessing stage. */
35 #define __DARWIN_OSSwapConstInt16(x) \
36     ((__uint16_t)((((__uint16_t)(x) & 0xff00U) >> 8) | \
37 	        (((__uint16_t)(x) & 0x00ffU) << 8)))
38 
39 #define __DARWIN_OSSwapConstInt32(x) \
40     ((__uint32_t)((((__uint32_t)(x) & 0xff000000U) >> 24) | \
41 	        (((__uint32_t)(x) & 0x00ff0000U) >>  8) | \
42 	        (((__uint32_t)(x) & 0x0000ff00U) <<  8) | \
43 	        (((__uint32_t)(x) & 0x000000ffU) << 24)))
44 
45 #define __DARWIN_OSSwapConstInt64(x) \
46     ((__uint64_t)((((__uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
47 	        (((__uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
48 	        (((__uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
49 	        (((__uint64_t)(x) & 0x000000ff00000000ULL) >>  8) | \
50 	        (((__uint64_t)(x) & 0x00000000ff000000ULL) <<  8) | \
51 	        (((__uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
52 	        (((__uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
53 	        (((__uint64_t)(x) & 0x00000000000000ffULL) << 56)))
54 
55 #if defined(__GNUC__)
56 
57 #if defined(__i386__) || defined(__x86_64__)
58 #include <libkern/i386/_OSByteOrder.h>
59 #endif
60 
61 #if defined (__arm__) || defined(__arm64__)
62 #include <libkern/arm/_OSByteOrder.h>
63 #endif
64 
65 
66 #define __DARWIN_OSSwapInt16(x) \
67     ((__uint16_t)(__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt16(x) : _OSSwapInt16(x)))
68 
69 #define __DARWIN_OSSwapInt32(x) \
70     (__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt32(x) : _OSSwapInt32(x))
71 
72 #define __DARWIN_OSSwapInt64(x) \
73     (__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt64(x) : _OSSwapInt64(x))
74 
75 #else /* ! __GNUC__ */
76 
77 #if defined(__i386__) || defined(__x86_64__)
78 
79 #if !defined(__DARWIN_OS_INLINE)
80 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
81 #        define __DARWIN_OS_INLINE static inline
82 # elif defined(__MWERKS__) || defined(__cplusplus)
83 #        define __DARWIN_OS_INLINE static inline
84 # else
85 #        define __DARWIN_OS_INLINE static __inline__
86 # endif
87 #endif
88 
89 __DARWIN_OS_INLINE
90 __uint16_t
_OSSwapInt16(__uint16_t data)91 _OSSwapInt16(
92 	__uint16_t                    data
93 	)
94 {
95 	return __DARWIN_OSSwapConstInt16(data);
96 }
97 
98 __DARWIN_OS_INLINE
99 __uint32_t
_OSSwapInt32(__uint32_t data)100 _OSSwapInt32(
101 	__uint32_t                    data
102 	)
103 {
104 	return __DARWIN_OSSwapConstInt32(data);
105 }
106 
107 __DARWIN_OS_INLINE
108 __uint64_t
_OSSwapInt64(__uint64_t data)109 _OSSwapInt64(
110 	__uint64_t                    data
111 	)
112 {
113 	return __DARWIN_OSSwapConstInt64(data);
114 }
115 #endif
116 
117 #define __DARWIN_OSSwapInt16(x) _OSSwapInt16(x)
118 
119 #define __DARWIN_OSSwapInt32(x) _OSSwapInt32(x)
120 
121 #define __DARWIN_OSSwapInt64(x) _OSSwapInt64(x)
122 
123 #endif /* __GNUC__ */
124 
125 #endif /* ! _OS__OSBYTEORDER_H */
126