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

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

--- 76 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
93//This lets the compiler figure out how to call the swap_byte functions above
94//for different data types.
95static inline uint64_t swap_byte(uint64_t x) {return swap_byte64(x);}
96static inline int64_t swap_byte(int64_t x) {return swap_byte64((uint64_t)x);}
97static inline uint32_t swap_byte(uint32_t x) {return swap_byte32(x);}
98static inline int32_t swap_byte(int32_t x) {return swap_byte32((uint32_t)x);}
99//This is to prevent the following two functions from compiling on
100//64bit machines. It won't detect everything, so it should be changed.
101#ifndef __x86_64__
102static inline long swap_byte(long x) {return swap_byte32((long)x);}
103static inline unsigned long swap_byte(unsigned long x)
104 { return swap_byte32((unsigned long)x);}
105#endif
106static inline uint16_t swap_byte(uint16_t x) {return swap_byte32(x);}
107static inline int16_t swap_byte(int16_t x) {return swap_byte16((uint16_t)x);}
108static inline uint8_t swap_byte(uint8_t x) {return x;}
109static inline int8_t swap_byte(int8_t x) {return x;}
110static inline double swap_byte(double x) {return swap_byte64((uint64_t)x);}
111static inline float swap_byte(float x) {return swap_byte32((uint32_t)x);}
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}
112
113//The conversion functions with fixed endianness on both ends don't need to
114//be in a namespace
115template <typename T> static inline T betole(T value) {return swap_byte(value);}
116template <typename T> static inline T letobe(T value) {return swap_byte(value);}
117
118//For conversions not involving the guest system, we can define the functions
119//conditionally based on the BYTE_ORDER macro and outside of the namespaces

--- 46 unchanged lines hidden ---
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 ---