Skip to content

Commit 28b373d

Browse files
committed
clean up
1 parent 816833c commit 28b373d

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

demo/test.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ static int test_mp_read_radix(void)
11581158
size_t written, maxlen;
11591159
int bignum, i;
11601160

1161-
char *buffer, *bcpy;
1161+
char *buffer, *bcpy, *startb;
11621162

11631163
clock_t start, stop, t_slow, t_fast;
11641164

@@ -1191,18 +1191,19 @@ static int test_mp_read_radix(void)
11911191

11921192
/* Test the fast method with a slightly larger number */
11931193

1194-
/* Must be bigger than the cut-off value, of course */
1195-
bignum = (2 * 20 * MP_RADIX_BARRETT_START_MULTIPLICATOR) * 10;
1194+
/* Needs to be big enough to make a sufficiently large timing difference */
1195+
bignum = 30000;
11961196
buffer = (char *)malloc((size_t)(bignum + 2));
11971197
if (buffer == NULL) {
11981198
goto LBL_ERR;
11991199
}
12001200
DO(mp_rand(&a, bignum / MP_DIGIT_BIT));
1201-
printf("\nNumber of limbs in &b = %d, bit_count of &b = %d\n", bignum / MP_DIGIT_BIT, mp_count_bits(&a));
1201+
fprintf(stderr,"\nNumber of limbs in &b = %d, bit_count of &b = %d\n", bignum / MP_DIGIT_BIT, mp_count_bits(&a));
12021202
start = clock();
12031203
for (i = 2; i < 65; i++) {
12041204
/* printf("FAST radix = %d\n",i); */
12051205
DO(mp_to_radix(&a, buffer, (size_t)(bignum + 1), &written, i));
1206+
mp_zero(&b);
12061207
DO(mp_read_radix(&b, buffer, i));
12071208
EXPECT(mp_cmp(&a, &b) == MP_EQ);
12081209
}
@@ -1215,7 +1216,11 @@ static int test_mp_read_radix(void)
12151216
/* printf("SLOW radix = %d\n",i); */
12161217
maxlen = (size_t)(bignum + 1);
12171218
bcpy = buffer;
1219+
/* s_mp_slower_to_radix is very rudimentary as a stand-alone */
1220+
startb = bcpy;
12181221
DO(s_mp_slower_to_radix(&a, &bcpy, &maxlen, &written, i, false));
1222+
bcpy = startb;
1223+
mp_zero(&b);
12191224
DO(s_mp_slower_read_radix(&b, bcpy, 0, strlen(bcpy), i));
12201225
EXPECT(mp_cmp(&a, &b) == MP_EQ);
12211226
}
@@ -1226,15 +1231,14 @@ static int test_mp_read_radix(void)
12261231
fprintf(stderr,"SLOW: %.10f, FAST: %.10f\n", (double)t_slow/(double)CLOCKS_PER_SEC,
12271232
(double)t_fast/(double)CLOCKS_PER_SEC);
12281233

1229-
/* Check if the branching works.
1234+
/* Check if the branching works. */
12301235
if (MP_HAS(S_MP_FASTER_READ_RADIX) && MP_HAS(S_MP_FASTER_TO_RADIX)) {
12311236
if (t_fast > t_slow) {
12321237
fprintf(stderr, "Timing suspicious in test_mp_read_radix. No fast multiplication? Cut-off too low?\n");
12331238
DO(mp_fwrite(&a, 16, stderr));
12341239
goto LBL_ERR;
12351240
}
12361241
}
1237-
*/
12381242

12391243
free(buffer);
12401244

