195b1d16dSTijl Coosemans /*- 295b1d16dSTijl Coosemans * Copyright (c) 1987, 1991 Regents of the University of California. 395b1d16dSTijl Coosemans * All rights reserved. 495b1d16dSTijl Coosemans * 595b1d16dSTijl Coosemans * Redistribution and use in source and binary forms, with or without 695b1d16dSTijl Coosemans * modification, are permitted provided that the following conditions 795b1d16dSTijl Coosemans * are met: 895b1d16dSTijl Coosemans * 1. Redistributions of source code must retain the above copyright 995b1d16dSTijl Coosemans * notice, this list of conditions and the following disclaimer. 1095b1d16dSTijl Coosemans * 2. Redistributions in binary form must reproduce the above copyright 1195b1d16dSTijl Coosemans * notice, this list of conditions and the following disclaimer in the 1295b1d16dSTijl Coosemans * documentation and/or other materials provided with the distribution. 1395b1d16dSTijl Coosemans * 4. Neither the name of the University nor the names of its contributors 1495b1d16dSTijl Coosemans * may be used to endorse or promote products derived from this software 1595b1d16dSTijl Coosemans * without specific prior written permission. 1695b1d16dSTijl Coosemans * 1795b1d16dSTijl Coosemans * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1895b1d16dSTijl Coosemans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1995b1d16dSTijl Coosemans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2095b1d16dSTijl Coosemans * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2195b1d16dSTijl Coosemans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2295b1d16dSTijl Coosemans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2395b1d16dSTijl Coosemans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2495b1d16dSTijl Coosemans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2595b1d16dSTijl Coosemans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2695b1d16dSTijl Coosemans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2795b1d16dSTijl Coosemans * SUCH DAMAGE. 2895b1d16dSTijl Coosemans * 2995b1d16dSTijl Coosemans * @(#)endian.h 7.8 (Berkeley) 4/3/91 3095b1d16dSTijl Coosemans * $FreeBSD$ 3195b1d16dSTijl Coosemans */ 3295b1d16dSTijl Coosemans 3395b1d16dSTijl Coosemans #ifndef _MACHINE_ENDIAN_H_ 3495b1d16dSTijl Coosemans #define _MACHINE_ENDIAN_H_ 3595b1d16dSTijl Coosemans 3695b1d16dSTijl Coosemans #include <sys/cdefs.h> 3795b1d16dSTijl Coosemans #include <sys/_types.h> 3895b1d16dSTijl Coosemans 3995b1d16dSTijl Coosemans /* 4095b1d16dSTijl Coosemans * Define the order of 32-bit words in 64-bit words. 4195b1d16dSTijl Coosemans */ 4295b1d16dSTijl Coosemans #define _QUAD_HIGHWORD 1 4395b1d16dSTijl Coosemans #define _QUAD_LOWWORD 0 4495b1d16dSTijl Coosemans 4595b1d16dSTijl Coosemans /* 4695b1d16dSTijl Coosemans * Definitions for byte order, according to byte significance from low 4795b1d16dSTijl Coosemans * address to high. 4895b1d16dSTijl Coosemans */ 4995b1d16dSTijl Coosemans #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ 5095b1d16dSTijl Coosemans #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ 5195b1d16dSTijl Coosemans #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ 5295b1d16dSTijl Coosemans 5395b1d16dSTijl Coosemans #define _BYTE_ORDER _LITTLE_ENDIAN 5495b1d16dSTijl Coosemans 5595b1d16dSTijl Coosemans /* 5695b1d16dSTijl Coosemans * Deprecated variants that don't have enough underscores to be useful in more 5795b1d16dSTijl Coosemans * strict namespaces. 5895b1d16dSTijl Coosemans */ 5995b1d16dSTijl Coosemans #if __BSD_VISIBLE 6095b1d16dSTijl Coosemans #define LITTLE_ENDIAN _LITTLE_ENDIAN 6195b1d16dSTijl Coosemans #define BIG_ENDIAN _BIG_ENDIAN 6295b1d16dSTijl Coosemans #define PDP_ENDIAN _PDP_ENDIAN 6395b1d16dSTijl Coosemans #define BYTE_ORDER _BYTE_ORDER 6495b1d16dSTijl Coosemans #endif 6595b1d16dSTijl Coosemans 66*4715a95fSDimitry Andric #define __bswap16_gen(x) (__uint16_t)((x) << 8 | (x) >> 8) 6705024677STijl Coosemans #define __bswap32_gen(x) \ 68*4715a95fSDimitry Andric (((__uint32_t)__bswap16(x) << 16) | __bswap16((x) >> 16)) 6905024677STijl Coosemans #define __bswap64_gen(x) \ 70*4715a95fSDimitry Andric (((__uint64_t)__bswap32(x) << 32) | __bswap32((x) >> 32)) 7195b1d16dSTijl Coosemans 7205024677STijl Coosemans #ifdef __GNUCLIKE_BUILTIN_CONSTANT_P 7305024677STijl Coosemans #define __bswap16(x) \ 7463d094a7SDimitry Andric ((__uint16_t)(__builtin_constant_p(x) ? \ 7563d094a7SDimitry Andric __bswap16_gen((__uint16_t)(x)) : __bswap16_var(x))) 7605024677STijl Coosemans #define __bswap32(x) \ 7705024677STijl Coosemans (__builtin_constant_p(x) ? \ 7805024677STijl Coosemans __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x)) 7905024677STijl Coosemans #define __bswap64(x) \ 8005024677STijl Coosemans (__builtin_constant_p(x) ? \ 8105024677STijl Coosemans __bswap64_gen((__uint64_t)(x)) : __bswap64_var(x)) 8205024677STijl Coosemans #else 8305024677STijl Coosemans /* XXX these are broken for use in static initializers. */ 8405024677STijl Coosemans #define __bswap16(x) __bswap16_var(x) 8505024677STijl Coosemans #define __bswap32(x) __bswap32_var(x) 8605024677STijl Coosemans #define __bswap64(x) __bswap64_var(x) 8705024677STijl Coosemans #endif 8895b1d16dSTijl Coosemans 8905024677STijl Coosemans /* These are defined as functions to avoid multiple evaluation of x. */ 9095b1d16dSTijl Coosemans 9195b1d16dSTijl Coosemans static __inline __uint16_t 9295b1d16dSTijl Coosemans __bswap16_var(__uint16_t _x) 9395b1d16dSTijl Coosemans { 9495b1d16dSTijl Coosemans 9505024677STijl Coosemans return (__bswap16_gen(_x)); 9695b1d16dSTijl Coosemans } 9795b1d16dSTijl Coosemans 9895b1d16dSTijl Coosemans static __inline __uint32_t 9995b1d16dSTijl Coosemans __bswap32_var(__uint32_t _x) 10095b1d16dSTijl Coosemans { 10195b1d16dSTijl Coosemans 10205024677STijl Coosemans #ifdef __GNUCLIKE_ASM 10395b1d16dSTijl Coosemans __asm("bswap %0" : "+r" (_x)); 10495b1d16dSTijl Coosemans return (_x); 10505024677STijl Coosemans #else 10605024677STijl Coosemans return (__bswap32_gen(_x)); 10705024677STijl Coosemans #endif 10895b1d16dSTijl Coosemans } 10995b1d16dSTijl Coosemans 11095b1d16dSTijl Coosemans static __inline __uint64_t 11195b1d16dSTijl Coosemans __bswap64_var(__uint64_t _x) 11295b1d16dSTijl Coosemans { 11305024677STijl Coosemans 11405024677STijl Coosemans #if defined(__amd64__) && defined(__GNUCLIKE_ASM) 11595b1d16dSTijl Coosemans __asm("bswap %0" : "+r" (_x)); 11695b1d16dSTijl Coosemans return (_x); 11795b1d16dSTijl Coosemans #else 11805024677STijl Coosemans /* 11905024677STijl Coosemans * It is important for the optimizations that the following is not 12005024677STijl Coosemans * really generic, but expands to 2 __bswap32_var()'s. 12105024677STijl Coosemans */ 12205024677STijl Coosemans return (__bswap64_gen(_x)); 12395b1d16dSTijl Coosemans #endif 12495b1d16dSTijl Coosemans } 12595b1d16dSTijl Coosemans 12695b1d16dSTijl Coosemans #define __htonl(x) __bswap32(x) 12795b1d16dSTijl Coosemans #define __htons(x) __bswap16(x) 12895b1d16dSTijl Coosemans #define __ntohl(x) __bswap32(x) 12995b1d16dSTijl Coosemans #define __ntohs(x) __bswap16(x) 13095b1d16dSTijl Coosemans 13195b1d16dSTijl Coosemans #endif /* !_MACHINE_ENDIAN_H_ */ 132