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