11849Swollman/* $NetBSD: byte_swap.h,v 1.9.58.1 2013/07/23 21:07:34 riastradh Exp $ */ 21849Swollman 31849Swollman/*- 41849Swollman * Copyright (c) 1998 The NetBSD Foundation, Inc. 51849Swollman * All rights reserved. 61849Swollman * 71849Swollman * This code is derived from software contributed to The NetBSD Foundation 81849Swollman * by Leo Weppelman. 91849Swollman * 101849Swollman * Redistribution and use in source and binary forms, with or without 111849Swollman * modification, are permitted provided that the following conditions 121849Swollman * are met: 131849Swollman * 1. Redistributions of source code must retain the above copyright 141849Swollman * notice, this list of conditions and the following disclaimer. 151849Swollman * 2. Redistributions in binary form must reproduce the above copyright 161849Swollman * notice, this list of conditions and the following disclaimer in the 171849Swollman * documentation and/or other materials provided with the distribution. 181849Swollman * 191849Swollman * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201849Swollman * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211849Swollman * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221849Swollman * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231849Swollman * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241849Swollman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251849Swollman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261849Swollman * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271849Swollman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281849Swollman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291849Swollman * POSSIBILITY OF SUCH DAMAGE. 301849Swollman */ 311849Swollman 321849Swollman#ifndef M68K_BYTE_SWAP_H_ 3392999Sobrien#define M68K_BYTE_SWAP_H_ 3492999Sobrien 3592999Sobrien#ifdef __GNUC__ 3692999Sobrien#include <sys/types.h> 3792999Sobrien__BEGIN_DECLS 381849Swollman 391849Swollman 401849Swollman#define __BYTE_SWAP_U16_VARIABLE __byte_swap_u16_variable 411849Swollmanstatic __inline uint16_t __byte_swap_u16_variable(uint16_t); 421849Swollmanstatic __inline uint16_t 431849Swollman__byte_swap_u16_variable(uint16_t var) 441849Swollman{ 4515634Speter#if defined(__mcfisac__) 461849Swollman __asm volatile ("swap %0; byterev %0" : "=d"(var) : "0" (var)); 471849Swollman#elif defined(__mcoldfire__) 481849Swollman return (var >> 8) || (var << 8); 491849Swollman#else 501849Swollman __asm volatile ("rorw #8, %0" : "=d" (var) : "0" (var)); 5115634Speter return (var); 5215634Speter#endif 53184548Speter} 54217106Skib 55217106Skib#define __BYTE_SWAP_U32_VARIABLE __byte_swap_u32_variable 56static __inline uint32_t __byte_swap_u32_variable(uint32_t); 57static __inline uint32_t 58__byte_swap_u32_variable(uint32_t var) 59{ 60#if defined(__mcfisac__) 61 __asm volatile ("byterev %0" : "=d"(var) : "0" (var)); 62#elif defined(__mcoldfire__) 63 return (var >> 24) | (var << 24) | ((var & 0x00ff0000) >> 8) 64 | ((var << 8) & 0x00ff0000); 65#else 66 __asm volatile ( 67 "rorw #8, %0; swap %0; rorw #8, %0" : "=d" (var) : "0" (var)); 68#endif 69 return (var); 70} 71 72__END_DECLS 73#endif 74 75#endif /* !M68K_BYTE_SWAP_H_ */ 76