Skip to content

Commit b849794

Browse files
committed
add tests for faster version to Travis; change some tests to accomodate for MP_8BIT; update documentation
1 parent 53791af commit b849794

File tree

5 files changed

+71
-24
lines changed

5 files changed

+71
-24
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ env:
2424
BUILDOPTIONS="--test-vs-mtest=333333 --mtest-real-rand"
2525
- |
2626
BUILDOPTIONS="--with-low-mp"
27+
- |
28+
BUILDOPTIONS="--with-low-mp --cflags=-DLTM_USE_FASTER_VERSIONS"
2729
- |
2830
BUILDOPTIONS="--with-m64 --with-m32 --with-mx32"
31+
- |
32+
BUILDOPTIONS="--with-m64 --with-m32 --with-mx32 --cflags=-DLTM_USE_FASTER_VERSIONS"
2933
3034
after_failure:
3135
- cat test_*.log

bn_mp_n_root_ex.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Result found such that (c)**b <= a and (c+1)**b > a
1919
*
2020
*/
21-
#if ( (defined LTM_USE_FASTER_VERSIONS) || (defined LTM_USE_FASTER_NTH_ROOT))
21+
#if (( (defined LTM_USE_FASTER_VERSIONS) || (defined LTM_USE_FASTER_NTH_ROOT)) )
2222
/* TODO: needs a benchmark script and be put in bncore.c */
2323
static const int NTHROOT_NEWTON_HALLEY_CUTOFF = 100000;
2424

@@ -421,7 +421,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
421421
ilog2 = mp_count_bits(a);
422422

