xref: /xnu-10063.121.3/libkern/libkern/machine/OSByteOrder.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa) !
1 /*
2  * Copyright (c) 2000-2005 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_OSBYTEORDERMACHINE_H
30 #define _OS_OSBYTEORDERMACHINE_H
31 
32 #include <stdint.h>
33 
34 #if !defined(OS_INLINE)
35 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
36 #        define OS_INLINE static inline
37 # elif defined(__MWERKS__) || defined(__cplusplus)
38 #        define OS_INLINE static inline
39 # else
40 #        define OS_INLINE static __inline__
41 # endif
42 #endif
43 
44 /* Generic byte swapping functions. */
45 
46 OS_INLINE
47 uint16_t
_OSSwapInt16(uint16_t data)48 _OSSwapInt16(
49 	uint16_t                      data
50 	)
51 {
52 	return OSSwapConstInt16(data);
53 }
54 
55 OS_INLINE
56 uint32_t
_OSSwapInt32(uint32_t data)57 _OSSwapInt32(
58 	uint32_t                      data
59 	)
60 {
61 	return OSSwapConstInt32(data);
62 }
63 
64 OS_INLINE
65 uint64_t
_OSSwapInt64(uint64_t data)66 _OSSwapInt64(
67 	uint64_t                        data
68 	)
69 {
70 	return OSSwapConstInt64(data);
71 }
72 
73 /* Functions for byte reversed loads. */
74 
75 OS_INLINE
76 uint16_t
OSReadSwapInt16(const volatile void * base,uintptr_t byteOffset)77 OSReadSwapInt16(
78 	const volatile void               * base,
79 	uintptr_t                     byteOffset
80 	)
81 {
82 	uint16_t data = *(volatile uint16_t *)((uintptr_t)base + byteOffset);
83 	return _OSSwapInt16(data);
84 }
85 
86 OS_INLINE
87 uint32_t
OSReadSwapInt32(const volatile void * base,uintptr_t byteOffset)88 OSReadSwapInt32(
89 	const volatile void               * base,
90 	uintptr_t                     byteOffset
91 	)
92 {
93 	uint32_t data = *(volatile uint32_t *)((uintptr_t)base + byteOffset);
94 	return _OSSwapInt32(data);
95 }
96 
97 OS_INLINE
98 uint64_t
OSReadSwapInt64(const volatile void * base,uintptr_t byteOffset)99 OSReadSwapInt64(
100 	const volatile void               * base,
101 	uintptr_t                     byteOffset
102 	)
103 {
104 	uint64_t data = *(volatile uint64_t *)((uintptr_t)base + byteOffset);
105 	return _OSSwapInt64(data);
106 }
107 
108 /* Functions for byte reversed stores. */
109 
110 OS_INLINE
111 void
OSWriteSwapInt16(volatile void * base,uintptr_t byteOffset,uint16_t data)112 OSWriteSwapInt16(
113 	volatile void               * base,
114 	uintptr_t                     byteOffset,
115 	uint16_t                      data
116 	)
117 {
118 	*(volatile uint16_t *)((uintptr_t)base + byteOffset) = _OSSwapInt16(data);
119 }
120 
121 OS_INLINE
122 void
OSWriteSwapInt32(volatile void * base,uintptr_t byteOffset,uint32_t data)123 OSWriteSwapInt32(
124 	volatile void               * base,
125 	uintptr_t                     byteOffset,
126 	uint32_t                      data
127 	)
128 {
129 	*(volatile uint32_t *)((uintptr_t)base + byteOffset) = _OSSwapInt32(data);
130 }
131 
132 OS_INLINE
133 void
OSWriteSwapInt64(volatile void * base,uintptr_t byteOffset,uint64_t data)134 OSWriteSwapInt64(
135 	volatile void               * base,
136 	uintptr_t                     byteOffset,
137 	uint64_t                      data
138 	)
139 {
140 	*(volatile uint64_t *)((uintptr_t)base + byteOffset) = _OSSwapInt64(data);
141 }
142 
143 #endif /* ! _OS_OSBYTEORDERMACHINE_H */
144