commit 0552439452889c80c6299cc11c34257395784d5a
parent 8b02b1da6f3e0c7949295aeaae9addd68e1d6125
Author: olikru <olikru@tkruger.se>
Date: Wed, 8 May 2024 12:49:30 +0200
improving context
Diffstat:
7 files changed, 23 insertions(+), 30 deletions(-)
diff --git a/context.c b/context.c
@@ -17,6 +17,15 @@ ctx_init(ctx_t *ctx)
void
ctx_require(ctx_t *ctx, const slong nzs, const slong nqs)
{
+ if (ctx->zs_alloc < nzs) {
+ ctx->zs = flint_realloc(ctx->zs, sizeof(*ctx->zs) * nzs);
+ memset(&(ctx->zs[ctx->zs_alloc]), 0, sizeof(*ctx->zs) * (nzs - ctx->zs_alloc));
+ }
+
+ if (ctx->qs_alloc < nqs) {
+ ctx->qs = flint_realloc(ctx->qs, sizeof(*ctx->qs) * nqs);
+ memset(&(ctx->qs[ctx->qs_alloc]), 0, sizeof(*ctx->qs) * (nqs - ctx->qs_alloc));
+ }
}
void
diff --git a/context.h b/context.h
@@ -7,6 +7,8 @@
#include <fmpz.h>
#include <fmpz_vec.h>
+#include <string.h>
+
#include "precomputed.h"
extern const ulong pc_primorial_ui_array[PC_PRIMORIAL_UI_ARRAY_LEN];
diff --git a/pierre.c b/pierre.c
@@ -1,25 +1,15 @@
#include "pierre.h"
void
-fermat_ctx_init(fermat_ctx *ctx)
-{
- ctx->zs = _fmpz_vec_init(FERMAT_CTX_NZS);
-}
-
-void
-fermat_ctx_clear(fermat_ctx *ctx)
-{
- _fmpz_vec_clear(ctx->zs, FERMAT_CTX_NZS);
-}
-
-void
-fermat_factor(fermat_ctx *ctx, fmpz_t r, fmpz_t n, size_t limit)
+fermat_factor(ctx_t *ctx, fmpz_t r, fmpz_t n, size_t limit)
{
size_t i;
uint64_t inc = 1;
int found = 0;
fmpz_set(r, n);
+ assert(ctx->zs_alloc >= 1);
+
for (i = 0; i < limit; i++) {
if (fmpz_is_square(r)) {
found = 1;
diff --git a/pierre.h b/pierre.h
@@ -6,15 +6,9 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <assert.h>
-#define FERMAT_CTX_NZS 1
-
-typedef struct __fermat_ctx {
- fmpz *zs;
-} fermat_ctx;
-
-void fermat_ctx_init(fermat_ctx *ctx);
-void fermat_ctx_clear(fermat_ctx *ctx);
+#include "context.h"
/**
* Fermat factorisation of an integer.
@@ -30,6 +24,6 @@ void fermat_ctx_clear(fermat_ctx *ctx);
* @param n the number to attempt to Fermat factorise
* @param limit iteration limit bound
*/
-void fermat_factor(fermat_ctx *ctx, fmpz_t r, fmpz_t n, size_t limit);
+void fermat_factor(ctx_t *ctx, fmpz_t r, fmpz_t n, size_t limit);
#endif
diff --git a/smallfactor.c b/smallfactor.c
@@ -6,8 +6,6 @@ smallfactor_euclidean(ctx_t *ctx, fmpz_t out, const fmpz_t n)
assert(ctx->has_primorial);
assert(ctx->nsmallprimes > 0);
assert(ctx->smallprimes != NULL);
- assert(ctx->zs_alloc > 1);
-
size_t i;
fmpz_gcd(out, n, ctx->primorial);
diff --git a/test_angrepp.c b/test_angrepp.c
@@ -41,9 +41,9 @@ static void
test_pierre_factor(void)
{
fmpz_t n, r;
- fermat_ctx ctx;
+ ctx_t ctx;
- fermat_ctx_init(&ctx);
+ ctx_init(&ctx);
fmpz_init(n);
fmpz_init(r);
@@ -54,7 +54,7 @@ test_pierre_factor(void)
assert(fmpz_divisible(n, r));
assert(fmpz_cmp_ui(r, 1) > 0);
- fermat_ctx_clear(&ctx);
+ ctx_clear(&ctx);
fmpz_clear(n);
fmpz_clear(r);
}
diff --git a/tools/pierre.c b/tools/pierre.c
@@ -12,9 +12,9 @@ int
main(void)
{
fmpz_t read, factor;
- fermat_ctx ctx;
+ ctx_t ctx;
- fermat_ctx_init(&ctx);
+ ctx_init(&ctx);
fmpz_init(factor);
fmpz_init(read);
@@ -24,7 +24,7 @@ main(void)
printf("\n");
}
- fermat_ctx_clear(&ctx);
+ ctx_clear(&ctx);
fmpz_clear(factor);
fmpz_clear(read);