byte_swap.h revision 1.1
11.1Sfvdl/*	$NetBSD: byte_swap.h,v 1.1 2003/04/26 18:39:38 fvdl Exp $	*/
21.1Sfvdl
31.1Sfvdl/*-
41.1Sfvdl * Copyright (c) 1998 The NetBSD Foundation, Inc.
51.1Sfvdl * All rights reserved.
61.1Sfvdl *
71.1Sfvdl * This code is derived from software contributed to The NetBSD Foundation
81.1Sfvdl * by Charles M. Hannum.
91.1Sfvdl *
101.1Sfvdl * Redistribution and use in source and binary forms, with or without
111.1Sfvdl * modification, are permitted provided that the following conditions
121.1Sfvdl * are met:
131.1Sfvdl * 1. Redistributions of source code must retain the above copyright
141.1Sfvdl *    notice, this list of conditions and the following disclaimer.
151.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
161.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
171.1Sfvdl *    documentation and/or other materials provided with the distribution.
181.1Sfvdl * 3. All advertising materials mentioning features or use of this software
191.1Sfvdl *    must display the following acknowledgement:
201.1Sfvdl *        This product includes software developed by the NetBSD
211.1Sfvdl *        Foundation, Inc. and its contributors.
221.1Sfvdl * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Sfvdl *    contributors may be used to endorse or promote products derived
241.1Sfvdl *    from this software without specific prior written permission.
251.1Sfvdl *
261.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Sfvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Sfvdl * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Sfvdl * POSSIBILITY OF SUCH DAMAGE.
371.1Sfvdl */
381.1Sfvdl
391.1Sfvdl/*
401.1Sfvdl * Copy of the i386 version. 64 bit versions may be added later.
411.1Sfvdl */
421.1Sfvdl
431.1Sfvdl#ifndef _AMD64_BYTE_SWAP_H_
441.1Sfvdl#define	_AMD64_BYTE_SWAP_H_
451.1Sfvdl
461.1Sfvdl#include <sys/types.h>
471.1Sfvdl
481.1Sfvdlstatic __inline u_int32_t __byte_swap_long_variable(u_int32_t);
491.1Sfvdlstatic __inline u_int16_t __byte_swap_word_variable(u_int16_t);
501.1Sfvdl
511.1Sfvdlstatic __inline u_int32_t
521.1Sfvdl__byte_swap_long_variable(u_int32_t x)
531.1Sfvdl{
541.1Sfvdl	__asm __volatile ( "bswap %1" : "=r" (x) : "0" (x));
551.1Sfvdl	return (x);
561.1Sfvdl}
571.1Sfvdl
581.1Sfvdlstatic __inline u_int16_t
591.1Sfvdl__byte_swap_word_variable(u_int16_t x)
601.1Sfvdl{
611.1Sfvdl	__asm __volatile ("rorw $8, %w1" : "=r" (x) : "0" (x));
621.1Sfvdl	return (x);
631.1Sfvdl}
641.1Sfvdl
651.1Sfvdl
661.1Sfvdl#ifdef __OPTIMIZE__
671.1Sfvdl
681.1Sfvdl#define	__byte_swap_long_constant(x) \
691.1Sfvdl	((((x) & 0xff000000) >> 24) | \
701.1Sfvdl	 (((x) & 0x00ff0000) >>  8) | \
711.1Sfvdl	 (((x) & 0x0000ff00) <<  8) | \
721.1Sfvdl	 (((x) & 0x000000ff) << 24))
731.1Sfvdl#define	__byte_swap_word_constant(x) \
741.1Sfvdl	((((x) & 0xff00) >> 8) | \
751.1Sfvdl	 (((x) & 0x00ff) << 8))
761.1Sfvdl#define	__byte_swap_long(x) \
771.1Sfvdl	(__builtin_constant_p((x)) ? \
781.1Sfvdl	 __byte_swap_long_constant(x) : __byte_swap_long_variable(x))
791.1Sfvdl#define	__byte_swap_word(x) \
801.1Sfvdl	(__builtin_constant_p((x)) ? \
811.1Sfvdl	 __byte_swap_word_constant(x) : __byte_swap_word_variable(x))
821.1Sfvdl
831.1Sfvdl#else /* __OPTIMIZE__ */
841.1Sfvdl
851.1Sfvdl#define	__byte_swap_long(x)	__byte_swap_long_variable(x)
861.1Sfvdl#define	__byte_swap_word(x)	__byte_swap_word_variable(x)
871.1Sfvdl
881.1Sfvdl#endif /* __OPTIMIZE__ */
891.1Sfvdl
901.1Sfvdl#endif /* !_AMD64_BYTE_SWAP_H_ */
91