byteswap.hh (3980:9bcb2a2e9bb8) | byteswap.hh (4176:2d52a9751dfc) |
---|---|
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; --- 23 unchanged lines hidden (view full) --- 32 33//The purpose of this file is to provide endainness conversion utility 34//functions. Depending on the endianness of the guest system, either 35//the LittleEndianGuest or BigEndianGuest namespace is used. 36 37#ifndef __SIM_BYTE_SWAP_HH__ 38#define __SIM_BYTE_SWAP_HH__ 39 | 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; --- 23 unchanged lines hidden (view full) --- 32 33//The purpose of this file is to provide endainness conversion utility 34//functions. Depending on the endianness of the guest system, either 35//the LittleEndianGuest or BigEndianGuest namespace is used. 36 37#ifndef __SIM_BYTE_SWAP_HH__ 38#define __SIM_BYTE_SWAP_HH__ 39 |
40#include "base/bigint.hh" |
|
40#include "base/misc.hh" 41#include "sim/host.hh" 42 43// This lets us figure out what the byte order of the host system is 44#if defined(linux) 45#include <endian.h> 46// If this is a linux system, lets used the optimized definitions if they exist. 47// If one doesn't exist, we pretty much get what is listed below, so it all --- 56 unchanged lines hidden (view full) --- 104#else 105 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 106 ((uint16_t)(x) & 0xff00) >> 8); 107#endif 108} 109 110// This function lets the compiler figure out how to call the 111// swap_byte functions above for different data types. Since the | 41#include "base/misc.hh" 42#include "sim/host.hh" 43 44// This lets us figure out what the byte order of the host system is 45#if defined(linux) 46#include <endian.h> 47// If this is a linux system, lets used the optimized definitions if they exist. 48// If one doesn't exist, we pretty much get what is listed below, so it all --- 56 unchanged lines hidden (view full) --- 105#else 106 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 | 107 ((uint16_t)(x) & 0xff00) >> 8); 108#endif 109} 110 111// This function lets the compiler figure out how to call the 112// swap_byte functions above for different data types. Since the |
112// sizeof() values are known at compiel time, it should inline to a | 113// sizeof() values are known at compile time, it should inline to a |
113// direct call to the right swap_byteNN() function. 114template <typename T> 115static inline T swap_byte(T x) { 116 if (sizeof(T) == 8) 117 return swap_byte64((uint64_t)x); 118 else if (sizeof(T) == 4) 119 return swap_byte32((uint32_t)x); 120 else if (sizeof(T) == 2) 121 return swap_byte16((uint16_t)x); 122 else if (sizeof(T) == 1) 123 return x; 124 else 125 panic("Can't byte-swap values larger than 64 bits"); 126} 127 | 114// direct call to the right swap_byteNN() function. 115template <typename T> 116static inline T swap_byte(T x) { 117 if (sizeof(T) == 8) 118 return swap_byte64((uint64_t)x); 119 else if (sizeof(T) == 4) 120 return swap_byte32((uint32_t)x); 121 else if (sizeof(T) == 2) 122 return swap_byte16((uint16_t)x); 123 else if (sizeof(T) == 1) 124 return x; 125 else 126 panic("Can't byte-swap values larger than 64 bits"); 127} 128 |
129template<> 130static inline Twin64_t swap_byte<Twin64_t>(Twin64_t x) 131{ 132 x.a = swap_byte(x.a); 133 x.b = swap_byte(x.b); 134 return x; 135} 136 137template<> 138static inline Twin32_t swap_byte<Twin32_t>(Twin32_t x) 139{ 140 x.a = swap_byte(x.a); 141 x.b = swap_byte(x.b); 142 return x; 143} 144 |
|
128//The conversion functions with fixed endianness on both ends don't need to 129//be in a namespace 130template <typename T> static inline T betole(T value) {return swap_byte(value);} 131template <typename T> static inline T letobe(T value) {return swap_byte(value);} 132 133//For conversions not involving the guest system, we can define the functions 134//conditionally based on the BYTE_ORDER macro and outside of the namespaces 135#if defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN) && BYTE_ORDER == BIG_ENDIAN --- 49 unchanged lines hidden --- | 145//The conversion functions with fixed endianness on both ends don't need to 146//be in a namespace 147template <typename T> static inline T betole(T value) {return swap_byte(value);} 148template <typename T> static inline T letobe(T value) {return swap_byte(value);} 149 150//For conversions not involving the guest system, we can define the functions 151//conditionally based on the BYTE_ORDER macro and outside of the namespaces 152#if defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN) && BYTE_ORDER == BIG_ENDIAN --- 49 unchanged lines hidden --- |