macros.hpp

View source code here on GitHub!

CL_COMPILER
CLANG_COMPILER
GCC_COMPILER

These macros detect which compiler the program is being made with. They will be 1 if it is that compiler, and 0 otherwise.

X64_COMPILER
X86_COMPILER
ARM_COMPILER
ARM_THUMB

These macros attempt to detect the architecture the program is being compiled for.

likely(x)
unlikely(x)

These macros implement the likely() and unlikely() flags, as in the Linux kernel to assist in branch prediction. On cl it has no effect.

MAX_FACTORIAL_64
MAX_FACTORIAL_128
PCC_SQRT_ACCURACY
MAX_POW_10_16
POW_OF_MAX_POW_10_16
MAX_POW_10_32
POW_OF_MAX_POW_10_32
MAX_POW_10_64
POW_OF_MAX_POW_10_64
MAX_POW_10_128
POW_OF_MAX_POW_10_128
PROGRAM_TAIL(prob)

Conditionally generates a main() function if running under the Python test runner or compiling as a standalone program. Takes in as an argument the name of the function which implements this Project Euler solution.

 1#pragma once
 2
 3// compiler info section
 4
 5#if (defined(_MSC_VER) && !defined(__clang__))
 6    #define CL_COMPILER 1
 7#else
 8    #define CL_COMPILER 0
 9#endif
10#if (defined(__clang__))
11    #define CLANG_COMPILER 1
12#else
13    #define CLANG_COMPILER 0
14#endif
15#if (defined(__GNUC__) && !defined(__clang__))
16    #define GCC_COMPILER 1
17#else
18    #define GCC_COMPILER 0
19#endif
20#ifdef __EMSCRIPTEN__
21    #define EMCC_COMPILER 1
22#else
23    #define EMCC_COMPILER 0
24#endif
25
26#if (defined(_M_X64) || defined(_M_AMD64) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64))
27    #define X64_COMPILER 1
28#else
29    #define X64_COMPILER 0
30#endif
31#if (!X64_COMPILER && (defined(_M_X86) || defined(_M_IX86) || defined(i386) || defined(__i386) || defined(__i386__) || defined(_X86_)))
32    #define X86_COMPILER 1
33#else
34    #define X86_COMPILER 0
35#endif
36#if (defined(__arm__) || defined(__aarch64__) || defined(__thumb__) || defined(_M_ARM) || defined(_M_ARMT) || defined(__ARM_ARCH))
37    #define ARM_COMPILER 1
38#else
39    #define ARM_COMPILER 0
40#endif
41#if (ARM_COMPILER && (defined(__thumb__) || defined(_M_ARMT)))
42    #define ARM_THUMB 1
43#else
44    #define ARM_THUMB 0
45#endif
46#if (defined(__wasm__) || defined(__wasm32__) || defined(__wasm64__))
47    #define WASM_COMPILER 1
48    #include <emscripten.h>
49#else
50    #define WASM_COMPILER 0
51    #define EMSCRIPTEN_KEEPALIVE
52#endif
53
54// helper macro function section
55
56#if !(CL_COMPILER)
57    #define likely(x)   __builtin_expect(!!(x), 1)
58    #define unlikely(x) __builtin_expect(!!(x), 0)
59#else
60    #define likely(x) x
61    #define unlikely(x) x
62#endif
63
64// constants section
65
66#define MAX_FACTORIAL_64 20
67#define MAX_FACTORIAL_128 34
68#define MAX_POW_10_16 10000U
69#define POW_OF_MAX_POW_10_16 4
70#define MAX_POW_10_32 1000000000UL
71#define POW_OF_MAX_POW_10_32 9
72#define MAX_POW_10_64 10000000000000000000ULL
73#define POW_OF_MAX_POW_10_64 19
74#define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64)
75#define POW_OF_MAX_POW_10_128 38
76
77#ifndef UNITY_END
78#define PROGRAM_TAIL(prob) \
79int main(int argc, char const *argv[]) { \
80    std::cout << prob() << std::endl; \
81    return 0; \
82}
83#else
84#define PROGRAM_TAIL(prob)
85#endif