TurboPFor: Config/Platform

This commit is contained in:
powturbo
2017-06-13 09:14:19 +02:00
parent f27434c46b
commit 0be4578eb9

76
conf.h
View File

@ -27,6 +27,7 @@
#define CONF_H #define CONF_H
//------------------------- Compiler ------------------------------------------ //------------------------- Compiler ------------------------------------------
#if defined(__GNUC__) #if defined(__GNUC__)
#include <stdint.h>
#define ALIGNED(t,v,n) t v __attribute__ ((aligned (n))) #define ALIGNED(t,v,n) t v __attribute__ ((aligned (n)))
#define ALWAYS_INLINE inline __attribute__((always_inline)) #define ALWAYS_INLINE inline __attribute__((always_inline))
#define NOINLINE __attribute__((noinline)) #define NOINLINE __attribute__((noinline))
@ -42,16 +43,16 @@
//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, //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,
static inline int __bsr32( int x) { asm("bsr %1,%0" : "=r" (x) : "rm" (x) ); return x; } static inline int __bsr32( int x) { asm("bsr %1,%0" : "=r" (x) : "rm" (x) ); return x; }
static inline int bsr32( int x) { int b = -1; asm("bsrl %1,%0" : "+r" (b) : "rm" (x) ); return b + 1; } static inline int bsr32( int x) { int b = -1; asm("bsrl %1,%0" : "+r" (b) : "rm" (x) ); return b + 1; }
static inline int bsr64(unsigned long long x) { return x?64 - __builtin_clzll(x):0; } static inline int bsr64(uint64_t x) { return x?64 - __builtin_clzll(x):0; }
static inline unsigned rol32(unsigned x, int s) { asm ("roll %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } static inline unsigned rol32(unsigned x, int s) { asm ("roll %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; }
static inline unsigned ror32(unsigned x, int s) { asm ("rorl %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } static inline unsigned ror32(unsigned x, int s) { asm ("rorl %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; }
static inline unsigned long long rol64(unsigned long long x, int s) { asm ("rolq %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } static inline uint64_t rol64(uint64_t x, int s) { asm ("rolq %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; }
static inline unsigned long long ror64(unsigned long long x, int s) { asm ("rorq %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; } static inline uint64_t ror64(uint64_t x, int s) { asm ("rorq %%cl,%0" :"=r" (x) :"0" (x),"c" (s)); return x; }
#else #else
static inline int __bsr32(unsigned x ) { return 31 - __builtin_clz( x); } static inline int __bsr32(unsigned x ) { return 31 - __builtin_clz( x); }
static inline int bsr32(int x ) { return x?32 - __builtin_clz( x):0; } static inline int bsr32(int x ) { return x?32 - __builtin_clz( x):0; }
static inline int bsr64(unsigned long long x) { return x?64 - __builtin_clzll(x):0; } static inline int bsr64(uint64_t x) { return x?64 - __builtin_clzll(x):0; }
static inline unsigned rol32(unsigned x, int s) { return x << s | x >> (32 - s); } static inline unsigned rol32(unsigned x, int s) { return x << s | x >> (32 - s); }
static inline unsigned ror32(unsigned x, int s) { return x >> s | x << (32 - s); } static inline unsigned ror32(unsigned x, int s) { return x >> s | x << (32 - s); }
@ -73,6 +74,15 @@ static inline unsigned short bswap16(unsigned short x) { return __builtin_bswap3
#define bswap64(x) __builtin_bswap64(x) #define bswap64(x) __builtin_bswap64(x)
#elif _MSC_VER //---------------------------------------------------- #elif _MSC_VER //----------------------------------------------------
#include <windows.h>
#if _MSC_VER < 1600
#include "vs/stdint.h"
#define __builtin_prefetch(x,a)
#else
#include <stdint.h>
#define __builtin_prefetch(x,a) _mm_prefetch(x, _MM_HINT_NTA)
#endif
#define ALIGNED(x) __declspec(align(x)) #define ALIGNED(x) __declspec(align(x))
#define ALWAYS_INLINE __forceinline #define ALWAYS_INLINE __forceinline
#define NOINLINE __declspec(noinline) #define NOINLINE __declspec(noinline)
@ -80,16 +90,16 @@ static inline unsigned short bswap16(unsigned short x) { return __builtin_bswap3
#define THREADLOCAL __declspec(thread) #define THREADLOCAL __declspec(thread)
#define likely(x) (x) #define likely(x) (x)
#define unlikely(x) (x) #define unlikely(x) (x)
#define __builtin_prefetch(x) //_mm_prefetch(x, _MM_HINT_NTA)
static inline int bsr32(int x) { return x ? 32 - __builtin_clz(x) : 0; } static inline int __bsr32(int x) { unsigned long z; _BitScanReverse(&z, x); return z; }
static inline int bsr32( int x) { unsigned long z; _BitScanReverse(&z, x); return x?z+1:0; }
#ifdef _WIN64 #ifdef _WIN64
static inline int bsr64(unsigned long long x) { unsigned long z = 0; _BitScanForward64(&z, x); return 64 - z; } static inline int bsr64(uint64_t x) { unsigned long z=0; _BitScanForward64(&z, x); return x?z+1:0; }
static inline int clz64(unsigned long long x) { unsigned long z = 0; _BitScanForward64(&z, x); return z; } static inline int ctz64(uint64_t x) { unsigned long z=0; _BitScanForward64(&z, x); return x?z:64;; }
static inline int ctz64(unsigned long long x) { unsigned long z = 0; _BitScanReverse64(&z, x); return z; } static inline int clz64(uint64_t x) { unsigned long z=0; _BitScanReverse64(&z, x); return x?63-z:64; }
#endif #endif
static inline int clz32(unsigned x) { unsigned z = 0; _BitScanForward( &z, x); return 32 - z; } static inline int ctz32(unsigned x) { unsigned long z=0; _BitScanForward( &z, x); return x?z:32; }
static inline int ctz32(unsigned x) { unsigned z = 0; _BitScanReverse( &z, x); return z; } static inline int clz32(unsigned x) { unsigned long z=0; _BitScanReverse( &z, x); return x?31-z:32; }
#define rol32(x,s) _lrotl(x, s) #define rol32(x,s) _lrotl(x, s)
#define ror32(x,s) _lrotr(x, s) #define ror32(x,s) _lrotr(x, s)
@ -97,11 +107,16 @@ static inline int ctz32(unsigned x) { unsigned z = 0; _BitScanRev
#define bswap32(x) _byteswap_ulong(x) #define bswap32(x) _byteswap_ulong(x)
#define bswap64(x) _byteswap_uint64(x) #define bswap64(x) _byteswap_uint64(x)
#define popcnt32(x) __popcnt(x)
#define popcnt64(x) __popcnt64(x)
#define sleep(x) Sleep(x/1000)
#define fseeko _fseeki64 #define fseeko _fseeki64
#define ftello _ftelli64 #define ftello _ftelli64
#define sleep(x) Sleep(x/1000)
#define strcasecmp _stricmp #define strcasecmp _stricmp
#define strncasecmp _strnicmp #define strncasecmp _strnicmp
#define strtoull _strtoui64
static inline double round(double num) { return (num > 0.0) ? floor(num + 0.5) : ceil(num - 0.5); }
#endif #endif
#define bsr8(_x_) bsr32(_x_) #define bsr8(_x_) bsr32(_x_)
@ -114,7 +129,7 @@ static inline int ctz32(unsigned x) { unsigned z = 0; _BitScanRev
//--------------- Unaligned memory access ------------------------------------- //--------------- Unaligned memory access -------------------------------------
/*# || defined(i386) || defined(_X86_) || defined(__THW_INTEL)*/ /*# || defined(i386) || defined(_X86_) || defined(__THW_INTEL)*/
#if defined(__i386__) || defined(__x86_64__) || \ #if defined(__i386__) || defined(__x86_64__) || \
defined(_M_IX86) || defined(_M_AMD64) || /*MSC_VER*/\ defined(_M_IX86) || defined(_M_AMD64) || _MSC_VER ||\
defined(__powerpc__) ||\ defined(__powerpc__) ||\
defined(__ARM_FEATURE_UNALIGNED) || defined(__aarch64__) || defined(__arm__) ||\ defined(__ARM_FEATURE_UNALIGNED) || defined(__aarch64__) || defined(__arm__) ||\
defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \ defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
@ -123,17 +138,17 @@ static inline int ctz32(unsigned x) { unsigned z = 0; _BitScanRev
#define ctou16(_cp_) (*(unsigned short *)(_cp_)) #define ctou16(_cp_) (*(unsigned short *)(_cp_))
#define ctou32(_cp_) (*(unsigned *)(_cp_)) #define ctou32(_cp_) (*(unsigned *)(_cp_))
#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || defined(_MSC_VER)
#define ctou64(_cp_) (*(unsigned long long *)(_cp_)) #define ctou64(_cp_) (*(uint64_t *)(_cp_))
#elif defined(__ARM_FEATURE_UNALIGNED) #elif defined(__ARM_FEATURE_UNALIGNED)
struct _PACKED longu { unsigned long long l; }; struct _PACKED longu { uint64_t l; };
#define ctou64(_cp_) ((struct longu *)(_cp_))->l #define ctou64(_cp_) ((struct longu *)(_cp_))->l
#endif #endif
#elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7S__) #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7S__)
struct _PACKED shortu { unsigned short s; }; struct _PACKED shortu { unsigned short s; };
struct _PACKED unsignedu { unsigned u; }; struct _PACKED unsignedu { unsigned u; };
struct _PACKED longu { unsigned long long l; }; struct _PACKED longu { uint64_t l; };
#define ctou16(_cp_) ((struct shortu *)(_cp_))->s #define ctou16(_cp_) ((struct shortu *)(_cp_))->s
#define ctou32(_cp_) ((struct unsignedu *)(_cp_))->u #define ctou32(_cp_) ((struct unsignedu *)(_cp_))->u
@ -145,22 +160,22 @@ struct _PACKED longu { unsigned long long l; };
#ifdef ctou16 #ifdef ctou16
//#define utoc16(_x_,_cp_) ctou16(_cp_) = _x_ //#define utoc16(_x_,_cp_) ctou16(_cp_) = _x_
#else #else
static inline unsigned short ctou16(const void *cp) { unsigned short x; memcpy(&x, cp, sizeof(x)); return x; } static inline unsigned short ctou16(void *cp) { unsigned short x; memcpy((void *)&x, cp, (unsigned int)sizeof(x)); return x; }
//static inline void utoc16(unsigned short x, void *cp ) { memcpy(cp, &x, sizeof(x)); } //static inline void utoc16(unsigned short x, void *cp ) { memcpy(cp, &x, sizeof(x)); }
#endif #endif
#ifdef ctou32 #ifdef ctou32
//#define utoc32(_x_,_cp_) ctou32(_cp_) = _x_ //#define utoc32(_x_,_cp_) ctou32(_cp_) = _x_
#else #else
static inline unsigned ctou32(const void *cp) { unsigned x; memcpy(&x, cp, sizeof(x)); return x; } static inline unsigned ctou32(void *cp) { unsigned x; memcpy(void *)&x, cp, (unsigned int)sizeof(x)); return x; }
//static inline void utoc32(unsigned x, void *cp ) { memcpy(cp, &x, sizeof(x)); } //static inline void utoc32(unsigned x, void *cp ) { memcpy(cp, &x, sizeof(x)); }
#endif #endif
#ifdef ctou64 #ifdef ctou64
//#define utoc64(_x_,_cp_) ctou64(_cp_) = _x_ //#define utoc64(_x_,_cp_) ctou64(_cp_) = _x_
#else #else
static inline unsigned long long ctou64(const void *cp) { unsigned long long x; memcpy(&x, cp, sizeof(x)); return x; } static inline uint64_t ctou64(void *cp) { uint64_t x; memcpy((void *)&x, cp, (unsigned int)sizeof(x)); return x; }
//static inline void utoc64(unsigned long long x, void *cp ) { memcpy(cp, &x, sizeof(x)); } //static inline void utoc64(uint64_t x, void *cp ) { memcpy(cp, &x, sizeof(x)); }
#endif #endif
#define ctou24(_cp_) (ctou32(_cp_) & 0xffffff) #define ctou24(_cp_) (ctou32(_cp_) & 0xffffff)
@ -181,13 +196,8 @@ static inline unsigned long long ctou64(const void *cp) { unsigned long long x;
#endif #endif
//---------------------misc --------------------------------------------------- //---------------------misc ---------------------------------------------------
//#define C64(x) x##ull
#define SIZE_ROUNDUP(_n_, _a_) (((size_t)(_n_) + (size_t)((_a_) - 1)) & ~(size_t)((_a_) - 1)) #define SIZE_ROUNDUP(_n_, _a_) (((size_t)(_n_) + (size_t)((_a_) - 1)) & ~(size_t)((_a_) - 1))
#ifndef min #define ALIGN_DOWN(__ptr, __a) ((void *)((uintptr_t)(__ptr) & ~(uintptr_t)((__a) - 1)))
#define min(x,y) (((x)<(y)) ? (x) : (y))
#define max(x,y) (((x)>(y)) ? (x) : (y))
#endif
#define TEMPLATE2_(_x_, _y_) _x_##_y_ #define TEMPLATE2_(_x_, _y_) _x_##_y_
#define TEMPLATE2(_x_, _y_) TEMPLATE2_(_x_,_y_) #define TEMPLATE2(_x_, _y_) TEMPLATE2_(_x_,_y_)
@ -199,13 +209,13 @@ static inline unsigned long long ctou64(const void *cp) { unsigned long long x;
#include <stdio.h> #include <stdio.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#ifdef NDEBUG #ifdef NDEBUG
#define AS(expr, fmt, ...) #define AS(expr, fmt, __VA_ARGS__)
#define AC(expr, fmt, ...) if(!(expr)) { fprintf(stderr, fmt, __VA_ARGS__ ); fflush(stderr); abort(); } #define AC(expr, fmt, __VA_ARGS__) if(!(expr)) { fprintf(stderr, fmt, __VA_ARGS__ ); fflush(stderr); abort(); }
#define die(fmt, ...) do { fprintf(stderr, fmt, __VA_ARGS__ ); fflush(stderr); exit(-1); } while(0) #define die(fmt, __VA_ARGS__) do { fprintf(stderr, fmt, __VA_ARGS__ ); fflush(stderr); exit(-1); } while(0)
#else #else
#define AS(expr, fmt, ...) if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ,__VA_ARGS__ ); fflush(stderr); abort(); } #define AS(expr, fmt, __VA_ARGS__) if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ,__VA_ARGS__ ); fflush(stderr); abort(); }
#define AC(expr, fmt, ...) if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ,__VA_ARGS__ ); fflush(stderr); abort(); } #define AC(expr, fmt, __VA_ARGS__) if(!(expr)) { fflush(stdout);fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ,__VA_ARGS__ ); fflush(stderr); abort(); }
#define die(fmt, ...) do { fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, ,__VA_ARGS__ ); fflush(stderr); exit(-1); } while(0) #define die(fmt, __VA_ARGS__) do { fprintf(stderr, "%s:%s:%d:", __FILE__, __FUNCTION__, __LINE__); fprintf(stderr, fmt, __VA_ARGS__ ); fflush(stderr); exit(-1); } while(0)
#endif #endif
#else #else
#ifdef NDEBUG #ifdef NDEBUG