-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.h
49 lines (39 loc) · 1.05 KB
/
utility.h
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
#pragma once
#include <stdint.h>
#include <fstream>
#include <cstring>
using namespace std;
#ifdef _MSC_VER
#define BSWAP32(x) _byteswap_ulong(x)
#define BSWAP16(x) _byteswap_ushort(x)
#else
inline uint32_t bswap32(uint32_t x) {
return (x >> 24) | ((x & 0xFF0000) >> 8) | ((x & 0xFF00) << 8) | ((x & 0xFF) << 24);
}
inline uint16_t bswap16(uint16_t x) {
return (x >> 8) | ((x & 0xFF) << 8);
}
#define BSWAP32(x) bswap32(x)
#define BSWAP16(x) bswap16(x)
#endif
inline int16_t clipInt16(int a)
{
if ((a + 0x8000U) & ~0xFFFF) return (a >> 31) ^ 0x7FFF;
else return a;
}
template <typename T>
inline T readValue(fstream& fs) {
T res;
fs.read((char*)&res, sizeof(T));
return res;
}
template <typename T>
inline void bufferWriteValue(uint8_t* &buf, T val) {
*reinterpret_cast<T*>(buf) = val;
buf += sizeof(T);
}
inline void bufferWrite16BEUnalign(uint8_t* &buf, uint16_t val) {
uint16_t valueBE = BSWAP16(val);
memcpy(buf, &valueBE, 2);
buf += 2;
}