-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpe_internal
507 lines (426 loc) · 11.8 KB
/
pe_internal
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#ifndef PE_INTERNAL_
#define PE_INTERNAL_
#include "pe_config"
// How to use a library in MinGW or VC++ (including compiled binaries on windows
// (x64)) https://github.com/baihacker/pe/blob/master/libraries_on_win64.md#use
// Compiler and cpp version check
// https://blog.kowalczyk.info/article/j/guide-to-predefined-macros-in-c-compilers-gcc-clang-msvc-etc..html
#if defined(__GNUC__)
// COMPILER_GNU: gcc/mingw32/mingw64/clang
#define COMPILER_GNU
#if defined(__clang__)
#define COMPILER_CLANG
#elif defined(__MINGW64__)
#define COMPILER_MINGW64
#elif defined(__MINGW32__)
#define COMPILER_MINGW32
#else
#define COMPILER_GCC
#endif
#define PE_CPP_VERSION __cplusplus
#elif defined(_MSC_VER)
// COMPILER_MSVC
#define COMPILER_MSVC
// VC2015 (1900) or above to support c++11
// https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B#Internal_version_numbering
// https://en.cppreference.com/w/cpp/compiler_support#cpp11
// https://blogs.msdn.microsoft.com/vcblog/2018/04/09/msvc-now-correctly-reports-__cplusplus/
#if defined(_MSVC_LANG)
#define PE_CPP_VERSION _MSVC_LANG
#elif _MSC_VER == 1900
#define PE_CPP_VERSION 201103L
#else
#define PE_CPP_VERSION 199711L
#endif
#else
#error "Unknown compiler."
#endif
// C++ version check
#if PE_CPP_VERSION < 201103L
#define PE_HAS_CPP11 0
#else
#define PE_HAS_CPP11 1
#endif
#if PE_CPP_VERSION < 201402L
#define PE_HAS_CPP14 0
#else
#define PE_HAS_CPP14 1
#endif
#if PE_CPP_VERSION < 201703L
#define PE_HAS_CPP17 0
#else
#define PE_HAS_CPP17 1
#endif
#if PE_CPP_VERSION < 202002L
#define PE_HAS_CPP20 0
#else
#define PE_HAS_CPP20 1
#endif
// Target CPU arch
// https://stackoverflow.com/questions/152016/detecting-cpu-architecture-compile-time
#if defined(__x86_64__) || defined(_M_X64)
#define PE_X86_64 1
#define PE_X86_32 0
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
#define PE_X86_64 0
#define PE_X86_32 1
#else
#define PE_X86_64 0
#define PE_X86_32 0
#if defined(COMPILER_GNU)
#warning "Unsupported target arch."
#else
#pragma message("Unsupported target arch.")
#endif
#endif
// Target OS type
#if _WIN32 || _WIN64
#define OS_TYPE_WIN 1
#define OS_TYPE_APPLE 0
#define OS_TYPE_LINUX 0
#elif defined(__APPLE__)
#define OS_TYPE_WIN 0
#define OS_TYPE_APPLE 1
#define OS_TYPE_LINUX 0
#else
#define OS_TYPE_WIN 0
#define OS_TYPE_APPLE 0
#define OS_TYPE_LINUX 1
#endif
#if PE_X86_64
#define _AMD64_
#else
#define _X86_
#endif
#if defined(__AVX2__) && __AVX2__
#define PE_HAS_AVX2 1
#else
#define PE_HAS_AVX2 0
#endif
// Checks endian.
// https://github.com/abseil/abseil-cpp/blob/master/absl/base/config.h
#if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#define PE_IS_LITTLE_ENDIAN 1
#define PE_IS_BIG_ENDIAN 0
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define PE_IS_LITTLE_ENDIAN 0
#define PE_IS_BIG_ENDIAN 1
#elif defined(OS_TYPE_WIN)
#define PE_IS_LITTLE_ENDIAN 1
#define PE_IS_BIG_ENDIAN 0
#else
#error "endian detection fails"
#endif
// At least c++17
#if !PE_HAS_CPP17
#error "c++17 or above only"
#endif
// TODO(baihacker): we may disable non-standard c++.
// Workaround to detect gnu++XX
// https://quuxplusone.github.io/blog/2019/02/28/is-int128-integral/
// https://stackoverflow.com/questions/41198673/uint128-t-not-working-with-clang-and-libstdc
// Other references
// https://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html
// https://stackoverflow.com/questions/16088282/is-there-a-128-bit-integer-in-gcc
// #if defined(__GLIBCXX_TYPE_INT_N_0)
// #error "Please add --std=c++XX to use standard c++ instead of --std=gnu++XX"
// #endif
// Configurations
// int128
#if !defined(TRY_TO_USE_INT128)
#define TRY_TO_USE_INT128 1
#endif
// Let int128 be recognized in vscode.
// Make the vscode support int128
#if defined(VSCODE_EDITOR)
// #define __SIZEOF_INT128__ 16
// #define __int128 long long
#endif
// Use __SIZEOF_INT128__ to detect int128 since gcc 4.6 and clang 3.3.
// https://stackoverflow.com/questions/16088282/is-there-a-128-bit-integer-in-gcc/54815033
#if TRY_TO_USE_INT128 && defined(__SIZEOF_INT128__)
#define PE_HAS_INT128 1
#else
#define PE_HAS_INT128 0
#endif
// https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html
// https://gcc.gnu.org/onlinedocs/gcc-10.2.0/libquadmath.pdf
#if defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
#define PE_HAS_FLOAT128 1
#else
#define PE_HAS_FLOAT128 0
#endif
#if !defined(ENABLE_ASSERT)
#define ENABLE_ASSERT 1
#endif
// Link: https://www.openmp.org/
// Doc: https://www.openmp.org/wp-content/uploads/openmp-4.5.pdf
// Add -fopenmp in gcc compile command of config it in vc solution property
// dialog. Change 1 to 0, if you want to prevent pe from using OPENMP even if
// OPENMP is enabled by compiler.
#if !defined(ENABLE_OPENMP)
#if defined(_OPENMP)
#define ENABLE_OPENMP 1
#else
#define ENABLE_OPENMP 0
#endif
#else
#if ENABLE_OPENMP && !defined(_OPENMP)
// Clang:
// Use -fopenmp to enable default openmp, the default openmp is specified when
// building the clang. You can find the default openmp in
// "include\clang\Config\config.h". Use -fopenmp=libgomp or -fopenmp=libomp to
// specify the openmp implementation explicitly. You need to specify the
// corresponding library explicitly, i.e. add -lomp or -lgomp. Clang only works
// with libomp. If the implementation is not libomp, _OPENMP is not defined and
// it works in one thread.
#if defined(COMPILER_GNU)
#warning "ENABLE_OPENMP is 1 but the openmp compiler option is off."
#else
#pragma message("ENABLE_OPENMP is 1 but the openmp compiler option is off.")
#endif
#endif
#endif
// Link: http://eigen.tuxfamily.org/index.php?title=Main_Page
// Doc: http://eigen.tuxfamily.org/dox/
// Please make sure #include <Eigen/Dense> work
#if !defined(ENABLE_EIGEN)
#define ENABLE_EIGEN 1
#endif
// Link: https://gmplib.org/
// Doc: https://gmplib.org/manual/C_002b_002b-Interface-General.html
#if !defined(ENABLE_GMP)
#define ENABLE_GMP 1
#endif
// Link: http://www.flintlib.org
// Doc: http://www.flintlib.org/flint-2.5.pdf
#if !defined(ENABLE_FLINT)
#define ENABLE_FLINT 1
#endif
// Link: https://www.mpfr.org/
#if !defined(ENABLE_MPFR)
#define ENABLE_MPFR 1
#endif
// Link: https://bellard.org/libbf/
#if !defined(ENABLE_LIBBF)
#define ENABLE_LIBBF 1
#endif
// https://www.shoup.net/ntl/download.html
#if !defined(ENABLE_NTL)
#define ENABLE_NTL
#endif
// https://www.shoup.net/ntl/download.html
#if !defined(ENABLE_NTL)
#define ENABLE_NTL 1
#endif
// https://zeromq.org
#if !defined(ENABLE_ZMQ)
#define ENABLE_ZMQ 1
#endif
// https://github.com/kimwalisch/primesieve
#if !defined(ENABLE_PRIME_SIEVE)
#define ENABLE_PRIME_SIEVE 1
#endif
// Check dependencies
#if ENABLE_FLINT
#if !ENABLE_GMP
#error gmp should be enabled if flint is enabled
#endif
#if !ENABLE_MPFR
#error mpfr should be enabled if flint is enabled
#endif
#endif
// Include third party libraries and apply fixes to them.
// Add the corresponding libraries if the compiler is msvc.
#if defined(COMPILER_MSVC)
#pragma warning(disable : 4996) // Always disable 4996
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4267)
#pragma warning(disable : 4146)
#endif
#if ENABLE_LIBBF
// Use the following fix if the libbf version is less than 2019-02-10.
// pe_ntt_libbf is also required to modify, see the modified code in pe4.0.
// #include <cstdint>
// #include <cstdlib>
//
// extern "C" {
//
// #include <libbf.h>
//
// void* bf_realloc(void* ptr, size_t size) { return realloc(ptr, size); }
// }
extern "C" {
#include <libbf.h>
}
#if defined(COMPILER_MSVC)
#pragma comment(lib, "libbf.a")
#endif
#endif
// flint must come before the following libs.
#if ENABLE_FLINT
#if defined(COMPILER_MSVC)
#include <malloc.h>
#define alloca _alloca
#endif
#include <flint.h>
#include <fmpz.h>
#include <fq_nmod_poly.h>
#include <nmod_poly.h>
#if defined(COMPILER_MSVC)
#pragma comment(lib, "libflint.a")
#endif
#endif
#if ENABLE_GMP
#include <gmp.h>
#include <gmpxx.h>
#if defined(COMPILER_MSVC)
#include <iostream>
// Not in pe namespace
static inline std::ostream& operator<<(std::ostream& o, __mpz_struct const* x) {
size_t size = mpz_sizeinbase(x, 10) + 1;
char* buff = new char[size + 1];
if (buff != NULL) {
gmp_sprintf(buff, "%Zd", x);
}
o << buff;
delete[] buff;
return o;
}
#endif
#if defined(COMPILER_MSVC)
#pragma comment(lib, "libgmpxx.a")
#pragma comment(lib, "libgmp.a")
#endif
#endif
#if ENABLE_MPFR
#include <mpfr.h>
// Don't use this adapter by default.
// Due to some compatibilities reason,
// If this adapter is enabled, the initialization from string will
// have precision issue, memory allocation function may fail.
// #include <mpf2mpfr.h>
#if defined(COMPILER_MSVC)
#pragma comment(lib, "libmpfr.a")
#endif
#endif
#if ENABLE_OPENMP
#include <omp.h>
#endif
#if ENABLE_EIGEN
#include <Eigen/Core>
#endif
#if ENABLE_NTL
#if defined(COMPILER_MSVC)
#pragma message( \
"Cannot support ntl in msvc due to pthread dependency and binary compatibility.")
#undef ENABLE_NTL
#define ENABLE_NTL 0
#endif
#endif
#if ENABLE_NTL
#include <NTL/ZZ.h>
#include <NTL/ZZ_p.h>
#include <NTL/ZZ_pX.h>
#include <NTL/lzz_p.h>
#include <NTL/lzz_pX.h>
#endif
#if ENABLE_ZMQ
#include <zmq/zmq.h>
#endif
#if ENABLE_PRIME_COUNT
#include <primecount.hpp>
#if defined(COMPILER_MSVC)
#pragma message( \
"Cannot support prime count in msvc due to pthread dependency and binary compatibility.")
#undef ENABLE_PRIME_COUNT
#define ENABLE_PRIME_COUNT 0
#pragma comment(lib, "libprimecount.a")
#endif
#endif
#if ENABLE_PRIME_SIEVE
#include <primesieve.hpp>
#if defined(COMPILER_MSVC)
#pragma message( \
"Cannot support prime sieve in msvc due to pthread dependency and binary compatibility.")
#undef ENABLE_PRIME_SIEVE
#define ENABLE_PRIME_SIEVE 0
#pragma comment(lib, "libprimesieve.a")
#endif
#endif
#if defined(COMPILER_MSVC)
#pragma warning(pop)
#endif
#if defined(COMPILER_MSVC) && \
(ENABLE_LIBBF || ENABLE_FLINT || ENABLE_GMP || ENABLE_MPFR)
#pragma comment(lib, "libgcc_s.a")
#pragma comment(lib, "libgcc.a")
#pragma comment(lib, "legacy_stdio_definitions.lib")
#endif
// Disable unnecessary features in windows.h
#if OS_TYPE_WIN
#define NOGDICAPMASKS
#define NOVIRTUALKEYCODES
#define NOWINMESSAGES
#define NOWINSTYLES
#define NOSYSMETRICS
#define NOMENUS
#define NOICONS
#define NOKEYSTATES
#define NOSYSCOMMANDS
#define NORASTEROPS
#define NOSHOWWINDOW
#define OEMRESOURCE
#define NOATOM
// #define NOCLIPBOARD
#define NOCOLOR
#define NOCTLMGR
#define NODRAWTEXT
#define NOGDI
#define NOKERNEL
// #define NOUSER
#define NONLS
// #define NOMB
#define NOMEMMGR
#define NOMETAFILE
#define NOMINMAX 1
#define NOMSG
#define NOOPENFILE
#define NOSCROLL
#define NOSERVICE
#define NOSOUND
#define NOTEXTMETRIC
#define NOWH
#define NOWINOFFSETS
#define NOCOMM
#define NOKANJI
#define NOHELP
#define NOPROFILER
#define NODEFERWINDOWPOS
#define NOMCX
#define NOAPISET
#define NOCRYPT
#endif
// Disable max/min in windows.h
#ifndef NOMINMAX
#define NOMINMAX 1
#endif
#if OS_TYPE_WIN
// #if defined(COMPILER_MSVC) && OS_TYPE_WIN && PE_X86_64
// pe_mod depends VirtualProtect when the target arch is x64.
// pe_memory depends on several APIs if the platform is windows.
#include <windows.h>
// Used by pe_parallel
#include <process.h>
#endif
#if defined(COMPILER_MSVC)
#pragma comment(linker, "/STACK:268435456")
#endif
// Used by pe_float128
#if PE_HAS_FLOAT128 && !defined(COMPILER_CLANG)
#include <quadmath.h>
#endif
#endif