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 * 3. All advertising materials mentioning features or use of this software 14fa561e1eSDavid E. O'Brien * must display the following acknowledgement: 15fa561e1eSDavid E. O'Brien * This product includes software developed by the University of 16fa561e1eSDavid E. O'Brien * California, Berkeley and its contributors. 17fa561e1eSDavid E. O'Brien * 4. Neither the name of the University nor the names of its contributors 18fa561e1eSDavid E. O'Brien * may be used to endorse or promote products derived from this software 19fa561e1eSDavid E. O'Brien * without specific prior written permission. 20fa561e1eSDavid E. O'Brien * 21fa561e1eSDavid E. O'Brien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22fa561e1eSDavid E. O'Brien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23fa561e1eSDavid E. O'Brien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24fa561e1eSDavid E. O'Brien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25fa561e1eSDavid E. O'Brien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26fa561e1eSDavid E. O'Brien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27fa561e1eSDavid E. O'Brien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28fa561e1eSDavid E. O'Brien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29fa561e1eSDavid E. O'Brien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30fa561e1eSDavid E. O'Brien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31fa561e1eSDavid E. O'Brien * SUCH DAMAGE. 32fa561e1eSDavid E. O'Brien * 33fa561e1eSDavid E. O'Brien * @(#)endian.h 8.1 (Berkeley) 6/10/93 34fa561e1eSDavid E. O'Brien * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $ 35fa561e1eSDavid E. O'Brien * $FreeBSD$ 36fa561e1eSDavid E. O'Brien */ 37fa561e1eSDavid E. O'Brien 38fd8e4ebcSMike Barcroft #ifndef _MACHINE_ENDIAN_H_ 39fd8e4ebcSMike Barcroft #define _MACHINE_ENDIAN_H_ 40fd8e4ebcSMike Barcroft 41795aff0eSMike Barcroft #include <sys/cdefs.h> 42abbd8902SMike Barcroft #include <sys/_types.h> 43fa561e1eSDavid E. O'Brien 44fa561e1eSDavid E. O'Brien /* 45fa561e1eSDavid E. O'Brien * Define the order of 32-bit words in 64-bit words. 46fa561e1eSDavid E. O'Brien */ 47fa561e1eSDavid E. O'Brien #define _QUAD_HIGHWORD 0 48fa561e1eSDavid E. O'Brien #define _QUAD_LOWWORD 1 49fa561e1eSDavid E. O'Brien 50fa561e1eSDavid E. O'Brien /* 51fa561e1eSDavid E. O'Brien * Definitions for byte order, according to byte significance from low 52fa561e1eSDavid E. O'Brien * address to high. 53fa561e1eSDavid E. O'Brien */ 547f0f1cfdSMike Barcroft #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 557f0f1cfdSMike Barcroft #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 567f0f1cfdSMike Barcroft #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 57fa561e1eSDavid E. O'Brien 587f0f1cfdSMike Barcroft #define _BYTE_ORDER _BIG_ENDIAN 597f0f1cfdSMike Barcroft 607f0f1cfdSMike Barcroft /* 617f0f1cfdSMike Barcroft * Deprecated variants that don't have enough underscores to be useful in more 627f0f1cfdSMike Barcroft * strict namespaces. 637f0f1cfdSMike Barcroft */ 647f0f1cfdSMike Barcroft #if __BSD_VISIBLE 657f0f1cfdSMike Barcroft #define LITTLE_ENDIAN _LITTLE_ENDIAN 667f0f1cfdSMike Barcroft #define BIG_ENDIAN _BIG_ENDIAN 677f0f1cfdSMike Barcroft #define PDP_ENDIAN _PDP_ENDIAN 687f0f1cfdSMike Barcroft #define BYTE_ORDER _BYTE_ORDER 697f0f1cfdSMike Barcroft #endif 70fa561e1eSDavid E. O'Brien 71d846855dSMike Barcroft #ifdef __GNUC__ 72d846855dSMike Barcroft 73d846855dSMike Barcroft static __inline __uint16_t 74d846855dSMike Barcroft __bswap16(__uint16_t _x) 75d846855dSMike Barcroft { 76d846855dSMike Barcroft 77d846855dSMike Barcroft return ((_x >> 8) | ((_x << 8) & 0xff00)); 78d846855dSMike Barcroft } 79d846855dSMike Barcroft 80d846855dSMike Barcroft static __inline __uint32_t 81d846855dSMike Barcroft __bswap32(__uint32_t _x) 82d846855dSMike Barcroft { 83d846855dSMike Barcroft 84d846855dSMike Barcroft return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) | 85d846855dSMike Barcroft ((_x << 24) & 0xff000000)); 86d846855dSMike Barcroft } 87d846855dSMike Barcroft 88d846855dSMike Barcroft static __inline __uint64_t 89d846855dSMike Barcroft __bswap64(__uint64_t _x) 90d846855dSMike Barcroft { 91d846855dSMike Barcroft 92d846855dSMike Barcroft return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) | 93d846855dSMike Barcroft ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) | 94d846855dSMike Barcroft ((_x << 24) & ((__uint64_t)0xff << 40)) | 95d846855dSMike Barcroft ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))); 96d846855dSMike Barcroft } 97d846855dSMike Barcroft 98d846855dSMike Barcroft #endif /* __GNUC__ */ 99d846855dSMike Barcroft 100d846855dSMike Barcroft #define __htonl(x) ((__uint32_t)(x)) 101d846855dSMike Barcroft #define __htons(x) ((__uint16_t)(x)) 102d846855dSMike Barcroft #define __ntohl(x) ((__uint32_t)(x)) 103d846855dSMike Barcroft #define __ntohs(x) ((__uint16_t)(x)) 104fa561e1eSDavid E. O'Brien 105fd8e4ebcSMike Barcroft #endif /* !_MACHINE_ENDIAN_H_ */ 106