COVISE Core
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
16namespace
17{
18
19inline bool machineIsLittleEndian()
20{
21 int x = 1;
22 return (1 == (int)*((char *)&x));
23}
24
25inline bool machineIsBigEndian()
26{
27 int x = 1;
28 return (1 != (int)*((char *)&x));
29}
30
31inline void byteSwap(uint32_t &value)
32{
33 value = ((value & 0x000000ff) << 24) | ((value & 0x0000ff00) << 8) | ((value & 0x00ff0000) >> 8) | ((value & 0xff000000) >> 24);
34}
35
36inline 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
42inline 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
48inline void byteSwap(uint32_t* values, uint64_t no)
49{
50 for (uint64_t i = 0; i < no; i++, values++)
51 *values = ((*values & 0x000000ff) << 24) | ((*values & 0x0000ff00) << 8) | ((*values & 0x00ff0000) >> 8) | ((*values & 0xff000000) >> 24);
52}
53inline void byteSwap(int32_t *values, int no)
54{
55 byteSwap((uint32_t *)values, no);
56}
57
58inline void byteSwap(uint64_t &value)
59{
60 value =
61#if defined(_WIN32) && !defined(__MINGW32__)
62 ((value & 0x00000000000000ff) << 56) | ((value & 0x000000000000ff00) << 40) | ((value & 0x0000000000ff0000) << 24) | ((value & 0x00000000ff000000) << 8) | ((value & 0x000000ff00000000) >> 8) | ((value & 0x0000ff0000000000) >> 24) | ((value & 0x00ff000000000000) >> 40) | ((value & 0xff00000000000000) >> 56);
63#else
64 ((value & 0x00000000000000ffll) << 56) | ((value & 0x000000000000ff00ll) << 40) | ((value & 0x0000000000ff0000ll) << 24) | ((value & 0x00000000ff000000ll) << 8) | ((value & 0x000000ff00000000ll) >> 8) | ((value & 0x0000ff0000000000ll) >> 24) | ((value & 0x00ff000000000000ll) >> 40) | ((value & 0xff00000000000000ll) >> 56);
65#endif
66}
67
68inline void byteSwap(int64_t &value)
69{
70 value =
71#if defined(_WIN32) && !defined(__MINGW32__)
72 ((value & 0x00000000000000ff) << 56) | ((value & 0x000000000000ff00) << 40) | ((value & 0x0000000000ff0000) << 24) | ((value & 0x00000000ff000000) << 8) | ((value & 0x000000ff00000000) >> 8) | ((value & 0x0000ff0000000000) >> 24) | ((value & 0x00ff000000000000) >> 40) | ((value & 0xff00000000000000) >> 56);
73#else
74 ((value & 0x00000000000000ffll) << 56) | ((value & 0x000000000000ff00ll) << 40) | ((value & 0x0000000000ff0000ll) << 24) | ((value & 0x00000000ff000000ll) << 8) | ((value & 0x000000ff00000000ll) >> 8) | ((value & 0x0000ff0000000000ll) >> 24) | ((value & 0x00ff000000000000ll) >> 40) | ((value & 0xff00000000000000ll) >> 56);
75#endif
76}
77
78inline void byteSwap(uint64_t *values, int no)
79{
80 int i;
81 for (i = 0; i < no; i++, values++)
82 {
83 *values =
84#if defined(_WIN32) && !defined(__MINGW32__)
85 ((*values & 0x00000000000000ff) << 56) | ((*values & 0x000000000000ff00) << 40) | ((*values & 0x0000000000ff0000) << 24) | ((*values & 0x00000000ff000000) << 8) | ((*values & 0x000000ff00000000) >> 8) | ((*values & 0x0000ff0000000000) >> 24) | ((*values & 0x00ff000000000000) >> 40) | ((*values & 0xff00000000000000) >> 56);
86#else
87 ((*values & 0x00000000000000ffll) << 56) | ((*values & 0x000000000000ff00ll) << 40) | ((*values & 0x0000000000ff0000ll) << 24) | ((*values & 0x00000000ff000000ll) << 8) | ((*values & 0x000000ff00000000ll) >> 8) | ((*values & 0x0000ff0000000000ll) >> 24) | ((*values & 0x00ff000000000000ll) >> 40) | ((*values & 0xff00000000000000ll) >> 56);
88#endif
89 }
90}
91
92inline void byteSwap(uint64_t* values, uint64_t no)
93{
94 uint64_t i;
95 for (i = 0; i < no; i++, values++)
96 {
97 *values =
98#if defined(_WIN32) && !defined(__MINGW32__)
99 ((*values & 0x00000000000000ff) << 56) | ((*values & 0x000000000000ff00) << 40) | ((*values & 0x0000000000ff0000) << 24) | ((*values & 0x00000000ff000000) << 8) | ((*values & 0x000000ff00000000) >> 8) | ((*values & 0x0000ff0000000000) >> 24) | ((*values & 0x00ff000000000000) >> 40) | ((*values & 0xff00000000000000) >> 56);
100#else
101 ((*values & 0x00000000000000ffll) << 56) | ((*values & 0x000000000000ff00ll) << 40) | ((*values & 0x0000000000ff0000ll) << 24) | ((*values & 0x00000000ff000000ll) << 8) | ((*values & 0x000000ff00000000ll) >> 8) | ((*values & 0x0000ff0000000000ll) >> 24) | ((*values & 0x00ff000000000000ll) >> 40) | ((*values & 0xff00000000000000ll) >> 56);
102#endif
103 }
104}
105
106//We need this, since we want a _binary_
107//conversion of the float value
108#define double2ll(value) (*(uint64_t *)(&value))
109inline void byteSwap(double &value)
110{
111
113#if defined(_WIN32) && !defined(__MINGW32__)
114 ((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);
115#else
116 ((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);
117#endif
118}
119
120inline void byteSwap(double *values, int no)
121{
122 for (int i = 0; i < no; i++)
123 {
124 byteSwap(values[i]);
125 }
126}
127
128#define float2int(value) (*(uint32_t *)(&value))
129inline void byteSwap(float &value)
130{
131
132 float2int(value) = ((float2int(value) & 0x000000ff) << 24) | ((float2int(value) & 0x0000ff00) << 8) | ((float2int(value) & 0x00ff0000) >> 8) | ((float2int(value) & 0xff000000) >> 24);
133}
134
135inline void byteSwap(float *values, int no)
136{
137 uint32_t *intValues = (uint32_t *)values;
138 for (int i = 0; i < no; i++, intValues++)
139 *intValues = ((*intValues & 0x000000ff) << 24) | ((*intValues & 0x0000ff00) << 8) | ((*intValues & 0x00ff0000) >> 8) | ((*intValues & 0xff000000) >> 24);
140}
141
142inline void byteSwap(uint16_t &value)
143{
144 value = ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
145}
146
147inline void byteSwap(uint16_t *values, int no)
148{
149 for (int i = 0; i < no; i++, values++)
150 *values = ((*values & 0x00ff) << 8) | ((*values & 0xff00) >> 8);
151}
152
153inline void byteSwap(int16_t &value)
154{
155 uint16_t uval = (uint16_t)value;
156 value = ((uval & 0x00ff) << 8) | ((uval & 0xff00) >> 8);
157}
158
159inline void byteSwap(int16_t *values, int no)
160{
161 byteSwap((uint16_t *)values, no);
162}
163}
164#endif
#define float2int(value)
Definition: byteswap.h:128
#define double2ll(value)
Definition: byteswap.h:108
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