1/* 2 * Copyright (c) 2000-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#include <stdint.h> 33#include <libkern/_OSByteOrder.h> 34 35/* Macros for swapping constant values in the preprocessing stage. */ 36#define OSSwapConstInt16(x) __DARWIN_OSSwapConstInt16(x) 37#define OSSwapConstInt32(x) __DARWIN_OSSwapConstInt32(x) 38#define OSSwapConstInt64(x) __DARWIN_OSSwapConstInt64(x) 39 40#if defined(__GNUC__) 41 42#if (defined(__i386__) || defined(__x86_64__)) 43#include <libkern/i386/OSByteOrder.h> 44#else 45#include <libkern/machine/OSByteOrder.h> 46#endif 47 48#else /* ! __GNUC__ */ 49 50#include <libkern/machine/OSByteOrder.h> 51 52#endif /* __GNUC__ */ 53 54#define OSSwapInt16(x) __DARWIN_OSSwapInt16(x) 55#define OSSwapInt32(x) __DARWIN_OSSwapInt32(x) 56#define OSSwapInt64(x) __DARWIN_OSSwapInt64(x) 57 58enum { 59 OSUnknownByteOrder, 60 OSLittleEndian, 61 OSBigEndian 62}; 63 64OS_INLINE 65int32_t 66OSHostByteOrder(void) { 67#if defined(__LITTLE_ENDIAN__) 68 return OSLittleEndian; 69#elif defined(__BIG_ENDIAN__) 70 return OSBigEndian; 71#else 72 return OSUnknownByteOrder; 73#endif 74} 75 76#define OSReadBigInt(x, y) OSReadBigInt32(x, y) 77#define OSWriteBigInt(x, y, z) OSWriteBigInt32(x, y, z) 78#define OSSwapBigToHostInt(x) OSSwapBigToHostInt32(x) 79#define OSSwapHostToBigInt(x) OSSwapHostToBigInt32(x) 80#define OSReadLittleInt(x, y) OSReadLittleInt32(x, y) 81#define OSWriteLittleInt(x, y, z) OSWriteLittleInt32(x, y, z) 82#define OSSwapHostToLittleInt(x) OSSwapHostToLittleInt32(x) 83#define OSSwapLittleToHostInt(x) OSSwapLittleToHostInt32(x) 84 85/* Functions for loading native endian values. */ 86 87OS_INLINE 88uint16_t 89_OSReadInt16( 90 const volatile void * base, 91 uintptr_t byteOffset 92) 93{ 94 return *(volatile uint16_t *)((uintptr_t)base + byteOffset); 95} 96 97OS_INLINE 98uint32_t 99_OSReadInt32( 100 const volatile void * base, 101 uintptr_t byteOffset 102) 103{ 104 return *(volatile uint32_t *)((uintptr_t)base + byteOffset); 105} 106 107OS_INLINE 108uint64_t 109_OSReadInt64( 110 const volatile void * base, 111 uintptr_t byteOffset 112) 113{ 114 return *(volatile uint64_t *)((uintptr_t)base + byteOffset); 115} 116 117/* Functions for storing native endian values. */ 118 119OS_INLINE 120void 121_OSWriteInt16( 122 volatile void * base, 123 uintptr_t byteOffset, 124 uint16_t data 125) 126{ 127 *(volatile uint16_t *)((uintptr_t)base + byteOffset) = data; 128} 129 130OS_INLINE 131void 132_OSWriteInt32( 133 volatile void * base, 134 uintptr_t byteOffset, 135 uint32_t data 136) 137{ 138 *(volatile uint32_t *)((uintptr_t)base + byteOffset) = data; 139} 140 141OS_INLINE 142void 143_OSWriteInt64( 144 volatile void * base, 145 uintptr_t byteOffset, 146 uint64_t data 147) 148{ 149 *(volatile uint64_t *)((uintptr_t)base + byteOffset) = data; 150} 151 152#if defined(__BIG_ENDIAN__) 153 154/* Functions for loading big endian to host endianess. */ 155 156#define OSReadBigInt16(base, byteOffset) _OSReadInt16(base, byteOffset) 157#define OSReadBigInt32(base, byteOffset) _OSReadInt32(base, byteOffset) 158#define OSReadBigInt64(base, byteOffset) _OSReadInt64(base, byteOffset) 159 160/* Functions for storing host endianess to big endian. */ 161 162#define OSWriteBigInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data) 163#define OSWriteBigInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data) 164#define OSWriteBigInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data) 165 166/* Functions for loading little endian to host endianess. */ 167 168#define OSReadLittleInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset) 169#define OSReadLittleInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset) 170#define OSReadLittleInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset) 171 172/* Functions for storing host endianess to little endian. */ 173 174#define OSWriteLittleInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data) 175#define OSWriteLittleInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data) 176#define OSWriteLittleInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data) 177 178/* Host endianess to big endian byte swapping macros for constants. */ 179 180#define OSSwapHostToBigConstInt16(x) ((uint16_t)(x)) 181#define OSSwapHostToBigConstInt32(x) ((uint32_t)(x)) 182#define OSSwapHostToBigConstInt64(x) ((uint64_t)(x)) 183 184/* Generic host endianess to big endian byte swapping functions. */ 185 186#define OSSwapHostToBigInt16(x) ((uint16_t)(x)) 187#define OSSwapHostToBigInt32(x) ((uint32_t)(x)) 188#define OSSwapHostToBigInt64(x) ((uint64_t)(x)) 189 190/* Host endianess to little endian byte swapping macros for constants. */ 191 192#define OSSwapHostToLittleConstInt16(x) OSSwapConstInt16(x) 193#define OSSwapHostToLittleConstInt32(x) OSSwapConstInt32(x) 194#define OSSwapHostToLittleConstInt64(x) OSSwapConstInt64(x) 195 196/* Generic host endianess to little endian byte swapping functions. */ 197 198#define OSSwapHostToLittleInt16(x) OSSwapInt16(x) 199#define OSSwapHostToLittleInt32(x) OSSwapInt32(x) 200#define OSSwapHostToLittleInt64(x) OSSwapInt64(x) 201 202/* Big endian to host endianess byte swapping macros for constants. */ 203 204#define OSSwapBigToHostConstInt16(x) ((uint16_t)(x)) 205#define OSSwapBigToHostConstInt32(x) ((uint32_t)(x)) 206#define OSSwapBigToHostConstInt64(x) ((uint64_t)(x)) 207 208/* Generic big endian to host endianess byte swapping functions. */ 209 210#define OSSwapBigToHostInt16(x) ((uint16_t)(x)) 211#define OSSwapBigToHostInt32(x) ((uint32_t)(x)) 212#define OSSwapBigToHostInt64(x) ((uint64_t)(x)) 213 214/* Little endian to host endianess byte swapping macros for constants. */ 215 216#define OSSwapLittleToHostConstInt16(x) OSSwapConstInt16(x) 217#define OSSwapLittleToHostConstInt32(x) OSSwapConstInt32(x) 218#define OSSwapLittleToHostConstInt64(x) OSSwapConstInt64(x) 219 220/* Generic little endian to host endianess byte swapping functions. */ 221 222#define OSSwapLittleToHostInt16(x) OSSwapInt16(x) 223#define OSSwapLittleToHostInt32(x) OSSwapInt32(x) 224#define OSSwapLittleToHostInt64(x) OSSwapInt64(x) 225 226#elif defined(__LITTLE_ENDIAN__) 227 228/* Functions for loading big endian to host endianess. */ 229 230#define OSReadBigInt16(base, byteOffset) OSReadSwapInt16(base, byteOffset) 231#define OSReadBigInt32(base, byteOffset) OSReadSwapInt32(base, byteOffset) 232#define OSReadBigInt64(base, byteOffset) OSReadSwapInt64(base, byteOffset) 233 234/* Functions for storing host endianess to big endian. */ 235 236#define OSWriteBigInt16(base, byteOffset, data) OSWriteSwapInt16(base, byteOffset, data) 237#define OSWriteBigInt32(base, byteOffset, data) OSWriteSwapInt32(base, byteOffset, data) 238#define OSWriteBigInt64(base, byteOffset, data) OSWriteSwapInt64(base, byteOffset, data) 239 240/* Functions for loading little endian to host endianess. */ 241 242#define OSReadLittleInt16(base, byteOffset) _OSReadInt16(base, byteOffset) 243#define OSReadLittleInt32(base, byteOffset) _OSReadInt32(base, byteOffset) 244#define OSReadLittleInt64(base, byteOffset) _OSReadInt64(base, byteOffset) 245 246/* Functions for storing host endianess to little endian. */ 247 248#define OSWriteLittleInt16(base, byteOffset, data) _OSWriteInt16(base, byteOffset, data) 249#define OSWriteLittleInt32(base, byteOffset, data) _OSWriteInt32(base, byteOffset, data) 250#define OSWriteLittleInt64(base, byteOffset, data) _OSWriteInt64(base, byteOffset, data) 251 252/* Host endianess to big endian byte swapping macros for constants. */ 253 254#define OSSwapHostToBigConstInt16(x) OSSwapConstInt16(x) 255#define OSSwapHostToBigConstInt32(x) OSSwapConstInt32(x) 256#define OSSwapHostToBigConstInt64(x) OSSwapConstInt64(x) 257 258/* Generic host endianess to big endian byte swapping functions. */ 259 260#define OSSwapHostToBigInt16(x) OSSwapInt16(x) 261#define OSSwapHostToBigInt32(x) OSSwapInt32(x) 262#define OSSwapHostToBigInt64(x) OSSwapInt64(x) 263 264/* Host endianess to little endian byte swapping macros for constants. */ 265 266#define OSSwapHostToLittleConstInt16(x) ((uint16_t)(x)) 267#define OSSwapHostToLittleConstInt32(x) ((uint32_t)(x)) 268#define OSSwapHostToLittleConstInt64(x) ((uint64_t)(x)) 269 270/* Generic host endianess to little endian byte swapping functions. */ 271 272#define OSSwapHostToLittleInt16(x) ((uint16_t)(x)) 273#define OSSwapHostToLittleInt32(x) ((uint32_t)(x)) 274#define OSSwapHostToLittleInt64(x) ((uint64_t)(x)) 275 276/* Big endian to host endianess byte swapping macros for constants. */ 277 278#define OSSwapBigToHostConstInt16(x) OSSwapConstInt16(x) 279#define OSSwapBigToHostConstInt32(x) OSSwapConstInt32(x) 280#define OSSwapBigToHostConstInt64(x) OSSwapConstInt64(x) 281 282/* Generic big endian to host endianess byte swapping functions. */ 283 284#define OSSwapBigToHostInt16(x) OSSwapInt16(x) 285#define OSSwapBigToHostInt32(x) OSSwapInt32(x) 286#define OSSwapBigToHostInt64(x) OSSwapInt64(x) 287 288/* Little endian to host endianess byte swapping macros for constants. */ 289 290#define OSSwapLittleToHostConstInt16(x) ((uint16_t)(x)) 291#define OSSwapLittleToHostConstInt32(x) ((uint32_t)(x)) 292#define OSSwapLittleToHostConstInt64(x) ((uint64_t)(x)) 293 294/* Generic little endian to host endianess byte swapping functions. */ 295 296#define OSSwapLittleToHostInt16(x) ((uint16_t)(x)) 297#define OSSwapLittleToHostInt32(x) ((uint32_t)(x)) 298#define OSSwapLittleToHostInt64(x) ((uint64_t)(x)) 299 300#else 301#error Unknown endianess. 302#endif 303 304#endif /* ! _OS_OSBYTEORDER_H */ 305 306 307