michael@0: /* michael@0: * Test whether to include squaring code given the current settings michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define MP_SQUARE 1 /* make sure squaring code is included */ michael@0: michael@0: #include "mpi.h" michael@0: #include "mpprime.h" michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: int ntests, prec, ix; michael@0: unsigned int seed; michael@0: clock_t start, stop; michael@0: double multime, sqrtime; michael@0: mp_int a, c; michael@0: michael@0: seed = (unsigned int)time(NULL); michael@0: michael@0: if(argc < 3) { michael@0: fprintf(stderr, "Usage: %s \n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: if((ntests = abs(atoi(argv[1]))) == 0) { michael@0: fprintf(stderr, "%s: must request at least 1 test.\n", argv[0]); michael@0: return 1; michael@0: } michael@0: if((prec = abs(atoi(argv[2]))) < CHAR_BIT) { michael@0: fprintf(stderr, "%s: must request at least %d bits.\n", argv[0], michael@0: CHAR_BIT); michael@0: return 1; michael@0: } michael@0: michael@0: prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT; michael@0: michael@0: mp_init_size(&a, prec); michael@0: mp_init_size(&c, 2 * prec); michael@0: michael@0: /* Test multiplication by self */ michael@0: srand(seed); michael@0: start = clock(); michael@0: for(ix = 0; ix < ntests; ix++) { michael@0: mpp_random_size(&a, prec); michael@0: mp_mul(&a, &a, &c); michael@0: } michael@0: stop = clock(); michael@0: michael@0: multime = (double)(stop - start) / CLOCKS_PER_SEC; michael@0: michael@0: /* Test squaring */ michael@0: srand(seed); michael@0: start = clock(); michael@0: for(ix = 0; ix < ntests; ix++) { michael@0: mpp_random_size(&a, prec); michael@0: mp_sqr(&a, &c); michael@0: } michael@0: stop = clock(); michael@0: michael@0: sqrtime = (double)(stop - start) / CLOCKS_PER_SEC; michael@0: michael@0: printf("Multiply: %.4f\n", multime); michael@0: printf("Square: %.4f\n", sqrtime); michael@0: if(multime < sqrtime) { michael@0: printf("Speedup: %.1f%%\n", 100.0 * (1.0 - multime / sqrtime)); michael@0: printf("Prefer: multiply\n"); michael@0: } else { michael@0: printf("Speedup: %.1f%%\n", 100.0 * (1.0 - sqrtime / multime)); michael@0: printf("Prefer: square\n"); michael@0: } michael@0: michael@0: mp_clear(&a); mp_clear(&c); michael@0: return 0; michael@0: michael@0: }