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