byteswap.hh (3483:edede8473667) byteswap.hh (3799:c8f88e7d82c1)
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;

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

52#else
53#include <machine/endian.h>
54#endif
55
56#if defined(__APPLE__)
57#include <libkern/OSByteOrder.h>
58#endif
59
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;

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

52#else
53#include <machine/endian.h>
54#endif
55
56#if defined(__APPLE__)
57#include <libkern/OSByteOrder.h>
58#endif
59
60enum ByteOrder {BigEndianByteOrder, LittleEndianByteOrder};
61
60//These functions actually perform the swapping for parameters
61//of various bit lengths
62static inline uint64_t
63swap_byte64(uint64_t x)
64{
65#if defined(linux)
66 return bswap_64(x);
67#elif defined(__APPLE__)

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

126//The conversion functions with fixed endianness on both ends don't need to
127//be in a namespace
128template <typename T> static inline T betole(T value) {return swap_byte(value);}
129template <typename T> static inline T letobe(T value) {return swap_byte(value);}
130
131//For conversions not involving the guest system, we can define the functions
132//conditionally based on the BYTE_ORDER macro and outside of the namespaces
133#if defined(_BIG_ENDIAN) || BYTE_ORDER == BIG_ENDIAN
62//These functions actually perform the swapping for parameters
63//of various bit lengths
64static inline uint64_t
65swap_byte64(uint64_t x)
66{
67#if defined(linux)
68 return bswap_64(x);
69#elif defined(__APPLE__)

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

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) || BYTE_ORDER == BIG_ENDIAN
136const ByteOrder HostByteOrder = BigEndianByteOrder;
134template <typename T> static inline T htole(T value) {return swap_byte(value);}
135template <typename T> static inline T letoh(T value) {return swap_byte(value);}
136template <typename T> static inline T htobe(T value) {return value;}
137template <typename T> static inline T betoh(T value) {return value;}
138#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
137template <typename T> static inline T htole(T value) {return swap_byte(value);}
138template <typename T> static inline T letoh(T value) {return swap_byte(value);}
139template <typename T> static inline T htobe(T value) {return value;}
140template <typename T> static inline T betoh(T value) {return value;}
141#elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
142const ByteOrder HostByteOrder = LittleEndianByteOrder;
139template <typename T> static inline T htole(T value) {return value;}
140template <typename T> static inline T letoh(T value) {return value;}
141template <typename T> static inline T htobe(T value) {return swap_byte(value);}
142template <typename T> static inline T betoh(T value) {return swap_byte(value);}
143#else
144 #error Invalid Endianess
145#endif
146
147namespace BigEndianGuest
148{
143template <typename T> static inline T htole(T value) {return value;}
144template <typename T> static inline T letoh(T value) {return value;}
145template <typename T> static inline T htobe(T value) {return swap_byte(value);}
146template <typename T> static inline T betoh(T value) {return swap_byte(value);}
147#else
148 #error Invalid Endianess
149#endif
150
151namespace BigEndianGuest
152{
149 template <typename T>
150 static inline T gtole(T value) {return betole(value);}
151 template <typename T>
152 static inline T letog(T value) {return letobe(value);}
153 template <typename T>
154 static inline T gtobe(T value) {return value;}
155 template <typename T>
156 static inline T betog(T value) {return value;}
157 template <typename T>
158 static inline T htog(T value) {return htobe(value);}
159 template <typename T>
160 static inline T gtoh(T value) {return betoh(value);}
153 const bool ByteOrderDiffers = (HostByteOrder != BigEndianByteOrder);
154 template <typename T>
155 static inline T gtole(T value) {return betole(value);}
156 template <typename T>
157 static inline T letog(T value) {return letobe(value);}
158 template <typename T>
159 static inline T gtobe(T value) {return value;}
160 template <typename T>
161 static inline T betog(T value) {return value;}
162 template <typename T>
163 static inline T htog(T value) {return htobe(value);}
164 template <typename T>
165 static inline T gtoh(T value) {return betoh(value);}
161}
162
163namespace LittleEndianGuest
164{
166}
167
168namespace LittleEndianGuest
169{
165 template <typename T>
166 static inline T gtole(T value) {return value;}
167 template <typename T>
168 static inline T letog(T value) {return value;}
169 template <typename T>
170 static inline T gtobe(T value) {return letobe(value);}
171 template <typename T>
172 static inline T betog(T value) {return betole(value);}
173 template <typename T>
174 static inline T htog(T value) {return htole(value);}
175 template <typename T>
176 static inline T gtoh(T value) {return letoh(value);}
170 const bool ByteOrderDiffers = (HostByteOrder != LittleEndianByteOrder);
171 template <typename T>
172 static inline T gtole(T value) {return value;}
173 template <typename T>
174 static inline T letog(T value) {return value;}
175 template <typename T>
176 static inline T gtobe(T value) {return letobe(value);}
177 template <typename T>
178 static inline T betog(T value) {return betole(value);}
179 template <typename T>
180 static inline T htog(T value) {return htole(value);}
181 template <typename T>
182 static inline T gtoh(T value) {return letoh(value);}
177}
178#endif // __SIM_BYTE_SWAP_HH__
183}
184#endif // __SIM_BYTE_SWAP_HH__