Skip to content

Commit

Permalink
Simplified code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
czurnieden committed Jun 9, 2024
1 parent 7415eda commit eb5f433
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 21 deletions.
3 changes: 2 additions & 1 deletion mp_cutoffs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
int MP_MUL_KARATSUBA_CUTOFF = MP_DEFAULT_MUL_KARATSUBA_CUTOFF,
MP_SQR_KARATSUBA_CUTOFF = MP_DEFAULT_SQR_KARATSUBA_CUTOFF,
MP_MUL_TOOM_CUTOFF = MP_DEFAULT_MUL_TOOM_CUTOFF,
MP_SQR_TOOM_CUTOFF = MP_DEFAULT_SQR_TOOM_CUTOFF;
MP_SQR_TOOM_CUTOFF = MP_DEFAULT_SQR_TOOM_CUTOFF,
MP_RADIX_CUTOFF = MP_DEFAULT_RADIX_CUTOFF;
#endif

#endif
2 changes: 1 addition & 1 deletion mp_to_radix.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
Include branches fror them and/or (tunable?) cutoff? */

/* TODO: check if it can be done better */
if (MP_HAS(S_MP_FASTER_TO_RADIX)) {
if (MP_HAS(S_MP_FASTER_TO_RADIX) && (a->used > (MP_RADIX_CUTOFF / MP_DIGIT_BIT))) {
if ((err = s_mp_faster_to_radix(&a_bar, str, maxlen, &part_written, radix)) != MP_OKAY) goto LBL_ERR;
} else if (MP_HAS(S_MP_SLOWER_TO_RADIX)) {
char *start = str;
Expand Down
12 changes: 1 addition & 11 deletions s_mp_faster_to_radix.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *w

/* Cutoff at about twice the size of P[0]. */
/* TODO: Check if it makes sense to make it tunable. */
if (ilog2a < (2 * k * MP_RADIX_BARRETT_START_MULTIPLICATOR)) {
if (ilog2a < (2 * k)) {
if ((err = s_mp_slower_to_radix(a, sptr, &part_maxlen, &part_written, radix, false)) != MP_OKAY) goto LTM_ERR;
/* part_written does not count EOS */
*written = part_written + 1;
Expand Down Expand Up @@ -165,18 +165,8 @@ mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *w
with R_0 = (2^(2*k))/b^y and k = ceil(log_2(n)) as computed above.
*/

/* To get the tree a bit flatter. Alternative: do it iteratively instead of recursively */
k = k * MP_RADIX_BARRETT_START_MULTIPLICATOR;

/* Compute initial reciprocal R[0] and expand it (R[0]^(2^k) */
if ((err = mp_init_i32(&P[0], n)) != MP_OKAY) goto LTM_ERR;
/* TODO: chunksize does not seem to matter much above the initial b^y, d.n.f.t. remove this line if
MP_RADIX_BARRETT_START_MULTIPLICATOR is removed but don't forget the possibility that
the OS does not like too many recursions. This routine does use a lot of stack
and it calls other D&C algorithms (fast multiplication, fast division) that need a little
slice of the stack, too (vid.: ulimit -s) */
if ((err = mp_expt_n(&P[0], MP_RADIX_BARRETT_START_MULTIPLICATOR, &P[0])) != MP_OKAY) goto LTM_ERR;

if ((err = mp_init(&R[0])) != MP_OKAY) goto LTM_ERR;
if ((err = mp_2expt(&R[0], 2*k)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_div(&R[0], &P[0], &R[0], NULL)) != MP_OKAY) goto LTM_ERR;
Expand Down
4 changes: 1 addition & 3 deletions s_mp_slower_to_radix.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ mp_err s_mp_slower_to_radix(const mp_int *a, char **str, size_t *part_maxlen, si

/* The number of digits of "radix" to be filled if this chunk is not the most significant one. */
if (pad) {
/* TODO: Disconnect MP_RADIX_BARRETT_START_MULTIPLICATOR from this branch and use a different macro.
Check if it makes sense to make that replacement tunable */
ybar = s_mp_radix_exponent_y[radix] * MP_RADIX_BARRETT_START_MULTIPLICATOR;
ybar = s_mp_radix_exponent_y[radix];
}

if ((err = mp_init_copy(&t, a)) != MP_OKAY) goto LTM_ERR;
Expand Down
9 changes: 4 additions & 5 deletions tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ extern int
MP_MUL_KARATSUBA_CUTOFF,
MP_SQR_KARATSUBA_CUTOFF,
MP_MUL_TOOM_CUTOFF,
MP_SQR_TOOM_CUTOFF;
MP_SQR_TOOM_CUTOFF,
MP_RADIX_CUTOFF;
#endif

/* Might be tunable. See comments in s_mp_faster_to_radix.c */
#ifndef MP_RADIX_BARRETT_START_MULTIPLICATOR
# define MP_RADIX_BARRETT_START_MULTIPLICATOR 10
#endif



/* define this to use lower memory usage routines (exptmods mostly) */
/* #define MP_LOW_MEM */
Expand Down
1 change: 1 addition & 0 deletions tommath_cutoffs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
#define MP_DEFAULT_SQR_KARATSUBA_CUTOFF 120
#define MP_DEFAULT_MUL_TOOM_CUTOFF 350
#define MP_DEFAULT_SQR_TOOM_CUTOFF 400
#define MP_DEFAULT_RADIX_CUTOFF 600

0 comments on commit eb5f433

Please sign in to comment.