Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
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 - 2023 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
38static inline int64 i8LSB(int64 i);
39static inline int i4LSB(int i);
40static inline short i2LSB(short i);
41
42static inline int64 LSBi8(int64 i);
43static inline int LSBi4(int i);
44static inline short LSBi2(short i);
45
46static inline int64 i8MSB(int64 i);
47static inline int64 MSBi8(int64 i);
48
50union 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
68static 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
76static 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
84union 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
98static 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
106static 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
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
125static 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
133static inline int64 i8LSB(int64 i) {
134 return swapi8(i);
135}
136
137static inline int i4LSB(int i) {
138 return swapi4(i);
139}
140
141static inline short i2LSB(short i) {
142 return swapi2(i);
143}
144
145static inline int64 LSBi8(int64 i) {
146 return swapi8(i);
147}
148
149static inline int LSBi4(int i) {
150 return swapi4(i);
151}
152
153static inline short LSBi2(short i) {
154 return swapi2(i);
155}
156
157static inline int64 i8MSB(int64 i) { return i; }
158static inline int64 MSBi8(int64 i) { return i; }
159
160static inline double f8LSB(double f) {
161 return swapf8(f);
162}
163
164static inline float f4LSB(float f) {
165 return swapf4(f);
166}
167
168static inline double LSBf8(double f) {
169 return swapf8(f);
170}
171
172static inline float LSBf4(float f) {
173 return swapf4(f);
174}
175
176static inline double f8MSB(double f) { return f; }
177static inline double MSBf8(double f) { return f; }
178static inline float f4MSB(float f) { return f; }
179static inline float MSBf4(float f) { return f; }
180
181#else // definitions for little endian machines below
182
183static inline int64 i8LSB(int64 i) { return i; }
184static inline int i4LSB(int i) { return i; }
185static inline short i2LSB(short i) { return i; }
186
187static inline int64 LSBi8(int64 i) { return i; }
188static inline int LSBi4(int i) { return i; }
189static inline short LSBi2(short i) { return i; }
190
191static inline int64 i8MSB(int64 i) {
192 return swapi8(i);
193}
194
195static inline int64 MSBi8(int64 i) {
196 return swapi8(i);
197}
198
199static inline double f8LSB(double f) { return f; }
200static inline float f4LSB(float f) { return f; }
201
202static inline double LSBf8(double f) { return f; }
203static inline float LSBf4(float f) { return f; }
204
205static inline double f8MSB(double f) {
206 return swapf8(f);
207}
208
209static inline double MSBf8(double f) {
210 return swapf8(f);
211}
212
213static inline float f4MSB(float f) {
214 return swapf4(f);
215}
216
217static inline float MSBf4(float f) {
218 return swapf4(f);
219}
220
221#endif
222
223#endif
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
used to swap 2-byte integers
Definition: qore_bitopts.h:114
used to swap byte order of 4-byte values
Definition: qore_bitopts.h:84
used to swap byte order of 8-byte values
Definition: qore_bitopts.h:50