423423
if (ilog2 < (int)(b)) {
424-
mp_set(c, 1);
424+
mp_set(c, 1uL);
425425
c->sign = neg;
426426
return MP_OKAY;
427427
}
@@ -462,7 +462,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
462462
}
463463
cmp = mp_cmp(c, &A);
464464
if (cmp == MP_GT) {
465-
if ((e = mp_sub_d(c, 1, c)) != MP_OKAY) {
465+
if ((e = mp_sub_d(c, 1u, c)) != MP_OKAY) {
466466
goto LTM_ERR;
467467
}
468468
for (;;) {
@@ -473,12 +473,12 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
473473
if (cmp != MP_GT) {
474474
break;
475475
}
476-
if ((e = mp_sub_d(c, 1, c)) != MP_OKAY) {
476+
if ((e = mp_sub_d(c, 1u, c)) != MP_OKAY) {
477477
goto LTM_ERR;
478478
}
479479
}
480480
} else if (cmp == MP_LT) {
481-
if ((e = mp_add_d(c, 1, c)) != MP_OKAY) {
481+
if ((e = mp_add_d(c, 1u, c)) != MP_OKAY) {
482482
goto LTM_ERR;
483483
}
484484
for (;;) {
@@ -489,13 +489,13 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
489489
if (cmp != MP_LT) {
490490
break;
491491
}
492-
if ((e = mp_add_d(c, 1, c)) != MP_OKAY) {
492+
if ((e = mp_add_d(c, 1u, c)) != MP_OKAY) {
493493
goto LTM_ERR;
494494
}
495495
}
496496
/* Does overshoot in contrast to the other branch above */
497497
if (cmp != MP_EQ) {
498-
if ((e = mp_sub_d(c, 1, c)) != MP_OKAY) {
498+
if ((e = mp_sub_d(c, 1u, c)) != MP_OKAY) {
499499
goto LTM_ERR;
500500
}
501501
}
@@ -512,6 +512,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
512512
{
513513
mp_int t1, t2, t3, a_;
514514
int res;
515+
int ilog2;
515516

516517
/* input must be positive if b is even */
517518
if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
@@ -534,8 +535,29 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
534535
a_ = *a;
535536
a_.sign = MP_ZPOS;
536537

538+
#if 0
537539
/* t2 = 2 */
538540
mp_set(&t2, 2uL);
541+
#endif
542+
ilog2 = mp_count_bits(a);
543+
if (ilog2 < (int)b) {
544+
mp_set(c, 1uL);
545+
c->sign = a->sign;
546+
res = MP_OKAY;
547+
goto LBL_T3;
548+
}
549+
550+
ilog2 = (int)( ( (mp_digit) ilog2 ) / b );
551+
if (ilog2 == 0) {
552+
mp_set(c, 1uL);
553+
c->sign = a->sign;
554+
res = MP_OKAY;
555+
goto LBL_T3;
556+
}
557+
/* Start value must be larger than root */
558+
if (( res = mp_2expt(&t2,ilog2 + 2)) != MP_OKAY) {
559+
goto LBL_T3;
560+
}
539561

540562
do {
541563
/* t1 = t2 */

demo/test.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,6 @@ static int test_mp_radix_size(void) {
12301230
mp_init(&a);
12311231

12321232
mp_read_radix(&a, rand_a,64);
1233-
12341233
for (i = 2; i < 65; i++) {
12351234
mp_radix_size(&a,i,&len);
12361235
if (len < length_rand[i]) {
@@ -1252,25 +1251,39 @@ static int test_mp_radix_size(void) {
12521251
return EXIT_FAILURE;
12531252
}
12541253

1255-
#if ( (defined LTM_USE_FASTER_VERSIONS) || (defined LTM_USE_FASTER_NTH_ROOT))
1254+
#if ( (defined LTM_USE_FASTER_VERSIONS) || (defined LTM_USE_FASTER_NTH_ROOT) )
12561255
static int test_mp_n_root(void) {
12571256
mp_int a, tmp, root;
12581257
mp_digit b;
12591258

12601259
mp_init_multi(&a, &tmp, &root, NULL);
1261-
/* a = 12345^{255} + 12345^{222} + 12345^{123} + 1 */
1260+
1261+
#ifdef MP_8BIT
1262+
/* a bit smaller, MP_MASK = 127 for MP_8BIT */
1263+
mp_set_int(&a, 12345ul);
1264+
mp_expt_d(&a, 126u, &tmp);
1265+
mp_expt_d(&a, 126u, &root);
1266+
mp_mul(&tmp, &root, &tmp);
1267+
mp_mul(&a, &tmp, &tmp);
1268+
mp_expt_d(&a, (mp_digit)123u, &root);
1269+
mp_add(&tmp, &root, &a);
1270+
mp_add_d(&a, 1u, &a);
1271+
#else
1272+
/* a = 12345^{255} + 12345^{222} + 12345^{123} + 1, 3465 bits */
12621273
mp_set_int(&a, 12345ul);
12631274
mp_expt_d(&a, (mp_digit)255u, &tmp);
12641275
mp_expt_d(&a, (mp_digit)222u, &root);
12651276
mp_add(&tmp, &root, &tmp);
12661277
mp_expt_d(&a, (mp_digit)123u, &root);
12671278
mp_add(&tmp, &root, &a);
12681279
mp_add_d(&a, 1u, &a);
1280+
#endif
12691281

1270-
#if (defined MP_8BIT)
1271-
for (b = 2u; b < 255u, b++) {
1282+
#ifdef MP_8BIT
1283+
for (b = 2u; b < MP_MASK; b++) {
12721284
#else
12731285
for (b = 2u; b < 3466u; b++) {
1286+
#endif
12741287
printf("Testing nth-root, large input with index %5u \r",(unsigned)b);
12751288
fflush(stdout);
12761289
mp_n_root(&a, b, &root);
@@ -1286,8 +1299,6 @@ static int test_mp_n_root(void) {
12861299
goto LBL_ERR;
12871300
}
12881301
}
1289-
#endif
1290-
12911302
mp_clear_multi(&a, &tmp, &root, NULL);
12921303
return EXIT_SUCCESS;
12931304
LBL_ERR:
@@ -1300,13 +1311,16 @@ static int test_mp_n_root(void) {
13001311
mp_digit b;
13011312

13021313
mp_init_multi(&a, &tmp, &root, NULL);
1303-
/* a = 123^{123}} + 1 */
1314+
/* a = 123^{123}} + 1, 853 bits */
13041315
mp_set_int(&a, 123ul);
13051316
mp_expt_d(&a, (mp_digit)123u, &a);
13061317
mp_add_d(&a, 1u, &a);
1307-
1308-
/* even 21 is much! */
1309-
for (b = 2u; b < 21u; b++) {
1318+
/* MP_8BIT is really slow here! */
1319+
#ifdef MP_8BIT
1320+
for (b = 2u; b < 10u; b++) {
1321+
#else
1322+
for (b = 2u; b < 834u; b++) {
1323+
#endif
13101324
printf("Testing nth-root, small input with index %2u \r",(unsigned)b);
13111325
fflush(stdout);
13121326
mp_n_root(&a, b, &root);
@@ -1363,7 +1377,7 @@ int unit_tests(void) {
13631377
T(mp_tc_or),
13641378
T(mp_tc_xor),
13651379
T(mp_radix_size),
1366-
T(mp_n_root),
1380+
T(mp_n_root)
13671381
#undef T
13681382
};
13691383
unsigned long i;

doc/bn.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ \subsection{Build Tweaks}\label{ssec:buildtweaks}
311311
\addtocounter{footnote}{1}
312312
\footnotetext{6248 bytes instead of 2736 bytes with GCC 4.8 on a 64-bit AMD A8-6600K}
313313

314+
Note that not all of the faster versions are compatible with \texttt{MP\_8BIT}!
314315

315316
\subsection{Build Trims}
316317
A trim is a manner of removing functionality from a function that is not required. For instance, to perform

testme.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,16 @@ _make()
9090
_runtest()
9191
{
9292
make clean > /dev/null
93-
_make "$1" "$2" "test_standalone"
93+
if (echo $CFLAGS | grep -q FASTER ); then
94+
echo -e "\rRun test $1 $2 faster_test"
95+
_make "$1" "$2" "faster_test"
96+
else
97+
echo -e "\rRun test $1 $2 test_standalone"
98+
_make "$1" "$2" "test_standalone"
99+
fi
94100
local _timeout=""
95101
which timeout >/dev/null && _timeout="timeout --foreground 90"
96-
echo -e "\rRun test $1 $2"
102+
97103
$_timeout ./test > test_${suffix}.log || _die "running tests" $?
98104
}
99105

@@ -107,9 +113,9 @@ _exit()
107113
{
108114
if [ "$ret" == "0" ]
109115
then
110-
echo "Tests successful"
116+
echo -e "\nTests successful"
111117
else
112-
echo "$ret tests timed out"
118+
echo -e "\n$ret tests timed out"
113119
fi
114120

115121
exit $ret
@@ -173,7 +179,7 @@ then
173179
elif [[ "$COMPILERS" == "" ]]
174180
then
175181
_banner gcc
176-
_runtest "gcc" ""
182+
_runtest "gcc" "$CFLAGS"
177183
_exit
178184
fi
179185

0 commit comments

Comments
 (0)