1fd8e4ebcSMike Barcroft /*- 2fa561e1eSDavid E. O'Brien * Copyright (c) 1987, 1991, 1993 3fa561e1eSDavid E. O'Brien * The Regents of the University of California. All rights reserved. 4fa561e1eSDavid E. O'Brien * 5fa561e1eSDavid E. O'Brien * Redistribution and use in source and binary forms, with or without 6fa561e1eSDavid E. O'Brien * modification, are permitted provided that the following conditions 7fa561e1eSDavid E. O'Brien * are met: 8fa561e1eSDavid E. O'Brien * 1. Redistributions of source code must retain the above copyright 9fa561e1eSDavid E. O'Brien * notice, this list of conditions and the following disclaimer. 10fa561e1eSDavid E. O'Brien * 2. Redistributions in binary form must reproduce the above copyright 11fa561e1eSDavid E. O'Brien * notice, this list of conditions and the following disclaimer in the 12fa561e1eSDavid E. O'Brien * documentation and/or other materials provided with the distribution. 13fa561e1eSDavid E. O'Brien * 4. Neither the name of the University nor the names of its contributors 14fa561e1eSDavid E. O'Brien * may be used to endorse or promote products derived from this software 15fa561e1eSDavid E. O'Brien * without specific prior written permission. 16fa561e1eSDavid E. O'Brien * 17fa561e1eSDavid E. O'Brien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18fa561e1eSDavid E. O'Brien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19fa561e1eSDavid E. O'Brien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20fa561e1eSDavid E. O'Brien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21fa561e1eSDavid E. O'Brien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22fa561e1eSDavid E. O'Brien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23fa561e1eSDavid E. O'Brien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24fa561e1eSDavid E. O'Brien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25fa561e1eSDavid E. O'Brien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26fa561e1eSDavid E. O'Brien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27fa561e1eSDavid E. O'Brien * SUCH DAMAGE. 28fa561e1eSDavid E. O'Brien * 29fa561e1eSDavid E. O'Brien * @(#)endian.h 8.1 (Berkeley) 6/10/93 30fa561e1eSDavid E. O'Brien * $FreeBSD$ 31fa561e1eSDavid E. O'Brien */ 32fa561e1eSDavid E. O'Brien 33fd8e4ebcSMike Barcroft #ifndef _MACHINE_ENDIAN_H_ 34fd8e4ebcSMike Barcroft #define _MACHINE_ENDIAN_H_ 35fd8e4ebcSMike Barcroft 36795aff0eSMike Barcroft #include <sys/cdefs.h> 37abbd8902SMike Barcroft #include <sys/_types.h> 38fa561e1eSDavid E. O'Brien 39fa561e1eSDavid E. O'Brien /* 40fa561e1eSDavid E. O'Brien * Define the order of 32-bit words in 64-bit words. 41fa561e1eSDavid E. O'Brien */ 42fa561e1eSDavid E. O'Brien #define _QUAD_HIGHWORD 0 43fa561e1eSDavid E. O'Brien #define _QUAD_LOWWORD 1 44fa561e1eSDavid E. O'Brien 45fa561e1eSDavid E. O'Brien /* 46636f2ebfSMarcel Moolenaar * GCC defines _BIG_ENDIAN and _LITTLE_ENDIAN equal to __BIG_ENDIAN__ 47636f2ebfSMarcel Moolenaar * and __LITTLE_ENDIAN__ (resp). 48636f2ebfSMarcel Moolenaar */ 49636f2ebfSMarcel Moolenaar #ifdef _BIG_ENDIAN 50636f2ebfSMarcel Moolenaar #undef _BIG_ENDIAN 51636f2ebfSMarcel Moolenaar #endif 52636f2ebfSMarcel Moolenaar #ifdef _LITTLE_ENDIAN 53636f2ebfSMarcel Moolenaar #undef _LITTLE_ENDIAN 54636f2ebfSMarcel Moolenaar #endif 55636f2ebfSMarcel Moolenaar 56636f2ebfSMarcel Moolenaar /* 57fa561e1eSDavid E. O'Brien * Definitions for byte order, according to byte significance from low 58fa561e1eSDavid E. O'Brien * address to high. 59fa561e1eSDavid E. O'Brien */ 607f0f1cfdSMike Barcroft #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 617f0f1cfdSMike Barcroft #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 627f0f1cfdSMike Barcroft #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 63fa561e1eSDavid E. O'Brien 64636f2ebfSMarcel Moolenaar #ifdef __LITTLE_ENDIAN__ 65636f2ebfSMarcel Moolenaar #define _BYTE_ORDER _LITTLE_ENDIAN 66636f2ebfSMarcel Moolenaar #else 677f0f1cfdSMike Barcroft #define _BYTE_ORDER _BIG_ENDIAN 68636f2ebfSMarcel Moolenaar #endif 697f0f1cfdSMike Barcroft 707f0f1cfdSMike Barcroft /* 717f0f1cfdSMike Barcroft * Deprecated variants that don't have enough underscores to be useful in more 727f0f1cfdSMike Barcroft * strict namespaces. 737f0f1cfdSMike Barcroft */ 747f0f1cfdSMike Barcroft #if __BSD_VISIBLE 757f0f1cfdSMike Barcroft #define LITTLE_ENDIAN _LITTLE_ENDIAN 767f0f1cfdSMike Barcroft #define BIG_ENDIAN _BIG_ENDIAN 777f0f1cfdSMike Barcroft #define PDP_ENDIAN _PDP_ENDIAN 787f0f1cfdSMike Barcroft #define BYTE_ORDER _BYTE_ORDER 797f0f1cfdSMike Barcroft #endif 80fa561e1eSDavid E. O'Brien 81e2c3e32aSNathan Whitehorn #if defined(__GNUCLIKE_BUILTIN_CONSTANT_P) 82e2c3e32aSNathan Whitehorn #define __is_constant(x) __builtin_constant_p(x) 83e2c3e32aSNathan Whitehorn #else 84e2c3e32aSNathan Whitehorn #define __is_constant(x) 0 85e2c3e32aSNathan Whitehorn #endif 86e2c3e32aSNathan Whitehorn 87e2c3e32aSNathan Whitehorn #define __bswap16_const(x) ((((__uint16_t)(x) >> 8) & 0xff) | \ 88e2c3e32aSNathan Whitehorn (((__uint16_t)(x) << 8) & 0xff00)) 89e2c3e32aSNathan Whitehorn #define __bswap32_const(x) ((((__uint32_t)(x) >> 24) & 0xff) | \ 90e2c3e32aSNathan Whitehorn (((__uint32_t)(x) >> 8) & 0xff00) | \ 91e2c3e32aSNathan Whitehorn (((__uint32_t)(x)<< 8) & 0xff0000) | \ 92e2c3e32aSNathan Whitehorn (((__uint32_t)(x) << 24) & 0xff000000)) 93e2c3e32aSNathan Whitehorn #define __bswap64_const(x) ((((__uint64_t)(x) >> 56) & 0xff) | \ 94e2c3e32aSNathan Whitehorn (((__uint64_t)(x) >> 40) & 0xff00) | \ 95e2c3e32aSNathan Whitehorn (((__uint64_t)(x) >> 24) & 0xff0000) | \ 96e2c3e32aSNathan Whitehorn (((__uint64_t)(x) >> 8) & 0xff000000) | \ 97e2c3e32aSNathan Whitehorn (((__uint64_t)(x) << 8) & ((__uint64_t)0xff << 32)) | \ 98e2c3e32aSNathan Whitehorn (((__uint64_t)(x) << 24) & ((__uint64_t)0xff << 40)) | \ 99e2c3e32aSNathan Whitehorn (((__uint64_t)(x) << 40) & ((__uint64_t)0xff << 48)) | \ 100e2c3e32aSNathan Whitehorn (((__uint64_t)(x) << 56) & ((__uint64_t)0xff << 56))) 101d846855dSMike Barcroft 102d846855dSMike Barcroft static __inline __uint16_t 103e2c3e32aSNathan Whitehorn __bswap16_var(__uint16_t _x) 104d846855dSMike Barcroft { 105d846855dSMike Barcroft 106d846855dSMike Barcroft return ((_x >> 8) | ((_x << 8) & 0xff00)); 107d846855dSMike Barcroft } 108d846855dSMike Barcroft 109d846855dSMike Barcroft static __inline __uint32_t 110e2c3e32aSNathan Whitehorn __bswap32_var(__uint32_t _x) 111d846855dSMike Barcroft { 112d846855dSMike Barcroft 113d846855dSMike Barcroft return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) | 114d846855dSMike Barcroft ((_x << 24) & 0xff000000)); 115d846855dSMike Barcroft } 116d846855dSMike Barcroft 117d846855dSMike Barcroft static __inline __uint64_t 118e2c3e32aSNathan Whitehorn __bswap64_var(__uint64_t _x) 119d846855dSMike Barcroft { 120d846855dSMike Barcroft 121d846855dSMike Barcroft return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) | 122d846855dSMike Barcroft ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) | 123d846855dSMike Barcroft ((_x << 24) & ((__uint64_t)0xff << 40)) | 124d846855dSMike Barcroft ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))); 125d846855dSMike Barcroft } 126d846855dSMike Barcroft 127*63d094a7SDimitry Andric #define __bswap16(x) ((__uint16_t)(__is_constant(x) ? __bswap16_const(x) : \ 128*63d094a7SDimitry Andric __bswap16_var(x))) 129e2c3e32aSNathan Whitehorn #define __bswap32(x) (__is_constant(x) ? __bswap32_const(x) : \ 130e2c3e32aSNathan Whitehorn __bswap32_var(x)) 131e2c3e32aSNathan Whitehorn #define __bswap64(x) (__is_constant(x) ? __bswap64_const(x) : \ 132e2c3e32aSNathan Whitehorn __bswap64_var(x)) 133e2c3e32aSNathan Whitehorn 134d846855dSMike Barcroft #define __htonl(x) ((__uint32_t)(x)) 135d846855dSMike Barcroft #define __htons(x) ((__uint16_t)(x)) 136d846855dSMike Barcroft #define __ntohl(x) ((__uint32_t)(x)) 137d846855dSMike Barcroft #define __ntohs(x) ((__uint16_t)(x)) 138fa561e1eSDavid E. O'Brien 139fd8e4ebcSMike Barcroft #endif /* !_MACHINE_ENDIAN_H_ */ 140