xref: /xnu-11215.61.5/libkern/libkern/i386/OSByteOrder.h (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1 /*
2  * Copyright (c) 1999-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_OSBYTEORDERI386_H
30 #define _OS_OSBYTEORDERI386_H
31 
32 #if defined(__i386__) || defined(__x86_64__)
33 
34 #include <stdint.h>
35 #include <libkern/i386/_OSByteOrder.h>
36 #include <sys/_types/_os_inline.h>
37 
38 /* Functions for byte reversed loads. */
39 
40 OS_INLINE
41 uint16_t
OSReadSwapInt16(const volatile void * base,uintptr_t byteOffset)42 OSReadSwapInt16(
43 	const volatile void   * base,
44 	uintptr_t       byteOffset
45 	)
46 {
47 	uint16_t result;
48 
49 	result = *(volatile uint16_t *)((uintptr_t)base + byteOffset);
50 	return _OSSwapInt16(result);
51 }
52 
53 OS_INLINE
54 uint32_t
OSReadSwapInt32(const volatile void * base,uintptr_t byteOffset)55 OSReadSwapInt32(
56 	const volatile void   * base,
57 	uintptr_t       byteOffset
58 	)
59 {
60 	uint32_t result;
61 
62 	result = *(volatile uint32_t *)((uintptr_t)base + byteOffset);
63 	return _OSSwapInt32(result);
64 }
65 
66 OS_INLINE
67 uint64_t
OSReadSwapInt64(const volatile void * base,uintptr_t byteOffset)68 OSReadSwapInt64(
69 	const volatile void   * base,
70 	uintptr_t       byteOffset
71 	)
72 {
73 	uint64_t result;
74 
75 	result = *(volatile uint64_t *)((uintptr_t)base + byteOffset);
76 	return _OSSwapInt64(result);
77 }
78 
79 /* Functions for byte reversed stores. */
80 
81 OS_INLINE
82 void
OSWriteSwapInt16(volatile void * base,uintptr_t byteOffset,uint16_t data)83 OSWriteSwapInt16(
84 	volatile void   * base,
85 	uintptr_t       byteOffset,
86 	uint16_t        data
87 	)
88 {
89 	*(volatile uint16_t *)((uintptr_t)base + byteOffset) = _OSSwapInt16(data);
90 }
91 
92 OS_INLINE
93 void
OSWriteSwapInt32(volatile void * base,uintptr_t byteOffset,uint32_t data)94 OSWriteSwapInt32(
95 	volatile void   * base,
96 	uintptr_t       byteOffset,
97 	uint32_t        data
98 	)
99 {
100 	*(volatile uint32_t *)((uintptr_t)base + byteOffset) = _OSSwapInt32(data);
101 }
102 
103 OS_INLINE
104 void
OSWriteSwapInt64(volatile void * base,uintptr_t byteOffset,uint64_t data)105 OSWriteSwapInt64(
106 	volatile void    * base,
107 	uintptr_t        byteOffset,
108 	uint64_t         data
109 	)
110 {
111 	*(volatile uint64_t *)((uintptr_t)base + byteOffset) = _OSSwapInt64(data);
112 }
113 
114 #endif /* defined(__i386__) || defined(__x86_64__) */
115 
116 #endif /* ! _OS_OSBYTEORDERI386_H */
117