1 #ifndef misc_h 2 #define misc_h 3 4 #include <cstdio> 5 #include <cassert> 6 #include <chrono> 7 #include <ctime> 8 #include <cstring> 9 10 #ifdef WIN32 11 #ifdef HAS_PROFILER 12 #include <winsock2.h> 13 #endif 14 #include <windows.h> 15 #define __SEP__ '\\' 16 #else 17 #include <sys/time.h> 18 #include <unistd.h> 19 #define __SEP__ '/' 20 #endif 21 22 #include <stdint.h> 23 #include <chuffed/support/vec.h> 24 25 extern uint64_t bit[65]; 26 27 #define low(s) ((int) (s)) 28 #define high(s) ((int) ((s) >> 32)) 29 #define inSet(i,s) (bit[(i)] & (s)) 30 #define getbit(i,s) (((s) >> (i)) & 1) 31 32 //------ 33 #ifdef NDEBUG 34 #define __FILENAME__ (strrchr(__FILE__, __SEP__) ? strrchr(__FILE__, __SEP__) + 1 : __FILE__) 35 #else 36 #define __FILENAME__ __FILE__ 37 #endif 38 #define CHUFFED_ERROR(...) do { \ 39 fprintf(stderr, "%s:%d: ", __FILENAME__, __LINE__); \ 40 fprintf(stderr, __VA_ARGS__); \ 41 abort(); \ 42 } while (0) 43 44 #define NOT_SUPPORTED CHUFFED_ERROR("Not yet supported\n") 45 #define NEVER CHUFFED_ERROR("Assertion failed.\n") 46 47 // Run time assert 48 #define rassert(expr) do { if (!(expr)) CHUFFED_ERROR("Assertion `%s' failed.\n", #expr); } while (0) 49 50 // Compile time assert, adapted from Boost library 51 template <int x> struct static_assert_test{}; 52 template <bool b> struct STATIC_ASSERTION_FAILURE; 53 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; }; 54 #define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y ) 55 #define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y) 56 #define BOOST_DO_JOIN2( X, Y ) X##Y 57 #define cassert(expr) \ 58 typedef static_assert_test<sizeof(STATIC_ASSERTION_FAILURE<(bool) (expr)>)> \ 59 BOOST_JOIN(boost_static_assert_typedef_, __LINE__) 60 61 #define TL_FAIL() do { printf("=====UNSATISFIABLE=====\n"); printf("%% Top level failure!\n"); exit(0); } while (0) 62 63 //------ 64 65 #define MYRAND_MAX 4294967296.0 66 67 static inline unsigned int myrand(int& rseed) { 68 return (rseed = (long long) 1103515245 * rseed + 12345); 69 } 70 71 #define irand(n) ((int) floor(myrand(so.rnd_seed) / MYRAND_MAX * n)) 72 73 //------ 74 75 using chuffed_clock = std::chrono::steady_clock; 76 using time_point = std::chrono::time_point<chuffed_clock>; 77 using duration = std::chrono::milliseconds; 78 79 static inline double to_sec(duration d) { 80 return std::chrono::duration_cast<std::chrono::duration<double>>(d).count(); 81 } 82 83 template <class T> 84 static inline int bitcount(T s) { 85 int c = 0; 86 while (s) { s &= s-1; c++; } 87 return c; 88 } 89 90 static int mylog2 (int val) { 91 int ret = -1; 92 while (val != 0) { val >>= 1; ret++; } 93 return ret; 94 } 95 96 static double memUsed() { 97 return 0; 98 /* char name[256]; */ 99 /* sprintf(name, "/proc/%d/statm", getpid()); */ 100 /* FILE* in = fopen(name, "rb"); */ 101 /* if (in == NULL) return 0; */ 102 /* int value; */ 103 /* rassert(fscanf(in, "%d", &value) == 1); */ 104 /* fclose(in); */ 105 /* return (double) value * getpagesize() / 1048576; */ 106 } 107 108 template <class T> 109 static T** new2d(int n, int m) { 110 T** a = new T*[n]; 111 T* b = new T[n*m]; 112 for (int i = 0; i < n; i++) { 113 a[i] = b + i * m; 114 } 115 return a; 116 } 117 118 #endif 119