byteswap.hh (2764:e6fea7527b3c) byteswap.hh (2772:f0f52cbe744d)
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;

--- 11 unchanged lines hidden (view full) ---

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 *
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;

--- 11 unchanged lines hidden (view full) ---

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
28 * Authors: Ali Saidi
29 * Nathan Binkert
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__

--- 48 unchanged lines hidden (view full) ---

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
30 */
31
32//The purpose of this file is to provide endainness conversion utility
33//functions. Depending on the endianness of the guest system, either
34//the LittleEndianGuest or BigEndianGuest namespace is used.
35
36#ifndef __SIM_BYTE_SWAP_HH__
37#define __SIM_BYTE_SWAP_HH__

--- 48 unchanged lines hidden (view full) ---

86#if defined(linux)
87 return bswap_16(x);
88#else
89 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
90 ((uint16_t)(x) & 0xff00) >> 8);
91#endif
92}
93
93// This function lets the compiler figure out how to call the
94// swap_byte functions above for different data types. Since the
95// sizeof() values are known at compiel time, it should inline to a
96// direct call to the right swap_byteNN() function.
97template <typename T>
98static inline T swap_byte(T x) {
99 if (sizeof(T) == 8)
100 return swap_byte64((uint64_t)x);
101 else if (sizeof(T) == 4)
102 return swap_byte32((uint32_t)x);
103 else if (sizeof(T) == 2)
104 return swap_byte16((uint16_t)x);
105 else if (sizeof(T) == 1)
106 return x;
107 else
108 panic("Can't byte-swap values larger than 64 bits");
109}
94//This lets the compiler figure out how to call the swap_byte functions above
95//for different data types.
96static inline uint64_t swap_byte(uint64_t x) {return swap_byte64(x);}
97static inline int64_t swap_byte(int64_t x) {return swap_byte64((uint64_t)x);}
98static inline uint32_t swap_byte(uint32_t x) {return swap_byte32(x);}
99static inline int32_t swap_byte(int32_t x) {return swap_byte32((uint32_t)x);}
100//This is to prevent the following two functions from compiling on
101//64bit machines. It won't detect everything, so it should be changed.
102#ifndef __x86_64__
103static inline long swap_byte(long x) {return swap_byte32((long)x);}
104static inline unsigned long swap_byte(unsigned long x)
105 { return swap_byte32((unsigned long)x);}
106#endif
107static inline uint16_t swap_byte(uint16_t x) {return swap_byte32(x);}
108static inline int16_t swap_byte(int16_t x) {return swap_byte16((uint16_t)x);}
109static inline uint8_t swap_byte(uint8_t x) {return x;}
110static inline int8_t swap_byte(int8_t x) {return x;}
111static inline double swap_byte(double x) {return swap_byte64((uint64_t)x);}
112static inline float swap_byte(float x) {return swap_byte32((uint32_t)x);}
110
111//The conversion functions with fixed endianness on both ends don't need to
112//be in a namespace
113template <typename T> static inline T betole(T value) {return swap_byte(value);}
114template <typename T> static inline T letobe(T value) {return swap_byte(value);}
115
116//For conversions not involving the guest system, we can define the functions
117//conditionally based on the BYTE_ORDER macro and outside of the namespaces

--- 46 unchanged lines hidden ---
113
114//The conversion functions with fixed endianness on both ends don't need to
115//be in a namespace
116template <typename T> static inline T betole(T value) {return swap_byte(value);}
117template <typename T> static inline T letobe(T value) {return swap_byte(value);}
118
119//For conversions not involving the guest system, we can define the functions
120//conditionally based on the BYTE_ORDER macro and outside of the namespaces

--- 46 unchanged lines hidden ---