00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00031 #if !defined(_TIFA_FUNCS_H_)
00032
00036 #define _TIFA_FUNCS_H_
00037
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041
00042 #include <limits.h>
00043 #include <inttypes.h>
00044 #include <math.h>
00045 #include <gmp.h>
00046
00047 #include "macros.h"
00048 #include "array.h"
00049 #include "tifa_config.h"
00050
00055 #define LARGEST_MULTIPLIER 97
00056
00061 #define BITSIZE_LARGEST_MULTIPLIER 7
00062
00068 #define MAX_IPRIME_IN_MULT_CALC 31
00069
00070
00071
00072
00073
00074
00075
00087 struct struct_mult_data_t {
00091 uint32_t multiplier;
00099 uint32_t count;
00104 double sum_inv_pi;
00105 };
00106
00111 typedef struct struct_mult_data_t mult_data_t;
00112
00113
00114
00115
00116
00117
00118
00134 uint32_t most_significant_bit(uint32_t n);
00135
00148 inline static uint32_t floor_log2(uint32_t n) {
00149 return most_significant_bit(n);
00150 }
00151
00162 inline static uint32_t ceil_log2(uint32_t n) {
00163 uint32_t e = most_significant_bit(n);
00164 if (! IS_POWER_OF_2_UI(n)) {
00165 e++;
00166 }
00167 return e;
00168 }
00169
00180 inline static uint32_t ceil_log2_mp_limb(mp_limb_t limb) {
00181 #if GMP_LIMB_BITS == 64
00182 uint64_t tmp = limb & GMP_NUMB_MASK;
00183 if (tmp >> 32) {
00184 return 32 + ceil_log2((uint32_t)(tmp >> 32));
00185 } else {
00186 return ceil_log2((uint32_t)tmp);
00187 }
00188 #else
00189 uint32_t tmp = (uint32_t)limb & GMP_NUMB_MASK;
00190 return ceil_log2((uint32_t)tmp);
00191 #endif
00192 }
00193
00234 void find_coprime_base(mpz_array_t* const base, const mpz_t n,
00235 const mpz_array_t* const factors);
00236
00237
00238
00239
00240
00241
00242
00248 #define NO_SQRT_MOD_P (UINT32_MAX)
00249
00255 #define NO_SQRT_MOD_P2 (ULONG_MAX)
00256
00267 int8_t kronecker_ui(uint32_t a, uint32_t b);
00268
00282 uint32_t powm(uint32_t base, uint32_t power, uint32_t modulus);
00283
00303 uint32_t sqrtm(uint32_t a, uint32_t p);
00304
00310 extern const unsigned short qres_mod_221[221] MAYBE_UNUSED;
00311
00317 extern const unsigned short qres_mod_256[256] MAYBE_UNUSED;
00318
00324 extern const unsigned short qres_mod_315[315] MAYBE_UNUSED;
00325
00335 inline static unsigned long int is_square(unsigned long int x) {
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 if (qres_mod_256[x & 255] == 0) {
00347
00348
00349
00350 return 0;
00351 }
00352 if (qres_mod_315[x % 315] == 0) {
00353
00354
00355
00356 return 0;
00357 }
00358 if (qres_mod_221[x % 221] == 0) {
00359
00360
00361
00362 return 0;
00363 }
00364 unsigned long int root = (unsigned long int)sqrt(x);
00365 if ((root * root) == x) {
00366 return root;
00367 }
00368 return 0;
00369 }
00370
00385 bool is_prime(uint32_t n);
00386
00397 unsigned long int gcd_ulint(unsigned long int a, unsigned long int b);
00398
00413 unsigned long int modinv_ui(unsigned long int n, unsigned long int p);
00414
00437 unsigned long int sqrtm_p2(uint32_t a, uint32_t p);
00438
00458 uint32_t ks_multiplier(const mpz_t n, const uint32_t size_base);
00459
00460
00461
00462
00463
00464
00465
00477 uint32_t hash_rj_32(const void* const keyptr);
00478
00492 uint32_t hash_pjw(const void* const keyptr);
00493
00505 uint32_t hash_sfh_ph(const void* const keyptr);
00506
00507
00508
00509
00510
00511
00512
00529 int mpz_cmp_func(const void* const mpza, const void* const mpzb);
00530
00544 int uint32_cmp_func(const void* const uinta, const void* const uintb);
00545
00561 int string_cmp_func(const void* const stra, const void* const strb);
00562
00597 int cmp_mult_data(const void* mda, const void* mdb);
00598
00599
00600
00601
00602
00603
00604
00621 uint32_t n_choose_k(uint8_t n, uint8_t k);
00622
00653 void next_subset_lex(uint32_t n, uint32_t k, uint32_t* subset, bool* end);
00654
00680 void unrank_subset_lex(uint32_t n, uint32_t k, uint32_t r, uint32_t* subset);
00681
00707 void unrank_subset_revdoor(uint32_t n, uint32_t k, uint32_t r,
00708 uint32_t* subset);
00709
00710
00711
00712
00713
00714
00715
00724 void tifa_srand(uint32_t seed);
00725
00734 uint32_t tifa_rand();
00735
00736 #ifdef __cplusplus
00737 }
00738 #endif
00739
00740 #endif