1/* 2 * Copyright (c) 2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Gabe Black 29 */ 30 31//The purpose of this file is to provide endainness conversion utility 32//functions. Depending on the endianness of the guest system, either 33//the LittleEndianGuest or BigEndianGuest namespace is used. 34 35#ifndef __SIM_BYTE_SWAP_HH__ 36#define __SIM_BYTE_SWAP_HH__ 37 38#include "sim/host.hh" 39 40// This lets us figure out what the byte order of the host system is 41#if defined(linux) 42#include <endian.h> 43// If this is a linux system, lets used the optimized definitions if they exist. 44// If one doesn't exist, we pretty much get what is listed below, so it all 45// works out 46#include <byteswap.h> 47#else 48#include <machine/endian.h> 49#endif 50 51//These functions actually perform the swapping for parameters 52//of various bit lengths 53static inline uint64_t 54swap_byte64(uint64_t x) 55{ 56#if defined(linux) 57 return bswap_64(x); 58#else 59 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) | 60 ((uint64_t)(x) & 0xff00ULL) << 40 | 61 ((uint64_t)(x) & 0xff0000ULL) << 24 | 62 ((uint64_t)(x) & 0xff000000ULL) << 8 | 63 ((uint64_t)(x) & 0xff00000000ULL) >> 8 | 64 ((uint64_t)(x) & 0xff0000000000ULL) >> 24 | 65 ((uint64_t)(x) & 0xff000000000000ULL) >> 40 | 66 ((uint64_t)(x) & 0xff00000000000000ULL) >> 56) ; 67#endif 68} 69 70static inline uint32_t 71swap_byte32(uint32_t x) 72{ 73#if defined(linux) 74 return bswap_32(x); 75#else 76 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 | 77 ((uint32_t)(x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 | 78 ((uint32_t)(x) & 0xff000000) >> 24); 79#endif 80} 81 82static inline uint16_t 83swap_byte16(uint16_t x) 84{ 85#if defined(linux) 86 return bswap_16(x); 87#else 88 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 89 ((uint16_t)(x) & 0xff00) >> 8); 90#endif 91} 92
|