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