mp_to_radix.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
4747
if ((err = s_mp_faster_to_radix(&a_bar, str, maxlen, &part_written, radix)) != MP_OKAY) goto LBL_ERR;
4848
} else {
4949
if (MP_HAS(S_MP_SLOWER_TO_RADIX)) {
50+
char *start = str;
5051
if ((err = s_mp_slower_to_radix(&a_bar, &str, &maxlen, &part_written, radix, false)) != MP_OKAY) goto LBL_ERR;
52+
str = start;
5153
/* part_written does not count EOS */
5254
part_written++;
5355
}

s_mp_faster_read_radix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

6-
/* TODO: It is tunable */
6+
/* TODO: It is tunable. Or is it? */
77
#define MP_READ_RADIX_CUTOFF_MULTIPLICATOR 3
88

99
/* for(n=2,64,t = ceil(log(2^100)/log(n)); printf(t", "); ) */
@@ -41,8 +41,8 @@ mp_err s_mp_faster_read_radix(mp_int *a, const char *str, size_t start, size_t e
4141
return err;
4242
}
4343

44-
if ((err = s_mp_slower_read_radix(&A, str, start, start + mid + 1, radix)) != MP_OKAY) goto LTM_ERR;
45-
if ((err = s_mp_slower_read_radix(&B, str, start + mid +1, end, radix)) != MP_OKAY) goto LTM_ERR;
44+
if ((err = s_mp_faster_read_radix(&A, str, start, start + mid + 1, radix)) != MP_OKAY) goto LTM_ERR;
45+
if ((err = s_mp_faster_read_radix(&B, str, start + mid +1, end, radix)) != MP_OKAY) goto LTM_ERR;
4646

4747
if (MP_IS_2EXPT(radix_)) {
4848
if ((err = mp_mul_2d(&A, (int)(((len - mid) - 1u) * s_mp_floor_ilog2(radix_)), &A)) != MP_OKAY) goto LTM_ERR;

s_mp_slower_to_radix.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,30 @@ mp_err s_mp_slower_to_radix(const mp_int *a, char **str,
2020
mp_int t;
2121
mp_digit d;
2222
mp_err err = MP_OKAY;
23-
24-
/* The number of digits of "radix" to be filled if this chunk is not the most significant one. */
25-
int ybar = s_mp_radix_exponent_y[radix] * MP_RADIX_BARRETT_START_MULTIPLICATOR;
23+
int ybar = 0;
2624

2725
/* A temporary pointer to the output string to make reversal simpler */
2826
char *s = *str;
2927

30-
/* TODO: input a is already a copy of the original and we could use it destructively? */
28+
/* The number of digits of "radix" to be filled if this chunk is not the most significant one. */
29+
if (pad) {
30+
ybar = s_mp_radix_exponent_y[radix] * MP_RADIX_BARRETT_START_MULTIPLICATOR;
31+
}
32+
3133
if ((err = mp_init_copy(&t, a)) != MP_OKAY) goto LTM_ERR;
3234

3335
while (!mp_iszero(&t)) {
34-
/* TODO: this method to decrease "maxlen" is not threadsafe! */
3536
if ((--(*part_maxlen)) < 1u) {
3637
/* no more room */
3738
err = MP_BUF;
3839
goto LTM_ERR;
3940
}
40-
if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
41-
goto LTM_ERR;
42-
}
41+
if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) goto LTM_ERR;
4342
*s++ = s_mp_radix_map[d];
4443
++digs;
45-
ybar--;
44+
if (pad) {
45+
ybar--;
46+
}
4647
}
4748

4849
/* Fill in leading zeros if this chunk does not contain the most significant digits. */
@@ -53,7 +54,6 @@ mp_err s_mp_slower_to_radix(const mp_int *a, char **str,
5354
}
5455
}
5556

56-
/* TODO: I think that can be done more elegantly */
5757
/* "rewind" */
5858
s = *str;
5959
/* reverse */
@@ -63,7 +63,6 @@ mp_err s_mp_slower_to_radix(const mp_int *a, char **str,
6363
/* Add EOS at the end of every chunk to allow this function to be used stand-alone */
6464
**str = '\0';
6565

66-
/* TODO: this method to increase "written" is not threadsafe! */
6766
if (part_written != NULL) {
6867
*part_written = *part_written + digs;
6968
}

0 commit comments

Comments
 (0)