void F_mpz_mod_poly_realloc(F_mpz_mod_poly_t poly, const ulong alloc) { if (!alloc) // alloc == 0, clear up { F_mpz_mod_poly_clear(poly); poly->coeffs = NULL; poly->alloc = 0; poly->length = 0; return; } if (poly->alloc) // realloc { F_mpz_mod_poly_truncate(poly, alloc); poly->coeffs = (F_mpz *) flint_heap_realloc(poly->coeffs, alloc); if (alloc > poly->alloc) F_mpn_clear(poly->coeffs + poly->alloc, alloc - poly->alloc); } else // nothing allocated already so do it now { poly->coeffs = (mp_limb_t*) flint_heap_alloc(alloc); F_mpn_clear(poly->coeffs, alloc); } poly->alloc = alloc; }
int test_F_mpn_mul_precache() { mp_limb_t * int1, * int2, * product, * product2; F_mpn_precache_t precache; mp_limb_t msl; int result = 1; unsigned long count; for (count = 0; (count < 30) && (result == 1); count++) { unsigned long limbs2 = randint(2*FLINT_FFT_LIMBS_CROSSOVER)+1; unsigned long limbs1 = randint(2*FLINT_FFT_LIMBS_CROSSOVER)+1; int1 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs1); mpn_random2(int1, limbs1); F_mpn_mul_precache_init(precache, int1, limbs1, limbs2); unsigned long count2; for (count2 = 0; (count2 < 30) && (result == 1); count2++) { #if DEBUG printf("%ld, %ld\n",limbs1, limbs2); #endif unsigned long limbs3 = randint(limbs2)+1; int2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs3); product = (mp_limb_t *) malloc(sizeof(mp_limb_t)*(limbs1+limbs2)); product2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*(limbs1+limbs2)); F_mpn_clear(int2, limbs3); mpn_random2(int2, limbs3); F_mpn_mul_precache(product, int2, limbs3, precache); if (limbs1 > limbs3) msl = mpn_mul(product2, int1, limbs1, int2, limbs3); else msl = mpn_mul(product2, int2, limbs3, int1, limbs1); unsigned long j; for (j = 0; j < limbs1+limbs3 - (msl == 0); j++) { if (product[j] != product2[j]) result = 0; } free(product2); free(product); free(int2); } F_mpn_mul_precache_clear(precache); free(int1); } return result; }
int test_F_mpn_mul() { mp_limb_t * int1, * int2, * product, * product2; mp_limb_t msl, msl2; int result = 1; unsigned long count; for (count = 0; (count < 30) && (result == 1); count++) { unsigned long limbs2 = randint(2*FLINT_FFT_LIMBS_CROSSOVER)+1; unsigned long limbs1 = limbs2 + randint(1000); int1 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs1); mpn_random2(int1, limbs1); unsigned long count2; for (count2 = 0; (count2 < 30) && (result == 1); count2++) { #if DEBUG printf("%ld, %ld\n",limbs1, limbs2); #endif int2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs2); product = (mp_limb_t *) malloc(sizeof(mp_limb_t)*(limbs1+limbs2)); product2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*(limbs1+limbs2)); F_mpn_clear(int2, limbs2); mpn_random2(int2, randint(limbs2-1)+1); msl = F_mpn_mul(product, int1, limbs1, int2, limbs2); msl2 = mpn_mul(product2, int1, limbs1, int2, limbs2); unsigned long j; for (j = 0; j < limbs1+limbs2 - (msl == 0); j++) { if (product[j] != product2[j]) result = 0; } result &= (msl == msl2); free(product2); free(product); free(int2); } free(int1); } return result; }
void F_mpz_mod_poly_init2(F_mpz_mod_poly_t poly, const F_mpz_t P, const ulong alloc) { if (alloc) // allocate space for alloc small coeffs { poly->coeffs = (F_mpz *) flint_heap_alloc(alloc); F_mpn_clear(poly->coeffs, alloc); } else poly->coeffs = NULL; F_mpz_init(poly->P); F_mpz_set(poly->P, P); poly->alloc = alloc; poly->length = 0; }
int test_F_mpn_splitcombine_bits() { mp_limb_t * int1, * int2; ZmodF_poly_t poly; int result = 1; unsigned long count; for (count = 0; (count < 30000) && (result == 1); count++) { unsigned long limbs = randint(300)+1; unsigned long bits = randint(500)+1; unsigned long coeff_limbs = randint(100) + (bits-1)/FLINT_BITS + 1; unsigned long length = (FLINT_BITS*limbs - 1)/bits + 1; unsigned long log_length = 0; while ((1L << log_length) < length) log_length++; #if DEBUG printf("limbs = %ld, bits = %ld, coeff_limbs = %ld\n", limbs, bits, coeff_limbs); #endif int1 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs); int2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs); ZmodF_poly_init(poly, log_length, coeff_limbs, 0); mpn_random2(int1, limbs); F_mpn_FFT_split_bits(poly, int1, limbs, bits, coeff_limbs); F_mpn_clear(int2, limbs); F_mpn_FFT_combine_bits(int2, poly, bits, coeff_limbs, limbs); #if DEBUG F_mpn_printx(int1, limbs); printf("\n\n"); unsigned long i; for (i = 0; i < length; i++) { F_mpn_printx(poly->coeffs[i], coeff_limbs); printf("\n");} printf("\n"); F_mpn_printx(int2, limbs); printf("\n\n"); #endif unsigned long j; for (j = 0; j < limbs; j++) { if (int1[j] != int2[j]) result = 0; } ZmodF_poly_clear(poly); free(int2); free(int1); } return result; }
void fmpz_mul_2exp(fmpz_t output, fmpz_t x, unsigned long exp) { unsigned long limbs = (exp >> FLINT_LG_BITS_PER_LIMB); unsigned long bits = (exp & (FLINT_BITS - 1)); mp_limb_t msl = 0L; if (x[0] == 0) { output[0] = 0L; return; } if (bits) { msl = mpn_lshift(output + limbs + 1, x + 1, FLINT_ABS(x[0]), bits); if (msl) output[limbs + FLINT_ABS(x[0]) + 1] = msl; } else F_mpn_copy(output + limbs + 1, x + 1, FLINT_ABS(x[0])); if (limbs) F_mpn_clear(output + 1, limbs); if ((long) x[0] >= 0L) output[0] = x[0] + limbs + (msl != 0L); else output[0] = x[0] - limbs - (msl != 0L); }
void compute_B_terms(QS_t * qs_inf, poly_t * poly_inf) { unsigned long s = poly_inf->s; unsigned long * A_ind = poly_inf->A_ind; unsigned long * A_modp = poly_inf->A_modp; unsigned long * B_terms = poly_inf->B_terms; prime_t * factor_base = qs_inf->factor_base; unsigned long limbs = qs_inf->prec+1; unsigned long limbs2; unsigned long * A = poly_inf->A; unsigned long * B = poly_inf->B; unsigned long p, i; unsigned long * temp1 = (unsigned long *) flint_stack_alloc(limbs); unsigned long temp; mp_limb_t msl; double pinv; for (i = 0; i < s; i++) { p = factor_base[A_ind[i]].p; pinv = z_precompute_inverse(p); mpn_divmod_1(temp1 + 1, A + 1, A[0], p); temp1[0] = A[0] - (temp1[A[0]] == 0); A_modp[i] = (temp = mpn_mod_1(temp1 + 1, temp1[0], p)); temp = z_invert(temp, p); temp = z_mulmod_precomp(temp, qs_inf->sqrts[A_ind[i]], p, pinv); if (temp > p/2) temp = p - temp; msl = mpn_mul_1(B_terms + i*limbs + 1, temp1 + 1, temp1[0], temp); if (msl) { B_terms[i*limbs + temp1[0] + 1] = msl; B_terms[i*limbs] = temp1[0] + 1; } else B_terms[i*limbs] = temp1[0]; #if B_TERMS mpz_t temp; mpz_init(temp); fmpz_to_mpz(temp, B_terms + i*limbs); gmp_printf("B_%ld = %Zd\n", i, temp); mpz_clear(temp); #endif } F_mpn_copy(B, B_terms, B_terms[0]+1); // Set B to the sum of the B terms if (limbs > B_terms[0] + 1) F_mpn_clear(B + B_terms[0] + 1, limbs - B_terms[0] - 1); for (i = 1; i < s; i++) { limbs2 = B_terms[i*limbs]; msl = mpn_add_n(B+1, B+1, B_terms + i*limbs + 1, limbs2); if (msl) mpn_add_1(B + limbs2 + 1, B + limbs2 + 1, limbs - limbs2 - 1, msl); } B[0] = limbs - 1; while (!B[B[0]] && B[0]) B[0]--; #if B_TERMS mpz_t temp2; mpz_init(temp2); fmpz_to_mpz(temp2, B); gmp_printf("B = %Zd\n", temp2); mpz_clear(temp2); #endif flint_stack_release(); // release temp1 }
int test_F_mpn_mul_precache_trunc() { mp_limb_t * int1, * int2, * product, * product2; F_mpn_precache_t precache; mp_limb_t msl; int result = 1; unsigned long count; for (count = 0; (count < 30) && (result == 1); count++) { unsigned long limbs2 = randint(2*FLINT_FFT_LIMBS_CROSSOVER)+1; unsigned long limbs1 = randint(2*FLINT_FFT_LIMBS_CROSSOVER)+1; int1 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs1); mpn_random2(int1, limbs1); F_mpn_mul_precache_init(precache, int1, limbs1, limbs2); unsigned long count2; for (count2 = 0; (count2 < 30) && (result == 1); count2++) { unsigned long limbs3 = randint(limbs2)+1; unsigned long trunc = randint(2*(limbs1+limbs3)); #if DEBUG printf("limbs1 = %ld, limbs3 = %ld, trunc = %ld\n", limbs1, limbs3, trunc); #endif int2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*limbs3); product = (mp_limb_t *) malloc(sizeof(mp_limb_t)*(limbs1+limbs2)); product2 = (mp_limb_t *) malloc(sizeof(mp_limb_t)*(limbs1+limbs2)); F_mpn_clear(int2, limbs3); mpn_random2(int2, limbs3); if (limbs1 > limbs3) F_mpn_mul_trunc(product2, int1, limbs1, int2, limbs3, trunc); else F_mpn_mul_trunc(product2, int2, limbs3, int1, limbs1, trunc); F_mpn_mul_precache_trunc(product, int2, limbs3, precache, trunc); unsigned long j; for (j = 0; j < FLINT_MIN(trunc, limbs1+limbs3); j++) { if (product[j] != product2[j]) { printf("Failure at %ld\n", j); result = 0; } } free(product2); free(product); free(int2); } F_mpn_mul_precache_clear(precache); free(int1); } return result; }