COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
byteswap.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef __COVISE_BYTESWAP_INCLUDED
9 #define __COVISE_BYTESWAP_INCLUDED
10 
11 #include "coTypes.h"
12 
13 namespace
14 {
15 
16 inline bool machineIsLittleEndian()
17 {
18  int x = 1;
19  return (1 == (int)*((char *)&x));
20 }
21 
22 inline bool machineIsBigEndian()
23 {
24  int x = 1;
25  return (1 != (int)*((char *)&x));
26 }
27 
28 inline void byteSwap(uint32_t &value)
29 {
30  value = ((value & 0x000000ff) << 24) | ((value & 0x0000ff00) << 8) | ((value & 0x00ff0000) >> 8) | ((value & 0xff000000) >> 24);
31 }
32 
33 inline void byteSwap(int32_t &value)
34 {
35  int uval = (uint32_t)value;
36  value = ((uval & 0x000000ff) << 24) | ((uval & 0x0000ff00) << 8) | ((uval & 0x00ff0000) >> 8) | ((uval & 0xff000000) >> 24);
37 }
38 
39 inline void byteSwap(uint32_t *values, int no)
40 {
41  for (int i = 0; i < no; i++, values++)
42  *values = ((*values & 0x000000ff) << 24) | ((*values & 0x0000ff00) << 8) | ((*values & 0x00ff0000) >> 8) | ((*values & 0xff000000) >> 24);
43 }
44 
45 inline void byteSwap(int32_t *values, int no)
46 {
47  byteSwap((uint32_t *)values, no);
48 }
49 
50 inline void byteSwap(uint64_t &value)
51 {
52  value =
53 #if defined(_WIN32) && !defined(__MINGW32__)
54  ((value & 0x00000000000000ff) << 56) | ((value & 0x000000000000ff00) << 40) | ((value & 0x0000000000ff0000) << 24) | ((value & 0x00000000ff000000) << 8) | ((value & 0x000000ff00000000) >> 8) | ((value & 0x0000ff0000000000) >> 24) | ((value & 0x00ff000000000000) >> 40) | ((value & 0xff00000000000000) >> 56);
55 #else
56  ((value & 0x00000000000000ffll) << 56) | ((value & 0x000000000000ff00ll) << 40) | ((value & 0x0000000000ff0000ll) << 24) | ((value & 0x00000000ff000000ll) << 8) | ((value & 0x000000ff00000000ll) >> 8) | ((value & 0x0000ff0000000000ll) >> 24) | ((value & 0x00ff000000000000ll) >> 40) | ((value & 0xff00000000000000ll) >> 56);
57 #endif
58 }
59 
60 inline void byteSwap(int64_t &value)
61 {
62  value =
63 #if defined(_WIN32) && !defined(__MINGW32__)
64  ((value & 0x00000000000000ff) << 56) | ((value & 0x000000000000ff00) << 40) | ((value & 0x0000000000ff0000) << 24) | ((value & 0x00000000ff000000) << 8) | ((value & 0x000000ff00000000) >> 8) | ((value & 0x0000ff0000000000) >> 24) | ((value & 0x00ff000000000000) >> 40) | ((value & 0xff00000000000000) >> 56);
65 #else
66  ((value & 0x00000000000000ffll) << 56) | ((value & 0x000000000000ff00ll) << 40) | ((value & 0x0000000000ff0000ll) << 24) | ((value & 0x00000000ff000000ll) << 8) | ((value & 0x000000ff00000000ll) >> 8) | ((value & 0x0000ff0000000000ll) >> 24) | ((value & 0x00ff000000000000ll) >> 40) | ((value & 0xff00000000000000ll) >> 56);
67 #endif
68 }
69 
70 inline void byteSwap(uint64_t *values, int no)
71 {
72  int i;
73  for (i = 0; i < no; i++, values++)
74  {
75  *values =
76 #if defined(_WIN32) && !defined(__MINGW32__)
77  ((*values & 0x00000000000000ff) << 56) | ((*values & 0x000000000000ff00) << 40) | ((*values & 0x0000000000ff0000) << 24) | ((*values & 0x00000000ff000000) << 8) | ((*values & 0x000000ff00000000) >> 8) | ((*values & 0x0000ff0000000000) >> 24) | ((*values & 0x00ff000000000000) >> 40) | ((*values & 0xff00000000000000) >> 56);
78 #else
79  ((*values & 0x00000000000000ffll) << 56) | ((*values & 0x000000000000ff00ll) << 40) | ((*values & 0x0000000000ff0000ll) << 24) | ((*values & 0x00000000ff000000ll) << 8) | ((*values & 0x000000ff00000000ll) >> 8) | ((*values & 0x0000ff0000000000ll) >> 24) | ((*values & 0x00ff000000000000ll) >> 40) | ((*values & 0xff00000000000000ll) >> 56);
80 #endif
81  }
82 }
83 
84 //We need this, since we want a _binary_
85 //conversion of the float value
86 #define double2ll(value) (*(uint64_t *)(&value))
87 inline void byteSwap(double &value)
88 {
89 
90  double2ll(value) =
91 #if defined(_WIN32) && !defined(__MINGW32__)
92  ((double2ll(value) & 0x00000000000000ff) << 56) | ((double2ll(value) & 0x000000000000ff00) << 40) | ((double2ll(value) & 0x0000000000ff0000) << 24) | ((double2ll(value) & 0x00000000ff000000) << 8) | ((double2ll(value) & 0x000000ff00000000) >> 8) | ((double2ll(value) & 0x0000ff0000000000) >> 24) | ((double2ll(value) & 0x00ff000000000000) >> 40) | ((double2ll(value) & 0xff00000000000000) >> 56);
93 #else
94  ((double2ll(value) & 0x00000000000000ffll) << 56) | ((double2ll(value) & 0x000000000000ff00ll) << 40) | ((double2ll(value) & 0x0000000000ff0000ll) << 24) | ((double2ll(value) & 0x00000000ff000000ll) << 8) | ((double2ll(value) & 0x000000ff00000000ll) >> 8) | ((double2ll(value) & 0x0000ff0000000000ll) >> 24) | ((double2ll(value) & 0x00ff000000000000ll) >> 40) | ((double2ll(value) & 0xff00000000000000ll) >> 56);
95 #endif
96 }
97 
98 inline void byteSwap(double *values, int no)
99 {
100  for (int i = 0; i < no; i++)
101  {
102  byteSwap(values[i]);
103  }
104 }
105 
106 #define float2int(value) (*(uint32_t *)(&value))
107 inline void byteSwap(float &value)
108 {
109 
110  float2int(value) = ((float2int(value) & 0x000000ff) << 24) | ((float2int(value) & 0x0000ff00) << 8) | ((float2int(value) & 0x00ff0000) >> 8) | ((float2int(value) & 0xff000000) >> 24);
111 }
112 
113 inline void byteSwap(float *values, int no)
114 {
115  uint32_t *intValues = (uint32_t *)values;
116  for (int i = 0; i < no; i++, intValues++)
117  *intValues = ((*intValues & 0x000000ff) << 24) | ((*intValues & 0x0000ff00) << 8) | ((*intValues & 0x00ff0000) >> 8) | ((*intValues & 0xff000000) >> 24);
118 }
119 
120 inline void byteSwap(uint16_t &value)
121 {
122  value = ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
123 }
124 
125 inline void byteSwap(uint16_t *values, int no)
126 {
127  for (int i = 0; i < no; i++, values++)
128  *values = ((*values & 0x00ff) << 8) | ((*values & 0xff00) >> 8);
129 }
130 
131 inline void byteSwap(int16_t &value)
132 {
133  uint16_t uval = (uint16_t)value;
134  value = ((uval & 0x00ff) << 8) | ((uval & 0xff00) >> 8);
135 }
136 
137 inline void byteSwap(int16_t *values, int no)
138 {
139  byteSwap((uint16_t *)values, no);
140 }
141 }
142 #endif
GLboolean GLenum GLenum GLvoid * values
Definition: khronos-glext.h:6369
GLint GLint GLint GLint GLint x
Definition: khronos-glext.h:6346
#define double2ll(value)
Definition: byteswap.h:86
GLsizei const GLfloat * value
Definition: khronos-glext.h:6760
#define float2int(value)
Definition: byteswap.h:106