Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / om-file-format-sys / c / include / conf.h
conf.h281 lines · 14.0 KiBRaw
| 1 | /** |
| 2 | Copyright (C) powturbo 2013-2019 |
| 3 | GPL v2 License |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License along |
| 16 | with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | |
| 19 | - homepage : https://sites.google.com/site/powturbo/ |
| 20 | - github : https://github.com/powturbo |
| 21 | - twitter : https://twitter.com/powturbo |
| 22 | - email : powturbo [_AT_] gmail [_DOT_] com |
| 23 | **/ |
| 24 | |
| 25 | // conf.h - config & common |
| 26 | #ifndef CONF_H |
| 27 | #define CONF_H |
| 28 | //------------------------- Compiler ------------------------------------------ |
| 29 | #if defined(__GNUC__) |
| 30 | #include <stdint.h> |
| 31 | #define ALIGNED(t,v,n) t v __attribute__ ((aligned (n))) |
| 32 | #define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 33 | #define NOINLINE __attribute__((noinline)) |
| 34 | #define _PACKED __attribute__ ((packed)) |
| 35 | #define likely(x) __builtin_expect((x),1) |
| 36 | #define unlikely(x) __builtin_expect((x),0) |
| 37 | |
| 38 | #define popcnt32(_x_) __builtin_popcount(_x_) |
| 39 | #define popcnt64(_x_) __builtin_popcountll(_x_) |
| 40 | |
| 41 | #if defined(__i386__) || defined(__x86_64__) |
| 42 | //x,__bsr32: 1:0,2:1,3:1,4:2,5:2,6:2,7:2,8:3,9:3,10:3,11:3,12:3,13:3,14:3,15:3,16:4,17:4,18:4,19:4,20:4,21:4,22:4,23:4,24:4,25:4,26:4,27:4,28:4,29:4,30:4,31:4,32:5 |
| 43 | // x,bsr32: 0:0,1:1,2:2,3:2,4:3,5:3,6:3,7:3,8:4,9:4,10:4,11:4,12:4,13:4,14:4,15:4,16:5,17:5,18:5,19:5,20:5,21:5,22:5,23:5,24:5,25:5,26:5,27:5,28:5,29:5,30:5,31:5,32:6, |
| 44 | static inline int __bsr32( int x) { asm("bsr %1,%0" : "=r" (x) : "rm" (x) ); return x; } |
| 45 | static inline int bsr32( int x) { int b = -1; asm("bsrl %1,%0" : "+r" (b) : "rm" (x) ); return b + 1; } |
| 46 | static inline int bsr64(uint64_t x ) { return x?64 - __builtin_clzll(x):0; } |
| 47 | static inline int __bsr64(uint64_t x ) { return 63 - __builtin_clzll(x); } |
| 48 | |
| 49 | static inline unsigned rol32(unsigned x, int s) { asm ("roll %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } |
| 50 | static inline unsigned ror32(unsigned x, int s) { asm ("rorl %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } |
| 51 | static inline uint64_t rol64(uint64_t x, int s) { asm ("rolq %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } |
| 52 | static inline uint64_t ror64(uint64_t x, int s) { asm ("rorq %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } |
| 53 | #else |
| 54 | static inline int __bsr32(unsigned x ) { return 31 - __builtin_clz( x); } |
| 55 | static inline int bsr32(int x ) { return x?32 - __builtin_clz( x):0; } |
| 56 | static inline int bsr64(uint64_t x) { return x?64 - __builtin_clzll(x):0; } |
| 57 | |
| 58 | static inline unsigned rol32(unsigned x, int s) { return x << s | x >> (32 - s); } |
| 59 | static inline unsigned ror32(unsigned x, int s) { return x >> s | x << (32 - s); } |
| 60 | static inline unsigned rol64(unsigned x, int s) { return x << s | x >> (64 - s); } |
| 61 | static inline unsigned ror64(unsigned x, int s) { return x >> s | x << (64 - s); } |
| 62 | #endif |
| 63 | |
| 64 | #define ctz64(_x_) __builtin_ctzll(_x_) |
| 65 | #define ctz32(_x_) __builtin_ctz(_x_) // 0:32 ctz32(1<<a) = a (a=1..31) |
| 66 | #define clz64(_x_) __builtin_clzll(_x_) |
| 67 | #define clz32(_x_) __builtin_clz(_x_) // 00000000 00000000 00000000 01000000 = 25 |
| 68 | |
| 69 | //#define bswap8(x) (x) |
| 70 | #if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 8 |
| 71 | #define bswap16(x) __builtin_bswap16(x) |
| 72 | #else |
| 73 | static inline unsigned short bswap16(unsigned short x) { return __builtin_bswap32(x << 16); } |
| 74 | #endif |
| 75 | #define bswap32(x) __builtin_bswap32(x) |
| 76 | #define bswap64(x) __builtin_bswap64(x) |
| 77 | |
| 78 | #elif _MSC_VER //---------------------------------------------------- |
| 79 | #include <windows.h> |
| 80 | #include <intrin.h> |
| 81 | #if _MSC_VER < 1600 |
| 82 | #include "vs/stdint.h" |
| 83 | #define __builtin_prefetch(x,a) |
| 84 | #define inline __inline |
| 85 | #else |
| 86 | #include <stdint.h> |
| 87 | #define __builtin_prefetch(x,a) _mm_prefetch(x, _MM_HINT_NTA) |
| 88 | #endif |
| 89 | |
| 90 | #define ALIGNED(t,v,n) __declspec(align(n)) t v |
| 91 | #define ALWAYS_INLINE __forceinline |
| 92 | #define NOINLINE __declspec(noinline) |
| 93 | #define THREADLOCAL __declspec(thread) |
| 94 | #define likely(x) (x) |
| 95 | #define unlikely(x) (x) |
| 96 | |
| 97 | static inline int __bsr32(unsigned x) { unsigned long z=0; _BitScanReverse(&z, x); return z; } |
| 98 | static inline int bsr32( unsigned x) { unsigned long z; _BitScanReverse(&z, x); return x?z+1:0; } |
| 99 | static inline int ctz32( unsigned x) { unsigned long z; _BitScanForward(&z, x); return x?z:32; } |
| 100 | static inline int clz32( unsigned x) { unsigned long z; _BitScanReverse(&z, x); return x?31-z:32; } |
| 101 | #if !defined(_M_ARM64) && !defined(_M_X64) |
| 102 | static inline unsigned char _BitScanForward64(unsigned long* ret, uint64_t x) { |
| 103 | unsigned long x0 = (unsigned long)x, top, bottom; _BitScanForward(&top, (unsigned long)(x >> 32)); _BitScanForward(&bottom, x0); |
| 104 | *ret = x0 ? bottom : 32 + top; return x != 0; |
| 105 | } |
| 106 | static unsigned char _BitScanReverse64(unsigned long* ret, uint64_t x) { |
| 107 | unsigned long x1 = (unsigned long)(x >> 32), top, bottom; _BitScanReverse(&top, x1); _BitScanReverse(&bottom, (unsigned long)x); |
| 108 | *ret = x1 ? top + 32 : bottom; return x != 0; |
| 109 | } |
| 110 | #endif |
| 111 | static inline int bsr64(uint64_t x) { unsigned long z=0; _BitScanReverse64(&z, x); return x?z+1:0; } |
| 112 | static inline int ctz64(uint64_t x) { unsigned long z; _BitScanForward64(&z, x); return x?z:64; } |
| 113 | static inline int clz64(uint64_t x) { unsigned long z; _BitScanReverse64(&z, x); return x?63-z:64; } |
| 114 | |
| 115 | #define rol32(x,s) _lrotl(x, s) |
| 116 | #define ror32(x,s) _lrotr(x, s) |
| 117 | |
| 118 | #define bswap16(x) _byteswap_ushort(x) |
| 119 | #define bswap32(x) _byteswap_ulong(x) |
| 120 | #define bswap64(x) _byteswap_uint64(x) |
| 121 | |
| 122 | #define popcnt32(x) __popcnt(x) |
| 123 | #ifdef _WIN64 |
| 124 | #define popcnt64(x) __popcnt64(x) |
| 125 | #else |
| 126 | #define popcnt64(x) (popcnt32(x) + popcnt32(x>>32)) |
| 127 | #endif |
| 128 | |
| 129 | #define sleep(x) Sleep(x/1000) |
| 130 | #define fseeko _fseeki64 |
| 131 | #define ftello _ftelli64 |
| 132 | #define strcasecmp _stricmp |
| 133 | #define strncasecmp _strnicmp |
| 134 | #define strtoull _strtoui64 |
| 135 | #endif |
| 136 | |
| 137 | #define __bsr8(_x_) __bsr32(_x_) |
| 138 | #define __bsr16(_x_) __bsr32(_x_) |
| 139 | #define bsr8(_x_) bsr32(_x_) |
| 140 | #define bsr16(_x_) bsr32(_x_) |
| 141 | #define ctz8(_x_) ctz32(_x_) |
| 142 | #define ctz16(_x_) ctz32(_x_) |
| 143 | #define clz8(_x_) (clz32(_x_)-24) |
| 144 | #define clz16(_x_) (clz32(_x_)-16) |
| 145 | |
| 146 | #define popcnt8(x) popcnt32(x) |
| 147 | #define popcnt16(x) popcnt32(x) |
| 148 | |
| 149 | //--------------- Unaligned memory access ------------------------------------- |
| 150 | #ifdef UA_MEMCPY |
| 151 | #include <string.h> |
| 152 | static inline unsigned short ctou16(const void *cp) { unsigned short x; memcpy(&x, cp, sizeof(x)); return x; } |
| 153 | static inline unsigned ctou32(const void *cp) { unsigned x; memcpy(&x, cp, sizeof(x)); return x; } |
| 154 | static inline unsigned long long ctou64(const void *cp) { unsigned long long x; memcpy(&x, cp, sizeof(x)); return x; } |
| 155 | static inline size_t ctousz(const void *cp) { size_t x; memcpy(&x, cp, sizeof(x)); return x; } |
| 156 | static inline float ctof32(const void *cp) { float x; memcpy(&x, cp, sizeof(x)); return x; } |
| 157 | static inline double ctof64(const void *cp) { double x; memcpy(&x, cp, sizeof(x)); return x; } |
| 158 | |
| 159 | static inline void stou16( void *cp, unsigned short x) { memcpy(cp, &x, sizeof(x)); } |
| 160 | static inline void stou32( void *cp, unsigned x) { memcpy(cp, &x, sizeof(x)); } |
| 161 | static inline void stou64( void *cp, unsigned long long x) { memcpy(cp, &x, sizeof(x)); } |
| 162 | static inline void stousz( void *cp, size_t x) { memcpy(cp, &x, sizeof(x)); } |
| 163 | static inline void stof32( void *cp, float x) { memcpy(cp, &x, sizeof(x)); } |
| 164 | static inline void stof64( void *cp, double x) { memcpy(cp, &x, sizeof(x)); } |
| 165 | #elif defined(__i386__) || defined(__x86_64__) || \ |
| 166 | defined(_M_IX86) || defined(_M_AMD64) || _MSC_VER ||\ |
| 167 | defined(__powerpc__) || defined(__s390__) ||\ |
| 168 | defined(__ARM_FEATURE_UNALIGNED) || defined(__aarch64__) || defined(__arm__) ||\ |
| 169 | defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \ |
| 170 | defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__) || \ |
| 171 | defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__wasm__) |
| 172 | #define ctou16(_cp_) (*(unsigned short *)(_cp_)) |
| 173 | #define ctou32(_cp_) (*(unsigned *)(_cp_)) |
| 174 | #define ctof32(_cp_) (*(float *)(_cp_)) |
| 175 | |
| 176 | #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || defined(__s390__) || defined(_MSC_VER) || defined(__wasm__) |
| 177 | #define ctou64(_cp_) (*(uint64_t *)(_cp_)) |
| 178 | #define ctof64(_cp_) (*(double *)(_cp_)) |
| 179 | #elif defined(__ARM_FEATURE_UNALIGNED) |
| 180 | struct _PACKED longu { uint64_t l; }; |
| 181 | struct _PACKED doubleu { double d; }; |
| 182 | #define ctou64(_cp_) ((struct longu *)(_cp_))->l |
| 183 | #define ctof64(_cp_) ((struct doubleu *)(_cp_))->d |
| 184 | #endif |
| 185 | |
| 186 | #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7S__) |
| 187 | struct _PACKED shortu { unsigned short s; }; |
| 188 | struct _PACKED unsignedu { unsigned u; }; |
| 189 | struct _PACKED longu { uint64_t l; }; |
| 190 | struct _PACKED floatu { float f; }; |
| 191 | struct _PACKED doubleu { double d; }; |
| 192 | |
| 193 | #define ctou16(_cp_) ((struct shortu *)(_cp_))->s |
| 194 | #define ctou32(_cp_) ((struct unsignedu *)(_cp_))->u |
| 195 | #define ctou64(_cp_) ((struct longu *)(_cp_))->l |
| 196 | #define ctof32(_cp_) ((struct floatu *)(_cp_))->f |
| 197 | #define ctof64(_cp_) ((struct doubleu *)(_cp_))->d |
| 198 | #else |
| 199 | #error "unknown cpu" |
| 200 | #endif |
| 201 | |
| 202 | #define ctou24(_cp_) (ctou32(_cp_) & 0xffffff) |
| 203 | #define ctou48(_cp_) (ctou64(_cp_) & 0xffffffffffffull) |
| 204 | #define ctou8(_cp_) (*(_cp_)) |
| 205 | //--------------------- wordsize ---------------------------------------------- |
| 206 | #if defined(__64BIT__) || defined(_LP64) || defined(__LP64__) || defined(_WIN64) ||\ |
| 207 | defined(__x86_64__) || defined(_M_X64) ||\ |
| 208 | defined(__ia64) || defined(_M_IA64) ||\ |
| 209 | defined(__aarch64__) ||\ |
| 210 | defined(__mips64) ||\ |
| 211 | defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) ||\ |
| 212 | defined(__s390x__) |
| 213 | #define __WORDSIZE 64 |
| 214 | #else |
| 215 | #define __WORDSIZE 32 |
| 216 | #endif |
| 217 | #endif |
| 218 | |
| 219 | //---------------------misc --------------------------------------------------- |
| 220 | #define BZHI64F(_u_, _b_) ((_u_) & ((1ull<<(_b_))-1)) // _b_ < 64 |
| 221 | #define BZHI32F(_u_, _b_) ((_u_) & ((1u <<(_b_))-1)) // _b_ < 32 |
| 222 | #define BZHI64( _u_, _b_) (_b_ == 64?0xffffffffffffffffull:((_u_) & ((1ull<<(_b_))-1))) // Constant |
| 223 | #define BZHI32( _u_, _b_) (_b_ == 32? 0xffffffffu :((_u_) & ((1u <<(_b_))-1))) |
| 224 | #define BZHI16( _u_, _b_) BZHI32(_u_, _b_) |
| 225 | #define BZHI8( _u_, _b_) BZHI32(_u_, _b_) |
| 226 | |
| 227 | #ifdef __AVX2__ |
| 228 | #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) |
| 229 | #include <intrin.h> |
| 230 | #else |
| 231 | #include <x86intrin.h> |
| 232 | #endif |
| 233 | #define bzhi32(_u_, _b_) _bzhi_u32(_u_, _b_) |
| 234 | |
| 235 | #if !(defined(_M_X64) || defined(__amd64__)) && (defined(__i386__) || defined(_M_IX86)) |
| 236 | #define bzhi64(_u_, _b_) ((_u_) & ((1ull<<(_b_))-1)) |
| 237 | #else |
| 238 | #define bzhi64(_u_, _b_) _bzhi_u64(_u_, _b_) |
| 239 | #endif |
| 240 | #else |
| 241 | #define bzhi_u64(_u_, _b_) BZHI64(_u_, _b_) |
| 242 | #define bzhi_u32(_u_, _b_) BZHI32(_u_, _b_) |
| 243 | #endif |
| 244 | |
| 245 | #define SIZE_ROUNDUP(_n_, _a_) (((size_t)(_n_) + (size_t)((_a_) - 1)) & ~(size_t)((_a_) - 1)) |
| 246 | #define ALIGN_DOWN(__ptr, __a) ((void *)((uintptr_t)(__ptr) & ~(uintptr_t)((__a) - 1))) |
| 247 | |
| 248 | #define TEMPLATE2_(_x_, _y_) _x_##_y_ |
| 249 | #define TEMPLATE2(_x_, _y_) TEMPLATE2_(_x_,_y_) |
| 250 | |
| 251 | #define TEMPLATE3_(_x_,_y_,_z_) _x_##_y_##_z_ |
| 252 | #define TEMPLATE3(_x_,_y_,_z_) TEMPLATE3_(_x_, _y_, _z_) |
| 253 | |
| 254 | #define CACHE_LINE_SIZE 64 |
| 255 | #define PREFETCH_DISTANCE (CACHE_LINE_SIZE*4) |
| 256 | |
| 257 | #define CLAMP(_x_, _low_, _high_) (((_x_) > (_high_)) ? (_high_) : (((_x_) < (_low_)) ? (_low_) : (_x_))) |
| 258 | |
| 259 | //--- NDEBUG ------- |
| 260 | #include <stdio.h> |
| 261 | #ifdef _MSC_VER |
| 262 | #ifdef NDEBUG |
| 263 | #define AS(expr, fmt, ...) |
| 264 | #define AC(expr, fmt, ...) do { if(!(expr)) { fprintf(stderr, fmt, ##__VA_ARGS__ ); fflush(stderr); abort(); } } while(0) |
| 265 | #define die(fmt, ...) do { fprintf(stderr, fmt, ##__VA_ARGS__ ); fflush(stderr); exit(-1); } while(0) |
| 266 | #else |
| 267 | #define AS(expr, fmt, ...) do { if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ##__VA_ARGS__ ); fflush(stderr); abort(); } } while(0) |
| 268 | #define AC(expr, fmt, ...) do { if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ##__VA_ARGS__ ); fflush(stderr); abort(); } } while(0) |
| 269 | #define die(fmt, ...) do { fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ##__VA_ARGS__ ); fflush(stderr); exit(-1); } while(0) |
| 270 | #endif |
| 271 | #else |
| 272 | #ifdef NDEBUG |
| 273 | #define AS(expr, fmt,args...) |
| 274 | #define AC(expr, fmt,args...) do { if(!(expr)) { fprintf(stderr, fmt, ## args ); fflush(stderr); abort(); } } while(0) |
| 275 | #define die(fmt,args...) do { fprintf(stderr, fmt, ## args ); fflush(stderr); exit(-1); } while(0) |
| 276 | #else |
| 277 | #define AS(expr, fmt,args...) do { if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ## args ); fflush(stderr); abort(); } } while(0) |
| 278 | #define AC(expr, fmt,args...) do { if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ## args ); fflush(stderr); abort(); } } while(0) |
| 279 | #define die(fmt,args...) do { fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ## args ); fflush(stderr); exit(-1); } while(0) |
| 280 | #endif |
| 281 | #endif |