/* Common utilities that are include everywhere */ #ifndef COMMON_H #define COMMON_H #include "../config.h" #include #include #include #include #include /* Allow for inclusion in C++ code. For easy of use, two macros BEGIN_C_DECLS and END_C_DECLS are defined. */ #ifdef __cplusplus # define BEGIN_C_DECLS extern "C" { # define END_C_DECLS } #else # define BEGIN_C_DECLS /* empty */ # define END_C_DECLS /* empty */ #endif #if __GNUC__ >= 4 # define DPT_EXTERN(type) extern __attribute__((visibility("default"))) type #elif defined(_MSC_VER) # define DPT_EXTERN(type) __declspec(dllexport) type #else # define DPT_EXTERN(type) extern type #endif #define B32 (double)(1ULL << 32) #define B64 ((double)B32*B32) #define B128 ((double)B64*B64) #define B192 ((double)B128*B64) #define B256 ((double)B128*B128) #define LOWMASK ((1ULL << 32) - 1) #define lo(x) ((x) & LOWMASK) #define hi(x) ((x) >> 32) #define u64(x) ((uint64_t)(x)) #define u64c(x) (__UINT64_C(x)) BEGIN_C_DECLS typedef unsigned char byte; typedef unsigned int uint; /** * Representation of 128-bit number. */ typedef struct __blk128_t { uint64_t hi; uint64_t lo; } blk128_t; /** * Representation of 256-bit number. 'mx' contains two bits of information: * Potentially a 257'th bit and a sign bit (is this number negative). * * The 64-bit array stores the digits of the number, where x[0] is the least * significant digit and x[3] is the most. */ typedef struct __blk256_t { char mx; /* More generally flags */ uint64_t x[4]; } blk256_t; #define BLK128SIZ (sizeof(blk128_t)) #define BLK256SIZ (sizeof(uint64_t) << 2) #define blk256_sign_bit(x) (((x)->mx & 0x2) >> 1) #define blk256_is_positive(x) (!blk256_sign_bit(x)) /* Zero is considered positive */ #define blk256_set_sign_bit(x) ((x)->mx |= 0x2) #define blk256_clear_sign_bit(x) ((x)->mx &= ~0x2) #define blk256_high_bit(x) ((x)->mx & 0x1) #define blk256_set_high_bit(x) ((x)->mx |= 1) #define blk256_clear_high_bit(x) ((x)->mx &= ~1) /** * sgn() function for 256-bit numbers. Contrary to standard definition, this * returns 1 when all digits are zero (it is considered "not a negative"-number) */ #define blk256_sign(b) (blk256_sign_bit(b) ? -1 : 1) END_C_DECLS #endif