46#ifndef XT_ARITHMETIC_UTIL_H
47#define XT_ARITHMETIC_UTIL_H
57#if HAVE_DECL___LZCNT || HAVE_DECL___LZCNT64 || HAVE_DECL___LZCNT16
75 return ((x >> (
sizeof (
Xt_int) * CHAR_BIT - 1)) |
78 return (
Xt_int)((x >= 0) - (x < 0));
89 return ((x >> (
sizeof (x) * CHAR_BIT - 1)) |
90 (
int)((
unsigned int)(~x) >> (
sizeof (x) * CHAR_BIT - 1)));
92 return (x >= 0) - (x < 0);
103 return x >> (
sizeof (int) * CHAR_BIT - 1);
105#warning Unusual behaviour of shift operator detected.
113static inline MPI_Aint
117 return x >> (
sizeof (MPI_Aint) * CHAR_BIT - 1);
119#warning Unusual behaviour of shift operator detected.
120 return (x < 0) * ~(MPI_Aint)0;
132 return x >> (
sizeof (x) * CHAR_BIT - 1);
134#warning Unusual behaviour of shift operator detected.
135 return (x < 0) * ~(
Xt_int)0;
142static inline long long
146 return ((x >> (
sizeof (x) * CHAR_BIT - 1)) |
147 (
long long)((
unsigned long long)(~x) >> (
sizeof (x) * CHAR_BIT - 1)));
149 return (x >= 0) - (x < 0);
156static inline long long
160 return x >> (
sizeof (x) * CHAR_BIT - 1);
162#warning Unusual behaviour of shift operator detected.
163 return (x < 0) * ~0LL;
173 return a <= b ? a : b;
178#define xinlz(v) XT_INT_CLZ(v)
184#if SIZEOF_XT_INT * CHAR_BIT == 64
185 if (v <= UINT64_C(0x00000000ffffffff)) {c += 32; v <<= 32;}
186 if (v <= UINT64_C(0x0000ffffffffffff)) {c += 16; v <<= 16;}
187 if (v <= UINT64_C(0x00ffffffffffffff)) {c += 8; v <<= 8;}
188 if (v <= UINT64_C(0x0fffffffffffffff)) {c += 4; v <<= 4;}
189 if (v <= UINT64_C(0x3fffffffffffffff)) {c += 2; v <<= 2;}
190 if (v <= UINT64_C(0x7fffffffffffffff)) {c += 1;}
191#elif SIZEOF_XT_INT * CHAR_BIT == 32
192 if (v <= 0x0000ffffUL) {c += 16; v <<= 16;}
193 if (v <= 0x00ffffffUL) {c += 8; v <<= 8;}
194 if (v <= 0x0fffffffUL) {c += 4; v <<= 4;}
195 if (v <= 0x3fffffffUL) {c += 2; v <<= 2;}
196 if (v <= 0x7fffffffUL) {c += 1;}
197#elif SIZEOF_XT_INT * CHAR_BIT == 16
198 if (v <= 0x00ffU) {c += 8; v <<= 8;}
199 if (v <= 0x0fffU) {c += 4; v <<= 4;}
200 if (v <= 0x3fffU) {c += 2; v <<= 2;}
201 if (v <= 0x7fffU) {c += 1;}
203#error "Unexpected size of Xt_int.\n"
211#define xintz(v) XT_INT_CTZ(v)
226 size_t_bits =
sizeof (size_t) * CHAR_BIT,
234 __asm__ (
"bsrq %1, %0" :
"=r" (ms1bpos) :
"r" (v-1));
235#elif SIZEOF_LONG == 4
236 __asm__ (
"bsrl %1, %0" :
"=r" (ms1bpos) :
"r" (v-1));
238#error "Unexpected size of size_t!"
240 r = (size_t)1 << (ms1bpos+1);
244#elif HAVE_DECL___BUILTIN_CLZL
245#define clzl(v) (__builtin_clzl(v))
246#elif HAVE_DECL___LZCNT && SIZEOF_LONG == SIZEOF_INT
247#define clzl(v) ((int)(__lzcnt(v)))
248#elif HAVE_DECL___LZCNT64 && SIZEOF_LONG == 8 && CHAR_BIT == 8
249#define clzl(v) ((int)(__lzcnt64(v)))
260#if SIZEOF_SIZE_T * CHAR_BIT == 64
271 size_t_bits =
sizeof (size_t) * CHAR_BIT,
278 r = (size_t)1 << (size_t_bits - r);
288 return (
Xt_int)((a - b) & -(a >= b));
341#if defined XT_USE_FAST_DIVISIBLE_TEST \
342 || (defined __ICC && defined __OPTIMIZE__) \
343 || (defined __GNUC__ && defined __OPTIMIZE__) \
345#undef XT_USE_FAST_DIVISIBLE_TEST
346#define XT_USE_FAST_DIVISIBLE_TEST
348struct xt_fast_div_coeff {
356static inline struct xt_fast_div_coeff
361 struct xt_fast_div_coeff coeff = { .bs = 0, .minv = 0, .a = 0, .max = 0 };
374#if SIZEOF_XT_INT * CHAR_BIT > 24
376#if SIZEOF_XT_INT * CHAR_BIT > 48
392fast_divisible(
struct xt_fast_div_coeff coeff,
Xt_int i)
398 return rot <= coeff.max;
401#define is_divisible(divisor, coeff, dividend) fast_divisible(coeff, dividend)
403#define is_divisible(divisor, coeff, dividend) (divisor==0 || (dividend)%(divisor)==0)
static int isign_mask(int x)
static Xt_int Xt_isign_mask(Xt_int x)
static MPI_Aint asign_mask(MPI_Aint x)
static size_t next_2_pow(size_t v)
static int xinlz(Xt_uint v)
static int imin(int a, int b)
static int xintz(Xt_uint v)
static Xt_int Xt_isign(Xt_int x)
static long long llsign(long long x)
static long long llsign_mask(long long x)
static Xt_int Xt_doz(Xt_int a, Xt_int b)
base definitions header file