Qore Programming Language: include/qore/qore_bitopts.h (6585) Source File

Qore Programming Language 0.8.11

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qore_bitopts.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3 qore_bit_opts.h
4
5 Qore Programming Language
6
7 Copyright 2003 - 2013 David Nichols
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _QORE_BITOPTS_H
25
26 #define _QORE_BITOPTS_H
27
28 #include <qore/common.h>
29
30 static inline int64 i8LSB(int64 i);
31 static inline int i4LSB(int i);
32 static inline short i2LSB(short i);
33
34 static inline int64 LSBi8(int64 i);
35 static inline int LSBi4(int i);
36 static inline short LSBi2(short i);
37
38 static inline int64 i8MSB(int64 i);
39 static inline int64 MSBi8(int64 i);
40
42 union qore_i8_u {
43 char buf[8];
44 int64 i;
45 double f;
46
47 DLLLOCAL void swap(char *ibuf) {
48 buf[7] = ibuf[0];
49 buf[6] = ibuf[1];
50 buf[5] = ibuf[2];
51 buf[4] = ibuf[3];
52 buf[3] = ibuf[4];
53 buf[2] = ibuf[5];
54 buf[1] = ibuf[6];
55 buf[0] = ibuf[7];
56 }
57 };
58
60 static inline int64 swapi8(int64 i) {
61 char *ibuf = (char *)&i;
62 qore_i8_u i8;
63 i8.swap(ibuf);
64 return i8.i;
65 }
66
68 static inline double swapf8(double f) {
69 char *ibuf = (char *)&f;
70 qore_i8_u f8;
71 f8.swap(ibuf);
72 return f8.f;
73 }
74
76 union qore_i4_u {
77 char buf[4];
78 int i;
79 float f;
80
81 DLLLOCAL void swap(char *ibuf) {
82 buf[3] = ibuf[0];
83 buf[2] = ibuf[1];
84 buf[1] = ibuf[2];
85 buf[0] = ibuf[3];
86 }
87 };
88
90 static inline int swapi4(int i) {
91 char *ibuf = (char *)&i;
92 qore_i4_u i4;
93 i4.swap(ibuf);
94 return i4.i;
95 }
96
98 static inline float swapf4(float f) {
99 char *ibuf = (char *)&f;
100 qore_i4_u f4;
101 f4.swap(ibuf);
102 return f4.f;
103 }
104
106 union qore_i2_u {
107 char buf[2];
108 short i;
109
110 DLLLOCAL void swap(char *ibuf) {
111 buf[1] = ibuf[0];
112 buf[0] = ibuf[1];
113 }
114 };
115
117 static inline short swapi2(short i) {
118 char *ibuf = (char *)&i;
119 qore_i2_u i2;
120 i2.swap(ibuf);
121 return i2.i;
122 }
123
124 #ifdef WORDS_BIGENDIAN
125 static inline int64 i8LSB(int64 i) {
126 return swapi8(i);
127 }
128
129 static inline int i4LSB(int i) {
130 return swapi4(i);
131 }
132
133 static inline short i2LSB(short i) {
134 return swapi2(i);
135 }
136
137 static inline int64 LSBi8(int64 i) {
138 return swapi8(i);
139 }
140
141 static inline int LSBi4(int i) {
142 return swapi4(i);
143 }
144
145 static inline short LSBi2(short i) {
146 return swapi2(i);
147 }
148
149 static inline int64 i8MSB(int64 i) { return i; }
150 static inline int64 MSBi8(int64 i) { return i; }
151
152 static inline double f8LSB(double f) {
153 return swapf8(f);
154 }
155
156 static inline float f4LSB(float f) {
157 return swapf4(f);
158 }
159
160 static inline double LSBf8(double f) {
161 return swapf8(f);
162 }
163
164 static inline float LSBf4(float f) {
165 return swapf4(f);
166 }
167
168 static inline double f8MSB(double f) { return f; }
169 static inline double MSBf8(double f) { return f; }
170 static inline float f4MSB(float f) { return f; }
171 static inline float MSBf4(float f) { return f; }
172
173 #else // definitions for little endian machines below
174
175 static inline int64 i8LSB(int64 i) { return i; }
176 static inline int i4LSB(int i) { return i; }
177 static inline short i2LSB(short i) { return i; }
178
179 static inline int64 LSBi8(int64 i) { return i; }
180 static inline int LSBi4(int i) { return i; }
181 static inline short LSBi2(short i) { return i; }
182
183 static inline int64 i8MSB(int64 i) {
184 return swapi8(i);
185 }
186
187 static inline int64 MSBi8(int64 i) {
188 return swapi8(i);
189 }
190
191 static inline double f8LSB(double f) { return f; }
192 static inline float f4LSB(float f) { return f; }
193
194 static inline double LSBf8(double f) { return f; }
195 static inline float LSBf4(float f) { return f; }
196
197 static inline double f8MSB(double f) {
198 return swapf8(f);
199 }
200
201 static inline double MSBf8(double f) {
202 return swapf8(f);
203 }
204
205 static inline float f4MSB(float f) {
206 return swapf4(f);
207 }
208
209 static inline float MSBf4(float f) {
210 return swapf4(f);
211 }
212
213 #endif
214
215 #endif
qore_i8_u
used to swap byte order of 8-byte values
Definition: qore_bitopts.h:42
qore_i2_u
used to swap 2-byte integers
Definition: qore_bitopts.h:106
common.h
qore_i4_u
used to swap byte order of 4-byte values
Definition: qore_bitopts.h:76