Skip to content

Commit eb5f433

Browse files
committed
Simplified code a bit
1 parent 7415eda commit eb5f433

File tree

6 files changed

+10
-21
lines changed

6 files changed

+10
-21
lines changed

mp_cutoffs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
int MP_MUL_KARATSUBA_CUTOFF = MP_DEFAULT_MUL_KARATSUBA_CUTOFF,
99
MP_SQR_KARATSUBA_CUTOFF = MP_DEFAULT_SQR_KARATSUBA_CUTOFF,
1010
MP_MUL_TOOM_CUTOFF = MP_DEFAULT_MUL_TOOM_CUTOFF,
11-
MP_SQR_TOOM_CUTOFF = MP_DEFAULT_SQR_TOOM_CUTOFF;
11+
MP_SQR_TOOM_CUTOFF = MP_DEFAULT_SQR_TOOM_CUTOFF,
12+
MP_RADIX_CUTOFF = MP_DEFAULT_RADIX_CUTOFF;
1213
#endif
1314

1415
#endif

mp_to_radix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
5050
Include branches fror them and/or (tunable?) cutoff? */
5151

5252
/* TODO: check if it can be done better */
53-
if (MP_HAS(S_MP_FASTER_TO_RADIX)) {
53+
if (MP_HAS(S_MP_FASTER_TO_RADIX) && (a->used > (MP_RADIX_CUTOFF / MP_DIGIT_BIT))) {
5454
if ((err = s_mp_faster_to_radix(&a_bar, str, maxlen, &part_written, radix)) != MP_OKAY) goto LBL_ERR;
5555
} else if (MP_HAS(S_MP_SLOWER_TO_RADIX)) {
5656
char *start = str;

s_mp_faster_to_radix.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *w
134134

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

168-
/* To get the tree a bit flatter. Alternative: do it iteratively instead of recursively */
169-
k = k * MP_RADIX_BARRETT_START_MULTIPLICATOR;
170-
171168
/* Compute initial reciprocal R[0] and expand it (R[0]^(2^k) */
172169
if ((err = mp_init_i32(&P[0], n)) != MP_OKAY) goto LTM_ERR;
173-
/* TODO: chunksize does not seem to matter much above the initial b^y, d.n.f.t. remove this line if
174-
MP_RADIX_BARRETT_START_MULTIPLICATOR is removed but don't forget the possibility that
175-
the OS does not like too many recursions. This routine does use a lot of stack
176-
and it calls other D&C algorithms (fast multiplication, fast division) that need a little
177-
slice of the stack, too (vid.: ulimit -s) */
178-
if ((err = mp_expt_n(&P[0], MP_RADIX_BARRETT_START_MULTIPLICATOR, &P[0])) != MP_OKAY) goto LTM_ERR;
179-
180170
if ((err = mp_init(&R[0])) != MP_OKAY) goto LTM_ERR;
181171
if ((err = mp_2expt(&R[0], 2*k)) != MP_OKAY) goto LTM_ERR;
182172
if ((err = mp_div(&R[0], &P[0], &R[0], NULL)) != MP_OKAY) goto LTM_ERR;

s_mp_slower_to_radix.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ mp_err s_mp_slower_to_radix(const mp_int *a, char **str, size_t *part_maxlen, si
2626

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

3432
if ((err = mp_init_copy(&t, a)) != MP_OKAY) goto LTM_ERR;

tommath.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,12 @@ extern int
121121
MP_MUL_KARATSUBA_CUTOFF,
122122
MP_SQR_KARATSUBA_CUTOFF,
123123
MP_MUL_TOOM_CUTOFF,
124-
MP_SQR_TOOM_CUTOFF;
124+
MP_SQR_TOOM_CUTOFF,
125+
MP_RADIX_CUTOFF;
125126
#endif
126127

127-
/* Might be tunable. See comments in s_mp_faster_to_radix.c */
128-
#ifndef MP_RADIX_BARRETT_START_MULTIPLICATOR
129-
# define MP_RADIX_BARRETT_START_MULTIPLICATOR 10
130-
#endif
128+
129+
131130

132131
/* define this to use lower memory usage routines (exptmods mostly) */
133132
/* #define MP_LOW_MEM */

tommath_cutoffs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
#define MP_DEFAULT_SQR_KARATSUBA_CUTOFF 120
1212
#define MP_DEFAULT_MUL_TOOM_CUTOFF 350
1313
#define MP_DEFAULT_SQR_TOOM_CUTOFF 400
14+
#define MP_DEFAULT_RADIX_CUTOFF 600

0 commit comments

Comments
 (0)