byteswap.hh (2772:f0f52cbe744d) | byteswap.hh (2777:599e7940aba5) |
---|---|
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; --- 77 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 | 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; --- 77 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 |
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);} | 94// This function lets the compiler figure out how to call the 95// swap_byte functions above for different data types. Since the 96// sizeof() values are known at compiel time, it should inline to a 97// direct call to the right swap_byteNN() function. 98template <typename T> 99static inline T swap_byte(T x) { 100 if (sizeof(T) == 8) 101 return swap_byte64((uint64_t)x); 102 else if (sizeof(T) == 4) 103 return swap_byte32((uint32_t)x); 104 else if (sizeof(T) == 2) 105 return swap_byte16((uint16_t)x); 106 else if (sizeof(T) == 1) 107 return x; 108 else 109 panic("Can't byte-swap values larger than 64 bits"); 110} |
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 --- | 111 112//The conversion functions with fixed endianness on both ends don't need to 113//be in a namespace 114template <typename T> static inline T betole(T value) {return swap_byte(value);} 115template <typename T> static inline T letobe(T value) {return swap_byte(value);} 116 117//For conversions not involving the guest system, we can define the functions 118//conditionally based on the BYTE_ORDER macro and outside of the namespaces --- 46 unchanged lines hidden --- |