xref: /netbsd-src/sys/arch/amd64/include/byte_swap.h (revision 63587e37ee62a993fb53e201ae81ff76361b4eb6)
1*63587e37Srillig /*	$NetBSD: byte_swap.h,v 1.8 2021/04/17 20:12:55 rillig Exp $	*/
281918bf8Sfvdl 
381918bf8Sfvdl /*-
4a6cc3b46Sjoerg  * Copyright (c) 1998, 2010 The NetBSD Foundation, Inc.
581918bf8Sfvdl  * All rights reserved.
681918bf8Sfvdl  *
781918bf8Sfvdl  * This code is derived from software contributed to The NetBSD Foundation
881918bf8Sfvdl  * by Charles M. Hannum.
981918bf8Sfvdl  *
1081918bf8Sfvdl  * Redistribution and use in source and binary forms, with or without
1181918bf8Sfvdl  * modification, are permitted provided that the following conditions
1281918bf8Sfvdl  * are met:
1381918bf8Sfvdl  * 1. Redistributions of source code must retain the above copyright
1481918bf8Sfvdl  *    notice, this list of conditions and the following disclaimer.
1581918bf8Sfvdl  * 2. Redistributions in binary form must reproduce the above copyright
1681918bf8Sfvdl  *    notice, this list of conditions and the following disclaimer in the
1781918bf8Sfvdl  *    documentation and/or other materials provided with the distribution.
1881918bf8Sfvdl  *
1981918bf8Sfvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2081918bf8Sfvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2181918bf8Sfvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2281918bf8Sfvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2381918bf8Sfvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2481918bf8Sfvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2581918bf8Sfvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2681918bf8Sfvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2781918bf8Sfvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2881918bf8Sfvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2981918bf8Sfvdl  * POSSIBILITY OF SUCH DAMAGE.
3081918bf8Sfvdl  */
3181918bf8Sfvdl 
3281918bf8Sfvdl /*
3381918bf8Sfvdl  * Copy of the i386 version. 64 bit versions may be added later.
3481918bf8Sfvdl  */
3581918bf8Sfvdl 
3681918bf8Sfvdl #ifndef _AMD64_BYTE_SWAP_H_
3781918bf8Sfvdl #define	_AMD64_BYTE_SWAP_H_
3881918bf8Sfvdl 
39433b5ddeSmrg #ifdef __x86_64__
40433b5ddeSmrg 
41c88ae1f9Sdsl #ifdef  __GNUC__
4281918bf8Sfvdl #include <sys/types.h>
43c88ae1f9Sdsl __BEGIN_DECLS
4481918bf8Sfvdl 
45a6cc3b46Sjoerg #define	__BYTE_SWAP_U64_VARIABLE __byte_swap_u64_variable
46a6cc3b46Sjoerg static __inline uint64_t __byte_swap_u64_variable(uint64_t);
47a6cc3b46Sjoerg static __inline uint64_t
__byte_swap_u64_variable(uint64_t x)48a6cc3b46Sjoerg __byte_swap_u64_variable(uint64_t x)
49a6cc3b46Sjoerg {
50a6cc3b46Sjoerg 	__asm volatile ( "bswap %1" : "=r" (x) : "0" (x));
51a6cc3b46Sjoerg 	return (x);
52a6cc3b46Sjoerg }
53a6cc3b46Sjoerg 
54c88ae1f9Sdsl #define	__BYTE_SWAP_U32_VARIABLE __byte_swap_u32_variable
55c88ae1f9Sdsl static __inline uint32_t __byte_swap_u32_variable(uint32_t);
56c88ae1f9Sdsl static __inline uint32_t
__byte_swap_u32_variable(uint32_t x)57c88ae1f9Sdsl __byte_swap_u32_variable(uint32_t x)
5881918bf8Sfvdl {
595f1c88d7Sperry 	__asm volatile ( "bswap %1" : "=r" (x) : "0" (x));
6081918bf8Sfvdl 	return (x);
6181918bf8Sfvdl }
6281918bf8Sfvdl 
63c88ae1f9Sdsl #define	__BYTE_SWAP_U16_VARIABLE __byte_swap_u16_variable
64c88ae1f9Sdsl static __inline uint16_t __byte_swap_u16_variable(uint16_t);
65c88ae1f9Sdsl static __inline uint16_t
__byte_swap_u16_variable(uint16_t x)66c88ae1f9Sdsl __byte_swap_u16_variable(uint16_t x)
6781918bf8Sfvdl {
685f1c88d7Sperry 	__asm volatile ("rorw $8, %w1" : "=r" (x) : "0" (x));
6981918bf8Sfvdl 	return (x);
7081918bf8Sfvdl }
7181918bf8Sfvdl 
72c88ae1f9Sdsl __END_DECLS
73c88ae1f9Sdsl #endif
7481918bf8Sfvdl 
75433b5ddeSmrg #else	/*	__x86_64__	*/
76433b5ddeSmrg 
77433b5ddeSmrg #include <i386/byte_swap.h>
78433b5ddeSmrg 
79433b5ddeSmrg #endif	/*	__x86_64__	*/
80433b5ddeSmrg 
8181918bf8Sfvdl #endif /* !_AMD64_BYTE_SWAP_H_ */
82