endian.h revision 133012
170651Sobrien/* 270651Sobrien * Copyright (c) 2001 David E. O'Brien 370651Sobrien * 470651Sobrien * Redistribution and use in source and binary forms, with or without 570651Sobrien * modification, are permitted provided that the following conditions 670651Sobrien * are met: 770651Sobrien * 1. Redistributions of source code must retain the above copyright 870651Sobrien * notice, this list of conditions and the following disclaimer. 970651Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1070651Sobrien * notice, this list of conditions and the following disclaimer in the 1170651Sobrien * documentation and/or other materials provided with the distribution. 1270651Sobrien * 3. Neither the name of the University nor the names of its contributors 1370651Sobrien * may be used to endorse or promote products derived from this software 1470651Sobrien * without specific prior written permission. 1570651Sobrien * 1670651Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1770651Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1870651Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1970651Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2070651Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2170651Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2270651Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2370651Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2470651Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2570651Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2670651Sobrien * SUCH DAMAGE. 2770651Sobrien * 2870651Sobrien * @(#)endian.h 8.1 (Berkeley) 6/10/93 2970651Sobrien * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $ 3070651Sobrien * $FreeBSD: head/sys/arm/include/endian.h 133012 2004-08-02 12:24:18Z cognet $ 3170651Sobrien */ 3270651Sobrien 3370651Sobrien#ifndef _ENDIAN_H_ 3470651Sobrien#define _ENDIAN_H_ 3570651Sobrien 36128938Scognet#include <sys/_types.h> 3770651Sobrien 38128938Scognet/* 39128938Scognet * Definitions for byte order, according to byte significance from low 40128938Scognet * address to high. 41128938Scognet */ 42128938Scognet#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 43128938Scognet#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 44128938Scognet#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 45128938Scognet 46128938Scognet#define _BYTE_ORDER _LITTLE_ENDIAN 47128938Scognet 48128938Scognet#if __BSD_VISIBLE 49128938Scognet#define LITTLE_ENDIAN _LITTLE_ENDIAN 50128938Scognet#define BIG_ENDIAN _BIG_ENDIAN 51128938Scognet#define PDP_ENDIAN _PDP_ENDIAN 52128938Scognet#define BYTE_ORDER _BYTE_ORDER 53128938Scognet#endif 54128938Scognet 55128938Scognet#define _QUAD_HIGHWORD 1 56128938Scognet#define _QUAD_LOWWORD 0 57128938Scognet#define __ntohl(x) (__bswap32(x)) 58128938Scognet#define __ntohs(x) (__bswap16(x)) 59133012Scognet#define __htonl(x) (__bswap32(x)) 60133012Scognet#define __htons(x) (__bswap16(x)) 61128938Scognet 62128938Scognetstatic __inline __uint64_t 63128938Scognet__bswap64(__uint64_t _x) 64128938Scognet{ 65128938Scognet 66128938Scognet return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) | 67128938Scognet ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) | 68128938Scognet ((_x << 24) & ((__uint64_t)0xff << 40)) | 69128938Scognet ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))); 70128938Scognet} 71128938Scognet 72128938Scognetstatic __inline __uint32_t 73128938Scognet__bswap32(__uint32_t v) 74128938Scognet{ 75128938Scognet __uint32_t t1; 76128938Scognet 77128938Scognet t1 = v ^ ((v << 16) | (v >> 16)); 78128938Scognet t1 &= 0xff00ffffU; 79128938Scognet v = (v >> 8) | (v << 24); 80128938Scognet v ^= (t1 >> 8); 81128938Scognet 82128938Scognet return (v); 83128938Scognet } 84128938Scognet 85128938Scognetstatic __inline __uint16_t 86128938Scognet__bswap16(__uint32_t v) 87128938Scognet{ 88128938Scognet __asm __volatile( 89128938Scognet "mov %0, %1, ror #8\n" 90128938Scognet "orr %0, %0, %0, lsr #16\n" 91128938Scognet "bic %0, %0, %0, lsl #16" 92128938Scognet : "=r" (v) 93128938Scognet : "0" (v)); 94128938Scognet 95128938Scognet return (v); 96128938Scognet} 9770651Sobrien#endif /* !_ENDIAN_H_ */ 98