HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Endian.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Endian.h (C++)
7  *
8  * COMMENTS:
9  * LITTLE-BIG ENDIAN CONVERSION
10  * ----------------------------
11  *
12  * The tomips/tovax will convert the data type to the appropriate
13  * format OR from the appropriate format. For example, if you
14  * have an integer that you know is in VAX format, you can call
15  * UTtomips(x)
16  * to get it into the native format for the machine you're on.
17  * If you have a MIPS format int and you want to convert to a VAX
18  * format int, you can also call
19  * UTtomips(x)
20  * In otherwords:
21  * UTtomips(x); UTtomips(x);
22  * will result in X being the same format as it was before.
23  *
24  * Yes, it's a bit confusing, but probably less confusing than
25  * something like UTtoLittleEndian().
26  *
27  * All data conversions are done in place.
28  *
29  * Note: The Intel machines are all little endian
30  *
31  * ARRAY CONVERSIONS
32  * -----------------
33  * Efficient array casting.
34  */
35 
36 #ifndef __UT_Endian_h__
37 #define __UT_Endian_h__
38 
39 #include "UT_API.h"
40 #include <string.h>
41 #include <SYS/SYS_Types.h>
42 
43 #define UTswap_int16(x,z) { x = (((z & 0xff) << 8) | ((z >> 8) & 0xff)); }
44 #define UTswap_int32(x,z) { x = ((z & 0xff) << 24) | ((z & 0xff00) << 8) \
45  | ((z >> 8) & 0xff00) | ((z >> 24) & 0xff); }
46 #define UTswap_int64(x,z) { x = (((((z ) & 0xff) << 56) | \
47  (((z >> 8) & 0xff) << 48) | \
48  (((z >> 16) & 0xff) << 40) | \
49  (((z >> 24) & 0xff) << 32) | \
50  (((z >> 32) & 0xff) << 24) | \
51  (((z >> 40) & 0xff) << 16) | \
52  (((z >> 48) & 0xff) << 8) | \
53  (((z >> 56) & 0xff) ))); }
54 #define UTswap_int16_inplace(z) UTswap_int16(z,z)
55 #define UTswap_int32_inplace(z) UTswap_int32(z,z)
56 #define UTswap_int64_inplace(z) UTswap_int64(z,z)
57 
58 UT_API extern void UTswapBytes(int16 *values, int64 num); // 16 bit
59 UT_API extern void UTswapBytes(int32 *values, int64 num); // 32 bit
60 UT_API extern void UTswapBytes(int64 *values, int64 num); // 64 bit
61 UT_API extern void UTswapBytes(fpreal16 *values, int64 num); // 16 bit
62 UT_API extern void UTswapBytes(fpreal32 *values, int64 num); // 32 bit
63 UT_API extern void UTswapBytes(fpreal64 *values, int64 num); // 64 bit
64 
65 UT_API extern void UTswapBytes(int16 *dest, const int16 *src, int64 num);
66 UT_API extern void UTswapBytes(int32 *dest, const int32 *src, int64 num);
67 UT_API extern void UTswapBytes(int64 *dest, const int64 *src, int64 num);
68 UT_API extern void UTswapBytes(fpreal16 *dest, const fpreal16 *src, int64 num);
69 UT_API extern void UTswapBytes(fpreal32 *dest, const fpreal32 *src, int64 num);
70 UT_API extern void UTswapBytes(fpreal64 *dest, const fpreal64 *src, int64 num);
71 
72 // Conversion along with swapping
73 UT_API extern void UTswapBytes(fpreal32 *dest, const fpreal64 *src, int64 num);
74 UT_API extern void UTswapBytes(fpreal64 *dest, const fpreal32 *src, int64 num);
75 
76 #define BYTE_ORDER_MATCHING_TAG16 0x3210
77 #define BYTE_ORDER_OPPOSITE_TAG16 0x1032
78 #define BYTE_ORDER_MATCHING_TAG32 0x76543210
79 #define BYTE_ORDER_OPPOSITE_TAG32 0x10325476
80 #define BYTE_ORDER_MATCHING_TAG64 0xFEDCBA9876543210ULL
81 #define BYTE_ORDER_OPPOSITE_TAG64 0x1032547698BADCFEULL
82 
83 #ifdef SESI_LITTLE_ENDIAN
84  // Swap byte routines for little endian machines
85 
86  /// NOTE: Don't use these! Write out BYTE_ORDER_MATCHING_TAG32, and have
87  /// the reader check if it's equal to that or BYTE_ORDER_OPPOSITE_TAG32.
88  /// Then, swapping only needs to be done if there's a mismatch, which
89  /// will probably never happen, unless we end up supporting a
90  /// big-endian platform at some point in the future and people take
91  /// data from one platform and use it on a platform with different
92  /// endianness.
93  /// @{
94  inline void UTtomips(int16 *values, int64 num) { UTswapBytes(values,num); }
95  inline void UTtomips(int32 *values, int64 num) { UTswapBytes(values,num); }
96  inline void UTtomips(int64 *values, int64 num) { UTswapBytes(values,num); }
97  inline void UTtomips(fpreal32 *values, int64 n) { UTswapBytes(values, n); }
98  inline void UTtomips(fpreal64 *values, int64 n) { UTswapBytes(values, n); }
99 
100  inline void UTtomips(int16 &z) UTswap_int16(z, z)
101  inline void UTtomips(uint16 &z) UTswap_int16(z, z)
102  inline void UTtomips(int32 &z) UTswap_int32(z, z)
103  inline void UTtomips(uint32 &z) UTswap_int32(z, z)
104  inline void UTtomips(int64 &z) { UTswapBytes(&z,1); }
105  inline void UTtomips(uint64 &z) { UTswapBytes((int64*)&z,1); }
106  inline void UTtomips(fpreal32 &z) { UTtomips(*(uint32 *)&z); }
107  inline void UTtomips(fpreal64 &z) { UTswapBytes(&z, 1); }
108 
109  inline void UTtovax(int16 *, int64) {}
110  inline void UTtovax(int32 *, int64) {}
111  inline void UTtovax(int64 *, int64) {}
112  inline void UTtovax(fpreal32 *, int64) {}
113  inline void UTtovax(fpreal64 *, int64) {}
114 
115  inline void UTtovax(int16) {}
116  inline void UTtovax(uint16) {}
117  inline void UTtovax(int32) {}
118  inline void UTtovax(uint32) {}
119  inline void UTtovax(int64) {}
120  inline void UTtovax(uint64) {}
121  inline void UTtovax(fpreal32) {}
122  inline void UTtovax(fpreal64) {}
123  /// @}
124 
125 #else
126  // Byte swap routines for big endian machines
127 
128  inline void UTtovax(int16 *values, int64 num) { UTswapBytes(values, num); }
129  inline void UTtovax(int32 *values, int64 num) { UTswapBytes(values, num); }
130  inline void UTtovax(int64 *values, int64 num) { UTswapBytes(values, num); }
131  inline void UTtovax(fpreal16 *values, int64 n) { UTswapBytes(values, n); }
132  inline void UTtovax(fpreal32 *values, int64 n) { UTswapBytes(values, n); }
133  inline void UTtovax(fpreal64 *values, int64 n) { UTswapBytes(values, n); }
134 
135  inline void UTtovax(int16 &z) UTswap_int16(z, z)
136  inline void UTtovax(uint16 &z) UTswap_int16(z, z)
137  inline void UTtovax(int32 &z) UTswap_int32(z, z)
138  inline void UTtovax(uint32 &z) UTswap_int32(z, z)
139  inline void UTtovax(int64 &z) { UTswapBytes(&z,1); }
140  inline void UTtovax(uint64 &z) { UTswapBytes((int64*)&z, 1); }
141  inline void UTtovax(fpreal16 &z) { UTtovax(*(uint16 *)&z); }
142  inline void UTtovax(fpreal32 &z) { UTtovax(*(uint32 *)&z); }
143  inline void UTtovax(fpreal64 &z) { UTswapBytes(&z, 1); }
144 
145  inline void UTtomips(int16 *, int64) {}
146  inline void UTtomips(int32 *, int64) {}
147  inline void UTtomips(int64 *, int64) {}
148  inline void UTtomips(fpreal32 *, int64) {}
149  inline void UTtomips(fpreal64 *, int64) {}
150 
151  inline void UTtomips(int16 &) {}
152  inline void UTtomips(uint16 &) {}
153  inline void UTtomips(int32 &) {}
154  inline void UTtomips(uint32 &) {}
155  inline void UTtomips(int64 &) {}
156  inline void UTtomips(uint64 &) {}
157  inline void UTtomips(fpreal32 &) {}
158  inline void UTtomips(fpreal64 &) {}
159 
160 #endif // End of the defines
161 
162 // Array casting conversion
163 #define CAST_SAME_TYPE(TYPE) \
164  inline static void UTcast(TYPE *dest, const TYPE *src, uint64 size) \
165  { memmove(dest, src, size*sizeof(TYPE)); } \
166  static inline void UTcastFast(TYPE *dest, const TYPE *src, uint64 size) \
167  { memcpy(dest, src, size*sizeof(TYPE)); } \
168  /**/
169 
170 #define CAST_DIFFERENT_TYPE(DTYPE, STYPE) \
171  UT_API void UTcast(DTYPE *dest, const STYPE *src, uint64 size); \
172  static inline void UTcastFast(DTYPE *dest, const STYPE *src, uint64 size) \
173  { \
174  for (uint64 i = 0; i < size; i++) \
175  dest[i]=(DTYPE)src[i]; \
176  } \
177  /**/
178 
190 
192 CAST_SAME_TYPE(uint8)
202 
204 CAST_DIFFERENT_TYPE(int16, uint8)
205 CAST_SAME_TYPE(int16)
214 
216 CAST_DIFFERENT_TYPE(uint16, uint8)
217 CAST_DIFFERENT_TYPE(uint16, int16)
218 CAST_SAME_TYPE(uint16)
219 CAST_DIFFERENT_TYPE(uint16, int32)
221 CAST_DIFFERENT_TYPE(uint16, int64)
226 
228 CAST_DIFFERENT_TYPE(int32, uint8)
229 CAST_DIFFERENT_TYPE(int32, int16)
230 CAST_DIFFERENT_TYPE(int32, uint16)
231 CAST_SAME_TYPE(int32)
238 
240 CAST_DIFFERENT_TYPE(uint32, uint8)
241 CAST_DIFFERENT_TYPE(uint32, int16)
242 CAST_DIFFERENT_TYPE(uint32, uint16)
243 CAST_DIFFERENT_TYPE(uint32, int32)
244 CAST_SAME_TYPE(uint32)
245 CAST_DIFFERENT_TYPE(uint32, int64)
250 
252 CAST_DIFFERENT_TYPE(int64, uint8)
253 CAST_DIFFERENT_TYPE(int64, int16)
254 CAST_DIFFERENT_TYPE(int64, uint16)
255 CAST_DIFFERENT_TYPE(int64, int32)
256 CAST_DIFFERENT_TYPE(int64, uint32)
257 CAST_SAME_TYPE(int64)
262 
264 CAST_DIFFERENT_TYPE(uint64, uint8)
265 CAST_DIFFERENT_TYPE(uint64, int16)
266 CAST_DIFFERENT_TYPE(uint64, uint16)
267 CAST_DIFFERENT_TYPE(uint64, int32)
268 CAST_DIFFERENT_TYPE(uint64, uint32)
269 CAST_DIFFERENT_TYPE(uint64, int64)
270 CAST_SAME_TYPE(uint64)
274 
276 CAST_DIFFERENT_TYPE(fpreal16, uint8)
277 CAST_DIFFERENT_TYPE(fpreal16, int16)
278 CAST_DIFFERENT_TYPE(fpreal16, uint16)
279 CAST_DIFFERENT_TYPE(fpreal16, int32)
280 CAST_DIFFERENT_TYPE(fpreal16, uint32)
281 CAST_DIFFERENT_TYPE(fpreal16, int64)
282 CAST_DIFFERENT_TYPE(fpreal16, uint64)
283 CAST_SAME_TYPE(fpreal16)
284 CAST_DIFFERENT_TYPE(fpreal16, fpreal32)
285 CAST_DIFFERENT_TYPE(fpreal16, fpreal64)
286 
288 CAST_DIFFERENT_TYPE(fpreal32, uint8)
289 CAST_DIFFERENT_TYPE(fpreal32, int16)
290 CAST_DIFFERENT_TYPE(fpreal32, uint16)
291 CAST_DIFFERENT_TYPE(fpreal32, int32)
292 CAST_DIFFERENT_TYPE(fpreal32, uint32)
293 CAST_DIFFERENT_TYPE(fpreal32, int64)
294 CAST_DIFFERENT_TYPE(fpreal32, uint64)
295 CAST_DIFFERENT_TYPE(fpreal32, fpreal16)
296 CAST_SAME_TYPE(fpreal32)
297 CAST_DIFFERENT_TYPE(fpreal32, fpreal64)
298 
300 CAST_DIFFERENT_TYPE(fpreal64, uint8)
301 CAST_DIFFERENT_TYPE(fpreal64, int16)
302 CAST_DIFFERENT_TYPE(fpreal64, uint16)
303 CAST_DIFFERENT_TYPE(fpreal64, int32)
304 CAST_DIFFERENT_TYPE(fpreal64, uint32)
305 CAST_DIFFERENT_TYPE(fpreal64, int64)
306 CAST_DIFFERENT_TYPE(fpreal64, uint64)
307 CAST_DIFFERENT_TYPE(fpreal64, fpreal16)
308 CAST_DIFFERENT_TYPE(fpreal64, fpreal32)
309 CAST_SAME_TYPE(fpreal64)
310 
311 #undef CAST_SAME_TYPE
312 #undef CAST_DIFFERENT_TYPE
313 
314 #endif
unsigned short uint16
Definition: SYS_Types.h:38
int int32
Definition: SYS_Types.h:39
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
#define UT_API
Definition: UT_API.h:14
unsigned long long uint64
Definition: SYS_Types.h:117
float fpreal32
Definition: SYS_Types.h:200
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
void UTtomips(int16 *, int64)
Definition: UT_Endian.h:145
GLdouble n
Definition: glcorearb.h:2008
#define CAST_SAME_TYPE(TYPE)
Definition: UT_Endian.h:163
long long int64
Definition: SYS_Types.h:116
signed char int8
Definition: SYS_Types.h:35
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
short int16
Definition: SYS_Types.h:37
UT_API void UTswapBytes(int16 *values, int64 num)
unsigned int uint32
Definition: SYS_Types.h:40
#define UTswap_int32(x, z)
Definition: UT_Endian.h:44
#define CAST_DIFFERENT_TYPE(DTYPE, STYPE)
Definition: UT_Endian.h:170
void UTtovax(int16 *values, int64 num)
Definition: UT_Endian.h:128
#define UTswap_int16(x, z)
Definition: UT_Endian.h:43
GLenum src
Definition: glcorearb.h:1793