forked from hxim/paq8px
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
348 lines (300 loc) · 8.38 KB
/
utils.hpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef PAQ8PX_UTILS_HPP
#define PAQ8PX_UTILS_HPP
#include <cstdint>
static_assert(sizeof(uint8_t) == 1, "sizeof(uint8_t)");
static_assert(sizeof(uint16_t) == 2, "sizeof(uint16_t)");
static_assert(sizeof(uint32_t) == 4, "sizeof(uint32_t)");
static_assert(sizeof(uint64_t) == 8, "sizeof(uint64_t)");
static_assert(sizeof(short) == 2, "sizeof(short)");
static_assert(sizeof(int) == 4, "sizeof(int)");
//////////////////////// Target OS/Compiler ////////////////////////////////
#if defined(_WIN32) || defined(_MSC_VER)
#ifndef WINDOWS
#define WINDOWS //to compile for Windows
#endif
#endif
#if defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__)
#ifndef UNIX
#define UNIX //to compile for Unix, Linux, Solaris, MacOS / Darwin, etc)
#endif
#endif
#if !defined(WINDOWS) && !defined(UNIX)
#error Unknown target system
#endif
// Floating point operations need IEEE compliance
// Do not use compiler optimization options such as the following:
// gcc : -ffast-math (and -Ofast, -funsafe-math-optimizations, -fno-rounding-math)
// vc++: /fp:fast
#if defined(__FAST_MATH__) || defined(_M_FP_FAST) // gcc vc++
#error Avoid using aggressive floating-point compiler optimization flags
#endif
#if defined(_MSC_VER)
#define ALWAYS_INLINE __forceinline
#elif defined(__GNUC__)
#define ALWAYS_INLINE inline __attribute__((always_inline))
#else
#define ALWAYS_INLINE inline
#endif
#include <algorithm>
#include <cstdio>
// Determining the proper printf() format specifier for 64 bit unsigned integers:
// - on Windows MSVC and MinGW-w64 use the MSVCRT runtime where it is "%I64u"
// - on Linux it is "%llu"
// The correct value is provided by the PRIu64 macro which is defined here:
#include <cinttypes> //PRIu64
// Platform-specific includes
#ifdef UNIX
#include <cerrno> //errno
#include <climits> //PATH_MAX (for OSX)
#include <cstring> //strlen(), strcpy(), strcat(), strerror(), memset(), memcpy(), memmove()
#include <unistd.h> //isatty()
#else
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h> //CreateDirectoryW, CommandLineToArgvW, GetConsoleOutputCP, SetConsoleOutputCP
//GetCommandLineW, GetModuleFileNameW, GetStdHandle, GetTempFileName
//MultiByteToWideChar, WideCharToMultiByte,
//FileType, FILE_TYPE_PIPE, FILE_TYPE_DISK,
//uRetVal, DWORD, UINT, TRUE, MAX_PATH, CP_UTF8, etc.
#endif
#define DEFAULT_LEARNING_RATE 7
typedef enum {
SIMD_NONE, SIMD_SSE2, SIMD_AVX2
} SIMD;
struct ErrorInfo {
uint32_t data[2], sum, mask, collected;
void reset() {
memset(this, 0, sizeof(*this));
}
};
static inline auto square(uint32_t x) -> uint32_t {
return x * x;
}
static inline auto min(int a, int b) -> int { return std::min<int>(a, b); }
static inline auto min(uint64_t a, uint64_t b) -> uint64_t { return std::min<uint64_t>(a, b); }
static inline auto max(int a, int b) -> int { return std::max<int>(a, b); }
template<typename T>
constexpr auto isPowerOf2(T x) -> bool {
return ((x & (x - 1)) == 0);
}
#ifndef NDEBUG
#if defined(UNIX)
#include <execinfo.h>
#define BACKTRACE() \
{ \
void *callstack[128]; \
int frames = backtrace(callstack, 128); \
char **strs = backtrace_symbols(callstack, frames); \
for (int i = 0; i < frames; ++i) { \
printf("%s\n", strs[i]); \
} \
free(strs); \
}
#else
// TODO: How to implement this on Windows?
#define BACKTRACE() \
{ }
#endif
#else
#define BACKTRACE()
#endif
// A basic exception class to let catch() in main() know
// that the exception was thrown intentionally.
class IntentionalException : public std::exception {};
// Error handler: print message if any, and exit
[[noreturn]] static void quit(const char *const message = nullptr) {
if( message != nullptr ) {
printf("\n%s", message);
}
printf("\n");
throw IntentionalException();
}
/////////////////////// Global context /////////////////////////
typedef enum {
DEFAULT = 0,
FILECONTAINER,
JPEG,
HDR,
IMAGE1,
IMAGE4,
IMAGE8,
IMAGE8GRAY,
IMAGE24,
IMAGE32,
AUDIO,
AUDIO_LE,
EXE,
CD,
ZLIB,
BASE64,
GIF,
PNG8,
PNG8GRAY,
PNG24,
PNG32,
TEXT,
TEXT_EOL,
RLE,
LZW
} BlockType;
static inline auto hasRecursion(BlockType ft) -> bool {
return ft == CD || ft == ZLIB || ft == BASE64 || ft == GIF || ft == RLE || ft == LZW || ft == FILECONTAINER;
}
static inline auto hasInfo(BlockType ft) -> bool {
return ft == IMAGE1 || ft == IMAGE4 || ft == IMAGE8 || ft == IMAGE8GRAY || ft == IMAGE24 || ft == IMAGE32 || ft == AUDIO ||
ft == AUDIO_LE || ft == PNG8 || ft == PNG8GRAY || ft == PNG24 || ft == PNG32;
}
static inline auto hasTransform(BlockType ft) -> bool {
return ft == IMAGE24 || ft == IMAGE32 || ft == AUDIO_LE || ft == EXE || ft == CD || ft == ZLIB || ft == BASE64 || ft == GIF ||
ft == TEXT_EOL || ft == RLE || ft == LZW;
}
static inline auto isPNG(BlockType ft) -> bool { return ft == PNG8 || ft == PNG8GRAY || ft == PNG24 || ft == PNG32; }
#define OPTION_MULTIPLE_FILE_MODE 1U
#define OPTION_BRUTE 2U
#define OPTION_TRAINEXE 4U
#define OPTION_TRAINTXT 8U
#define OPTION_ADAPTIVE 16U
#define OPTION_SKIPRGB 32U
//////////////////// Cross-platform definitions /////////////////////////////////////
#ifdef _MSC_VER
#define fseeko(a, b, c) _fseeki64(a, b, c)
#define ftello(a) _ftelli64(a)
#else
#ifndef UNIX
#ifndef fseeko
#define fseeko(a, b, c) fseeko64(a, b, c)
#endif
#ifndef ftello
#define ftello(a) ftello64(a)
#endif
#endif
#endif
#ifdef WINDOWS
#define strcasecmp _stricmp
#endif
#if defined(__GNUC__) || defined(__clang__)
#define bswap(x) __builtin_bswap32(x)
#define bswap64(x) __builtin_bswap64(x)
#elif defined(_MSC_VER)
#define bswap(x) _byteswap_ulong(x)
#define bswap64(x) _byteswap_uint64(x)
#else
#define bswap(x) \
+(((( x ) &0xff000000) >> 24) | +((( x ) &0x00ff0000) >> 8) | +((( x ) &0x0000ff00) << 8) | +((( x ) &0x000000ff) << 24))
#define bswap64(x) \
+((x) >> 56) |
+ (((x)<<40) & 0x00FF000000000000) | \
+ (((x)<<24) & 0x0000FF0000000000) | \
+ (((x)<<8 ) & 0x000000FF00000000) | \
+ (((x)>>8 ) & 0x00000000FF000000) | \
+ (((x)>>24) & 0x0000000000FF0000) | \
+ (((x)>>40) & 0x000000000000FF00) | \
+ ((x) << 56))
#endif
#if defined(_MSC_VER)
#define ALWAYS_INLINE __forceinline
#elif defined(__GNUC__)
#define ALWAYS_INLINE inline __attribute__((always_inline))
#else
#define ALWAYS_INLINE inline
#endif
#define TAB 0x09
#define NEW_LINE 0x0A
#define CARRIAGE_RETURN 0x0D
#define SPACE 0x20
#define QUOTE 0x22
#define APOSTROPHE 0x27
static inline auto clip(int const px) -> uint8_t {
if( px > 255 ) {
return 255;
}
if( px < 0 ) {
return 0;
}
return px;
}
static inline auto clamp4(const int px, const uint8_t n1, const uint8_t n2, const uint8_t n3, const uint8_t n4) -> uint8_t {
int maximum = n1;
if( maximum < n2 ) {
maximum = n2;
}
if( maximum < n3 ) {
maximum = n3;
}
if( maximum < n4 ) {
maximum = n4;
}
int minimum = n1;
if( minimum > n2 ) {
minimum = n2;
}
if( minimum > n3 ) {
minimum = n3;
}
if( minimum > n4 ) {
minimum = n4;
}
if( px < minimum ) {
return minimum;
}
if( px > maximum ) {
return maximum;
}
return px;
}
#ifdef _MSC_VER
#include <intrin.h>
#ifndef __GNUC__
inline int __builtin_clz( unsigned long v ) {
unsigned long r = 0;
if (_BitScanReverse(&r, v)) {
return 31 - r;
}
return 31;
}
inline int __builtin_ctz( unsigned long v ) {
unsigned long r = 0;
_BitScanForward(&r, v);
return (int) r;
}
#endif
#endif
/**
* Returns floor(log2(x)).
* 0/1->0, 2->1, 3->1, 4->2 ..., 30->4, 31->4, 32->5, 33->5
* @param x
* @return floor(log2(x))
*/
static auto ilog2(uint32_t x) -> uint32_t {
#ifdef _MSC_VER
DWORD tmp = 0;
if (x != 0) _BitScanReverse(&tmp, x);
return tmp;
#elif __GNUC__
if( x != 0 ) {
x = 31 - __builtin_clz(x);
}
return x;
#else
//copy the leading "1" bit to its left (0x03000000 -> 0x03ffffff)
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
//how many trailing bits do we have (except the first)?
return BitCount(x >> 1);
#endif
}
static inline auto logMeanDiffQt(const uint8_t a, const uint8_t b, const uint8_t limit = 7) -> uint8_t {
if( a == b ) {
return 0;
}
uint8_t sign = a > b ? 8 : 0;
return sign | min(limit, ilog2((a + b) / max(2, abs(a - b) * 2) + 1));
}
static inline auto logQt(const uint8_t px, const uint8_t bits) -> uint32_t {
return (uint32_t(0x100 | px)) >> max(0, static_cast<int>(ilog2(px) - bits));
}
#endif //PAQ8PX_UTILS_HPP