xref: /minix3/sys/arch/i386/include/byte_swap.h (revision f6aac1c3b5ca21b829888677e8ee6bc9cda07e52)
1*f6aac1c3SLionel Sambuc /*	$NetBSD: byte_swap.h,v 1.15 2008/04/28 20:23:24 martin Exp $	*/
2*f6aac1c3SLionel Sambuc 
3*f6aac1c3SLionel Sambuc /*-
4*f6aac1c3SLionel Sambuc  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5*f6aac1c3SLionel Sambuc  * All rights reserved.
6*f6aac1c3SLionel Sambuc  *
7*f6aac1c3SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*f6aac1c3SLionel Sambuc  * by Charles M. Hannum.
9*f6aac1c3SLionel Sambuc  *
10*f6aac1c3SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*f6aac1c3SLionel Sambuc  * modification, are permitted provided that the following conditions
12*f6aac1c3SLionel Sambuc  * are met:
13*f6aac1c3SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*f6aac1c3SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*f6aac1c3SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*f6aac1c3SLionel Sambuc  *
19*f6aac1c3SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*f6aac1c3SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*f6aac1c3SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*f6aac1c3SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*f6aac1c3SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*f6aac1c3SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*f6aac1c3SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*f6aac1c3SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*f6aac1c3SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*f6aac1c3SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*f6aac1c3SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*f6aac1c3SLionel Sambuc  */
31*f6aac1c3SLionel Sambuc 
32*f6aac1c3SLionel Sambuc #ifndef _I386_BYTE_SWAP_H_
33*f6aac1c3SLionel Sambuc #define	_I386_BYTE_SWAP_H_
34*f6aac1c3SLionel Sambuc 
35*f6aac1c3SLionel Sambuc #include <sys/types.h>
36*f6aac1c3SLionel Sambuc 
37*f6aac1c3SLionel Sambuc #ifdef  __GNUC__
38*f6aac1c3SLionel Sambuc __BEGIN_DECLS
39*f6aac1c3SLionel Sambuc 
40*f6aac1c3SLionel Sambuc #define	__BYTE_SWAP_U32_VARIABLE __byte_swap_u32_variable
41*f6aac1c3SLionel Sambuc static __inline uint32_t __byte_swap_u32_variable(uint32_t);
42*f6aac1c3SLionel Sambuc static __inline uint32_t
__byte_swap_u32_variable(uint32_t x)43*f6aac1c3SLionel Sambuc __byte_swap_u32_variable(uint32_t x)
44*f6aac1c3SLionel Sambuc {
45*f6aac1c3SLionel Sambuc 	__asm volatile (
46*f6aac1c3SLionel Sambuc 	    "bswap %1"
47*f6aac1c3SLionel Sambuc 	    : "=r" (x) : "0" (x));
48*f6aac1c3SLionel Sambuc 	return (x);
49*f6aac1c3SLionel Sambuc }
50*f6aac1c3SLionel Sambuc 
51*f6aac1c3SLionel Sambuc #define	__BYTE_SWAP_U16_VARIABLE __byte_swap_u16_variable
52*f6aac1c3SLionel Sambuc static __inline uint16_t __byte_swap_u16_variable(uint16_t);
53*f6aac1c3SLionel Sambuc static __inline uint16_t
__byte_swap_u16_variable(uint16_t x)54*f6aac1c3SLionel Sambuc __byte_swap_u16_variable(uint16_t x)
55*f6aac1c3SLionel Sambuc {
56*f6aac1c3SLionel Sambuc 	__asm volatile ("rorw $8, %w1" : "=r" (x) : "0" (x));
57*f6aac1c3SLionel Sambuc 	return (x);
58*f6aac1c3SLionel Sambuc }
59*f6aac1c3SLionel Sambuc 
60*f6aac1c3SLionel Sambuc __END_DECLS
61*f6aac1c3SLionel Sambuc #elif defined(_KERNEL) || defined(_LKM)
62*f6aac1c3SLionel Sambuc #define	__BYTE_SWAP_U32_VARIABLE __byte_swap_u32_variable
63*f6aac1c3SLionel Sambuc #define	__BYTE_SWAP_U16_VARIABLE __byte_swap_u16_variable
64*f6aac1c3SLionel Sambuc uint32_t	__byte_swap_u32_variable(uint32_t);
65*f6aac1c3SLionel Sambuc uint16_t	__byte_swap_u16_variable(uint16_t);
66*f6aac1c3SLionel Sambuc #endif
67*f6aac1c3SLionel Sambuc 
68*f6aac1c3SLionel Sambuc #endif /* !_I386_BYTE_SWAP_H_ */
69