1139735Simp/*-
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$
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
46140425Scognet#ifdef __ARMEB__
47140425Scognet#define _BYTE_ORDER	_BIG_ENDIAN
48140425Scognet#else
49128938Scognet#define	_BYTE_ORDER	_LITTLE_ENDIAN
50140425Scognet#endif /* __ARMEB__ */
51128938Scognet
52128938Scognet#if __BSD_VISIBLE
53128938Scognet#define LITTLE_ENDIAN   _LITTLE_ENDIAN
54128938Scognet#define BIG_ENDIAN      _BIG_ENDIAN
55128938Scognet#define PDP_ENDIAN      _PDP_ENDIAN
56128938Scognet#define BYTE_ORDER      _BYTE_ORDER
57128938Scognet#endif
58128938Scognet
59140425Scognet#ifdef __ARMEB__
60140425Scognet#define _QUAD_HIGHWORD 0
61140425Scognet#define _QUAD_LOWWORD 1
62140425Scognet#define __ntohl(x)	((__uint32_t)(x))
63140425Scognet#define __ntohs(x)	((__uint16_t)(x))
64140425Scognet#define __htonl(x)	((__uint32_t)(x))
65140425Scognet#define __htons(x)	((__uint16_t)(x))
66140425Scognet#else
67128938Scognet#define _QUAD_HIGHWORD  1
68128938Scognet#define _QUAD_LOWWORD 0
69128938Scognet#define __ntohl(x)        (__bswap32(x))
70128938Scognet#define __ntohs(x)        (__bswap16(x))
71133012Scognet#define __htonl(x)        (__bswap32(x))
72133012Scognet#define __htons(x)        (__bswap16(x))
73140425Scognet#endif /* __ARMEB__ */
74128938Scognet
75128938Scognetstatic __inline __uint64_t
76128938Scognet__bswap64(__uint64_t _x)
77128938Scognet{
78128938Scognet
79128938Scognet	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
80128938Scognet	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
81236992Simp	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
82128938Scognet	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
83128938Scognet}
84128938Scognet
85128938Scognetstatic __inline __uint32_t
86136033Scognet__bswap32_var(__uint32_t v)
87128938Scognet{
88128938Scognet	__uint32_t t1;
89128938Scognet
90146592Scognet	__asm __volatile("eor %1, %0, %0, ror #16\n"
91146592Scognet	    		"bic %1, %1, #0x00ff0000\n"
92146592Scognet			"mov %0, %0, ror #8\n"
93146592Scognet			"eor %0, %0, %1, lsr #8\n"
94146592Scognet			 : "+r" (v), "=r" (t1));
95290648Smmel
96128938Scognet	return (v);
97182086Simp}
98128938Scognet
99128938Scognetstatic __inline __uint16_t
100146592Scognet__bswap16_var(__uint16_t v)
101128938Scognet{
102172104Scognet	__uint32_t ret = v & 0xffff;
103172104Scognet
104128938Scognet	__asm __volatile(
105148452Sjhb	    "mov    %0, %0, ror #8\n"
106128938Scognet	    "orr    %0, %0, %0, lsr #16\n"
107128938Scognet	    "bic    %0, %0, %0, lsl #16"
108172104Scognet	    : "+r" (ret));
109290648Smmel
110172104Scognet	return ((__uint16_t)ret);
111290648Smmel}
112136033Scognet
113136033Scognet#ifdef __OPTIMIZE__
114136033Scognet
115136033Scognet#define __bswap32_constant(x)	\
116136033Scognet    ((((x) & 0xff000000U) >> 24) |	\
117136033Scognet     (((x) & 0x00ff0000U) >>  8) |	\
118136033Scognet     (((x) & 0x0000ff00U) <<  8) |	\
119136033Scognet     (((x) & 0x000000ffU) << 24))
120136033Scognet
121136033Scognet#define __bswap16_constant(x)	\
122136033Scognet    ((((x) & 0xff00) >> 8) |		\
123136033Scognet     (((x) & 0x00ff) << 8))
124136033Scognet
125136033Scognet#define __bswap16(x)	\
126137216Scognet    ((__uint16_t)(__builtin_constant_p(x) ?	\
127137216Scognet     __bswap16_constant(x) :			\
128137216Scognet     __bswap16_var(x)))
129136033Scognet
130136033Scognet#define __bswap32(x)	\
131137216Scognet    ((__uint32_t)(__builtin_constant_p(x) ? 	\
132137216Scognet     __bswap32_constant(x) :			\
133137216Scognet     __bswap32_var(x)))
134136033Scognet
135136033Scognet#else
136136033Scognet#define __bswap16(x)	__bswap16_var(x)
137136033Scognet#define __bswap32(x)	__bswap32_var(x)
138136033Scognet
139136033Scognet#endif /* __OPTIMIZE__ */
14070651Sobrien#endif /* !_ENDIAN_H_ */
141