]> git.saurik.com Git - apple/xnu.git/blame - libkern/libkern/i386/OSByteOrder.h
xnu-517.tar.gz
[apple/xnu.git] / libkern / libkern / i386 / OSByteOrder.h
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
1c79356b
A
25
26#ifndef _OS_OSBYTEORDERI386_H
27#define _OS_OSBYTEORDERI386_H
28
55e303ae 29#include <stdint.h>
1c79356b 30
55e303ae
A
31#if !defined(OS_INLINE)
32# define OS_INLINE static inline
33#endif
1c79356b 34
55e303ae 35/* Generic byte swapping functions. */
1c79356b
A
36
37OS_INLINE
55e303ae
A
38uint16_t
39_OSSwapInt16(
40 uint16_t data
1c79356b
A
41)
42{
55e303ae
A
43 __asm__ ("xchgb %b0, %h0" : "+q" (data));
44 return data;
1c79356b
A
45}
46
47OS_INLINE
55e303ae
A
48uint32_t
49_OSSwapInt32(
50 uint32_t data
1c79356b
A
51)
52{
55e303ae
A
53 __asm__ ("bswap %0" : "+r" (data));
54 return data;
1c79356b
A
55}
56
57OS_INLINE
55e303ae
A
58uint64_t
59_OSSwapInt64(
60 uint64_t data
1c79356b
A
61)
62{
55e303ae
A
63 union {
64 uint64_t ull;
65 uint32_t ul[2];
66 } u;
67
68 /* This actually generates the best code */
69 u.ul[0] = data >> 32;
70 u.ul[1] = data & 0xffffffff;
71 u.ul[0] = _OSSwapInt32(u.ul[0]);
72 u.ul[1] = _OSSwapInt32(u.ul[1]);
73 return u.ull;
1c79356b
A
74}
75
55e303ae 76/* Functions for byte reversed loads. */
1c79356b
A
77
78OS_INLINE
55e303ae
A
79uint16_t
80OSReadSwapInt16(
81 volatile void * base,
82 uintptr_t offset
1c79356b
A
83)
84{
55e303ae
A
85 uint16_t result;
86
87 result = *(uint16_t *)((uintptr_t)base + offset);
88 return _OSSwapInt16(result);
1c79356b
A
89}
90
91OS_INLINE
55e303ae
A
92uint32_t
93OSReadSwapInt32(
94 volatile void * base,
95 uintptr_t offset
1c79356b
A
96)
97{
55e303ae
A
98 uint32_t result;
99
100 result = *(uint32_t *)((uintptr_t)base + offset);
101 return _OSSwapInt32(result);
1c79356b
A
102}
103
104OS_INLINE
55e303ae
A
105uint64_t
106OSReadSwapInt64(
107 volatile void * base,
108 uintptr_t offset
1c79356b
A
109)
110{
55e303ae 111 uint32_t * inp;
1c79356b 112 union ullc {
55e303ae
A
113 uint64_t ull;
114 uint32_t ul[2];
115 } outv;
1c79356b 116
55e303ae
A
117 inp = (uint32_t *)((uintptr_t)base + offset);
118 outv.ul[0] = inp[1];
119 outv.ul[1] = inp[0];
120 outv.ul[0] = _OSSwapInt32(outv.ul[0]);
121 outv.ul[1] = _OSSwapInt32(outv.ul[1]);
122 return outv.ull;
1c79356b
A
123}
124
55e303ae 125/* Functions for byte reversed stores. */
1c79356b
A
126
127OS_INLINE
55e303ae
A
128void
129OSWriteSwapInt16(
130 volatile void * base,
131 uintptr_t offset,
132 uint16_t data
1c79356b
A
133)
134{
55e303ae 135 *(uint16_t *)((uintptr_t)base + offset) = _OSSwapInt16(data);
1c79356b
A
136}
137
138OS_INLINE
55e303ae
A
139void
140OSWriteSwapInt32(
141 volatile void * base,
142 uintptr_t offset,
143 uint32_t data
1c79356b
A
144)
145{
55e303ae 146 *(uint32_t *)((uintptr_t)base + offset) = _OSSwapInt32(data);
1c79356b
A
147}
148
149OS_INLINE
55e303ae
A
150void
151OSWriteSwapInt64(
152 volatile void * base,
153 uintptr_t offset,
154 uint64_t data
1c79356b
A
155)
156{
55e303ae 157 *(uint64_t *)((uintptr_t)base + offset) = _OSSwapInt64(data);
1c79356b
A
158}
159
160#endif /* ! _OS_OSBYTEORDERI386_H */