ScummVM API documentation
bbop.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Various utility bit and byte operations
25 //
26 //=============================================================================
27 
28 #ifndef AGS_SHARED_UTIL_BBOP_H
29 #define AGS_SHARED_UTIL_BBOP_H
30 
31 #include "ags/shared/core/platform.h"
32 #include "ags/shared/core/types.h"
33 
34 namespace AGS3 {
35 
36 #if AGS_PLATFORM_ENDIAN_BIG || defined (TEST_BIGENDIAN)
37 #define BITBYTE_BIG_ENDIAN
38 #endif
39 
40 namespace AGS {
41 namespace Shared {
42 
43 enum DataEndianess {
44  kBigEndian,
45  kLittleEndian,
46  #if defined (BITBYTE_BIG_ENDIAN)
47  kDefaultSystemEndianess = kBigEndian
48  #else
49  kDefaultSystemEndianess = kLittleEndian
50  #endif
51 };
52 
53 
54 //
55 // Various bit flags operations
56 //
57 // Converts from one flag into another:
58 // sets flag2 if flag1 IS set
59 // TODO: find more optimal way, using bitwise ops?
60 inline int FlagToFlag(int value, int flag1, int flag2) {
61  return ((value & flag1) != 0) * flag2;
62 }
63 // Sets flag2 if flag1 is NOT set
64 inline int FlagToNoFlag(int value, int flag1, int flag2) {
65  return ((value & flag1) == 0) * flag2;
66 }
67 
68 
69 namespace BitByteOperations {
70 
71 struct IntFloatSwap {
72  union {
73  float f;
74  int32_t i32;
75  } val;
76 
77  explicit IntFloatSwap(int32_t i) { val.i32 = i; }
78  explicit IntFloatSwap(float f) { val.f = f; }
79 };
80 
81 inline int16_t SwapBytesInt16(const int16_t val) {
82  return ((val >> 8) & 0xFF) | ((val << 8) & 0xFF00);
83 }
84 
85 inline int32_t SwapBytesInt32(const int32_t val) {
86  return ((val >> 24) & 0xFF) | ((val >> 8) & 0xFF00) | ((val << 8) & 0xFF0000) | ((val << 24) & 0xFF000000);
87 }
88 
89 inline int64_t SwapBytesInt64(const int64_t val) {
90  return ((val >> 56) & 0xFF) | ((val >> 40) & 0xFF00) | ((val >> 24) & 0xFF0000) |
91  ((val >> 8) & 0xFF000000) | ((val << 8) & 0xFF00000000LL) |
92  ((val << 24) & 0xFF0000000000LL) | ((val << 40) & 0xFF000000000000LL) | ((val << 56) & 0xFF00000000000000LL);
93 }
94 
95 inline float SwapBytesFloat(const float val) {
96  IntFloatSwap swap(val);
97  swap.val.i32 = SwapBytesInt32(swap.val.i32);
98  return swap.val.f;
99 }
100 
101 inline int16_t Int16FromLE(const int16_t val) {
102  #if defined (BITBYTE_BIG_ENDIAN)
103  return SwapBytesInt16(val);
104  #else
105  return val;
106  #endif
107 }
108 
109 inline int32_t Int32FromLE(const int32_t val) {
110  #if defined (BITBYTE_BIG_ENDIAN)
111  return SwapBytesInt32(val);
112  #else
113  return val;
114  #endif
115 }
116 
117 inline int64_t Int64FromLE(const int64_t val) {
118  #if defined (BITBYTE_BIG_ENDIAN)
119  return SwapBytesInt64(val);
120  #else
121  return val;
122  #endif
123 }
124 
125 inline float FloatFromLE(const float val) {
126  #if defined (BITBYTE_BIG_ENDIAN)
127  return SwapBytesFloat(val);
128  #else
129  return val;
130  #endif
131 }
132 
133 inline int16_t Int16FromBE(const int16_t val) {
134  #if defined (BITBYTE_BIG_ENDIAN)
135  return val;
136  #else
137  return SwapBytesInt16(val);
138  #endif
139 }
140 
141 inline int32_t Int32FromBE(const int32_t val) {
142  #if defined (BITBYTE_BIG_ENDIAN)
143  return val;
144  #else
145  return SwapBytesInt32(val);
146  #endif
147 }
148 
149 inline int64_t Int64FromBE(const int64_t val) {
150  #if defined (BITBYTE_BIG_ENDIAN)
151  return val;
152  #else
153  return SwapBytesInt64(val);
154  #endif
155 }
156 
157 inline float FloatFromBE(const float val) {
158  #if defined (BITBYTE_BIG_ENDIAN)
159  return val;
160  #else
161  return SwapBytesFloat(val);
162  #endif
163 }
164 
165 } // namespace BitByteOperations
166 
167 
168 // Aliases for easier calling
169 namespace BBOp = BitByteOperations;
170 
171 
172 } // namespace Shared
173 } // namespace AGS
174 } // namespace AGS3
175 
176 #endif
Definition: achievements_tables.h:27
Definition: ags.h:40