| Qore Programming Language - C/C++ Library 1.0.8

qore_bitopts.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3 qore_bit_opts.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2015 David Nichols
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30 */
31
32 #ifndef _QORE_BITOPTS_H
33
34 #define _QORE_BITOPTS_H
35
36 #include <qore/common.h>
37
38 static inline int64 i8LSB(int64 i);
39 static inline int i4LSB(int i);
40 static inline short i2LSB(short i);
41
42 static inline int64 LSBi8(int64 i);
43 static inline int LSBi4(int i);
44 static inline short LSBi2(short i);
45
46 static inline int64 i8MSB(int64 i);
47 static inline int64 MSBi8(int64 i);
48
50 union qore_i8_u {
51 char buf[8];
52 int64 i;
53 double f;
54
55 DLLLOCAL void swap(char *ibuf) {
56 buf[7] = ibuf[0];
57 buf[6] = ibuf[1];
58 buf[5] = ibuf[2];
59 buf[4] = ibuf[3];
60 buf[3] = ibuf[4];
61 buf[2] = ibuf[5];
62 buf[1] = ibuf[6];
63 buf[0] = ibuf[7];
64 }
65 };
66
68 static inline int64 swapi8(int64 i) {
69 char *ibuf = (char *)&i;
70 qore_i8_u i8;
71 i8.swap(ibuf);
72 return i8.i;
73 }
74
76 static inline double swapf8(double f) {
77 char *ibuf = (char *)&f;
78 qore_i8_u f8;
79 f8.swap(ibuf);
80 return f8.f;
81 }
82
84 union qore_i4_u {
85 char buf[4];
86 int i;
87 float f;
88
89 DLLLOCAL void swap(char *ibuf) {
90 buf[3] = ibuf[0];
91 buf[2] = ibuf[1];
92 buf[1] = ibuf[2];
93 buf[0] = ibuf[3];
94 }
95 };
96
98 static inline int swapi4(int i) {
99 char *ibuf = (char *)&i;
100 qore_i4_u i4;
101 i4.swap(ibuf);
102 return i4.i;
103 }
104
106 static inline float swapf4(float f) {
107 char *ibuf = (char *)&f;
108 qore_i4_u f4;
109 f4.swap(ibuf);
110 return f4.f;
111 }
112
114 union qore_i2_u {
115 char buf[2];
116 short i;
117
118 DLLLOCAL void swap(char *ibuf) {
119 buf[1] = ibuf[0];
120 buf[0] = ibuf[1];
121 }
122 };
123
125 static inline short swapi2(short i) {
126 char *ibuf = (char *)&i;
127 qore_i2_u i2;
128 i2.swap(ibuf);
129 return i2.i;
130 }
131
132 #ifdef WORDS_BIGENDIAN
133 static inline int64 i8LSB(int64 i) {
134 return swapi8(i);
135 }
136
137 static inline int i4LSB(int i) {
138 return swapi4(i);
139 }
140
141 static inline short i2LSB(short i) {
142 return swapi2(i);
143 }
144
145 static inline int64 LSBi8(int64 i) {
146 return swapi8(i);
147 }
148
149 static inline int LSBi4(int i) {
150 return swapi4(i);
151 }
152
153 static inline short LSBi2(short i) {
154 return swapi2(i);
155 }
156
157 static inline int64 i8MSB(int64 i) { return i; }
158 static inline int64 MSBi8(int64 i) { return i; }
159
160 static inline double f8LSB(double f) {
161 return swapf8(f);
162 }
163
164 static inline float f4LSB(float f) {
165 return swapf4(f);
166 }
167
168 static inline double LSBf8(double f) {
169 return swapf8(f);
170 }
171
172 static inline float LSBf4(float f) {
173 return swapf4(f);
174 }
175
176 static inline double f8MSB(double f) { return f; }
177 static inline double MSBf8(double f) { return f; }
178 static inline float f4MSB(float f) { return f; }
179 static inline float MSBf4(float f) { return f; }
180
181 #else // definitions for little endian machines below
182
183 static inline int64 i8LSB(int64 i) { return i; }
184 static inline int i4LSB(int i) { return i; }
185 static inline short i2LSB(short i) { return i; }
186
187 static inline int64 LSBi8(int64 i) { return i; }
188 static inline int LSBi4(int i) { return i; }
189 static inline short LSBi2(short i) { return i; }
190
191 static inline int64 i8MSB(int64 i) {
192 return swapi8(i);
193 }
194
195 static inline int64 MSBi8(int64 i) {
196 return swapi8(i);
197 }
198
199 static inline double f8LSB(double f) { return f; }
200 static inline float f4LSB(float f) { return f; }
201
202 static inline double LSBf8(double f) { return f; }
203 static inline float LSBf4(float f) { return f; }
204
205 static inline double f8MSB(double f) {
206 return swapf8(f);
207 }
208
209 static inline double MSBf8(double f) {
210 return swapf8(f);
211 }
212
213 static inline float f4MSB(float f) {
214 return swapf4(f);
215 }
216
217 static inline float MSBf4(float f) {
218 return swapf4(f);
219 }
220
221 #endif
222
223 #endif
qore_i8_u
used to swap byte order of 8-byte values
Definition: qore_bitopts.h:50
qore_i2_u
used to swap 2-byte integers
Definition: qore_bitopts.h:114
int64
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
common.h
qore_i4_u
used to swap byte order of 4-byte values
Definition: qore_bitopts.h:84