context.c (1635B)
1 #include "context.h" 2 3 void 4 ctx_init(ctx_t *ctx) 5 { 6 ctx->zs_alloc = INITIAL_NUMBER_OF_ZS; 7 ctx->zs = _fmpz_vec_init(ctx->zs_alloc); 8 9 ctx->qs_alloc = INITIAL_NUMBER_OF_QS; 10 ctx->qs = _fmpq_vec_init(ctx->qs_alloc); 11 12 ctx->has_primorial = 0; 13 ctx->smallprimes = NULL; 14 ctx->nsmallprimes = 0; 15 } 16 17 void 18 ctx_require(ctx_t *ctx, const slong nzs, const slong nqs) 19 { 20 if (ctx->zs_alloc < nzs) { 21 ctx->zs = flint_realloc(ctx->zs, sizeof(*ctx->zs) * nzs); 22 memset(&(ctx->zs[ctx->zs_alloc]), 0, sizeof(*ctx->zs) * (nzs - ctx->zs_alloc)); 23 24 ctx->zs_alloc = nzs; 25 } 26 27 if (ctx->qs_alloc < nqs) { 28 ctx->qs = flint_realloc(ctx->qs, sizeof(*ctx->qs) * nqs); 29 memset(&(ctx->qs[ctx->qs_alloc]), 0, sizeof(*ctx->qs) * (nqs - ctx->qs_alloc)); 30 31 ctx->qs_alloc = nqs; 32 } 33 } 34 35 void 36 ctx_clear(ctx_t *ctx) 37 { 38 _fmpz_vec_clear(ctx->zs, ctx->zs_alloc); 39 _fmpq_vec_clear(ctx->qs, ctx->qs_alloc); 40 41 if (ctx->has_primorial) { 42 fmpz_clear(ctx->primorial); 43 ctx->has_primorial = 0; 44 } 45 46 if (ctx->smallprimes != NULL) { 47 _fmpz_vec_clear(ctx->smallprimes, ctx->nsmallprimes); 48 ctx->smallprimes = NULL; 49 ctx->nsmallprimes = 0; 50 } 51 } 52 53 void 54 ctx_set_precomputed_primorial(ctx_t *ctx) 55 { 56 fmpz_init(ctx->primorial); 57 fmpz_set_ui_array(ctx->primorial, pc_primorial_ui_array, 58 PC_PRIMORIAL_UI_ARRAY_LEN); 59 ctx->has_primorial = 1; 60 } 61 62 void 63 ctx_set_precomputed_smallprimes(ctx_t *ctx) 64 { 65 size_t i; 66 67 ctx->nsmallprimes = PC_SMALLPRIMES_LEN; 68 ctx->smallprimes = _fmpz_vec_init(ctx->nsmallprimes); 69 70 for (i = 0; i < PC_SMALLPRIMES_LEN; i++) { 71 fmpz_init_set_ui(&(ctx->smallprimes[i]), pc_smallprimes[i]); 72 } 73 }