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