1178172Simp/*- 2178172Simp * Copyright (c) 1987, 1991 Regents of the University of California. 3178172Simp * All rights reserved. 4178172Simp * 5178172Simp * Redistribution and use in source and binary forms, with or without 6178172Simp * modification, are permitted provided that the following conditions 7178172Simp * are met: 8178172Simp * 1. Redistributions of source code must retain the above copyright 9178172Simp * notice, this list of conditions and the following disclaimer. 10178172Simp * 2. Redistributions in binary form must reproduce the above copyright 11178172Simp * notice, this list of conditions and the following disclaimer in the 12178172Simp * documentation and/or other materials provided with the distribution. 13178172Simp * 4. Neither the name of the University nor the names of its contributors 14178172Simp * may be used to endorse or promote products derived from this software 15178172Simp * without specific prior written permission. 16178172Simp * 17178172Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18178172Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19178172Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20178172Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21178172Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22178172Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23178172Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24178172Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25178172Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26178172Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27178172Simp * SUCH DAMAGE. 28178172Simp * 29178172Simp * @(#)endian.h 7.8 (Berkeley) 4/3/91 30178172Simp * $FreeBSD$ 31178172Simp */ 32178172Simp 33178172Simp#ifndef _MACHINE_ENDIAN_H_ 34178172Simp#define _MACHINE_ENDIAN_H_ 35178172Simp 36178172Simp#include <sys/cdefs.h> 37178172Simp#ifndef __ASSEMBLER__ 38178172Simp#include <sys/_types.h> 39178172Simp#endif 40178172Simp 41178172Simp#ifdef __cplusplus 42178172Simpextern "C" { 43178172Simp#endif 44178172Simp 45178172Simp/* 46178172Simp * Definitions for byte order, according to byte significance from low 47178172Simp * address to high. 48178172Simp */ 49178172Simp#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 50178172Simp#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 51178172Simp#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 52178172Simp 53178172Simp#ifdef __MIPSEB__ 54178172Simp#define _BYTE_ORDER _BIG_ENDIAN 55178172Simp#else 56178172Simp#define _BYTE_ORDER _LITTLE_ENDIAN 57178172Simp#endif /* __MIBSEB__ */ 58178172Simp 59178172Simp/* 60178172Simp * Deprecated variants that don't have enough underscores to be useful in more 61178172Simp * strict namespaces. 62178172Simp */ 63178172Simp#if __BSD_VISIBLE 64178172Simp#define LITTLE_ENDIAN _LITTLE_ENDIAN 65178172Simp#define BIG_ENDIAN _BIG_ENDIAN 66178172Simp#define PDP_ENDIAN _PDP_ENDIAN 67178172Simp#define BYTE_ORDER _BYTE_ORDER 68178172Simp#endif 69178172Simp 70178172Simp#ifndef __ASSEMBLER__ 71178172Simp#if defined(__GNUCLIKE_BUILTIN_CONSTANT_P) && defined(__OPTIMIZE__) 72178172Simp#define __is_constant(x) __builtin_constant_p(x) 73178172Simp#else 74178172Simp#define __is_constant(x) 0 75178172Simp#endif 76178172Simp 77178172Simp#define __bswap16_const(x) (((x) >> 8) | (((x) << 8) & 0xff00)) 78178172Simp#define __bswap32_const(x) (((x) >> 24) | (((x) >> 8) & 0xff00) | \ 79178172Simp (((x) << 8) & 0xff0000) | (((x) << 24) & 0xff000000)) 80178172Simp#define __bswap64_const(x) (((x) >> 56) | (((x) >> 40) & 0xff00) | \ 81178172Simp (((x) >> 24) & 0xff0000) | (((x) >> 8) & 0xff000000) | \ 82178172Simp (((x) << 8) & ((__uint64_t)0xff << 32)) | \ 83178172Simp (((x) << 24) & ((__uint64_t)0xff << 40)) | \ 84178172Simp (((x) << 40) & ((__uint64_t)0xff << 48)) | (((x) << 56))) 85178172Simp 86178172Simpstatic __inline __uint16_t 87178172Simp__bswap16_var(__uint16_t _x) 88178172Simp{ 89178172Simp 90178172Simp return ((_x >> 8) | ((_x << 8) & 0xff00)); 91178172Simp} 92178172Simp 93178172Simpstatic __inline __uint32_t 94178172Simp__bswap32_var(__uint32_t _x) 95178172Simp{ 96178172Simp 97178172Simp return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) | 98178172Simp ((_x << 24) & 0xff000000)); 99178172Simp} 100178172Simp 101178172Simpstatic __inline __uint64_t 102178172Simp__bswap64_var(__uint64_t _x) 103178172Simp{ 104178172Simp 105178172Simp return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) | 106178172Simp ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) | 107178172Simp ((_x << 24) & ((__uint64_t)0xff << 40)) | 108178172Simp ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))); 109178172Simp} 110178172Simp 111211159Sneel#define __bswap16(x) ((__uint16_t)(__is_constant((x)) ? \ 112211159Sneel __bswap16_const((__uint16_t)(x)) : __bswap16_var((__uint16_t)(x)))) 113211159Sneel#define __bswap32(x) ((__uint32_t)(__is_constant((x)) ? \ 114211159Sneel __bswap32_const((__uint32_t)(x)) : __bswap32_var((__uint32_t)(x)))) 115211159Sneel#define __bswap64(x) ((__uint64_t)(__is_constant((x)) ? \ 116211159Sneel __bswap64_const((__uint64_t)(x)) : __bswap64_var((__uint64_t)(x)))) 117178172Simp 118178172Simp#ifdef __MIPSEB__ 119178172Simp#define __htonl(x) ((__uint32_t)(x)) 120178172Simp#define __htons(x) ((__uint16_t)(x)) 121178172Simp#define __ntohl(x) ((__uint32_t)(x)) 122178172Simp#define __ntohs(x) ((__uint16_t)(x)) 123178172Simp/* 124178172Simp * Define the order of 32-bit words in 64-bit words. 125178172Simp */ 126178172Simp/* 127178172Simp * XXXMIPS: Additional parentheses to make gcc more happy. 128178172Simp */ 129178172Simp#define _QUAD_HIGHWORD 0 130178172Simp#define _QUAD_LOWWORD 1 131178172Simp#else 132178172Simp#define _QUAD_HIGHWORD 1 133178172Simp#define _QUAD_LOWWORD 0 134178172Simp#define __ntohl(x) (__bswap32((x))) 135178172Simp#define __ntohs(x) (__bswap16((x))) 136178172Simp#define __htonl(x) (__bswap32((x))) 137178172Simp#define __htons(x) (__bswap16((x))) 138178172Simp#endif /* _MIPSEB */ 139178172Simp 140178172Simp#endif /* _ASSEMBLER_ */ 141178172Simp 142178172Simp#ifdef __cplusplus 143178172Simp} 144178172Simp#endif 145178172Simp 146178172Simp#endif /* !_MACHINE_ENDIAN_H_ */ 147