199129Sobrien/*- 299129Sobrien * Copyright (c) 1987, 1991 Regents of the University of California. 399129Sobrien * All rights reserved. 499129Sobrien * 599129Sobrien * Redistribution and use in source and binary forms, with or without 699129Sobrien * modification, are permitted provided that the following conditions 799129Sobrien * are met: 899129Sobrien * 1. Redistributions of source code must retain the above copyright 999129Sobrien * notice, this list of conditions and the following disclaimer. 1099129Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1199129Sobrien * notice, this list of conditions and the following disclaimer in the 1299129Sobrien * documentation and/or other materials provided with the distribution. 1399129Sobrien * 4. Neither the name of the University nor the names of its contributors 1499129Sobrien * may be used to endorse or promote products derived from this software 1599129Sobrien * without specific prior written permission. 1699129Sobrien * 1799129Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1899129Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1999129Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2099129Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2199129Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2299129Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2399129Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2499129Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2599129Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2699129Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2799129Sobrien * SUCH DAMAGE. 2899129Sobrien * 2999129Sobrien * @(#)endian.h 7.8 (Berkeley) 4/3/91 3099129Sobrien * $FreeBSD$ 3199129Sobrien */ 3299129Sobrien 3399129Sobrien#ifndef _MACHINE_ENDIAN_H_ 3499129Sobrien#define _MACHINE_ENDIAN_H_ 3599129Sobrien 3699129Sobrien#include <sys/cdefs.h> 37102227Smike#include <sys/_types.h> 3899129Sobrien 39143429Speter#ifdef __cplusplus 40143429Speterextern "C" { 41143429Speter#endif 42143429Speter 4399129Sobrien/* 4499129Sobrien * Define the order of 32-bit words in 64-bit words. 4599129Sobrien */ 4699129Sobrien#define _QUAD_HIGHWORD 1 4799129Sobrien#define _QUAD_LOWWORD 0 4899129Sobrien 4999129Sobrien/* 5099129Sobrien * Definitions for byte order, according to byte significance from low 5199129Sobrien * address to high. 5299129Sobrien */ 5399129Sobrien#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 5499129Sobrien#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 5599129Sobrien#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 5699129Sobrien 5799129Sobrien#define _BYTE_ORDER _LITTLE_ENDIAN 5899129Sobrien 5999129Sobrien/* 6099129Sobrien * Deprecated variants that don't have enough underscores to be useful in more 6199129Sobrien * strict namespaces. 6299129Sobrien */ 6399129Sobrien#if __BSD_VISIBLE 6499129Sobrien#define LITTLE_ENDIAN _LITTLE_ENDIAN 6599129Sobrien#define BIG_ENDIAN _BIG_ENDIAN 6699129Sobrien#define PDP_ENDIAN _PDP_ENDIAN 6799129Sobrien#define BYTE_ORDER _BYTE_ORDER 6899129Sobrien#endif 6999129Sobrien 70143063Sjoerg#if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P) 7199129Sobrien 72219819Sjeff#define __bswap64_const(_x) \ 73219819Sjeff (((_x) >> 56) | \ 74219819Sjeff (((_x) >> 40) & (0xffUL << 8)) | \ 75219819Sjeff (((_x) >> 24) & (0xffUL << 16)) | \ 76219819Sjeff (((_x) >> 8) & (0xffUL << 24)) | \ 77219819Sjeff (((_x) << 8) & (0xffUL << 32)) | \ 78219819Sjeff (((_x) << 24) & (0xffUL << 40)) | \ 79219819Sjeff (((_x) << 40) & (0xffUL << 48)) | \ 80219819Sjeff ((_x) << 56)) 8199129Sobrien 82219819Sjeff#define __bswap32_const(_x) \ 83219819Sjeff (((_x) >> 24) | \ 84219819Sjeff (((_x) & (0xff << 16)) >> 8) | \ 85219819Sjeff (((_x) & (0xff << 8)) << 8) | \ 86219819Sjeff ((_x) << 24)) 87120350Speter 88219819Sjeff#define __bswap16_const(_x) (__uint16_t)((_x) << 8 | (_x) >> 8) 89120350Speter 9099129Sobrienstatic __inline __uint64_t 91219819Sjeff__bswap64_var(__uint64_t _x) 9299129Sobrien{ 93114349Speter 94219819Sjeff __asm ("bswap %0" : "+r" (_x)); 95219819Sjeff return (_x); 9699129Sobrien} 9799129Sobrien 9899129Sobrienstatic __inline __uint32_t 99219819Sjeff__bswap32_var(__uint32_t _x) 10099129Sobrien{ 10199129Sobrien 102219819Sjeff __asm ("bswap %0" : "+r" (_x)); 103219819Sjeff return (_x); 10499129Sobrien} 10599129Sobrien 10699129Sobrienstatic __inline __uint16_t 107219819Sjeff__bswap16_var(__uint16_t _x) 10899129Sobrien{ 109219819Sjeff 110219819Sjeff return (__bswap16_const(_x)); 11199129Sobrien} 11299129Sobrien 113219819Sjeff#define __bswap64(_x) \ 114219819Sjeff (__builtin_constant_p(_x) ? \ 115219819Sjeff __bswap64_const((__uint64_t)(_x)) : __bswap64_var(_x)) 116219819Sjeff 117219819Sjeff#define __bswap32(_x) \ 118219819Sjeff (__builtin_constant_p(_x) ? \ 119219819Sjeff __bswap32_const((__uint32_t)(_x)) : __bswap32_var(_x)) 120219819Sjeff 121219819Sjeff#define __bswap16(_x) \ 122233349Sdim ((__uint16_t)(__builtin_constant_p(_x) ? \ 123233349Sdim __bswap16_const((__uint16_t)(_x)) : __bswap16_var(_x))) 124219819Sjeff 12599129Sobrien#define __htonl(x) __bswap32(x) 12699129Sobrien#define __htons(x) __bswap16(x) 12799129Sobrien#define __ntohl(x) __bswap32(x) 12899129Sobrien#define __ntohs(x) __bswap16(x) 12999129Sobrien 130143063Sjoerg#else /* !(__GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P) */ 131103814Smike 132103814Smike/* 133103814Smike * No optimizations are available for this compiler. Fall back to 134103814Smike * non-optimized functions by defining the constant usually used to prevent 135103814Smike * redefinition. 136103814Smike */ 137103814Smike#define _BYTEORDER_FUNC_DEFINED 138103814Smike 139143063Sjoerg#endif /* __GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P */ 140103814Smike 141143429Speter#ifdef __cplusplus 142143429Speter} 143143429Speter#endif 144143429Speter 14599129Sobrien#endif /* !_MACHINE_ENDIAN_H_ */ 146